2023-05-31 22:09:09 +01:00
|
|
|
import yaml, hashlib
|
|
|
|
import logger as log
|
|
|
|
import mongo
|
2023-05-08 19:55:09 +01:00
|
|
|
|
2023-05-31 22:09:09 +01:00
|
|
|
print("pis.py: PIS Module Loaded", "DBUG")
|
|
|
|
file_location :str = "/app/data/pis/gwr.yaml"
|
|
|
|
|
|
|
|
def runUpdate():
|
|
|
|
if (not requiresUpdate()):
|
|
|
|
log.out('pis.runUpdate: PIS Codes do not need updating', 'INFO')
|
|
|
|
return
|
|
|
|
log.out(f"pis.runUpdate: Update required", "INFO")
|
|
|
|
pis_data = load()
|
|
|
|
pis_parsed = parse(pis_data)
|
|
|
|
mongo.dropCollection("pis")
|
|
|
|
mongo.putMany("pis", pis_parsed)
|
|
|
|
|
|
|
|
def requiresUpdate():
|
|
|
|
currentHash = mongo.getMetaHash("pis")
|
|
|
|
with open(file_location, "r") as f:
|
|
|
|
text = f.read()
|
|
|
|
newHash = hashlib.md5(text.encode()).hexdigest()
|
|
|
|
if currentHash is None or newHash != currentHash:
|
|
|
|
log.out(f"pis.requiresUpdate: currentHash: {currentHash}, newHash: {newHash}", "INFO")
|
|
|
|
mongo.putMetaHash("pis", newHash)
|
|
|
|
return True
|
|
|
|
mongo.putMetaHash("pis", newHash)
|
|
|
|
return False
|
2023-05-08 19:55:09 +01:00
|
|
|
|
|
|
|
def load(): # Programatically add a `toc` field to each entry.
|
2023-05-31 22:09:09 +01:00
|
|
|
with open(file_location, "r") as data:
|
2023-05-08 19:55:09 +01:00
|
|
|
try:
|
|
|
|
pis = yaml.safe_load(data)
|
|
|
|
print(pis)
|
|
|
|
return pis["pis"]
|
|
|
|
except yaml.YAMLError as exc:
|
|
|
|
print(exc)
|
|
|
|
return exc
|
|
|
|
|
|
|
|
def parse(codeList):
|
|
|
|
StartLen = len(codeList)
|
|
|
|
print(f"pis.parse: codeList starting length: {StartLen}")
|
|
|
|
for i in codeList:
|
|
|
|
stops = i['stops']
|
|
|
|
code = i['code']
|
|
|
|
for ii in codeList:
|
|
|
|
if stops == ii['stops'] and code != ii['code']:
|
|
|
|
print(f"Identical stopping pattern found: {ii['code']}")
|
|
|
|
codeList.remove(ii)
|
|
|
|
print(f"pis.parse: Removed {StartLen - len(codeList)} duplicates")
|
2023-05-31 22:09:09 +01:00
|
|
|
return codeList
|