From 33c0857e1b2feba50905fe24f71c8fad154dfca1 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Sat, 12 May 2018 09:49:51 -0400 Subject: [PATCH] Move CSV update functions into common.py --- common.py | 34 ++++++++++++++++++++++++++++++++++ doorUpdater.py | 36 ++---------------------------------- 2 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 common.py diff --git a/common.py b/common.py new file mode 100644 index 0000000..97d751f --- /dev/null +++ b/common.py @@ -0,0 +1,34 @@ +import urllib3 +from xml.etree import ElementTree as ET +import requests + +from passwords import * + +# it's fine, ssl certs are for losers anyway +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + +fieldnames = "CardNumber,CardFormat,PinRequired,PinCode,ExtendedAccess,ExpiryDate,Forename,Initial,Surname,Email,Phone,Custom1,Custom2,Schedule1,Schedule2,Schedule3,Schedule4,Schedule5,Schedule6,Schedule7,Schedule8".split(",") + +def doImportRequest(ip, params=None, files=None): + """Send a request to the door control import script""" + r = requests.post( + 'https://' + ip + '/cgi-bin/import.cgi', + params=params, + files=files, + auth=requests.auth.HTTPDigestAuth(DOOR_USERNAME, DOOR_PASSWORD), + timeout=10, + verify=False) # ignore insecure SSL + xml = ET.XML(r.text) + if r.status_code != 200 \ + or len(xml.findall("{http://www.hidglobal.com/VertX}Error")) > 0: + print("Door Updating Error: ", r.status_code, r.reason) + print(r.text) + sys.exit(1) + +def doCSVImport(doorIP, csv): + """Do the CSV import procedure on a door control""" + doImportRequest(doorIP, {"task": "importInit"}) + doImportRequest(doorIP, + {"task": "importCardsPeople", "name": "cardspeopleschedule.csv"}, + {"importCardsPeopleButton": ("cardspeopleschedule.csv", csv, 'text/csv')}) + doImportRequest(doorIP, {"task": "importDone"}) diff --git a/doorUpdater.py b/doorUpdater.py index 2f07082..d30553f 100755 --- a/doorUpdater.py +++ b/doorUpdater.py @@ -3,17 +3,10 @@ import sys import requests import csv from io import StringIO -import urllib3 from hashlib import md5 -from xml.etree import ElementTree as ET import os -from passwords import * - -# it's fine, ssl certs are for losers anyway -urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) - -fieldnames = "CardNumber,CardFormat,PinRequired,PinCode,ExtendedAccess,ExpiryDate,Forename,Initial,Surname,Email,Phone,Custom1,Custom2,Schedule1,Schedule2,Schedule3,Schedule4,Schedule5,Schedule6,Schedule7,Schedule8".split(",") +from common import * # mapping of member levels to schedules memberLevels = {"CMS Staff": "7x24", @@ -108,31 +101,6 @@ def makeMember(member, doorAuth): return out -def doRequest(ip, params=None, files=None): - """Send a request to the door control import script""" - r = requests.post( - 'https://' + ip + '/cgi-bin/import.cgi', - params=params, - files=files, - auth=requests.auth.HTTPDigestAuth(DOOR_USERNAME, DOOR_PASSWORD), - timeout=10, - verify=False) # ignore insecure SSL - xml = ET.XML(r.text) - if r.status_code != 200 \ - or len(xml.findall("{http://www.hidglobal.com/VertX}Error")) > 0: - print("Door Updating Error: ", r.status_code, r.reason) - print(r.text) - sys.exit(1) - -def updateDoor(doorIP, csv): - """Do the update import procedure on a door control""" - - doRequest(doorIP, {"task": "importInit"}) - doRequest(doorIP, - {"task": "importCardsPeople", "name": "cardspeopleschedule.csv"}, - {"importCardsPeopleButton": ("cardspeopleschedule.csv", csv, 'text/csv')}) - doRequest(doorIP, {"task": "importDone"}) - def makeDoor(doorName, doorIPs, members): """Create a CSV for the given door""" outString = StringIO() @@ -146,7 +114,7 @@ def makeDoor(doorName, doorIPs, members): print(outString.getvalue()) for doorIP in doorIPs: outString.seek(0) - updateDoor(doorIP, outString) + doCSVImport(doorIP, outString) pass def main():