def __init__(self, filler=None, configFilePath=None): def getName(prefix=''): if configFilePath: return ('%s.%s' % (prefix, utils.getRootName(configFilePath))).strip('.') elif prefix: return prefix return 'unnamed' pathMain = os.getcwd() if configFilePath: pathMain = os.path.dirname( utils.resolvePath(configFilePath, searchPaths=[os.getcwd()], ErrorClass=ConfigError)) # Init config containers self._curContainer = ConfigContainer('current') if filler: # Read in the current configuration ... filler.fill(self._curContainer) logging.getLogger('config.stored').propagate = False oldContainer = ConfigContainer('stored') oldContainer.enabled = False # Create config view and temporary config interface self._view = SimpleConfigView(getName(), oldContainer, self._curContainer) self._view.pathDict['search_paths'] = UniqueList( [os.getcwd(), pathMain]) # Determine work directory using config interface with "global" scope tmpInterface = SimpleConfigInterface( self._view.getView(setSections=['global'])) wdBase = tmpInterface.getPath('workdir base', pathMain, mustExist=False) pathWork = tmpInterface.getPath('workdir', os.path.join(wdBase, getName('work')), mustExist=False) self._view.pathDict[ '<WORKDIR>'] = pathWork # tmpInterface still has undefinied # Set dynamic plugin search path sys.path.extend(tmpInterface.getPaths('plugin paths', [os.getcwd()])) # Determine and load stored config settings self._flatCfgPath = os.path.join(pathWork, 'current.conf') # Minimal config file self._oldCfgPath = os.path.join( pathWork, 'work.conf') # Config file with saved settings if os.path.exists(self._oldCfgPath): GeneralFileConfigFiller([self._oldCfgPath]).fill(oldContainer) CompatConfigFiller(os.path.join(pathWork, 'task.dat')).fill(oldContainer) oldContainer.enabled = True oldContainer.setReadOnly() # Get persistent variables - only possible after oldContainer was enabled self._view.setConfigName( tmpInterface.get('config id', getName(), persistent=True))
def __init__(self, filler = None, configFilePath = None): def getName(prefix = ''): if configFilePath: return ('%s.%s' % (prefix, utils.getRootName(configFilePath))).strip('.') elif prefix: return prefix return 'unnamed' try: pathMain = os.getcwd() except Exception: raise ConfigError('The current directory does not exist!') if configFilePath: pathMain = os.path.dirname(utils.resolvePath(configFilePath, searchPaths = [os.getcwd()], ErrorClass = ConfigError)) # Init config containers self._curContainer = ConfigContainer('current') if filler: # Read in the current configuration ... filler.fill(self._curContainer) self._curContainer.resolve() # resolve interpolations logging.getLogger('config.stored').propagate = False oldContainer = ConfigContainer('stored') oldContainer.enabled = False # Create config view and temporary config interface self._view = SimpleConfigView(getName(), oldContainer, self._curContainer) self._view.pathDict['search_paths'] = UniqueList([os.getcwd(), pathMain]) # Determine work directory using config interface with "global" scope tmp_config = SimpleConfigInterface(self._view.getView(setSections = ['global'])) wdBase = tmp_config.getPath('workdir base', pathMain, mustExist = False) pathWork = tmp_config.getPath('workdir', os.path.join(wdBase, getName('work')), mustExist = False) self._view.pathDict['<WORKDIR>'] = pathWork # tmp_config still has undefinied # Set dynamic plugin search path sys.path.extend(tmp_config.getPaths('plugin paths', [os.getcwd()])) # Determine and load stored config settings self._flatCfgPath = os.path.join(pathWork, 'current.conf') # Minimal config file self._oldCfgPath = os.path.join(pathWork, 'work.conf') # Config file with saved settings if os.path.exists(self._oldCfgPath): GeneralFileConfigFiller([self._oldCfgPath]).fill(oldContainer) CompatConfigFiller(os.path.join(pathWork, 'task.dat')).fill(oldContainer) oldContainer.enabled = True oldContainer.setReadOnly() # Get persistent variables - only possible after oldContainer was enabled self._view.setConfigName(tmp_config.get('config id', getName(), persistent = True))
class ConfigFactory(object): def __init__(self, filler = None, configFilePath = None): def getName(prefix = ''): if configFilePath: return ('%s.%s' % (prefix, utils.getRootName(configFilePath))).strip('.') elif prefix: return prefix return 'unnamed' try: pathMain = os.getcwd() except Exception: raise ConfigError('The current directory does not exist!') if configFilePath: pathMain = os.path.dirname(utils.resolvePath(configFilePath, searchPaths = [os.getcwd()], ErrorClass = ConfigError)) # Init config containers self._curContainer = ConfigContainer('current') if filler: # Read in the current configuration ... filler.fill(self._curContainer) self._curContainer.resolve() # resolve interpolations logging.getLogger('config.stored').propagate = False oldContainer = ConfigContainer('stored') oldContainer.enabled = False # Create config view and temporary config interface self._view = SimpleConfigView(getName(), oldContainer, self._curContainer) self._view.pathDict['search_paths'] = UniqueList([os.getcwd(), pathMain]) # Determine work directory using config interface with "global" scope tmp_config = SimpleConfigInterface(self._view.getView(setSections = ['global'])) wdBase = tmp_config.getPath('workdir base', pathMain, mustExist = False) pathWork = tmp_config.getPath('workdir', os.path.join(wdBase, getName('work')), mustExist = False) self._view.pathDict['<WORKDIR>'] = pathWork # tmp_config still has undefinied # Set dynamic plugin search path sys.path.extend(tmp_config.getPaths('plugin paths', [os.getcwd()])) # Determine and load stored config settings self._flatCfgPath = os.path.join(pathWork, 'current.conf') # Minimal config file self._oldCfgPath = os.path.join(pathWork, 'work.conf') # Config file with saved settings if os.path.exists(self._oldCfgPath): GeneralFileConfigFiller([self._oldCfgPath]).fill(oldContainer) CompatConfigFiller(os.path.join(pathWork, 'task.dat')).fill(oldContainer) oldContainer.enabled = True oldContainer.setReadOnly() # Get persistent variables - only possible after oldContainer was enabled self._view.setConfigName(tmp_config.get('config id', getName(), persistent = True)) def getConfig(self, **kwargs): result = SimpleConfigInterface(self._view) if kwargs: result = result.changeView(**kwargs) result.factory = self return result def _write_file(self, fn, message = None, **kwargs): fp = SafeFile(fn, 'w') if message is not None: fp.write(message) self._view.write(fp, **kwargs) fp.close() def freezeConfig(self, writeConfig = True): self._curContainer.setReadOnly() # Inform the user about unused options unused = lfilter(lambda entry: ('!' not in entry.section) and not entry.accessed, self._view.iterContent()) log = logging.getLogger('config.freeze') if unused: log.log(logging.INFO1, 'There are %s unused config options!', len(unused)) for entry in unused: log.log(logging.INFO1, '\t%s', entry.format(printSection = True)) if writeConfig or not os.path.exists(self._oldCfgPath): if not os.path.exists(os.path.dirname(self._oldCfgPath)): os.makedirs(os.path.dirname(self._oldCfgPath)) # Write user friendly, flat config file and config file with saved settings self._write_file(self._flatCfgPath, printDefault = False, printUnused = False, printMinimal = True) self._write_file(self._oldCfgPath, printDefault = True, printUnused = True, printMinimal = True, printSource = True, message = '; ==> DO NOT EDIT THIS FILE! <==\n; This file is used to find config changes!\n')
class ConfigFactory(object): def __init__(self, filler=None, configFilePath=None): def getName(prefix=''): if configFilePath: return ('%s.%s' % (prefix, utils.getRootName(configFilePath))).strip('.') elif prefix: return prefix return 'unnamed' pathMain = os.getcwd() if configFilePath: pathMain = os.path.dirname( utils.resolvePath(configFilePath, searchPaths=[os.getcwd()], ErrorClass=ConfigError)) # Init config containers self._curContainer = ConfigContainer('current') if filler: # Read in the current configuration ... filler.fill(self._curContainer) logging.getLogger('config.stored').propagate = False oldContainer = ConfigContainer('stored') oldContainer.enabled = False # Create config view and temporary config interface self._view = SimpleConfigView(getName(), oldContainer, self._curContainer) self._view.pathDict['search_paths'] = UniqueList( [os.getcwd(), pathMain]) # Determine work directory using config interface with "global" scope tmpInterface = SimpleConfigInterface( self._view.getView(setSections=['global'])) wdBase = tmpInterface.getPath('workdir base', pathMain, mustExist=False) pathWork = tmpInterface.getPath('workdir', os.path.join(wdBase, getName('work')), mustExist=False) self._view.pathDict[ '<WORKDIR>'] = pathWork # tmpInterface still has undefinied # Set dynamic plugin search path sys.path.extend(tmpInterface.getPaths('plugin paths', [os.getcwd()])) # Determine and load stored config settings self._flatCfgPath = os.path.join(pathWork, 'current.conf') # Minimal config file self._oldCfgPath = os.path.join( pathWork, 'work.conf') # Config file with saved settings if os.path.exists(self._oldCfgPath): GeneralFileConfigFiller([self._oldCfgPath]).fill(oldContainer) CompatConfigFiller(os.path.join(pathWork, 'task.dat')).fill(oldContainer) oldContainer.enabled = True oldContainer.setReadOnly() # Get persistent variables - only possible after oldContainer was enabled self._view.setConfigName( tmpInterface.get('config id', getName(), persistent=True)) def getConfig(self, **kwargs): result = SimpleConfigInterface(self._view) if kwargs: result = result.changeView(**kwargs) result.factory = self return result def _write_file(self, fn, message=None, **kwargs): fp = SafeFile(fn, 'w') if message is not None: fp.write(message) self._view.write(fp, **kwargs) fp.close() def freezeConfig(self, writeConfig=True): self._curContainer.setReadOnly() # Inform the user about unused options unused = lfilter( lambda entry: ('!' not in entry.section) and not entry.accessed, self._view.iterContent()) log = logging.getLogger('config.freeze') if unused: log.log(logging.INFO1, 'There are %s unused config options!', len(unused)) for entry in unused: log.log(logging.INFO1, '\t%s', entry.format(printSection=True)) if writeConfig or not os.path.exists(self._oldCfgPath): if not os.path.exists(os.path.dirname(self._oldCfgPath)): os.makedirs(os.path.dirname(self._oldCfgPath)) # Write user friendly, flat config file and config file with saved settings self._write_file(self._flatCfgPath, printDefault=False, printUnused=False, printMinimal=True) self._write_file( self._oldCfgPath, printDefault=True, printUnused=True, printMinimal=True, printSource=True, message= '; ==> DO NOT EDIT THIS FILE! <==\n; This file is used to find config changes!\n' )