def _check_duplicate_db(self): ''' 检查是否有数据库出现重名现象,如果没有,则再检查该数据库是否注册,若没有则直接注册 仅在数据库对象初始化时检查 存储文件的格式为{db_name: main_path} Parameter --------- result: boolean True表示没有重复,False表示重复 ''' metadata_path = parse_config(CONFIG_PATH)['database_metadata_path'] metadata_path = os_path.join(metadata_path, '$metadata.json') if not os_path.exists(metadata_path): # 存储所有数据库信息的文件不存在,则创建,然后存储 if not os_path.exists(os_path.dirname(metadata_path)): makedirs(os_path.dirname(metadata_path)) metadata = {self._db_name: self._main_path} with open(metadata_path, 'w', encoding=ENCODING) as f: json.dump(metadata, f) return True with open(metadata_path, 'r', encoding=ENCODING) as f: metadata = json.load(f) if self._db_name not in metadata: # 当前数据库未注册 metadata[self._db_name] = self._main_path with open(metadata_path, 'w', encoding=ENCODING) as f: json.dump(metadata, f) else: if metadata[self._db_name] != self._main_path: return False return True
def set_db_logger(): ''' 设置主数据库的日志 Return ------ logger_name: string logger的名称,若没有启用日志,则返回None ''' logger_config = qrtutils.parse_config(CONFIG_PATH)['log'] logger_config['name'] = MODULE_NAME # 相对路径转绝对路径 path = logger_config['log_path'] if path.startswith(REL_PATH_HEADER): logger_config['log_path'] = qrtutils.relpath2abs(MODULE_PATH, path) return set_logger(logger_config)
def submodule_initialization(db_name, path, submod_depth=1): ''' 对子模块根据配置进行初始化设置 Parameter --------- db_name: string 数据库子模块名称,必须与配置文件对应 path: string 调用模块所在文件夹的路径,通常为dirname(__file__),用于将相对路径转化为绝对路径, 此处假设子模块文件夹的母文件夹为主模块文件夹 submod_depth: int, default 1 子模块在模块文件夹的深度(在获取主模块文件夹路径时使用) 例如: 主模块文件夹为c:/main_module,子模块文件夹为c:/main_module/sub_module 则此时submod_depth=1,以此类推 Return ------ db_config: dict 数据库子模块的配置 ''' config = qrtutils.parse_config(CONFIG_PATH) db_config = config[db_name] path = qrtutils.trans_config_sep(path) if not db_config['log']['enable_log']: # 当前未启用单独记录日志,使用系统日志设置 db_config['log'] = config['log'] # 添加当前日志名 db_config['log']['name'] = MODULE_NAME log_path = db_config['log']['log_path'] if log_path.startswith(REL_PATH_HEADER): # 相对路径转绝对路径 module_path = sep.join(path.split(sep)[:-submod_depth]) log_path = qrtutils.relpath2abs(module_path, log_path) else: db_config['log']['name'] = MODULE_NAME + '.' + db_name log_path = db_config['log']['log_path'] if log_path.startswith(REL_PATH_HEADER): log_path = qrtutils.relpath2abs(path, log_path) db_config['log']['log_path'] = log_path return db_config
#!/usr/bin/env python # -*- coding:utf-8 """ Author: Hao Li Email: [email protected] Github: https://github.com/SAmmer0 Created: 2018-05-31 15:38 """ from os.path import dirname, join from qrtutils import set_logger, parse_config CONFIG = parse_config(join(dirname(__file__), 'config.json')) MAIN_LOGGER_NAME = set_logger(CONFIG['log'], dirname(__file__), 'DATA_SOURCE_MAIN_LOGGER')
from os.path import join, dirname from qrtutils import parse_config, trans_config_sep from qrtconst import Frequency, REL_PATH_HEADER, REL_PATH_HEADER_LEN from tdtools.utils import set_logger # 周期标记 class TargetSign(enum.Enum): LAST = enum.auto() FIRST = enum.auto() # 模块文件夹 MODULE_PATH = dirname(__file__) # 配置文件路径,可为相对路径和绝对路径,路径格式为./config.json或者C:\\Users\\default\\config.json CONFIG_PATH = './config.json' # 将路径分隔符转换为系统分隔符,并将相对路径转换为绝对路径 CONFIG_PATH = trans_config_sep(CONFIG_PATH) if CONFIG_PATH.startswith(REL_PATH_HEADER): CONFIG_PATH = join(MODULE_PATH, CONFIG_PATH[REL_PATH_HEADER_LEN:]) # 加载配置文件 CONFIG = parse_config(CONFIG_PATH) # 设置日志 LOGGER_NAME = set_logger(CONFIG['log'], MODULE_PATH) # 交易所开市收市时间设置 TRADING_TIME = {'stock.sse': (('09:30', '11:30'), ('13:00', '15:00'))}