ppt_control / config.pyon commit add config file comments, minor JS refactoring (ae22cb8)
   1from configparser import ConfigParser
   2
   3prefs = None
   4
   5defaults = {
   6        'Main': {
   7            'logging': 'info',
   8            'cache': r'''C:\Windows\Temp\ppt-cache''',
   9            'cache_format': 'JPG',
  10            'cache_timeout': 5*60,
  11            'cache_init': True,
  12            'blackwhite': 'both',
  13            'refresh': 2,
  14            'disable_protected': True
  15        },
  16        'HTTP': {
  17            'interface': '',
  18            'port': 80
  19        },
  20        'WebSocket': {
  21            'interface': '0.0.0.0',
  22            'port': 5678
  23        }
  24}
  25
  26
  27def loadconf(configpaths):
  28    """
  29    Initial setup for a ConfigParser object. `configpaths` should be a list of
  30    configuration files to load (typically only one). To use the generated
  31    ConfigParser, use `import logparse.config` and then `config.prefs.get(..)`.
  32    The prefs object is returned after creation as a convenience but this method
  33    should only be called once per runtime.
  34    """
  35    prefs = ConfigParser()
  36    prefs.read_dict(defaults)
  37    try:
  38        success = prefs.read(configpaths)
  39        print("Loaded {0} config file(s): {1}".format(
  40                str(len(success)), str(success)))
  41    except Exception as e:
  42        print("Error processing config: " + str(e))
  43    return prefs
  44
  45def export_defaults(file):
  46    """
  47    Write the default settings to a file object, including comments and with spaces around 
  48    delimeters. This function is intended to be used to populate the config file with defaults
  49    after installation.
  50    """
  51    prefs = loadconf([])
  52    prefs.write(file, space_around_delimiters=True)