config.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. __filename__ = "config.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "1.0.0"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import os
  9. import json
  10. import commentjson
  11. def createConfig(baseDir: str) -> None:
  12. """Creates a configuration file
  13. """
  14. configFilename=baseDir+'/config.json'
  15. if os.path.isfile(configFilename):
  16. return
  17. configJson = {
  18. }
  19. with open(configFilename, 'w') as fp:
  20. commentjson.dump(configJson, fp, indent=4, sort_keys=True)
  21. def setConfigParam(baseDir: str, variableName: str, variableValue) -> None:
  22. """Sets a configuration value
  23. """
  24. createConfig(baseDir)
  25. configFilename=baseDir+'/config.json'
  26. with open(configFilename, 'r') as fp:
  27. configJson=commentjson.load(fp)
  28. configJson[variableName]=variableValue
  29. with open(configFilename, 'w') as fp:
  30. commentjson.dump(configJson, fp, indent=4, sort_keys=True)
  31. def getConfigParam(baseDir: str, variableName: str) -> None:
  32. """Gets a configuration value
  33. """
  34. createConfig(baseDir)
  35. configFilename=baseDir+'/config.json'
  36. with open(configFilename, 'r') as fp:
  37. configJson=commentjson.load(fp)
  38. if configJson.get(variableName):
  39. return configJson[variableName]
  40. return None