From 2c3cf277798672b3324da15b85d36206d956b0de Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 14 Apr 2020 01:18:20 -0400 Subject: [PATCH] Rewrite common.py into a more generic Config class --- .gitignore | 3 +-- config.yaml => config.example.yaml | 19 +++++++++++++++- memberPlumbing/common.py | 33 --------------------------- memberPlumbing/config.py | 36 ++++++++++++++++++++++++++++++ memberPlumbing/doorUpdater.py | 18 ++++++++------- memberPlumbing/hidEvents.py | 16 ++++++------- memberPlumbing/mw_models.py | 7 +----- memberPlumbing/sqlExport.py | 15 +++++++++++-- memberPlumbing/ucsAccounts.py | 6 +++-- 9 files changed, 91 insertions(+), 62 deletions(-) rename config.yaml => config.example.yaml (79%) delete mode 100644 memberPlumbing/common.py create mode 100644 memberPlumbing/config.py diff --git a/.gitignore b/.gitignore index 7f57a90..a859250 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ __pycache__/ -/passwords.py -/passwords.yaml /venv/ +/config.yaml diff --git a/config.yaml b/config.example.yaml similarity index 79% rename from config.yaml rename to config.example.yaml index 5375051..8f0bc85 100644 --- a/config.yaml +++ b/config.example.yaml @@ -1,4 +1,4 @@ -doors: +doorControllers: Studio Space: {ip: 172.18.51.11, access: Studio Space} Front Door: {ip: 172.18.51.12, access: Front Door} Metal Shop: {ip: 172.18.51.13, access: Metal Shop} @@ -26,3 +26,20 @@ doorSpecificSchedules: - Metal Shop - Wood Shop - Wood Shop Rear + + +DOOR_USERNAME: "" +DOOR_PASSWORD: "" + +MEMBERSHIPWORKS_USERNAME: "" +MEMBERSHIPWORKS_PASSWORD: "" + +MEMBERSHIPWORKS_DB: + database: "" + user: "" + password: "" + +HID_DB: + database: "" + user: "" + password: "" diff --git a/memberPlumbing/common.py b/memberPlumbing/common.py deleted file mode 100644 index c43fc4f..0000000 --- a/memberPlumbing/common.py +++ /dev/null @@ -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) diff --git a/memberPlumbing/config.py b/memberPlumbing/config.py new file mode 100644 index 0000000..18c9817 --- /dev/null +++ b/memberPlumbing/config.py @@ -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 diff --git a/memberPlumbing/doorUpdater.py b/memberPlumbing/doorUpdater.py index e09af79..87113f3 100755 --- a/memberPlumbing/doorUpdater.py +++ b/memberPlumbing/doorUpdater.py @@ -2,7 +2,7 @@ import copy -from .common import doors, doorSpecificSchedules, memberLevels, membershipworks +from .config import Config from .hid.Credential import Credential from .hid.DoorController import ROOT, E @@ -49,7 +49,7 @@ Schedules: {self.schedules} class MembershipworksMember(Member): - def __init__(self, data, formerMember=False): + def __init__(self, config, data, formerMember=False): super().__init__( data["First Name"], data["Last Name"], @@ -78,7 +78,7 @@ class MembershipworksMember(Member): ) 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.schedules = list(levels.values()) @@ -86,12 +86,12 @@ class MembershipworksMember(Member): schedule: sum( (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 = [ door - for door, doorData in doors.items() + for door, doorData in config.doors.items() if data["Access " + doorData.access + "?"] == "Y" ] @@ -333,6 +333,8 @@ def update_door(door, members): def main(): + config = Config() + membershipworks = config.membershipworks membershipworks_attributes = ( "_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( ["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( ["Former Members"], membershipworks_attributes ) formerMembers = [ - MembershipworksMember(m, formerMember=True) for m in formerMemberData + MembershipworksMember(config, m, formerMember=True) for m in formerMemberData ] for formerMember in formerMembers: @@ -366,7 +368,7 @@ def main(): formerMember.formerMember = True members.append(formerMember) - for door in doors.values(): + for door in config.doors.values(): print(door.name, door.ip) update_door(door, members) diff --git a/memberPlumbing/hidEvents.py b/memberPlumbing/hidEvents.py index 99a1e32..2e8e4bf 100755 --- a/memberPlumbing/hidEvents.py +++ b/memberPlumbing/hidEvents.py @@ -16,14 +16,9 @@ from peewee import ( TextField, ) -import passwords +from .config import Config -from .common import doors - -database = MySQLDatabase( - **passwords.MEMBERSHIPWORKS_DB, - **{"charset": "utf8", "sql_mode": "PIPES_AS_CONCAT", "use_unicode": True,} -) +database = MySQLDatabase(None) class HIDEvent(Model): @@ -99,8 +94,13 @@ def dups(events): def main(): + config = Config() + database.init( + **config.HID_DB, + **{"charset": "utf8", "sql_mode": "PIPES_AS_CONCAT", "use_unicode": True,} + ) HIDEvent.create_table() - for door in doors.values(): + for door in config.doors.values(): getMessages(door) diff --git a/memberPlumbing/mw_models.py b/memberPlumbing/mw_models.py index 512ad6b..4f26756 100644 --- a/memberPlumbing/mw_models.py +++ b/memberPlumbing/mw_models.py @@ -13,12 +13,7 @@ from peewee import ( TextField, ) -import passwords - -database = MySQLDatabase( - **passwords.MEMBERSHIPWORKS_DB, - **{"charset": "utf8", "sql_mode": "PIPES_AS_CONCAT", "use_unicode": True,} -) +database = MySQLDatabase(None) class BaseModel(Model): diff --git a/memberPlumbing/sqlExport.py b/memberPlumbing/sqlExport.py index ade47f3..6dde25c 100755 --- a/memberPlumbing/sqlExport.py +++ b/memberPlumbing/sqlExport.py @@ -2,12 +2,13 @@ from datetime import datetime -from .common import membershipworks +from .config import Config from .mw_models import Label, Member, MemberLabel, Transaction, database @database.atomic() -def main(): +def do_import(config): + membershipworks = config.membershipworks print("Creating tables") database.create_tables([Label, Member, MemberLabel, Transaction]) @@ -62,5 +63,15 @@ def main(): # 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__": main() diff --git a/memberPlumbing/ucsAccounts.py b/memberPlumbing/ucsAccounts.py index 84911c2..5625334 100755 --- a/memberPlumbing/ucsAccounts.py +++ b/memberPlumbing/ucsAccounts.py @@ -4,7 +4,7 @@ import re import string import subprocess -from .common import membershipworks +from .config import Config LDAP_BASE = "cn=users,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(): - members = membershipworks.get_members( + config = Config() + + members = config.membershipworks.get_members( ["Members", "CMS Staff"], "lvl,phn,eml,lbl,nam,end,_id" ) makeGroups(members)