def _parse(self, path): cfg = RawConfigParser(allow_no_value=True) cfg.optionxform = str cfg.read(path) def parse_trigger(str): val = str.split(" ") args = dict(map(lambda x: (x.split("=")[0], x.split("=")[1]), val[2:])) trigger = Triggers().get(val[1], args) trigger.roxbury = Players().get(val[0]) return trigger for key in cfg.defaults(): value = cfg.defaults()[key] if key == "shuffle" and value == "true": self._shuffle = True if key[:7] == "trigger": trigger = parse_trigger(value) trigger.add_playlist(self) for section in cfg.sections(): pl = Playlist() for (key, value) in cfg.items(section): if not value: pl.add(key) if key == "cron": pl.schedule(value) if key == "shuffle" and value == "true": pl._shuffle = True if key[:7] == "trigger": trigger = parse_trigger(value) trigger.add_playlist(pl) self.add(pl)
def initConfig(self): config = RawConfigParser() config.optionxform = str config.read('robot.cfg') for (item, value) in config.items('Robot'): setattr(self, item, float(value)) print("Robot config loaded")
def make_vmu(in_path, out_base): path_conf = out_base + '.vmu' path_0 = out_base + '.l0' path_1 = out_base + '.l1' with OpenSlide(in_path) as osr: l0 = VmuLevel(osr, 0) l1 = VmuLevel(osr, osr.get_best_level_for_downsample(32)) for i, l in enumerate([l0, l1]): print 'Level %d: %d pixels/column' % (i, l.column_width) l0.save(path_0) l1.save(path_1) section = 'Uncompressed Virtual Microscope Specimen' conf = { 'NoLayers': '1', 'ImageFile': path_0, 'MapFile': path_1, 'BitsPerPixel': '36', 'PixelOrder': 'RGB', } c = RawConfigParser() c.optionxform = str c.add_section(section) for k, v in conf.iteritems(): c.set(section, k, v) with open(path_conf, 'w') as fh: c.write(fh)
def save(): """Saves FileDirectives into ConfigFile.""" section = "*" module = sys.modules[__name__] parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names parser.add_section(section) try: f, fname = open(ConfigFile, "wb"), util.longpath(ConfigFile) f.write("# %s configuration autowritten on %s.\n" % (fname, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) for name in FileDirectives: try: parser.set(section, name, json.dumps(getattr(module, name))) except Exception: pass for name in OptionalFileDirectives: try: value = getattr(module, name, None) if OptionalFileDirectiveDefaults.get(name) != value: parser.set(section, name, json.dumps(value)) except Exception: pass parser.write(f) f.close() except Exception: pass # Fail silently
def load(*cfgs, **kwargs): '''Load betterconfig config files into a python dict. cfgs - iterable of file-ish types (file, StringIO, etc) include='include' - name of the 'include' directive in a betterconfig file, to disable includes, send None default='default' - name used for default section, whose values are stored that the top level of the returned python dictionary. seen=set() - a set of seen cfg identifiers (inodes), to prevent infinitely recursive includes. raw=True - enables interpolation of %(tokens)s when falsy mapper=dict - type to use for the mapping.''' opts = { 'include': kwargs.pop('include', 'include'), 'default': kwargs.pop('default', '_'), 'seen': kwargs.pop('seen', set()), 'raw': kwargs.pop('raw', True), 'mapper': kwargs.pop('mapper', dict) } if kwargs: raise TypeError('{} is an invalid keyword argument'\ 'for this function'.format(kwargs.keys()[0])) compiled_config = opts['mapper']() for cfg in cfgs: includes = [] with _Sectionless(cfg, opts['default']) as cfg_file: # prevent infinite include recursion id_, cfg_dir = _id_and_dir(cfg_file) if id_ in opts['seen']: continue elif id_ is not None: opts['seen'].add(id_) parser = RawConfigParser() if opts['raw'] else SafeConfigParser() parser.optionxform = lambda x: x #ConfigParser calls lower() on each key. parser.readfp(cfg_file) for sect in parser.sections(): sect_config = compiled_config if sect != opts['default']: sect_config = compiled_config.setdefault(sect, {}) for key, val in parser.items(sect): val = ast.literal_eval(val) if sect == opts['default'] and key == opts['include']: includes.extend(_expand_includes(val, cfg_dir)) else: sect_config[key] = val # after we've finished one file, overlay any includes if includes: compiled_config.update(load(*includes, **opts)) return compiled_config
def parse_config_files(options): """Parse configuration files. First ~/.taperc is parsed, then .taperc, then whatever is passed via --config. Finally the command line options are applied. """ config = RawConfigParser() config.optionxform = str # set defaults config.add_section('server') config.add_section('proxies') config.set('server', 'port', '8273') files = ['%s/.taperc' % os.environ['HOME'], '.taperc'] if options.config: files.append(options.config) config.read(files) # update config with command line options if options.root: config.set('server', 'root', options.root) else: config.set('server', 'root', '.') if options.port: config.set('server', 'port', options.port) if options.proxies: for p in options.proxies: lurl, rurl = p.split('=', 1) config.set('proxies', lurl, rurl) return config
def configure(self, settings=None): """Parses arg `settings` or settings file if None passed and sets class attributes accordingly """ config = ConfigParser() # using str instead of optionxform not to .lower() options config.optionxform = str if settings is not None: for section in settings: config.add_section(section) for key, value in settings[section].items(): config.set(section, key, str(value)) else: settings_path = os.path.join(get_project_root(), SETTINGS_FILE_NAME) config.read(settings_path) for section in config.sections(): # legacy format. use following after dropping py2 support: # for key, value in config[section].items(): # setattr(getattr(self, section), key, value) for key in config.options(section): setattr(getattr(self, section), key, config.get(section, key)) self._configure_logging() self._configure_thirdparty_logging() self.configured = True
def buildout_parse(filename): """ Given a filename, parse the buildout config and return a BuildoutConfig object that represents it """ parser = RawConfigParser(dict_type=SortedDict) # Don't ask me, buildout had it... parser.optionxform = lambda s: s if isinstance(filename, basestring): parser.read(filename) else: parser.readfp(filename) config = BuildoutConfig() for section in parser.sections(): for key, value in parser.items(section): value = value.strip() if "\n" in value: value = [x for x in value.split("\n") if x] config[section][key] = value config.sanitize_parts_list() return config
def buildout_write(fp, config): """ Given a filename and a BuildoutConfig, write the contents of the BuildoutConfig to the file """ if isinstance(fp, basestring): fp = open(fp, "w") close = True else: close = False parser = RawConfigParser(dict_type=SortedDict) # Don't ask me, buildout had it... parser.optionxform = lambda s: s for section in config: parser.add_section(section) for key, value in config[section].iteritems(): if isinstance(value, list): value = "\n".join([""] + value) parser.set(section, key, value) parser.write(fp) if close: fp.close()
def load(): """Loads FileDirectives from ConfigFile into this module's attributes.""" section = "*" module = sys.modules[__name__] parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names try: parser.read(ConfigFile) def parse_value(name): try: # parser.get can throw an error if value not found value_raw = parser.get(section, name) except Exception: return False, None # First, try to interpret as JSON try: value = json.loads(value_raw) except ValueError: # JSON failed, try to eval it try: value = eval(value_raw) except SyntaxError: # Fall back to string value = value_raw return True, value for oldname, name in FileDirectiveCompatiblity.items(): [setattr(module, name, v) for s, v in [parse_value(oldname)] if s] for name in FileDirectives: [setattr(module, name, v) for s, v in [parse_value(name)] if s] for name in OptionalFileDirectives: OptionalFileDirectiveDefaults[name] = getattr(module, name, None) success, value = parse_value(name) if success: setattr(module, name, value) except Exception: pass # Fail silently
def read_ini(ini): """Read CAMB ini into dictionary""" if os.path.exists(ini): ini = open(ini).read() config = RawConfigParser() config.optionxform=str config.readfp(StringIO('[root]\n'+ini)) return dict(config.items('root'))
def _init(fname): # _os_type = platform.system() _config_file = fname if not os.path.exists(_config_file): raise RuntimeError("unabled read config ,file not found") if _Configuration.INSTANCE: return _Configuration.INSTANCE try: __MUTEX.acquire() if _Configuration.INSTANCE: return _Configuration.INSTANCE configParser = RawConfigParser() configParser.optionxform = unicode if isinstance(_config_file, (str, unicode)): configParser.readfp(codecs.open(_config_file, "r", "utf-8")) _Configuration.INSTANCE = _Configuration(configParser) baseConfig = _BaseConfig(get("app", "alias"), get("config", "url"), get("config", "env")) localmode = getboolean("config", "localmode") if not localmode or _Configuration._SERVER_MODE: loader = Loader(baseConfig, configParser, _config_file) loader.loadAndSave() # reload config _Configuration.INSTANCE = _Configuration(configParser) _Configuration._IS_INIT = True return _Configuration.INSTANCE finally: __MUTEX.release() raise IOError('read config file error,file name: %s server mode: %s' % (_config_file, str(_Configuration._SERVER_MODE)))
def save(): """Saves FileDirectives into ConfigFile.""" section = "*" module = sys.modules[__name__] parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names parser.add_section(section) try: f, fname = open(ConfigFile, "wb"), util.longpath(ConfigFile) f.write("# %s %s configuration written on %s.\n" % (Title, Version, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) for name in FileDirectives: try: parser.set(section, name, json.dumps(getattr(module, name))) except Exception: pass for name in OptionalFileDirectives: try: value = getattr(module, name, None) if OptionalFileDirectiveDefaults.get(name) != value: parser.set(section, name, json.dumps(value)) except Exception: pass parser.write(f) f.close() except Exception: pass # Fail silently
def load(): """Loads FileDirectives from ConfigFile into this module's attributes.""" section = "*" module = sys.modules[__name__] parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names try: parser.read(ConfigFile) for name in FileDirectives: try: # parser.get can throw an error if not found value_raw = parser.get(section, name) success = False # First, try to interpret as JSON try: value = json.loads(value_raw) success = True except: pass if not success: # JSON failed, try to eval it try: value = eval(value_raw) success = True except: # JSON and eval failed, fall back to string value = value_raw success = True if success: setattr(module, name, value) except: pass except Exception, e: pass # Fail silently
def read_config(filename): config_parser = RawConfigParser() # case sensitive options (used for section 'global_vars') config_parser.optionxform = str with open(filename) as fobj: config_parser.readfp(fobj) return _CONFIG_SPEC.read_config(config_parser)
def parse_package_metadata(): """ Read the 'metadata' section of 'setup.cfg' to calculate the package metadata (at least those parts that can be configured staticly). """ try: from ConfigParser import RawConfigParser except ImportError: from configparser import RawConfigParser cfg = RawConfigParser() with open('setup.cfg') as fp: cfg.readfp(fp) cfg.optionxform = lambda x: x metadata = {} for opt in cfg.options('metadata'): val = cfg.get('metadata', opt) if opt in ('classifiers',): metadata[opt] = [x for x in val.splitlines() if x] elif opt in ('long_description',): metadata[opt] = val[1:] elif opt in ('packages', 'namespace_packages', 'platforms', 'keywords'): metadata[opt] = [x.strip() for x in val.split(',')] elif opt in ['zip-safe']: metadata['zip_safe'] = int(val) else: metadata[opt] = val metadata['version'] = package_version() return metadata
def make_vmu(in_path, out_base): path_conf = out_base + '.vmu' path_0 = out_base + '.l0' path_1 = out_base + '.l1' with OpenSlide(in_path) as osr: l0 = VmuLevel(osr, 0) l1 = VmuLevel(osr, osr.get_best_level_for_downsample(32)) for i, l in enumerate([l0, l1]): print 'Level %d: %d pixels/column' % (i, l.column_width) l0.save(path_0) l1.save(path_1) section = 'Uncompressed Virtual Microscope Specimen' conf = { 'NoLayers': '1', 'ImageFile': path_0, 'MapFile': path_1, 'BitsPerPixel': '36', 'PixelOrder': 'RGB', } c = RawConfigParser() c.optionxform = str c.add_section(section) for k, v in conf.iteritems(): c.set(section, k, v) with open(path_conf, 'w') as fh: c.write(fh)
def __init__(self, config): _config = RawConfigParser() _config.optionxform = lambda s: s _config.read(config) self.path = os.path.dirname(config) for section in _config.sections(): if ':' in section: sectiongroupname, sectionname = section.split(':') else: sectiongroupname, sectionname = 'global', section items = dict(_config.items(section)) sectiongroup = self.setdefault(sectiongroupname, {}) sectiongroup.setdefault(sectionname, {}).update(items) seen = set() for sectiongroupname in self: sectiongroup = self[sectiongroupname] for sectionname in sectiongroup: section = sectiongroup[sectionname] if '<' in section: self._expand(sectiongroupname, sectionname, section, seen) for key in section: fname = 'massage_%s_%s' % (sectiongroupname, key) massage = getattr(self, fname, None) if callable(massage): section[key] = massage(section[key])
def load(): """Loads FileDirectives from ConfigFile into this module's attributes.""" section = "*" module = sys.modules[__name__] parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names try: parser.read(ConfigFile) def parse_value(name): try: # parser.get can throw an error if value not found value_raw = parser.get(section, name) except Exception: return False, None # First, try to interpret as JSON try: value = json.loads(value_raw) except ValueError: # JSON failed, try to eval it try: value = eval(value_raw) except SyntaxError: # Fall back to string value = value_raw return True, value for name in FileDirectives: [setattr(module, name, v) for s, v in [parse_value(name)] if s] for name in OptionalFileDirectives: OptionalFileDirectiveDefaults[name] = getattr(module, name, None) success, value = parse_value(name) if success: setattr(module, name, value) except Exception: pass # Fail silently
def load(): """Loads FileDirectives from ConfigFile into this module's attributes.""" section = "*" module = sys.modules[__name__] parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names try: parser.read(ConfigFile) for name in FileDirectives: try: # parser.get can throw an error if not found value_raw = parser.get(section, name) success = False # First, try to interpret as JSON try: value = json.loads(value_raw) success = True except: pass if not success: # JSON failed, try to eval it try: value = eval(value_raw) success = True except: # JSON and eval failed, fall back to string value = value_raw success = True if success: setattr(module, name, value) except: pass except Exception, e: pass # Fail silently
def read_ini(ini): """Read CAMB ini into dictionary""" if os.path.exists(ini): ini = open(ini).read() config = RawConfigParser() config.optionxform = str config.readfp(StringIO('[root]\n' + ini)) return dict(config.items('root'))
def parse(self): _config = RawConfigParser() _config.optionxform = lambda s: s if getattr(self.config, 'read', None) is not None: _config.readfp(self.config) else: _config.read(self.config) self.path = os.path.dirname(self.config) for section in _config.sections(): if ':' in section: sectiongroupname, sectionname = section.split(':') else: sectiongroupname, sectionname = 'global', section items = dict(_config.items(section)) sectiongroup = self.setdefault(sectiongroupname, {}) sectiongroup.setdefault(sectionname, {}).update(items) self._load_plugins() seen = set() for sectiongroupname in self: sectiongroup = self[sectiongroupname] for sectionname in sectiongroup: section = sectiongroup[sectionname] if '<' in section: self._expand(sectiongroupname, sectionname, section, seen) for key in section: massage = self.massagers.get((sectiongroupname, key)) if callable(massage): section[key] = massage(self, sectionname) return self
def read_ini(path): print("Loading config from path %s" % path) parser = RawConfigParser() # make option names case sensitive parser.optionxform = str loaded = parser.read(path) if path not in loaded: raise ValueError("Failed to load ini file %s" % path) return parser
def read_config(self, path): config = RawConfigParser() config.optionxform = lambda s: s config.read(path) return self.update( (x, dict( (y, config.get(x, y)) for y in config.options(x))) for x in config.sections())
def read_config(filename): config = dict(DEFAULT_CONFIG) parser = RawConfigParser() parser.optionxform = str parser.read(filename) items = parser.items('main') config.update(items) logging.debug('Config: %s', config) return config
def add_to_ini(section='DEFAULT', values={}): ini = RawConfigParser() ini.optionxform = str # ensure keys are case-sensitive as expected ini.read('%s/juju.update' % REDDIT_INSTALL_PATH) for k in values.keys(): ini.set(section, k, values[k]) with open('%s/juju.update' % REDDIT_INSTALL_PATH, 'w') as cf: ini.write(cf)
def read_ini(ini): """Load an ini file or string into a dictionary.""" if isinstance(ini,dict): return ini if isinstance(ini,str): if os.path.exists(ini): ini = open(ini).read() config = RawConfigParser() config.optionxform=str config.readfp(StringIO('[root]\n'+ini)) return dict(config.items('root')) else: raise ValueError('Unexpected type for ini file %s'%type(ini))
def __init__(self, buildout): # TODO: some logic should move to factory buildout_dir = self.buildout_dir = buildout['buildout']['directory'] self.cfg_path = os.path.join(self.buildout_dir, '.mr.developer.cfg') self._config = RawConfigParser() self._config.optionxform = lambda s: s self._config.read(self.cfg_path) self.develop = {} self.buildout_args = [] if os.path.split(sys.argv[0])[1] == 'buildout': self.buildout_args = list(sys.argv) self.namedrepos = NAMED_REPOS self.rewrites = { 'defaultcfg': [], 'local': [] } sources_dir = self.sources_dir = \ buildout['buildout'].get('sources-dir', 'src') if not os.path.isabs(sources_dir): sources_dir = os.path.join(buildout_dir, sources_dir) if self._config.has_section('develop'): for package, value in self._config.items('develop'): value = value.lower() if value == 'true': self.develop[package] = True elif value == 'false': self.develop[package] = False else: raise ValueError("Invalid value in 'develop' section of '%s'" % self.cfg_path) if self._config.has_option('buildout', 'args'): args = self._config.get('buildout', 'args').split("\n") for arg in args: arg = arg.strip() if arg.startswith("'") and arg.endswith("'"): arg = arg[1:-1].replace("\\'", "'") elif arg.startswith('"') and arg.endswith('"'): arg = arg[1:-1].replace('\\"', '"') self.buildout_args.append(arg) if self._config.has_option('mr.developer', 'rewrites'): for rewrite in self._config.get('mr.developer', 'rewrites').split('\n'): self.rewrites['local'].append(rewrite.split()) # rewrites from default.cfg defaultcfg_path = os.path.join(os.path.expanduser('~'), '.buildout', 'default.cfg') if os.path.exists(defaultcfg_path): defaultcfg = RawConfigParser() # no idea what this does - copy pasted from above defaultcfg.optionxform = lambda s: s defaultcfg.read(defaultcfg_path) if defaultcfg.has_option('mr.developer', 'rewrites'): for rewrite in defaultcfg.get('mr.developer', 'rewrites').split('\n'): self.rewrites['defaultcfg'].append(rewrite.split())
def parse_package_metadata(): """ Read the 'metadata' section of 'setup.cfg' to calculate the package metadata (at least those parts that can be configured staticly). """ try: from ConfigParser import RawConfigParser except ImportError: from configparser import RawConfigParser cfg = RawConfigParser() cfg.optionxform = lambda x: x if sys.version_info.major == 2: with open('setup.cfg') as fp: cfg.readfp(fp) else: with open('setup.cfg') as fp: cfg.read_file(fp) metadata = {} for opt in cfg.options('x-metadata'): val = cfg.get('x-metadata', opt) if opt in ('classifiers',): metadata[opt] = [x for x in val.splitlines() if x] elif opt in ('long_description',): # In python 2.7 empty lines in the long description are handled incorrectly, # therefore setup.cfg uses '$' at the start of empty lines. Remove that # character from the description val = val[1:] val = val.replace('$', '') metadata[opt] = val # Add links to interesting location to the long_description metadata[opt] += "\n\nProject links\n" metadata[opt] += "-------------\n" metadata[opt] += "\n" metadata[opt] += "* `Documentation <https://pyobjc.readthedocs.io/en/latest/>`_\n\n" metadata[opt] += "* `Issue Tracker <https://bitbucket.org/ronaldoussoren/pyobjc/issues?status=new&status=open>`_\n\n" metadata[opt] += "* `Repository <https://bitbucket.org/ronaldoussoren/pyobjc/>`_\n\n" elif opt in ('packages', 'namespace_packages', 'platforms', 'keywords'): metadata[opt] = [x.strip() for x in val.split(',')] elif opt in ['zip-safe']: metadata['zip_safe'] = int(val) else: metadata[opt] = val metadata['version'] = package_version() return metadata
def handle(self, *args, **options): cfg = RawConfigParser() cfg.optionxform = str cfg.read(settings.CONFIG_FILE) if not cfg.has_section('APP'): cfg.add_section('APP') cfg.set('APP', 'SECRET_KEY', self.generate_secret()) cfg.write(open(settings.CONFIG_FILE, 'w')) self.stdout.write('Successfully updated SECRET_KEY in %s' % settings.CONFIG_FILE)
def __init__(self, buildout_dir, sources_dir=None): self.cfg_path = os.path.join(buildout_dir, '.mr.developer.cfg') self._config = RawConfigParser() self._config.optionxform = lambda s: s self._config.read(self.cfg_path) if sources_dir is not None: self.sources_dir = sources_dir self.develop = {} self.buildout_args = [] self.rewrites = { 'defaultcfg': [], 'local': [] } self.namedrepos = NAMED_REPOS if self._config.has_section('develop'): for package, value in self._config.items('develop'): value = value.lower() if value == 'true': self.develop[package] = True elif value == 'false': self.develop[package] = False elif value == 'auto': self.develop[package] = 'auto' else: raise ValueError("Invalid value in 'develop' section of " "'%s'" % self.cfg_path) if self._config.has_option('buildout', 'args'): args = self._config.get('buildout', 'args').split("\n") for arg in args: arg = arg.strip() if arg.startswith("'") and arg.endswith("'"): arg = arg[1:-1].replace("\\'", "'") elif arg.startswith('"') and arg.endswith('"'): arg = arg[1:-1].replace('\\"', '"') self.buildout_args.append(arg) if self._config.has_option('mr.developer', 'rewrites'): rewrites = self._config.get('mr.developer', 'rewrites') for rewrite in rewrites.split('\n'): self.rewrites['local'].append(rewrite.split()) # rewrites from default.cfg defaultcfg_path = os.path.join(os.path.expanduser('~'), '.buildout', 'default.cfg') if os.path.exists(defaultcfg_path): defaultcfg = RawConfigParser() # no idea what this does - copy pasted from above defaultcfg.optionxform = lambda s: s defaultcfg.read(defaultcfg_path) if defaultcfg.has_option('mr.developer', 'rewrites'): rewrites = defaultcfg.get('mr.developer', 'rewrites') for rewrite in rewrites.split('\n'): self.rewrites['defaultcfg'].append(rewrite.split())
def _parseConfigFile(self, fileName): """ TODO doc """ if not os.path.exists(fileName): raise ConfigurationException("Config file %s does not exist." % fileName) # Raw - doesn't do any interpolation config = RawConfigParser() # by default it seems that value names are converted to lower case, # this way they should be case-sensitive config.optionxform = str config.read(fileName) # does not fail even on non existing file try: # assume only one section 'general' for (name, value) in config.items("general"): # setting only values which do not already exist, if a value # already exists - it means it was specified on the command # line and such value takes precedence over configuration file # beware - attribute may exist from command line parser # and be None - then set proper value here if self.get(name) == None: self._options[name] = value # to some strings types processing ... if isinstance(self.get(name), bytes): # convert 'True', 'False' strings to bool values # True, False if value.lower() == 'true': self._options[name] = True if value.lower() == 'false': self._options[name] = False # if the configuration value is string and has been defined # (in config file or CLI) with surrounding " or ', remove that # have to check type because among self._mandatoryStr may be # boolean types ... r = self.get(name) if isinstance(r, bytes): if r[0] in ("'", '"'): r = r[1:] if r[-1] in ("'", '"'): r = r[:-1] self._options[name] = r except Exception as ex: m = "Error while parsing %s, reason %s" % (fileName, ex) raise ConfigurationException(m) # safe location of the file from which the configuration was loaded # apart from this newly defined config value, there will also be # 'config' which remains None, unless specific config file # specified on CLI self._options["currentConfigFile"] = fileName
def parse_package_metadata(): """ Read the 'metadata' section of 'setup.cfg' to calculate the package metadata (at least those parts that can be configured staticly). """ try: from ConfigParser import RawConfigParser except ImportError: from configparser import RawConfigParser cfg = RawConfigParser() cfg.optionxform = lambda x: x with open("setup.cfg") as fp: cfg.read_file(fp) metadata = {} for opt in cfg.options("x-metadata"): val = cfg.get("x-metadata", opt) if opt in ("classifiers", ): metadata[opt] = [x for x in val.splitlines() if x] elif opt in ("long_description", ): # In python 2.7 empty lines in the long description are handled incorrectly, # therefore setup.cfg uses '$' at the start of empty lines. Remove that # character from the description val = val[1:] val = val.replace("$", "") metadata[opt] = val # Add links to interesting location to the long_description metadata[opt] += "\n\nProject links\n" metadata[opt] += "-------------\n" metadata[opt] += "\n" metadata[ opt] += "* `Documentation <https://pyobjc.readthedocs.io/en/latest/>`_\n\n" metadata[ opt] += "* `Issue Tracker <https://github.com/ronaldoussoren/pyobjc/issues>`_\n\n" metadata[ opt] += "* `Repository <https://github.com/ronaldoussoren/pyobjc/>`_\n\n" elif opt in ("packages", "namespace_packages", "platforms", "keywords"): metadata[opt] = [x.strip() for x in val.split(",")] elif opt in ["zip-safe"]: metadata["zip_safe"] = int(val) else: metadata[opt] = val metadata["version"] = package_version() return metadata
def __load_config(cls, fname): from ConfigParser import RawConfigParser config = RawConfigParser() config.optionxform = str with open(fname, 'rb') as fp: config.readfp(fp, fname) data = {} for s in config.sections(): data[s] = dict(config.items(s)) return data
def createdrivers(numdrivers, sourcename, pubrate, endpoints, archiverip, apikey): s = StringIO(configfile.format(uuid=str(uuid.uuid1()),driver=sourcename, rate=pubrate, number=endpoints, archiverip=archiverip, apikey=apikey)) source = RawConfigParser() source.optionxform=str source.readfp(s) for i in xrange(numdrivers): print 'creating source at', source.get('server','port') port = int(source.get('server','port')) source.set('server', 'port', port+1) source.set('/','uuid', str(uuid.uuid1())) source.write(open('{0}{1}.ini'.format(sourcename.replace('.','_'), i), 'wb'))
def _parseConfigFile(self, fileName): """ TODO doc """ if not os.path.exists(fileName): raise ConfigurationException("Config file %s does not exist." % fileName) # Raw - doesn't do any interpolation config = RawConfigParser() # by default it seems that value names are converted to lower case, # this way they should be case-sensitive config.optionxform = str config.read(fileName) # does not fail even on non existing file try: # assume only one section 'general' for (name, value) in config.items("general"): # setting only values which do not already exist, if a value # already exists - it means it was specified on the command # line and such value takes precedence over configuration file # beware - attribute may exist from command line parser # and be None - then set proper value here if self.get(name) == None: self._options[name] = value # to some strings types processing ... if isinstance(self.get(name), bytes): # convert 'True', 'False' strings to bool values # True, False if value.lower() == "true": self._options[name] = True if value.lower() == "false": self._options[name] = False # if the configuration value is string and has been defined # (in config file or CLI) with surrounding " or ', remove that # have to check type because among self._mandatoryStr may be # boolean types ... r = self.get(name) if isinstance(r, bytes): if r[0] in ("'", '"'): r = r[1:] if r[-1] in ("'", '"'): r = r[:-1] self._options[name] = r except Exception as ex: m = "Error while parsing %s, reason %s" % (fileName, ex) raise ConfigurationException(m) # safe location of the file from which the configuration was loaded # apart from this newly defined config value, there will also be # 'config' which remains None, unless specific config file # specified on CLI self._options["currentConfigFile"] = fileName
def parse_zbx_conf(path, is_multi=False): """分析 zabbix-agentd 和 zabbix-agent 等的 conf 文件,转为换列表。 Args: path: 配置文件路径。 Returns: <list>: 配置文件的键值对。 """ cp = RawConfigParser() if not is_multi else RawConfigParser(dict_type=MultiOrderedDict) cp.optionxform = str with open(path, "r") as f: stream = StringIO("[dummy_section]\n" + f.read()) cp.readfp(stream) return cp.items("dummy_section")
def parse_rc(file): defaults = {} rc = RawConfigParser() rc.optionxform = optionxform rc.read(file) if rc.has_section('core'): parser.set_defaults(**{ k: v for (k,v) in rc.items('core') }) if rc.has_section('tag'): mtag.subparser.set_defaults(**{ k: v for (k,v) in rc.items('tag') }) if rc.has_section('sort'): msort.subparser.set_defaults(**{ k: v for (k,v) in rc.items('sort') }) if rc.has_section('stat'): mstat.subparser.set_defaults(**{ k: v for (k,v) in rc.items('stat') }) return defaults
def load(): """Loads FileDirectives from ConfigFile into this module's attributes.""" global Defaults, VarDirectory, ConfigFile configpaths = [ConfigFile] if not Defaults: # Instantiate OS- and user-specific paths try: p = appdirs.user_config_dir(Title, appauthor=False) # Try user-specific path first, then path under application folder configpaths.insert(0, os.path.join(p, "%s.ini" % Title.lower())) except Exception: pass try: VarDirectory = appdirs.user_data_dir(Title, False) except Exception: pass section = "*" module = sys.modules[__name__] VARTYPES = (basestring, bool, int, long, list, tuple, dict, type(None)) Defaults = { k: v for k, v in vars(module).items() if not k.startswith("_") and isinstance(v, VARTYPES) } parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names try: for path in configpaths[::-1]: if os.path.isfile(path) and parser.read(path): break # for path def parse_value(name): try: # parser.get can throw an error if value not found value_raw = parser.get(section, name) except Exception: return None, False try: # Try to interpret as JSON, fall back on raw string value = json.loads(value_raw) except ValueError: value = value_raw return value, True for name in FileDirectives + OptionalFileDirectives: [setattr(module, name, v) for v, s in [parse_value(name)] if s] except Exception: pass # Fail silently
def test_conf_loading(tmpdir): cfg_filename = str(tmpdir.join('myconf.ini')) with open(cfg_filename, 'w') as fp: fp.write( textwrap.dedent(""" [trunk] SPAM = spam EGGS = eggs BACON = bacon [branch-1] inherit = trunk SPAM = Spam Spam Spam [branch-2] inherit = trunk EGGS = Eggs! [branch-3] inherit = trunk SPAM = Spam! Spam! Spam! BACON = Lovely Bacon [leaf] inherit = branch-1,branch-2,branch-3 """)) cfgparser = RawConfigParser() cfgparser.optionxform = str cfgparser.read(cfg_filename) assert get_inheritance( cfgparser, 'leaf') == ['branch-1', 'branch-2', 'branch-3', 'trunk'] assert get_section(cfgparser, 'trunk') == { 'SPAM': 'spam', 'EGGS': 'eggs', 'BACON': 'bacon', } assert get_section(cfgparser, 'branch-1') == { 'SPAM': 'Spam Spam Spam', 'EGGS': 'eggs', 'BACON': 'bacon', } assert get_section(cfgparser, 'leaf') == { 'SPAM': 'Spam Spam Spam', 'EGGS': 'Eggs!', 'BACON': 'Lovely Bacon', }
def execute(self, instance): filename = self.evaluate_index(0) try: file = open_file(filename, 'wb') except IOError: return config = RawConfigParser() config.optionxform = str value = instance.objectPlayer.split() config.add_section('List') config.set('List', 'Elements', str(len(value))) for i, item in enumerate(value): config.set('List', 'Element%s' % (i+1), item) config.write(file) file.close()
def main(): try: file_name = sys.argv[1] section_name = sys.argv[2] args = sys.argv[3:] except IndexError: print(USAGE) sys.exit(2) cfgparser = RawConfigParser() cfgparser.optionxform = str cfgparser.read(file_name) env = get_section(cfgparser, section_name) os.execvpe(args[0], args, env)
def main(): try: file_name = sys.argv[1] section_name = sys.argv[2] args = sys.argv[3:] except IndexError: print(USAGE) sys.exit(2) cfgparser = RawConfigParser() cfgparser.optionxform = str cfgparser.read(file_name) env = get_section(cfgparser, section_name) os.execvpe(args[0], args, env)
def execute(self, instance): filename = self.evaluate_expression(self.get_parameter(0)) try: file = open_file(filename, 'wb') except IOError: return config = RawConfigParser() config.optionxform = str value = instance.objectPlayer.split() config.add_section('List') config.set('List', 'Elements', str(len(value))) for i, item in enumerate(value): config.set('List', 'Element%s' % (i + 1), item) config.write(file) file.close()
def save(): """Saves FileDirectives into ConfigFile.""" configpaths = [ConfigFile] try: p = appdirs.user_config_dir(Title, appauthor=False) userpath = os.path.join(p, "%s.ini" % Title.lower()) # Pick only userpath if exists, else try application folder first if os.path.isfile(userpath): configpaths = [userpath] else: configpaths.append(userpath) except Exception: pass section = "*" module = sys.modules[__name__] parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names parser.add_section(section) try: for path in configpaths: try: os.makedirs(os.path.dirname(path)) except Exception: pass try: f = open(path, "wb") except Exception: continue # for path else: break # for path f.write("# %s configuration written on %s.\n" % (Title, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) for name in FileDirectives: try: parser.set(section, name, json.dumps(getattr(module, name))) except Exception: pass for name in OptionalFileDirectives: try: value = getattr(module, name, None) if Defaults.get(name) != value: parser.set(section, name, json.dumps(value)) except Exception: pass parser.write(f) f.close() except Exception: pass # Fail silently
def main(): if len(sys.argv) != 4: raise ValueError("Required three arguments; path, section, option") path = sys.argv[1] section = sys.argv[2] option = sys.argv[3] parser = RawConfigParser() # make option names case sensitive parser.optionxform = str loaded = parser.read(path) if path not in loaded: raise ValueError("%s not loaded" % path) value = parser.get(section, option) print value
def from_cfg_fp(cls, fp): """ Generate a versions specification from a file-like object containing buildout cfg-style versions tuples :param fp: A file-like object (specifically requires ``.readline()``) :returns: An instantiated `Versions` object. """ config = RawConfigParser() config.optionxform = str # incantation to make keys case-sensitive config.readfp(fp) try: versions_name = config.get("buildout", "versions") except NoSectionError, NoOptionError: versions_name = "versions"
def create_config(args): if (len(args) == 2): args.append("") # empty EXPECTED_SSID elif (len(args) == 3): pass else: raise Exception("Invalid args: {args}.\nMust be either 2 or 3 variables. LOC_DIR_PATH NETWORK_DIR_PATH [EXPECTED_SSID]".format(args=args)) path_to_module = path.dirname(__file__) if len(path_to_module) == 0: path_to_module = "." CONFIG_FILE = path.join(path_to_module, "config.conf") conf_parser = RawConfigParser() targ_section = "networkFolderSync" if args[0] == ".": args[0] = getcwd() if not path.isdir(args[0]): raise Exception("Invalid directory argument: " + args[0]) if not path.isdir(args[1]): raise Exception("Invalid directory argument: " + args[1]) config_data_dict = OrderedDict( ( ("LOC_DIR_PATH", args[0]), ("NETWORK_DIR_PATH", args[1]), # ("PI_USER", args[2]), # ("PI_PASSWORD", args[3]), ("EXPECTED_SSID", args[2]) ) ) conf_parser.add_section(targ_section) conf_parser.optionxform = str for key in config_data_dict.keys(): conf_parser.set(targ_section, key, config_data_dict[key]) with open(CONFIG_FILE, 'wb') as configfile: conf_parser.write(configfile)
def writeResults(self, uiMapFile): for name, shortcut in self.shortcutManager.getShortcuts(): if shortcut in self.shortcutsUsed: self.storeUsage(shortcut) else: print "Shortcut not used", shortcut.name writeParser = RawConfigParser(dict_type=OrderedDict) writeParser.optionxform = str for section in self.uiMapFileHandler.sections(): usedActions = self.uiMapFileUsed.get(section, []) entriesToAdd = filter(lambda (action, event): action in usedActions or len(event.strip()) == 0, self.uiMapFileHandler.items(section)) if entriesToAdd: writeParser.add_section(section) for actionName, eventName in entriesToAdd: writeParser.set(section, actionName, eventName) print "Updating UI map file..." writeParser.write(open(uiMapFile, "w"))
def __init__(self, configs): _config = RawConfigParser() _config.optionxform = lambda s: s _config.read(configs) for section in _config.sections(): sectiongroupname, sectionname = section.split(":") items = dict(_config.items(section)) sectiongroup = self.setdefault(sectiongroupname, {}) sectiongroup.setdefault(sectionname, {}).update(items) for sectiongroupname in self: sectiongroup = self[sectiongroupname] for sectionname in sectiongroup: section = sectiongroup[sectionname] for key in section: fname = "massage_%s_%s" % (sectiongroupname, key) massage = getattr(self, fname, None) if callable(massage): section[key] = massage(section[key])
def generate_new_ssl_conf(options, orig_config_string, ca=False): """Take the original openssl.conf contents, make it readable to RawConfigParser, then edit it to allow for self-signed subjectAltName support per http://stackoverflow.com/a/21494483 . Returns the RawConfigParser. """ config = RawConfigParser() # stop lowercasing key names!! config.optionxform = lambda option: option # add [default] section at the top to keep configparser from barfing config_string = "# Modified per http://stackoverflow.com/a/21494483\n" + \ "[ default ]\n{}".format(orig_config_string) if PYTHON2: with tempfile.TemporaryFile() as fh: fh.write(config_string) fh.seek(0) config.readfp(fh) else: config.read_string(config_string) log.debug(config.sections()) # The section names barf without the spaces =\ # Changes for subjectAltName: if not ca: config.set(' CA_default ', 'copy_extensions', r'copy') config.set(' v3_ca ', 'subjectAltName', r'$ENV::ALTNAME') # Changes for our own CA (http://stackoverflow.com/a/7770075): config.set(' CA_default ', 'dir', options.ca_dir) config.set(' CA_default ', 'certs', '$dir') config.set(' CA_default ', 'new_certs_dir', '$dir/ca.db.certs') config.set(' CA_default ', 'database', '$dir/ca.db.index') config.set(' CA_default ', 'serial', '$dir/ca.db.serial') config.set(' CA_default ', 'RANDFILE', '$dir/ca.db.rand') config.set(' CA_default ', 'certificate', '$dir/ca.crt') config.set(' CA_default ', 'private_key', '$dir/ca.key') config.set(' CA_default ', 'default_days', '365') config.set(' CA_default ', 'default_crl_days', '30') config.set(' CA_default ', 'default_md', 'md5') config.set(' CA_default ', 'preserve', 'no') config.set(' CA_default ', 'policy', 'policy_anything') return config
def read_permissions_file(filename): """ Reads permissions from an INI file and returns a dict mapping paths to sets of groups. Example file: [permissions] / = superusers Data/Special Data = special_people, other_special_people """ parser = RawConfigParser() # This forces parser to preseve case parser.optionxform=str parser.read([filename]) permissions = {} for path, group_string in parser.items('permissions'): path = path.strip().strip('/') groups = set([group.strip() for group in group_string.split(',')]) permissions[path] = groups return permissions
def generate_new_ssl_conf(options, orig_config_string, ca=False): """Take the original openssl.conf contents, make it readable to RawConfigParser, then edit it to allow for self-signed subjectAltName support per http://stackoverflow.com/a/21494483 . Returns the RawConfigParser. """ config = RawConfigParser() # stop lowercasing key names!! config.optionxform = lambda option: option # add [default] section at the top to keep configparser from barfing config_string = "# Modified per http://stackoverflow.com/a/21494483\n" + \ "[ default ]\n{}".format(orig_config_string) if PYTHON2: with tempfile.TemporaryFile() as fh: fh.write(config_string) fh.seek(0) config.readfp(fh) else: config.read_string(config_string) log.debug(config.sections()) # The section names barf without the spaces =\ # Changes for subjectAltName: if not ca: config.set(' CA_default ', 'copy_extensions', r'copy') config.set(' v3_ca ', 'subjectAltName', r'$ENV::ALTNAME') # Changes for our own CA (http://stackoverflow.com/a/7770075): config.set(' CA_default ', 'dir', options.ca_dir) config.set(' CA_default ', 'certs', '$dir') config.set(' CA_default ', 'new_certs_dir', '$dir/ca.db.certs') config.set(' CA_default ', 'database', '$dir/ca.db.index') config.set(' CA_default ', 'serial', '$dir/ca.db.serial') config.set(' CA_default ', 'RANDFILE', '$dir/ca.db.rand') config.set(' CA_default ', 'certificate', '$dir/ca.crt') config.set(' CA_default ', 'private_key', '$dir/ca.key') config.set(' CA_default ', 'default_days', '365') config.set(' CA_default ', 'default_crl_days', '30') config.set(' CA_default ', 'default_md', 'md5') config.set(' CA_default ', 'preserve', 'no') config.set(' CA_default ', 'policy', 'policy_anything') return config
def _confglobs(self): """get a list of patterns that match text files from the svn config """ config = RawConfigParser() config.optionxform = lambda x: x # stock parser is case-insensitive config.read(os.path.expanduser('~/.subversion/config')) if not config.has_section('auto-props'): print 'your subversion config file has no auto-props section' sys.exit(1) autoprops = config.options('auto-props') globs = Set() for pattern in autoprops: if 'svn:eol-style' in config.get('auto-props', pattern): globs.add(pattern) globs = list(globs) globs.sort() return globs
def writeResults(self, uiMapFile): for name, shortcut in self.shortcutManager.getShortcuts(): if shortcut in self.shortcutsUsed: self.storeUsage(shortcut) else: print "Shortcut not used", shortcut.name writeParser = RawConfigParser(dict_type=OrderedDict) writeParser.optionxform = str for section in self.uiMapFileHandler.sections(): usedActions = self.uiMapFileUsed.get(section, []) entriesToAdd = filter( lambda (action, event): action in usedActions or len( event.strip()) == 0, self.uiMapFileHandler.items(section)) if entriesToAdd: writeParser.add_section(section) for actionName, eventName in entriesToAdd: writeParser.set(section, actionName, eventName) print "Updating UI map file..." writeParser.write(open(uiMapFile, "w"))
def parameter_config_setting(local_file_path, data, config_key_values): """ 把参数配置放入缓存并写入备份文件 :return: """ raw_config = RawConfigParser() raw_config.optionxform = str raw_config.read(local_file_path) get_config(data, raw_config, config_key_values) # 保存到配置文件 try: cf = open(local_file_path, 'w') raw_config.write(cf) cf.close() except Exception as e: LOGGER.critical(traceback.print_exc()) else: LOGGER.info("Config write success")
def generate_config_dynamic(resource_details): log.info('Generating config-dynamic...') dynamic_config_name = '/path/to/etc-like/dir/etc/config-dynamic.ini' dynamic_config = RawConfigParser() dynamic_config.optionxform = str dynamic_config.add_section('dynamic_resources') for app in resource_details: dataset = json.dumps(resource_details[app], sort_keys=True) if dataset: dynamic_config.set('dynamic_resources', 'WF_' + app.upper() + '_SHARDS', "'" + dataset + "'") else: log.warning('got invalid dataset for: ' + app + ' value: ' + str(resource_details[app])) buffer = BytesIO() dynamic_config.write(buffer) return safe_write_file(buffer.getvalue(), dynamic_config_name)
def save(): """Saves FileDirectives into ConfigFile.""" section = "*" module = sys.modules[__name__] parser = RawConfigParser() parser.optionxform = str # Force case-sensitivity on names parser.add_section(section) try: f = open(ConfigFile, "wb") f.write("# %s configuration autowritten on %s.\n" % (ConfigFile, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) for name in FileDirectives: try: value = getattr(module, name) parser.set(section, name, json.dumps(value)) except: pass parser.write(f) f.close() except Exception, e: pass # Fail silently