Websocket server implementing a clock handling alarms and timezones
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import argparse
  2. import configparser
  3. from .errors import ConfigError
  4. # Config file section
  5. CONFIG_SECTION = 'pyws_clock'
  6. # Maps config key name to argparse arg name and type
  7. CONFIG_ARGS = {
  8. 'port': ('port', int),
  9. 'listen': ('listen_address', str),
  10. 'session_directory': ('session_dir', str),
  11. 'loglevel': ('verbose', int),
  12. 'logfile': ('logfile', str),
  13. }
  14. def parse_config(filename:str, args:argparse.Namespace):
  15. """ Updates args parsed by argparse using a configuration file
  16. @param filename : configuration file name
  17. @param args : Namespace returned by ArgumentParser.parse_args()
  18. @return updated Namespace
  19. @note args is modified by reference
  20. """
  21. config = configparser.ConfigParser()
  22. config.read(filename)
  23. if CONFIG_SECTION not in config:
  24. err = 'There is no section named %r in %r' % (CONFIG_SECTION, filename)
  25. raise ConfigError(err)
  26. err_key = []
  27. for key in config[CONFIG_SECTION]:
  28. if key not in CONFIG_ARGS:
  29. err_key.append(key)
  30. continue
  31. argname, argtype = CONFIG_ARGS[key]
  32. setattr(args, argname, argtype(config[CONFIG_SECTION][key]))
  33. if len(err_key) > 0:
  34. err = 'Invalid configuration key in %r : %r' % (filename,
  35. ','.join(err_key))
  36. raise ConfigError(err)
  37. return args