Rewrite common.py into a more generic Config class

This commit is contained in:
Adam Goldsmith 2020-04-14 01:18:20 -04:00
parent e37770dbe2
commit 2c3cf27779
9 changed files with 91 additions and 62 deletions

3
.gitignore vendored
View File

@ -1,4 +1,3 @@
__pycache__/ __pycache__/
/passwords.py
/passwords.yaml
/venv/ /venv/
/config.yaml

View File

@ -1,4 +1,4 @@
doors: doorControllers:
Studio Space: {ip: 172.18.51.11, access: Studio Space} Studio Space: {ip: 172.18.51.11, access: Studio Space}
Front Door: {ip: 172.18.51.12, access: Front Door} Front Door: {ip: 172.18.51.12, access: Front Door}
Metal Shop: {ip: 172.18.51.13, access: Metal Shop} Metal Shop: {ip: 172.18.51.13, access: Metal Shop}
@ -26,3 +26,20 @@ doorSpecificSchedules:
- Metal Shop - Metal Shop
- Wood Shop - Wood Shop
- Wood Shop Rear - Wood Shop Rear
DOOR_USERNAME: ""
DOOR_PASSWORD: ""
MEMBERSHIPWORKS_USERNAME: ""
MEMBERSHIPWORKS_PASSWORD: ""
MEMBERSHIPWORKS_DB:
database: ""
user: ""
password: ""
HID_DB:
database: ""
user: ""
password: ""

View File

@ -1,33 +0,0 @@
from ruamel.yaml import YAML
from .hid.DoorController import DoorController
from .MembershipWorks import MembershipWorks
with open("passwords.yaml") as f:
passwords = YAML().load(f)
DOOR_USERNAME = passwords["DOOR_USERNAME"]
DOOR_PASSWORD = passwords["DOOR_PASSWORD"]
MEMBERSHIPWORKS_USERNAME = passwords["MEMBERSHIPWORKS_USERNAME"]
MEMBERSHIPWORKS_PASSWORD = passwords["MEMBERSHIPWORKS_PASSWORD"]
with open("config.yaml") as f:
config = YAML().load(f)
doors = {
doorName: DoorController(
doorData["ip"],
DOOR_USERNAME,
DOOR_PASSWORD,
name=doorName,
access=doorData["access"],
)
for doorName, doorData in config["doors"].items()
}
memberLevels = config["memberLevels"]
doorSpecificSchedules = config["doorSpecificSchedules"]
membershipworks = MembershipWorks()
membershipworks.login(MEMBERSHIPWORKS_USERNAME, MEMBERSHIPWORKS_PASSWORD)

36
memberPlumbing/config.py Normal file
View File

@ -0,0 +1,36 @@
from ruamel.yaml import YAML
from .hid.DoorController import DoorController
from .MembershipWorks import MembershipWorks
class Config:
def __init__(self, path="config.yaml"):
with open(path) as f:
self._data = YAML().load(f)
self.__dict__.update(self._data)
# lazy init, because this actually talks to an external server
self._membershipworks = None
@property
def doors(self):
return {
doorName: DoorController(
doorData["ip"],
self.DOOR_USERNAME,
self.DOOR_PASSWORD,
name=doorName,
access=doorData["access"],
)
for doorName, doorData in self.doorControllers.items()
}
@property
def membershipworks(self):
if not self._membershipworks:
self._membershipworks = MembershipWorks()
self._membershipworks.login(
self.MEMBERSHIPWORKS_USERNAME, self.MEMBERSHIPWORKS_PASSWORD
)
return self._membershipworks

View File

