From 2b894d3cc9dc081c8d41f73776f76342cddeb112 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Fri, 8 Nov 2019 16:07:26 -0500 Subject: [PATCH] Make executable, clean up style, and rename new door updater --- new_xml_based.py => doorUpdater.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) rename new_xml_based.py => doorUpdater.py (94%) mode change 100644 => 100755 diff --git a/new_xml_based.py b/doorUpdater.py old mode 100644 new mode 100755 similarity index 94% rename from new_xml_based.py rename to doorUpdater.py index d7b14db..ddcd46e --- a/new_xml_based.py +++ b/doorUpdater.py @@ -1,11 +1,10 @@ #!/usr/bin/env python3 -import requests -from lxml import etree from common import doors, getMembershipworksData, memberLevels from hid.DoorController import E, ROOT from hid.Credential import Credential + class Member(): def __init__(self, forename, surname, membershipWorksID, middleName="", email="", phone="", @@ -129,9 +128,10 @@ def update_door(door, members): allCredentials = set(Credential(hex=c.attrib['rawCardNumber']) for c in door.get_credentials()) + # TODO: can I combine requests? for member in members: - # TODO: can I combine requests? - if member.membershipWorksID in cardholders: # cardholder exists, compare contents + # cardholder exists, compare contents + if member.membershipWorksID in cardholders: ch = cardholders.pop(member.membershipWorksID) member.cardholderID = ch.cardholderID if member.attribs() != ch.attribs(): # update cardholder attributes @@ -179,25 +179,28 @@ def update_door(door, members): door.doXMLRequest(ROOT(member.make_credentials( newCards - allCredentials, cardFormats))) - if member.schedulesForDoor(door) != ch.schedules: print("- Updating schedule for" + f" {member.forename} {member.surname}:" + f" {ch.schedules} -> {member.schedulesForDoor(door)}") - door.doXMLRequest(ROOT(member.make_schedules(door, schedulesMap))) + door.doXMLRequest(ROOT( + member.make_schedules(door, schedulesMap))) else: # cardholder did not exist, so add them print(f"- Adding Member:") print(member) resp = door.doXMLRequest(ROOT( - E.Cardholders({"action": "AD"}, + E.Cardholders( + {"action": "AD"}, E.Cardholder(member.attribs())))) member.cardholderID = resp.find('{*}Cardholders/{*}Cardholder') \ .attrib["cardholderID"] - door.doXMLRequest(ROOT(member.make_credentials(member.credentials, cardFormats), - member.make_schedules(door, schedulesMap))) + door.doXMLRequest(ROOT( + member.make_credentials(member.credentials, cardFormats), + member.make_schedules(door, schedulesMap))) # TODO: delete cardholders that are no longer members? + def main(): memberData = getMembershipworksData( ['members', 'staff', 'misc'], @@ -209,4 +212,6 @@ def main(): print(door.name, door.ip) update_door(door, members) -main() + +if __name__ == '__main__': + main()