Exception handling

main2
Bob Mottram 2019-09-30 23:05:43 +01:00
parent 6a6f814e2c
commit 358cd2ea40
1 changed files with 17 additions and 8 deletions

View File

@ -18,8 +18,11 @@ def createConfig(baseDir: str) -> None:
return return
configJson = { configJson = {
} }
with open(configFilename, 'w') as fp: try:
commentjson.dump(configJson, fp, indent=4, sort_keys=False) with open(configFilename, 'w') as fp:
commentjson.dump(configJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def setConfigParam(baseDir: str, variableName: str, variableValue) -> None: def setConfigParam(baseDir: str, variableName: str, variableValue) -> None:
"""Sets a configuration value """Sets a configuration value
@ -29,16 +32,22 @@ def setConfigParam(baseDir: str, variableName: str, variableValue) -> None:
with open(configFilename, 'r') as fp: with open(configFilename, 'r') as fp:
configJson=commentjson.load(fp) configJson=commentjson.load(fp)
configJson[variableName]=variableValue configJson[variableName]=variableValue
with open(configFilename, 'w') as fp: try:
commentjson.dump(configJson, fp, indent=4, sort_keys=False) with open(configFilename, 'w') as fp:
commentjson.dump(configJson, fp, indent=4, sort_keys=False)
except Exception as e:
print(e)
def getConfigParam(baseDir: str, variableName: str): def getConfigParam(baseDir: str, variableName: str):
"""Gets a configuration value """Gets a configuration value
""" """
createConfig(baseDir) createConfig(baseDir)
configFilename=baseDir+'/config.json' configFilename=baseDir+'/config.json'
with open(configFilename, 'r') as fp: try:
configJson=commentjson.load(fp) with open(configFilename, 'r') as fp:
if configJson.get(variableName): configJson=commentjson.load(fp)
return configJson[variableName] if configJson.get(variableName):
return configJson[variableName]
except Exception as e:
print(e)
return None return None