aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/hydroroll/config.py
diff options
context:
space:
mode:
author简律纯 <hsiangnianian@outlook.com>2023-05-02 23:30:19 +0800
committer简律纯 <hsiangnianian@outlook.com>2023-05-02 23:30:19 +0800
commitdfa97c9d24b8124ca38ec5eb605cde932cd6ea78 (patch)
tree470a8bfd1960964b8c9724c19de876cf52cf60c3 /hydroroll/config.py
parent45c49007ee01143ad26899751b856658321cd3ae (diff)
downloadHydroRoll-dfa97c9d24b8124ca38ec5eb605cde932cd6ea78.tar.gz
HydroRoll-dfa97c9d24b8124ca38ec5eb605cde932cd6ea78.zip
Diffstat (limited to 'hydroroll/config.py')
-rw-r--r--hydroroll/config.py97
1 files changed, 4 insertions, 93 deletions
diff --git a/hydroroll/config.py b/hydroroll/config.py
index 6739249..4258606 100644
--- a/hydroroll/config.py
+++ b/hydroroll/config.py
@@ -4,9 +4,6 @@ import platform
from importlib.metadata import version
from iamai import Plugin
import os
-import pickle
-import threading
-
# 创建全局 ArgumentParser 对象
global_parser = argparse.ArgumentParser(description='hydroroll[水系] 全局命令参数')
@@ -20,8 +17,9 @@ class GlobalConfig:
_iamai_version = version('iamai')
_python_ver = sys.version
_python_ver_raw= '.'.join(map(str, platform.python_version_tuple()[:3]))
- current_path = os.path.dirname(os.path.abspath('__file__'))
-
+ _current_path = os.path.dirname(os.path.abspath('__file__'))
+ _folders = {'config':{},'data':{},'logs':{},'models':{},'rules':{'rules_default'},'users':{},'web':{'frontend':{'static','js','css','public'},'backend':{'app','template'}}}
+
# 定义系统组件
class HydroSystem:
def __init__(self):
@@ -34,91 +32,4 @@ class GlobalConfig:
class HydroBot:
def __init__(self) -> None:
self.parser = argparse.ArgumentParser(description="Bot命令")
-
-
-class ConfigManager:
- def __init__(self, directory):
- self.directory = directory
- self.configs = {}
- self.locks = {}
-
- def get(self, filename, section, key, default=None):
- if not self._check_file_exists(filename):
- return default
- config = self._get_config(filename)
- if section not in config:
- return default
- return config[section].get(key, default)
-
- def set(self, filename, section, key, value):
- config = self._get_config(filename)
- if section not in config:
- config[section] = {}
- config[section][key] = value
- self._save_config(filename, config)
-
- def delete(self, filename, section, key):
- config = self._get_config(filename)
- if section not in config:
- return
- if key not in config[section]:
- return
- del config[section][key]
- self._save_config(filename, config)
-
- def sections(self, filename):
- if not self._check_file_exists(filename):
- return []
- config = self._get_config(filename)
- return list(config.keys())
-
- def section_items(self, filename, section):
- if not self._check_file_exists(filename):
- return []
- config = self._get_config(filename)
- if section not in config:
- return []
- return list(config[section].items())
-
- def files(self):
- return [filename for filename in os.listdir(self.directory) if filename.endswith('.dat')]
-
- def _get_lock(self, filename):
- if filename not in self.locks:
- self.locks[filename] = threading.Lock()
- return self.locks[filename]
-
- def _get_config(self, filename):
- with self._get_lock(filename):
- if filename not in self.configs:
- filepath = os.path.join(self.directory, filename)
- if os.path.exists(filepath):
- try:
- with open(filepath, 'rb') as f:
- self.configs[filename] = pickle.load(f)
- except:
- pass
- if filename not in self.configs:
- self.configs[filename] = {}
- return self.configs[filename]
-
- def _save_config(self, filename, config):
- with self._get_lock(filename):
- try:
- filepath = os.path.join(self.directory, filename)
- backuppath = os.path.join(self.directory, filename + '.bak')
- lockpath = os.path.join(self.directory, filename + '.lock')
- with open(lockpath, 'wb') as f:
- pass
- if os.path.exists(filepath):
- os.replace(filepath, backuppath)
- with open(filepath, 'wb') as f:
- pickle.dump(config, f)
- os.remove(backuppath)
- os.remove(lockpath)
- except:
- pass
-
- def _check_file_exists(self, filename):
- filepath = os.path.join(self.directory, filename)
- return os.path.exists(filepath) and filename.endswith('.dat')
+ \ No newline at end of file