コード例 #1
0
def create_app(config_filename=None, host="localhost"):
    setup_logging()
    flask_app = Flask("API Logic Server")
    flask_app.config.from_object("config.Config")
    db = safrs.DB  # opens database per config, setting session
    Base: declarative_base = db.Model
    session: Session = db.session
    print("app/__init__#create_app - got session: " + str(session))

    def constraint_handler(message: str, constraint: object,
                           logic_row: LogicRow):
        if constraint.error_attributes:
            detail = {
                "model": logic_row.name,
                "error_attributes": constraint.error_attributes
            }
        else:
            detail = {"model": logic_row.name}
        raise ValidationErrorExt(message=message, detail=detail)

    LogicBank.activate(session=session,
                       activator=logic_bank.declare_logic,
                       constraint_event=constraint_handler)

    with flask_app.app_context():
        db.init_app(flask_app)
        safrs_api = expose_api_models.expose_models(flask_app, host)
        expose_services.expose_services(flask_app, safrs_api)
        SAFRSBase._s_auto_commit = False
        session.close()

    return flask_app, safrs_api
コード例 #2
0
def create_app(config_filename=None, host="localhost"):
    app = Flask("demo_app")
    app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://")
    db.init_app(app)
    with app.app_context():
        db.create_all()
        create_api(app, host)
        # User the safrs.DB, not db!
        LogicBank.activate(session=DB.session, activator=declare_logic)
    return app
コード例 #3
0
def create_app(config_filename=None, host="localhost"):
    setup_logging()
    app = Flask("API Logic Server")
    app.config.from_object("config.Config")
    db = safrs.DB  # opens (what?) database, returning session
    Base: declarative_base = db.Model
    session: Session = db.session
    print("app/__init__#create_app - got session: " + str(session))

    def constraint_handler(
        message: str, constraint: object, logic_row: LogicRow
    ):  # message: str, constr: constraint, row: logic_row):
        raise ValidationError(message)

    LogicBank.activate(session=session,
                       activator=logic_bank.declare_logic,
                       constraint_event=constraint_handler)

    with app.app_context():
        db.init_app(app)
        expose_api_models.expose_models(app, host)

    return app
コード例 #4
0
do_engine_logging = False  # TODO move to config file, reconsider level
engine_logger = logging.getLogger('engine_logger')  # for internals
if do_engine_logging:
    engine_logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(message)s - %(asctime)s - %(name)s - %(levelname)s')
    handler.setFormatter(formatter)
    engine_logger.addHandler(handler)

basedir = os.path.abspath(os.path.dirname(__file__))
basedir = os.path.dirname(basedir)

banking_loc = basedir + "/db/database.db"
banking_source = basedir + "/db/database-gold.db"

conn_string = "sqlite:///" + banking_loc
engine = sqlalchemy.create_engine(conn_string, pool_pre_ping=True,
                                  echo=False)  # sqlalchemy sqls...

session_maker = sqlalchemy.orm.sessionmaker()
session_maker.configure(bind=engine)
session = session_maker()

rule_list = None
db = None
LogicBank.activate(session=session, activator=activate_basic_rules)
print("\n" + prt("session created, listeners registered\n"))
コード例 #5
0
    engine_logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(message)s - %(asctime)s - %(name)s - %(levelname)s')
    handler.setFormatter(formatter)
    engine_logger.addHandler(handler)

basedir = os.path.abspath(os.path.dirname(__file__))
basedir = os.path.dirname(basedir)

nw_loc = os.path.join(basedir, "db/database.db")

conn_string = "sqlite:///" + nw_loc
engine = sqlalchemy.create_engine(conn_string,
                                  echo=False)  # sqlalchemy sqls...

session_maker = sqlalchemy.orm.sessionmaker()
session_maker.configure(bind=engine)
session = session_maker()

by_rules = True  # True => use rules, False => use legacy hand code (for comparison)
rule_list = None
db = None
if by_rules:
    LogicBank.activate(session=session, activator=declare_logic)
else:
    legacy_setup.setup(session)  # test asserts fail due to counts (else ok)

print("\n" + prt("END - connected, session created, listeners registered\n"))
コード例 #6
0
formatter = logging.Formatter(
    '%(message)s - %(asctime)s - %(name)s - %(levelname)s')
handler.setFormatter(formatter)
logic_logger.addHandler(handler)

do_engine_logging = True
engine_logger = logging.getLogger('engine_logger')  # for internals
if do_engine_logging:
    engine_logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
        '%(message)s - %(asctime)s - %(name)s - %(levelname)s')
    handler.setFormatter(formatter)
    engine_logger.addHandler(handler)

session: session = db.session


def constraint_handler(message: str, constraint: constraint,
                       logic_row: logic_row
                       ):  # message: str, constr: constraint, row: logic_row):
    raise ValidationError(message)


LogicBank.activate(session=session,
                   activator=declare_logic,
                   constraint_event=constraint_handler)

util.log("LogicBank activated\n")