forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_class.py
More file actions
55 lines (43 loc) · 2.07 KB
/
Copy pathstart_class.py
File metadata and controls
55 lines (43 loc) · 2.07 KB
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
import sqlalchemy
class AccessTable(object):
engine = None
def __init__(self, db_config):
self.db_config = db_config
def initialize(self, engine):
"""
Set up schema if it does not already exist
"""
self.metadata = sqlalchemy.MetaData()
sqlalchemy.Table('access', self.metadata,
sqlalchemy.Column('name', sqlalchemy.String(256), primary_key=True),
sqlalchemy.Column('group', sqlalchemy.String(256), nullable=False),
sqlalchemy.Column('privilege', sqlalchemy.String(64), nullable=False),
sqlalchemy.Index('name', 'group', unique=True))
self.metadata.create_all(engine)
def _get_engine(self):
"""
Get a connection to the sqlalchemy db engine
If the initial connection fails, it will try again next time
The engine connection is kept as a class variable since sqlalchemy engines are intended to be
started once in the app's lifetime, not started up and shut down repeatedly. So this is just
a plain ol singleton
"""
if not AccessTable.engine:
AccessTable.engine = sqlalchemy.create_engine(self.db_config['engine'])
self.initialize(AccessTable.engine)
return AccessTable.engine
def get_privilege(self, name, group):
"""
Get the privilege for the given name and group
"""
# TODO consider making self.engine a @property instead
engine = self._get_engine()
# TODO consider making self.metadata a @property
# Also, I don't like how self.metadata is not initialized until _get_engine is called ... should be more
# automatic than that
access = self.metadata.tables['access']
select = sqlalchemy.select([access.c.privilege]).where(sqlalchemy.sql.and_(access.c.name == name,
access.c.group == group))
# TODO handle result including case where it doesn't find any records
_result = engine.execute(select)
return 'readwrite'