Skip to content

Commit

Permalink
Improved configuration loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Luois45 committed Jun 25, 2022
1 parent 674f5ef commit 61301cb
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions activate_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@

log = logging.getLogger('urbanGUI')

try:
config = json.load(open("config.json"))
log.debug("Found config file")
except FileNotFoundError:
log.debug("Couldn't find config file")

def loadConfig():
try:
with open('config.json', 'r') as f:
config = json.load(f)
return config
except FileNotFoundError:
return createConfig()
except json.decoder.JSONDecodeError:
print(
"Config file is not valid JSON. Please delete the config file and run the script again."
)
sys.exit(0)


def createConfig():
config = {
"IPC": {
"host": "http://localhost:1242",
Expand All @@ -45,9 +56,10 @@
with open("config.json", "w") as f:
f.write(json.dumps(config))
log.debug("Saved config file")
except json.JSONDecodeError:
log.error("Couldn't decode config to json")
sys.exit()
return config


config = loadConfig()


async def activatePackages(asf, tries):
Expand Down

0 comments on commit 61301cb

Please sign in to comment.