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 27defloadconf(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) 37try: 38 success = prefs.read(configpaths) 39print("Loaded{0}config file(s):{1}".format( 40str(len(success)),str(success))) 41exceptExceptionas e: 42print("Error processing config: "+str(e)) 43return prefs 44 45defexport_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)