chutney/garage_updater.py

36 lines
986 B
Python
Raw Normal View History

2022-12-26 19:37:50 -05:00
import os
import requests
from configparser import ConfigParser
class GarageUpdater:
def __init__(self, configFile):
config = ConfigParser()
config.read(configFile)
self.uri = config['garage'].get('uri')
self.garageTempFile = 'garage.tmp'
self.garageFile = 'garage.json'
self.persistGarageState('{"error": "init"}')
def updateGarageState(self):
try:
state = self.getGarageState()
self.persistGarageState(state)
2022-12-28 18:37:32 -05:00
except Exception as e:
2022-12-26 19:37:50 -05:00
print(e, flush=True)
2022-12-28 18:37:32 -05:00
self.persistGarageState('{"error": "exception"}')
2022-12-26 19:37:50 -05:00
def clearGarageState(self):
self.persistGarageState('{"error": "halted"}')
def getGarageState(self):
2022-12-30 15:19:28 -05:00
return requests.get(self.uri, timeout=6).text
2022-12-26 19:37:50 -05:00
def persistGarageState(self, state):
with open(self.garageTempFile, 'w') as file:
file.write(state)
os.replace(self.garageTempFile, self.garageFile)