Module pywander.config
Functions
def get_config_path(app_name='test')
-
Expand source code
def get_config_path(app_name='test'): """ 获取配置文件路径 """ return normalized_path(os.path.join('~', 'Pywander', app_name, 'config.py'))
获取配置文件路径
def import_from_path(module_name, file_path, saved_module_name=None)
-
Expand source code
def import_from_path(module_name, file_path, saved_module_name=None): spec = importlib.util.spec_from_file_location(module_name, file_path) if saved_module_name is None: saved_module_name = module_name if spec is None: raise ImportError("构建模块失败") module = importlib.util.module_from_spec(spec) sys.modules[saved_module_name] = module spec.loader.exec_module(module) return module
def lazy_import_from_path(module_name, file_path)
-
Expand source code
def lazy_import_from_path(module_name, file_path): spec = importlib.util.spec_from_file_location(module_name, file_path) spec.loader = importlib.util.LazyLoader(spec.loader) module = importlib.util.module_from_spec(spec) sys.modules[module_name] = module spec.loader.exec_module(module) return module
def load_all_config()
-
Expand source code
def load_all_config(): """ 现在加载默认配置 先加载home文件夹下的配置文件 再加载当前文件夹下的配置文件 """ config = DEFUALT_CONFIG config.update(load_home_config()) config.update(load_current_config()) return config
现在加载默认配置 先加载home文件夹下的配置文件 再加载当前文件夹下的配置文件
def load_current_config(config_name='pywander.json')
-
Expand source code
def load_current_config(config_name='pywander.json'): if not os.path.exists(config_name): return {} config = get_json_data(config_name) new_config = {} for k in config: if k: if k[0] == '_': continue if k.isupper(): v = config.get(k) new_config[k] = v return new_config
def load_home_config(app_name='test', module_name='config')
-
Expand source code
def load_home_config(app_name='test', module_name='config'): """ 加载配置文件 所有配置的key为大写字母形式,不得以下划线开头。 """ config_path = get_config_path(app_name=app_name) saved_module_name = f'pywander_{app_name}_{module_name}' try: config = import_from_path(module_name, config_path, saved_module_name=saved_module_name) except ImportError as e: logger.warning(f"构建模块{module_name}失败: {config_path}") return {} except FileNotFoundError: logger.warning(f'can not found config.py in: {config_path} ') return {} new_config = {} for k in dir(config): if k: if k[0] == '_': continue if k.isupper(): v = getattr(config, k) new_config[k] = v return new_config
加载配置文件
所有配置的key为大写字母形式,不得以下划线开头。