-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
64 lines (49 loc) · 2.06 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import sqlalchemy
from sqlalchemy.orm import session
from logic_bank.logic_bank import LogicBank
from logic_bank.rule_bank import rule_bank_withdraw # FIXME design why required to avoid circular imports??
# from logic_bank.rule_bank import logic_bank_setup
from nw.logic.rules_bank import declare_logic
import nw.logic.legacy.setup as legacy_setup
from logic_bank.util import prt
""" Initialization
1 - Connect
2 - Register listeners (either hand-coded ones above, or the logic-engine listeners).
"""
print("\n" + prt("BEGIN - setup logging, connect to db, register listeners"))
# Initialize Logging
import logging
import sys
logic_logger = logging.getLogger('logic_logger') # for debugging user logic
logic_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)
logic_logger.addHandler(handler)
do_engine_logging = False
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)
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"))