@ -2,7 +2,7 @@
import copy import copy
from .common import doors, doorSpecificSchedules, memberLevels, membershipworks from .config import Config
from .hid.Credential import Credential from .hid.Credential import Credential
from .hid.DoorController import ROOT, E from .hid.DoorController import ROOT, E
@ -49,7 +49,7 @@ Schedules: {self.schedules}
class MembershipworksMember(Member): class MembershipworksMember(Member):
def __init__(self, data, formerMember=False): def __init__(self, config, data, formerMember=False):
super().__init__( super().__init__(
data["First Name"], data["First Name"],
data["Last Name"], data["Last Name"],
@ -78,7 +78,7 @@ class MembershipworksMember(Member):
) )
self.formerMember = formerMember self.formerMember = formerMember
levels = {k: v for k, v in memberLevels.items() if data[k] == k} levels = {k: v for k, v in config.memberLevels.items() if data[k] == k}
self.levels = list(levels.keys()) self.levels = list(levels.keys())
self.schedules = list(levels.values()) self.schedules = list(levels.values())
@ -86,12 +86,12 @@ class MembershipworksMember(Member):
schedule: sum( schedule: sum(
(doors for prop, doors in props.items() if data[prop] == "Y"), [] (doors for prop, doors in props.items() if data[prop] == "Y"), []
) )
for schedule, props in doorSpecificSchedules.items() for schedule, props in config.doorSpecificSchedules.items()
} }
self.doorAccess = [ self.doorAccess = [
door door
for door, doorData in doors.items() for door, doorData in config.doors.items()
if data["Access " + doorData.access + "?"] == "Y" if data["Access " + doorData.access + "?"] == "Y"
] ]
@ -333,6 +333,8 @@ def update_door(door, members):
def main(): def main():
config = Config()
membershipworks = config.membershipworks
membershipworks_attributes = ( membershipworks_attributes = (
"_id,nam,phn,eml,lvl,lbl,xws,xms,xsc,xas,xfd,xac,xcf,xeh,xse,xlo" "_id,nam,phn,eml,lvl,lbl,xws,xms,xsc,xas,xfd,xac,xcf,xeh,xse,xlo"
) )
@ -340,13 +342,13 @@ def main():
memberData = membershipworks.get_members( memberData = membershipworks.get_members(
["Members", "CMS Staff", "Misc. Access"], membershipworks_attributes ["Members", "CMS Staff", "Misc. Access"], membershipworks_attributes
) )
members = [MembershipworksMember(m) for m in memberData] members = [MembershipworksMember(config, m) for m in memberData]
formerMemberData = membershipworks.get_members( formerMemberData = membershipworks.get_members(
["Former Members"], membershipworks_attributes ["Former Members"], membershipworks_attributes
) )
formerMembers = [ formerMembers = [
MembershipworksMember(m, formerMember=True) for m in formerMemberData MembershipworksMember(config, m, formerMember=True) for m in formerMemberData
] ]
for formerMember in formerMembers: for formerMember in formerMembers:
@ -366,7 +368,7 @@ def main():
formerMember.formerMember = True formerMember.formerMember = True
members.append(formerMember) members.append(formerMember)
for door in doors.values(): for door in config.doors.values():
print(door.name, door.ip) print(door.name, door.ip)
update_door(door, members) update_door(door, members)

View File

@ -16,14 +16,9 @@ from peewee import (
TextField, TextField,
) )
import passwords from .config import Config
from .common import doors database = MySQLDatabase(None)
database = MySQLDatabase(
**passwords.MEMBERSHIPWORKS_DB,
**{"charset": "utf8", "sql_mode": "PIPES_AS_CONCAT", "use_unicode": True,}
)
class HIDEvent(Model): class HIDEvent(Model):
@ -99,8 +94,13 @@ def dups(events):
def main(): def main():
config = Config()
database.init(
**config.HID_DB,
**{"charset": "utf8", "sql_mode": "PIPES_AS_CONCAT", "use_unicode": True,}
)
HIDEvent.create_table() HIDEvent.create_table()
for door in doors.values(): for door in config.doors.values():
getMessages(door) getMessages(door)

View File

@ -13,12 +13,7 @@ from peewee import (
TextField, TextField,
) )
import passwords database = MySQLDatabase(None)
database = MySQLDatabase(
**passwords.MEMBERSHIPWORKS_DB,
**{"charset": "utf8", "sql_mode": "PIPES_AS_CONCAT", "use_unicode": True,}
)
class BaseModel(Model): class BaseModel(Model):

View File

@ -2,12 +2,13 @@
from datetime import datetime from datetime import datetime
from .common import membershipworks from .config import Config
from .mw_models import Label, Member, MemberLabel, Transaction, database from .mw_models import Label, Member, MemberLabel, Transaction, database
@database.atomic() @database.atomic()
def main(): def do_import(config):
membershipworks = config.membershipworks
print("Creating tables") print("Creating tables")
database.create_tables([Label, Member, MemberLabel, Transaction]) database.create_tables([Label, Member, MemberLabel, Transaction])
@ -62,5 +63,15 @@ def main():
# TODO: folders, levels, addons # TODO: folders, levels, addons
def main():
config = Config()
database.init(
**config.MEMBERSHIPWORKS_DB,
**{"charset": "utf8", "sql_mode": "PIPES_AS_CONCAT", "use_unicode": True,}
)
do_import(config)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -4,7 +4,7 @@ import re
import string import string
import subprocess import subprocess
from .common import membershipworks from .config import Config
LDAP_BASE = "cn=users,dc=sawtooth,dc=claremontmakerspace,dc=org" LDAP_BASE = "cn=users,dc=sawtooth,dc=claremontmakerspace,dc=org"
GROUP_BASE = "cn=groups,dc=sawtooth,dc=claremontmakerspace,dc=org" GROUP_BASE = "cn=groups,dc=sawtooth,dc=claremontmakerspace,dc=org"
@ -48,7 +48,9 @@ def makeAppendGroups(member):
def main(): def main():
members = membershipworks.get_members( config = Config()
members = config.membershipworks.get_members(
["Members", "CMS Staff"], "lvl,phn,eml,lbl,nam,end,_id" ["Members", "CMS Staff"], "lvl,phn,eml,lbl,nam,end,_id"
) )
makeGroups(members) makeGroups(members)