def parseLine(self, item, parser, package_configs): dir = self.get_config_dir() include_sections = [] if item is not "": if item.strip().startswith('from'): # from x.cfg import xmlpull annotation file_section = item.replace('from', '').strip().split('import') file = file_section[0].strip() config_file = dir + '/' + file if len(file_section) != 2: raise SyntaxError("Invalid syntax for include; possible missing import on '%s'" %item) if file_section[1].strip().__contains__(' '): section = re.split("\\s+", file_section[1].strip()) for sec in section: include_sections.append(sec) else: include_sections.append(file_section[1].strip()) elif item.strip().startswith("import"): # import x.cfg file = item.replace('import', '').strip() config_file = dir + "/" + file else: raise SyntaxError("Invalid syntax: %s ,Please verify that the syntax is correct " %item) parser.read(config_file) if os.path.dirname(config_file): config_path = os.path.dirname(config_file) else: config_path = os.getcwd() logging.info("Configuration file is %s and path %s", os.path.basename(config_file), + config_path) if include_sections.__len__() > 0: for include_section in include_sections: if include_section is not "": if not parser._sections.get(include_section): raise NoSectionError( "No such %s section exists in %s configuration" % (include_section, file)) parser._sections.get(include_section)['buildrequires'] = '' parser._sections.get(include_section)['config_type'] = 'bom-builder-meta' package_configs[include_section] = parser._sections.get(include_section) else: for section in parser.sections(): config_type = self.read_config_type(parser, section) # we need to ignore two bom-builder if section == 'common' or config_type == "bom-builder": logging.debug('Skipping import for section %s', section) continue if not parser._sections.get(section): raise NoSectionError("No such %s section exists in %s configuration" % (section, file)) parser._sections.get(section)['buildrequires'] = '' parser._sections.get(section)['config_type'] = 'bom-builder-meta' package_configs[section] = parser._sections.get(section)
def get(self, section, option, subsection=None): """Get an option""" if not section in self.sections(): raise NoSectionError(section) if subsection: if not subsection in self._sections[section]: raise NoSectionError(subsection) if not option in self._sections[section][subsection]: raise NoOptionError(option, subsection) return self._sections[section][subsection][option] if not option in self._sections[section]: raise NoOptionError(option, section) return self._sections[section][option]
def options(self, section, subsection=None): try: opts = self._sections[section].copy() except KeyError: raise NoSectionError(section) if subsection: try: opts = opts[subsection].copy() except KeyError: raise NoSectionError(subsection) opts = update(opts, self._defaults) if '__name__' in opts: del opts['__name__'] return opts.keys()
def validate_config(self): if not self.has_section('default'): raise NoSectionError('default') for name in self.defaults(): if not self.has_option('default', name): raise NoOptionError(name, 'default') sender = self.get('default', 'sender') if not self.has_section(sender): raise NoSectionError(sender) loglevel = self.get('default', 'loglevel') if not hasattr(logging, loglevel.upper()): raise ValueError('Unknown loglevel: %r' % (loglevel))
def set(self, section, option, value=None, subsection=None): """Set an option""" if not section: sectdict = self._defaults else: try: sectdict = self._sections[section] except KeyError: raise NoSectionError(section) if subsection: try: sectdict = sectdict[subsection] except KeyError: raise NoSectionError(subsection) sectdict[self.optionxform(option)] = value
def get_config(section, key): try: value = cfg.get(section, key) except: raise NoSectionError("config file %s has no section %s or no key %s" % (config_file, section, key)) return value
def on_btn_update_clicked(self, *args): try: login = const.CONFIG.get('auth', 'login') password = const.CONFIG.get('auth', 'password') if not (login and password): raise NoSectionError('auth') except (NoSectionError, NoOptionError): login, password, captcha = forms.get_auth_data() if not (login and password): return svc = transport.get_service(login, password) try: svc.ProgrammaticLogin() except service.CaptchaRequired: url = svc.captcha_url token = svc.captcha_token login, password, captcha = forms.get_auth_data(url) svc.ProgrammaticLogin(token, captcha) except service.BadAuthentication: # show the error pass self.idle_timer = gobject.idle_add(self._pulse) updater = UpdaterThread(svc, self.queue) updater.setDaemon(True) updater.start()
def options(self, section): if self.has_section(section): hkey = "%s:section:%s:options" % (self.namespace, section) options = self.redis.lrange(hkey, 0, self.redis.llen(hkey)) return options else: raise NoSectionError(section)
def items(self, section, raw=False, vars=None): """Return a list of tuples with (name, value) for each option in the section. All % interpolations are expanded in the return values, based on the defaults passed into the constructor, unless the optional argument `raw' is true. Additional substitutions may be provided using the `vars' argument, which must be a dictionary whose contents overrides any pre-existing defaults. The section DEFAULT is special. """ if section != DEFAULTSECT and not self.has_section(section): raise NoSectionError(section) if vars is None: options = list(self.data[section]) else: options = [] for x in self.data[section]: if x not in vars: options.append(x) options.extend(vars.keys()) if "__name__" in options: options.remove("__name__") d = ConfigDict(self, section, vars) if raw: return [(option, d[option]) for option in options] else: return [(option, self._interpolate(section, option, d[option], d)) for option in options]
def set(self, section, option, value=None): """Set an option.""" try: sectdict = self._sections[section] except KeyError: raise NoSectionError(section) sectdict[self.optionxform(option)] = value
def has_comment(self, section, option=None): """ Return if the given section heading or option within a section has an associated comment. @type section: str @type option: str or None @rtype: bool @raise NoSectionError: The provided section does not exist """ if not section: section = DEFAULTSECT if not self.has_section(section) and section != DEFAULTSECT: raise NoSectionError(section) if option: option = self.optionxform(option) if not self.has_option(section, option): raise NoOptionError(option, section) else: # looking for section comment option = '__name__' return bool( self._comments.get(section, False) and self._comments[section].get(option, False))
def get(self, section, option, raw=False, vars=None, fallback=_UNSET): d = self._defaults.copy() try: d.update(self._sections[section]) except KeyError: if section != DEFAULTSECT: raise NoSectionError(section) # Update the entry specific variables if vars: for key, value in vars.items(): d[self.optionxform(key)] = value option = self.optionxform(option) try: value = d[option] except KeyError: if fallback is _UNSET: raise NoOptionError(option, section) else: return fallback if raw or value is None: return value else: return self._interpolate(section, option, value, d)
def set_comment(self, comment, section, option=None): """ Set the comment for a section or option @type comment: str or None @type section: str @type option: str or None """ if not section: section = DEFAULTSECT if not self.has_section(section) and section != DEFAULTSECT: raise NoSectionError(section) if section not in self._comments: self._comments[section] = self._dict() if option: option = self.optionxform(option) if not self.has_option(section, option): raise NoOptionError(option, section) else: # setting section comment option = '__name__' self._comments[section][option] = comment
def __find_section(self, domain): l = self.sections() for section in iter(l): if section.lower() == domain.lower(): return section raise NoSectionError(domain)
def write_ini_config_file(self, command, data): """ Write the new command configuration in the plugin configuration file """ # read the config file config = ConfigParser() config.read(command.plugin.config.fileName) # if there is no commands section if not config.has_section('commands'): raise NoSectionError( 'could not find <commands> section in plugin <%s> config file' % data['plugin_name']) # remove the old entry found = False for temp in config.options('commands'): search = temp.split('-')[0] if search == command.command: config.remove_option('commands', temp) found = True # set the new command option value config.set('commands', data['command_name'], data['command_level']) self.debug('%s command <%s> in plugin <%s> config file', 'updated' if found else 'created new entry for', command.command, data['plugin_name']) # write the updated configuration file with open(command.plugin.config.fileName, 'wb') as configfile: config.write(configfile)
def has_option(self, section, option): """Check for the existence of a given option in a given section.""" if section in self.data: sec = self.data[section] else: raise NoSectionError(section) return (option in sec)
def parse(self, section, option, value): """Parse the value of an option. This method raises NoSectionError if an invalid section name is passed in. This method raises ValueError if the value is not parseable. """ if section == '__main__': option_obj = getattr(self.schema, option, None) else: section_obj = getattr(self.schema, section, None) if section_obj is not None: option_obj = getattr(section_obj, option, None) else: raise NoSectionError(section) if option_obj is not None: kwargs = {} if option_obj.require_parser: kwargs = {'parser': self} try: value = option_obj.parse(value, **kwargs) except ValueError, e: raise ValueError( "Invalid value '%s' for %s '%s' in" " section '%s'. Original exception was: %s" % (value, option_obj.__class__.__name__, option, section, e))
def remove_comment(self, section, option=None): """ Remove the comment from a section or option. @type section: str @param section: The section to remove from. @type option: str or None @param option: The option to remove from or None to remove a section's comment. @rtype: bool @return: True if a comment was removed and False if no comment was removed. """ if not section: section = DEFAULTSECT elif not self.has_section(section) and section != DEFAULTSECT: raise NoSectionError(section) if option: option = self.optionxform(option) if not self.has_option(section, option): raise NoOptionError(option, section) else: # setting section comment option = '__name__' del self._comments[section][option]
def get(self, section, option): if section == 'memcache': if option == 'memcache_servers': return '1.2.3.4:5' else: raise NoOptionError(option) else: raise NoSectionError(option)
def _read_value(parser, section, option): if not parser.has_section(section): raise NoSectionError("%s (available sections: %s)" % (section, sorted(parser.sections()))) if not parser.has_option(section, option): raise NoOptionError("%s (available options: %s)" % (option, sorted(parser.options(section))), section) else: return parser.get(section, option)
def items(self, section): if section in self.data: ans = [] for opt in self.data[section]: ans.append((opt, self.get(section, opt))) return ans else: raise NoSectionError(section)
def has_option(self, section, option): hkey = "%s:section:%s:option:%s" % (self.namespace, section, option) if self.has_section(section): if not self.redis.exists(hkey): raise NoOptionError(section, option) else: raise NoSectionError(section) return True
def items(self, section): try: ans = [] for opt in self.data[section]: ans.append((opt, self.data[section][opt])) return ans except KeyError: raise NoSectionError(section)
def validate_config(self): if not self.has_section('input'): raise NoSectionError('input') must_haves = ('file', 'type', 'tags', 'pattern') for key in must_haves: if not self.has_option('input', key): raise NoOptionError(key, 'input')
def items(self, section_name): if section_name != section: raise NoSectionError(section_name) return { 'memcache_servers': memcache_servers, 'memcache_serialization_support': memcache_serialization_support, 'memcache_max_connections': memcache_max_connections, }
def get(self, section, option, vars=None): if not self.has_section(section): raise NoSectionError(section) if vars is not None and option in vars: value = vars[option] try: return self.data[section][option] except KeyError: raise NoOptionError(option, section)
def del_section(self, section, subsection=None): """ Deletes a block section to the config. :param section: The section to delete. :param subsection: The section to delete. """ if subsection: if not self.has_section(section): raise NoSectionError(section) if not subsection in self._sections[section]: raise NoSectionError(subsection) del self._sections[section][subsection] else: if not self.has_section(section): raise NoSectionError(section) del self._sections[section]
def options(self, section, withDefault=False): """Return a list of option names for the given section name. Parameter `withDefault` controls the include of names from section `[DEFAULT]` """ try: return self._cfg.options(section, withDefault) except AttributeError: raise NoSectionError(section)
def parse_config_files(self, git_treeish=None): """ Parse the possible config files and set appropriate values default values """ parser = SafeConfigParser() # Fill in the built in values self.config = dict(self.__class__.defaults) # Update with the values from the defaults section. This is needed # in case the config file doesn't have a [<command>] section at all config_files = self.get_config_files() try: repo = GitRepository(".") except GitRepositoryError: repo = None # Read all config files for filename in config_files: self._read_config_file(parser, repo, filename, git_treeish) self.config.update(dict(parser.defaults())) # Make sure we read any legacy sections prior to the real subcommands # section i.e. read [gbp-pull] prior to [pull] if (self.command.startswith('gbp-') or self.command.startswith('git-')): oldcmd = self.command if parser.has_section(oldcmd): self.config.update(dict(parser.items(oldcmd, raw=True))) cmd = self.command[4:] else: for prefix in ['gbp', 'git']: oldcmd = '%s-%s' % (prefix, self.command) if parser.has_section(oldcmd): self.config.update(dict(parser.items(oldcmd, raw=True))) cmd = self.command # Update with command specific settings if parser.has_section(cmd): # Don't use items() until we got rid of the compat sections # since this pulls in the defaults again self.config.update(dict(parser._sections[cmd].items())) for section in self.sections: if parser.has_section(section): self.config.update(dict(parser._sections[section].items())) else: raise NoSectionError("Mandatory section [%s] does not exist." % section) # filter can be either a list or a string, always build a list: if self.config['filter']: if self.config['filter'].startswith('['): self.config['filter'] = eval(self.config['filter']) else: self.config['filter'] = [self.config['filter']] else: self.config['filter'] = []
def get(self, section, option, default=NoDefault): """ Get an option section=None: attribute a default section name default: default value (if not specified, an exception will be raised if option doesn't exist) """ section = self.__check_section_option(section, option) if not self.has_section(section): if default is NoDefault: raise NoSectionError(section) else: self.add_section(section) if not self.has_option(section, option): if default is NoDefault: raise NoOptionError(option, section) else: self.set(section, option, default) return default value = ConfigParser.get(self, section, option, self.raw) default_value = self.get_default(section, option) try: # really ugly from datetime import datetime value = datetime.strptime(value ,"%Y-%m-%d").date() return default_value except: pass if isinstance(default_value, bool): value = eval(value) elif isinstance(default_value, float): value = float(value) elif isinstance(default_value, int): value = int(value) else: if isinstance(default_value, basestring): try: value = value.decode('utf-8') except (UnicodeEncodeError, UnicodeDecodeError): pass try: # lists, tuples, ... value = eval(value) except: pass return value