def __init__(self, path=None): super().__init__() if not path or path.isspace(): ROOT_DIR = application.root_dir() DEFAULT_NAME = 'lyrebird.db' database_uri = ROOT_DIR / DEFAULT_NAME else: path_obj = Path(path).expanduser() if path_obj.is_absolute(): database_uri = path_obj else: database_uri = path_obj.absolute() sqlite_path = 'sqlite:///' + str( database_uri) + '?check_same_thread=False' engine = create_engine(str(sqlite_path)) # Create all tables Base.metadata.create_all(engine) # Create session factory session_factory = sessionmaker(bind=engine) Session = scoped_session(session_factory) self._scoped_session = Session # init queue self.storage_queue = Queue() # subscribe all channel application.server['event'].subscribe('any', self.event_receiver)
def _init_file_logger(custom_log_path=None): file_formater = logging.Formatter( fmt='%(asctime)s %(levelname)s [%(module)s] - %(threadName)s [PID] %(process)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) if custom_log_path: log_file = custom_log_path else: log_file = application.root_dir()/'lyrebird.log' file_handler = logging.handlers.TimedRotatingFileHandler(log_file, backupCount=1, encoding='utf-8', when='midnight') file_handler.setFormatter(file_formater) logger:logging.Logger = logging.getLogger('lyrebird') logger.addHandler(file_handler)
def __init__(self, path=None): self.database_uri = None super().__init__() if not path or path.isspace(): ROOT_DIR = application.root_dir() DEFAULT_NAME = 'lyrebird.db' self.database_uri = ROOT_DIR / DEFAULT_NAME else: self.database_uri = Path(path).expanduser().absolute() self.init_engine() # init queue self.storage_queue = Queue() # subscribe all channel application.server['event'].subscribe('any', self.event_receiver)
def __init__(self): super().__init__() DB_FILE_NAME = 'lyrebird.db' ROOT_DIR = application.root_dir() SQLALCHEMY_DATABASE_URI = ROOT_DIR / DB_FILE_NAME sqlite_path = 'sqlite:///' + str(SQLALCHEMY_DATABASE_URI) engine = create_engine(str(sqlite_path)) # Create all tables Base.metadata.create_all(engine) # Create session factory session_factory = sessionmaker(bind=engine) Session = scoped_session(session_factory) self._scoped_session = Session # init queue self.storage_queue = Queue() # subscribe all channel application.server['event'].subscribe('any', self.event_receiver)
def __init__(self): super().__init__() # init sqlite DB_FILE_NAME = 'lyrebird.db' ROOT_DIR = application.root_dir() SQLALCHEMY_DATABASE_URI = ROOT_DIR / DB_FILE_NAME sqlite_path = 'sqlite:///' + str(SQLALCHEMY_DATABASE_URI) # TODO: 'sqlite:///' is unfriendly to windows engine = create_engine(str(sqlite_path)) Base.metadata.create_all(engine) DBSession = sessionmaker(bind=engine) self.session = DBSession() # init queue self.storage_queue = Queue() # subscribe all channel application.server['event'].subscribe('any', self.put_queue, event=True)
def __init__(self): super().__init__() self.conf = application.config # TODO rm conf rom mock context context.application.conf = application.config self.debug = False self.port = 9090 self._working_thread = None self.app = Flask('MOCK') self.app.jinja_env.block_start_string = '[%' self.app.jinja_env.block_end_string = '%]' self.app.jinja_env.variable_start_string = '[[' self.app.jinja_env.variable_end_string = ']]' self.app.jinja_env.comment_start_string = '[#' self.app.jinja_env.comment_end_string = '#]' # Add global function for templates self.app.jinja_env.globals['time'] = time.time self.app.jinja_env.globals['datetime'] = datetime.datetime self.app.jinja_env.globals['version'] = VERSION # async_mode = threading / eventlet / gevent / gevent_uwsgi self.socket_io = SocketIO(self.app, async_mode='threading', log_output=False) # 存储socket-io context.application.socket_io = self.socket_io # 生成过滤器实例 if self.conf: self.port = self.conf.get('mock.port') else: _logger.error('Can not start mock server without config file') raise SyntaxError('Can not start mock server without config file.' ' Default config file path = api-mock/conf.json') # sqlite初始化 ROOT_DIR = application.root_dir() DB_FILE_NAME = 'lyrebird.db' if ROOT_DIR: SQLALCHEMY_DATABASE_URI = ROOT_DIR / DB_FILE_NAME # TODO: 'sqlite:///' is unfriendly to windows self.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + str( SQLALCHEMY_DATABASE_URI) self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False context.db = DataBase(self.app) # Plugin # init plugin plugin_manager.load() # load plugin frontend plugin_manager.add_view_to_blueprint(ui) plugin_manager.add_view_to_blueprint(plugin) # Register event socket plugin_manager.add_event_rules(self.socket_io) # Register blueprints self.app.register_blueprint(api) self.app.register_blueprint(api_mock) self.app.register_blueprint(ui) self.app.register_blueprint(plugin) @self.app.route('/') def index(): """ 设置默认页面为UI首页 """ return redirect(url_for('ui.index')) @self.app.after_request def after_request(response: Response): """ 输出每条请求概要信息 """ lyrebird_info = response.headers.get('lyrebird', default='') _logger.info( f'{response.status_code} {lyrebird_info} {request.method} {request.url[:100]}' ) return response