def create_app(mode='local'): # update current config based on given mode from main.settings import set_config set_config(mode) # import updated config from main.settings import Config app = Flask(__name__) app.config.from_object(Config) from main.web import base as base_bp app.register_blueprint(base_bp) from main.web.api import api as api_bp app.register_blueprint(api_bp, url_prefix='/api') engine = app.config['DATABASE_URI'] from main.orm.db import session_builder, init_db session_builder() with app.app_context(): init_db() @app.teardown_appcontext def shutdown_session(exception=None): from main.orm.db import session_removal session_removal() return app
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import unittest from main.settings import set_config set_config('test') from main.models.data import Image, Question, WeightFigure from .base import _Base class ImageModelTest(_Base): def test_image_model_save_and_extract(self): fname = 'test.jpg' img = Image(filename=fname) img.save() data = Image.query().first() self.assertEqual(data.id, img.id) self.assertEqual(data.filename, fname) def test_update_image_state(self): fname = 'test.jpg' img = Image(filename=fname) img.save() # before update, no data is stored self.assertIsNotNone(img.original)
default=0, help='port number to connect') parser.add_argument('-c', '--config', type=str, default='local', help='Config type: production, ' 'local, development, test') args = parser.parse_args() host = args.host port = args.port from main.settings import set_config config = args.config # TODO: add production if config not in ('local', 'development', 'test'): log.warning(f'Invalid config: {config} "local" config will be used') config = 'local' set_config(args.config) from main.settings import Config # if host and port is not set, use from config if not host: host = Config.MODEL_SERVER.get('host', 'localhost') if not port: port = Config.MODEL_SERVER.get('port', 12345) asyncio.run(run_server(host, port))
parser = argparse.ArgumentParser(description='Running web server') parser.add_argument('-p', '--port', type=int, default=5000, help='port') parser.add_argument('-c', '--config', type=str, default='development', help='set configuration to run web server') def main(config): from main.web.app import create_app if config not in ('production', 'local', 'development', 'test'): config = 'developement' app = create_app(config) return app if __name__ == '__main__': args = parser.parse_args() from main.settings import set_config config = args.config set_config(config) app = main(config) port = args.port app.run('127.0.0.1', port)