def getName(prefix=''): if configFilePath: return ('%s.%s' % (prefix, utils.getRootName(configFilePath))).strip('.') elif prefix: return prefix return 'unnamed'
def _fillContentWithIncludes(self, configFile, searchPaths, configContent): log = logging.getLogger(('config.%s' % utils.getRootName(configFile)).rstrip('.').lower()) log.log(logging.INFO1, 'Reading config file %s', configFile) configFile = utils.resolvePath(configFile, searchPaths, ErrorClass = ConfigError) configFileLines = SafeFile(configFile).readlines() # Single pass, non-recursive list retrieval tmpConfigContent = {} self._fillContentSingleFile(configFile, configFileLines, searchPaths, tmpConfigContent) def getFlatList(section, option): for (opt, value, src) in tmpConfigContent.get(section, []): try: if opt == option: for entry in parseList(value, None): yield entry except Exception: raise ConfigError('Unable to parse [%s] %s from %s' % (section, option, src)) newSearchPaths = [os.path.dirname(configFile)] # Add entries from include statement recursively for includeFile in getFlatList('global', 'include'): self._fillContentWithIncludes(includeFile, searchPaths + newSearchPaths, configContent) # Process all other entries in current file self._fillContentSingleFile(configFile, configFileLines, searchPaths, configContent) # Override entries in current config file for overrideFile in getFlatList('global', 'include override'): self._fillContentWithIncludes(overrideFile, searchPaths + newSearchPaths, configContent) # Filter special global options if configContent.get('global', []): configContent['global'] = lfilter(lambda opt_v_s: opt_v_s[0] not in ['include', 'include override'], configContent['global']) return searchPaths + newSearchPaths
def _parseFile(self, container, configFile, defaults = None, searchPaths = []): try: configFile = utils.resolvePath(configFile, searchPaths, ErrorClass = ConfigError) log = logging.getLogger(('config.%s' % utils.getRootName(configFile)).rstrip('.')) log.log(logging.INFO1, 'Reading config file %s' % configFile) for line in map(lambda x: x.rstrip() + '=:', open(configFile, 'r').readlines()): if line.startswith('[') or line.lstrip().startswith(';'): continue # skip section and comment lines # Abort if non-indented line with ":" preceeding "=" was found if (line.lstrip() == line) and (line.find(":") < line.find("=")): raise ConfigError('Invalid config line:\n\t%s\nPlease use "key = value" syntax or indent values!' % line) # Parse with python config parser parser = ConfigParser.ConfigParser(defaults) parser.readfp(open(configFile, 'r')) # Parse include files if parser.has_option('global', 'include'): includeFiles = parser.get('global', 'include').split('#')[0].split(';')[0] for includeFile in utils.parseList(includeFiles, None): self._parseFile(container, includeFile, parser.defaults(), searchPaths + [os.path.dirname(configFile)]) # Store config settings for section in parser.sections(): for option in parser.options(section): if (section, option) != ('global', 'include'): value_list = parser.get(section, option).splitlines() # Strip comments value_list = map(lambda l: l.rsplit(';', 1)[0].strip(), value_list) value_list = filter(lambda l: l != '', value_list) container.setEntry(section, option, str.join('\n', value_list), configFile) except: raise RethrowError('Error while reading configuration file "%s"!' % configFile, ConfigError)
def _fillContentFromFile(self, configFile, searchPaths, configContent = {}): log = logging.getLogger(('config.%s' % utils.getRootName(configFile)).rstrip('.')) log.log(logging.INFO1, 'Reading config file %s' % configFile) configFile = utils.resolvePath(configFile, searchPaths, ErrorClass = ConfigError) configFileData = open(configFile, 'r').readlines() # Single pass, non-recursive list retrieval tmpConfigContent = {} self._fillContentFromSingleFile(configFile, configFileData, searchPaths, tmpConfigContent) def getFlatList(section, option): for (opt, value, s) in filter(lambda (opt, v, s): opt == option, tmpConfigContent.get(section, [])): for entry in utils.parseList(value, None): yield entry
def getName(prefix = ''): if configFilePath: return ('%s.%s' % (prefix, utils.getRootName(configFilePath))).strip('.') elif prefix: return prefix return 'unnamed'