def __init__(self, name, container_old, container_cur, parent=None, set_sections=unspecified, add_sections=None, set_names=unspecified, add_names=None, set_tags=unspecified, add_tags=None, set_classes=unspecified, add_classes=None, inherit_sections=False): parent = parent or self if inherit_sections and isinstance(parent, TaggedConfigView): add_sections = (parent.get_class_section_list() or []) + (add_sections or []) SimpleConfigView.__init__(self, name, container_old, container_cur, parent, set_sections=set_sections, add_sections=add_sections) self._class_section_list = self._init_variable(parent, '_class_section_list', None, set_classes, add_classes, norm_config_locations, lambda x: x.config_section_list) self._section_name_list = self._init_variable(parent, '_section_name_list', [], set_names, add_names, norm_config_locations) def _get_tag_tuple(tag_obj): try: config_tag_name = tag_obj.config_tag_name.lower() except Exception: raise APIError('Class %r does not define a valid tag name!' % tag_obj.__class__.__name__) return [(config_tag_name, tag_obj.get_object_name().lower())] self._section_tag_list = self._init_variable(parent, '_section_tag_list', [], set_tags, add_tags, identity, _get_tag_tuple) self._section_tag_order = lmap(itemgetter(0), self._section_tag_list)
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, config_file_path=None, load_old_config=True, path_base=None): def _get_name(prefix=''): if config_file_path: return ('%s.%s' % (prefix, get_file_name(config_file_path))).strip('.') elif prefix: return prefix return 'unnamed' try: config_dn = os.getcwd() except Exception: raise ConfigError('The current directory does not exist!') if config_file_path: config_dn = os.path.dirname(resolve_path(config_file_path, search_path_list=[os.getcwd()], exception_type=ConfigError)) if path_base: config_dn = os.path.dirname(resolve_path(path_base, search_path_list=[os.getcwd()])) # Init config containers self._container_cur = ConfigContainer('current') if filler: # Read in the current configuration ... filler.fill(self._container_cur) self._container_cur.resolve() # resolve interpolations logging.getLogger('config.stored').propagate = False container_old = ConfigContainer('stored') container_old.enabled = False # Create config view and temporary config interface self._view = SimpleConfigView(_get_name(), container_old, self._container_cur) self._view.config_vault['path:search'] = UniqueList([os.getcwd(), config_dn]) # Determine work directory using config interface with "global" scope tmp_config = SimpleConfigInterface(self._view.get_view(set_sections=['global'])) work_dn_base = tmp_config.get_dn('workdir base', config_dn, must_exist=False) work_dn_default = os.path.join(work_dn_base, _get_name('work')) work_dn = tmp_config.get_dn('workdir', work_dn_default, must_exist=False) self._view.config_vault['path:work_dn'] = work_dn # tmp_config still has undefinied # Set dynamic plugin search path sys.path.extend(tmp_config.get_dn_list('plugin paths', [os.getcwd()])) # Determine and load stored config settings self._config_path_min = os.path.join(work_dn, 'current.conf') # Minimal config file self._config_path_old = os.path.join(work_dn, 'work.conf') # Config file with saved settings if load_old_config: if os.path.exists(self._config_path_old): GeneralFileConfigFiller([self._config_path_old]).fill(container_old) old_setting_file = os.path.join(work_dn, 'task.dat') if os.path.exists(old_setting_file): ConfigFiller.create_instance('CompatConfigFiller', old_setting_file).fill(container_old) container_old.enabled = True container_old.protect() # Get persistent variables - only possible after container_old was enabled self._view.set_config_name(tmp_config.get('config id', _get_name(), persistent=True))
def _getSection(self, specific): if specific: if self._cfgClassSections: section = self._cfgClassSections[-1] else: section = SimpleConfigView._getSection(self, specific) if self._cfgNames: section += ' %s' % str.join(' ', self._cfgNames) if self._cfgTags: section += ' %s' % str.join(' ', imap(lambda t: '%s:%s' % t, self._cfgTags)) return section elif self._cfgClassSections: return self._cfgClassSections[0] return SimpleConfigView._getSection(self, specific)
def _get_section(self, specific): if specific: if self._class_section_list: section = self._class_section_list[-1] else: section = SimpleConfigView._get_section(self, specific) if self._section_name_list: section += ' %s' % str.join(' ', self._section_name_list) if self._section_tag_list: section += ' %s' % str.join(' ', imap(lambda t: '%s:%s' % t, self._section_tag_list)) return section elif self._class_section_list: return self._class_section_list[0] return SimpleConfigView._get_section(self, specific)
def _getSection(self, specific): if specific: if self._cfgClassSections: section = self._cfgClassSections[-1] else: section = SimpleConfigView._getSection(self, specific) if self._cfgNames: section += ' %s' % str.join(' ', self._cfgNames) if self._cfgTags: section += ' %s' % str.join( ' ', imap(lambda t: '%s:%s' % t, self._cfgTags)) return section elif self._cfgClassSections: return self._cfgClassSections[0] return SimpleConfigView._getSection(self, specific)
def _get_section(self, specific): if specific: if self._class_section_list: section = self._class_section_list[-1] else: section = SimpleConfigView._get_section(self, specific) if self._section_name_list: section += ' %s' % str.join(' ', self._section_name_list) if self._section_tag_list: section += ' %s' % str.join( ' ', imap(lambda t: '%s:%s' % t, self._section_tag_list)) return section elif self._class_section_list: return self._class_section_list[0] return SimpleConfigView._get_section(self, specific)
def __init__(self, name, container_old, container_cur, parent=None, set_sections=unspecified, add_sections=None, set_names=unspecified, add_names=None, set_tags=unspecified, add_tags=None, set_classes=unspecified, add_classes=None, inherit_sections=False): parent = parent or self if inherit_sections and isinstance(parent, TaggedConfigView): add_sections = (parent.get_class_section_list() or []) + (add_sections or []) SimpleConfigView.__init__(self, name, container_old, container_cur, parent, set_sections=set_sections, add_sections=add_sections) self._class_section_list = self._init_variable( parent, '_class_section_list', None, set_classes, add_classes, norm_config_locations, lambda x: x.config_section_list) self._section_name_list = self._init_variable(parent, '_section_name_list', [], set_names, add_names, norm_config_locations) def _get_tag_tuple(tag_obj): try: config_tag_name = tag_obj.config_tag_name.lower() except Exception: raise APIError('Class %r does not define a valid tag name!' % tag_obj.__class__.__name__) return [(config_tag_name, tag_obj.get_object_name().lower())] self._section_tag_list = self._init_variable(parent, '_section_tag_list', [], set_tags, add_tags, identity, _get_tag_tuple) self._section_tag_order = lmap(itemgetter(0), self._section_tag_list)
def __init__(self, name, oldContainer, curContainer, parent=None, setSections=selectorUnchanged, addSections=None, setNames=selectorUnchanged, addNames=None, setTags=selectorUnchanged, addTags=None, setClasses=selectorUnchanged, addClasses=None, inheritSections=False): parent = parent or self if inheritSections and isinstance(parent, TaggedConfigView): addSections = (parent.getClassSections() or []) + (addSections or []) SimpleConfigView.__init__(self, name, oldContainer, curContainer, parent, setSections=setSections, addSections=addSections) self._initVariable(parent, '_cfgClassSections', None, setClasses, addClasses, standardConfigForm, lambda x: x.configSections) self._initVariable(parent, '_cfgNames', [], setNames, addNames, standardConfigForm) def makeTagTuple(t): try: tagName = t.tagName.lower() except Exception: raise APIError('Class %r does not define a valid tag name!' % t.__class__.__name__) return [(tagName, t.getObjectName().lower())] self._initVariable(parent, '_cfgTags', [], setTags, addTags, identity, makeTagTuple) self._cfgTagsOrder = lmap(lambda tagName_tagValue: tagName_tagValue[0], self._cfgTags)
def __init__(self, name, oldContainer, curContainer, parent = None, setSections = selectorUnchanged, addSections = None, setNames = selectorUnchanged, addNames = None, setTags = selectorUnchanged, addTags = None, setClasses = selectorUnchanged, addClasses = None, inheritSections = False): if inheritSections and parent: addSections = parent.getClassSections() + (addSections or []) SimpleConfigView.__init__(self, name, oldContainer, curContainer, parent, setSections = setSections, addSections = addSections) self._initVariable('_cfgClassSections', None, setClasses, addClasses, standardConfigForm, lambda x: x.configSections) self._initVariable('_cfgNames', [], setNames, addNames, standardConfigForm) def makeTagTuple(t): try: tagName = t.tagName.lower() except Exception: raise APIError('Class %r does not define a valid tag name!' % t.__class__.__name__) return [(tagName, t.getObjectName().lower())] self._initVariable('_cfgTags', [], setTags, addTags, identity, makeTagTuple) self._cfgTagsOrder = lmap(lambda tagName_tagValue: tagName_tagValue[0], self._cfgTags)
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' )
class ConfigFactory(object): # Main config interface def __init__(self, filler=None, config_file_path=None, load_old_config=True, path_base=None): def _get_name(prefix=''): if config_file_path: return ('%s.%s' % (prefix, get_file_name(config_file_path))).strip('.') elif prefix: return prefix return 'unnamed' try: config_dn = os.getcwd() except Exception: raise ConfigError('The current directory does not exist!') if config_file_path: config_dn = os.path.dirname(resolve_path(config_file_path, search_path_list=[os.getcwd()], exception_type=ConfigError)) if path_base: config_dn = os.path.dirname(resolve_path(path_base, search_path_list=[os.getcwd()])) # Init config containers self._container_cur = ConfigContainer('current') if filler: # Read in the current configuration ... filler.fill(self._container_cur) self._container_cur.resolve() # resolve interpolations logging.getLogger('config.stored').propagate = False container_old = ConfigContainer('stored') container_old.enabled = False # Create config view and temporary config interface self._view = SimpleConfigView(_get_name(), container_old, self._container_cur) self._view.config_vault['path:search'] = UniqueList([os.getcwd(), config_dn]) # Determine work directory using config interface with "global" scope tmp_config = SimpleConfigInterface(self._view.get_view(set_sections=['global'])) work_dn_base = tmp_config.get_dn('workdir base', config_dn, must_exist=False) work_dn_default = os.path.join(work_dn_base, _get_name('work')) work_dn = tmp_config.get_dn('workdir', work_dn_default, must_exist=False) self._view.config_vault['path:work_dn'] = work_dn # tmp_config still has undefinied # Set dynamic plugin search path sys.path.extend(tmp_config.get_dn_list('plugin paths', [os.getcwd()])) # Determine and load stored config settings self._config_path_min = os.path.join(work_dn, 'current.conf') # Minimal config file self._config_path_old = os.path.join(work_dn, 'work.conf') # Config file with saved settings if load_old_config: if os.path.exists(self._config_path_old): GeneralFileConfigFiller([self._config_path_old]).fill(container_old) old_setting_file = os.path.join(work_dn, 'task.dat') if os.path.exists(old_setting_file): ConfigFiller.create_instance('CompatConfigFiller', old_setting_file).fill(container_old) container_old.enabled = True container_old.protect() # Get persistent variables - only possible after container_old was enabled self._view.set_config_name(tmp_config.get('config id', _get_name(), persistent=True)) def freeze(self, write_config=True, show_unused=True, raise_on_change=True): # Inform the user about unused options def _match_unused_entries(entry): return ('!' not in entry.section) and not entry.accessed self._container_cur.protect(raise_on_change) if show_unused: unused = lfilter(_match_unused_entries, self._view.iter_entries()) 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(print_section=True)) if write_config or not os.path.exists(self._config_path_old): ensure_dir_exists(os.path.dirname(self._config_path_old), 'config storage directory', ConfigError) # Write user friendly, flat config file and config file with saved settings self._write_file(self._config_path_min, print_minimal=True, print_default=False, print_workdir=True, print_unused=False) self._write_file(self._config_path_old, print_minimal=True, print_default=True, print_source=True, print_unused=True, msg='; ==> DO NOT EDIT THIS FILE! <==\n; This file is used to find config changes!\n') def get_config(self): result = SimpleConfigInterface(self._view) result.factory = self return result def _write_file(self, fn, msg=None, **kwargs): def _write_msg_view(fp): if msg is not None: fp.write(msg) self._view.write(fp, **kwargs) with_file(SafeFile(fn, 'w'), _write_msg_view)