def load_cfg(): defaults = { 'name': 'cz_conventional_changelog' } config = RawConfigParser('') home = str(os.path.expanduser("~")) config_file = '.cz' # load cfg from home folder global_cfg = os.path.join(home, config_file) if os.path.exists(global_cfg): config.readfp(io.open(global_cfg, 'rt', encoding='utf-8')) log_config = io.StringIO() config.write(log_config) defaults.update(dict(config.items("commitizen"))) # load cfg from current project configs = ['setup.cfg', '.cz.cfg'] for cfg in configs: if not os.path.exists(config_file) and os.path.exists(cfg): config_file = cfg break config_file_exists = os.path.exists(config_file) if config_file_exists: logger.debug('Reading file "%s"', config_file) config.readfp(io.open(config_file, 'rt', encoding='utf-8')) log_config = io.StringIO() config.write(log_config) defaults.update(dict(config.items("commitizen"))) return defaults
def load_rcfile(self): self.process_args() if self.RCFILE == parser.get_default('RCFILE') and \ not os.path.exists(self.RCFILE) and os.path.exists(OLD_RCFILE): logger.warning("Using old rcfile found at %s, " "please rename to %s.", OLD_RCFILE, self.RCFILE) self.RCFILE = OLD_RCFILE if os.path.exists(self.RCFILE): config = RawConfigParser() config.optionxform = lambda x: x.upper() config.read(self.RCFILE) items = [] if config.has_section('global'): items.extend(config.items('global')) if self.NAME is not None and config.has_section(self.NAME): items.extend(config.items(self.NAME)) try: return self.parse_rcvalues(items) except (ValueError, argparse.ArgumentTypeError) as e: raise RuntimeError("Unable to parse RC values: %s" % e) return {}
def load_rcfile(self): if os.path.exists(self.RCFILE): config = RawConfigParser() config.optionxform = lambda x: x.upper() config.read(self.RCFILE) items = [] if config.has_section('global'): items.extend(config.items('global')) if self.NAME is not None and config.has_section(self.NAME): items.extend(config.items(self.NAME)) for k,v in items: if k in CONFIG_TYPES and getattr(self,k) == DEFAULT_SETTINGS[k]: if CONFIG_TYPES[k] == 'str': setattr(self, k, v) elif CONFIG_TYPES[k] == 'int': setattr(self, k, int(v)) elif CONFIG_TYPES[k] == 'float': setattr(self, k, float(v)) elif CONFIG_TYPES[k] == 'list': setattr(self, k, [i.strip() for i in v.split(",")]) elif CONFIG_TYPES[k] == 'bool': if v.lower() in ('1', 'yes', 'true', 'on'): setattr(self, k, True) elif v.lower() in ('0', 'no', 'false', 'off'): setattr(self, k, False) else: raise ValueError("Not a boolean: %s" % v)
def load_cfg(): defaults = {"name": "cz_conventional_commits"} config = RawConfigParser("") try: home = str(Path.home()) except AttributeError: home = os.path.expanduser("~") config_file = ".cz" # load cfg from home folder global_cfg = os.path.join(home, config_file) if os.path.exists(global_cfg): config.readfp(io.open(global_cfg, "rt", encoding="utf-8")) log_config = io.StringIO() config.write(log_config) defaults.update(dict(config.items("commitizen"))) # load cfg from current project configs = ["setup.cfg", ".cz.cfg"] for cfg in configs: if not os.path.exists(config_file) and os.path.exists(cfg): config_file = cfg break config_file_exists = os.path.exists(config_file) if config_file_exists: logger.debug('Reading file "%s"', config_file) config.readfp(io.open(config_file, "rt", encoding="utf-8")) log_config = io.StringIO() config.write(log_config) defaults.update(dict(config.items("commitizen"))) return defaults
def parse_config(filename, dirs=None): if dirs: filenames = [os.path.join(d, filename) for d in dirs] else: filenames = [filename] config = RawConfigParser() n = config.read(filenames) if not len(n) >= 1: raise PkgNotFound("Could not find file(s) %s" % str(filenames)) # Parse meta and variables sections meta = parse_meta(config) vars = {} if config.has_section('variables'): for name, value in config.items("variables"): vars[name] = _escape_backslash(value) # Parse "normal" sections secs = [s for s in config.sections() if not s in ['meta', 'variables']] sections = {} requires = {} for s in secs: d = {} if config.has_option(s, "requires"): requires[s] = config.get(s, 'requires') for name, value in config.items(s): d[name] = value sections[s] = d return meta, vars, sections, requires
def import_library(libfilepointer): """ Import a units library, replacing any existing definitions. Parameters ---------- libfilepointer : file new library file to work with Returns ------- ConfigParser newly updated units library for the module """ global _UNIT_LIB global _UNIT_CACHE _UNIT_CACHE = {} _UNIT_LIB = ConfigParser() _UNIT_LIB.optionxform = _do_nothing # New in Python 3.2: read_file() replaces readfp(). if sys.version_info >= (3, 2): _UNIT_LIB.read_file(libfilepointer) else: _UNIT_LIB.readfp(libfilepointer) required_base_types = ['length', 'mass', 'time', 'temperature', 'angle'] _UNIT_LIB.base_names = list() # used to is_angle() and other base type checking _UNIT_LIB.base_types = dict() _UNIT_LIB.unit_table = dict() _UNIT_LIB.prefixes = dict() _UNIT_LIB.help = list() for prefix, factor in _UNIT_LIB.items('prefixes'): factor, comma, comment = factor.partition(',') _UNIT_LIB.prefixes[prefix] = float(factor) base_list = [0] * len(_UNIT_LIB.items('base_units')) for i, (unit_type, name) in enumerate(_UNIT_LIB.items('base_units')): _UNIT_LIB.base_types[unit_type] = i powers = list(base_list) powers[i] = 1 # print '%20s'%unit_type, powers # cant use add_unit because no base units exist yet _new_unit(name, 1, powers) _UNIT_LIB.base_names.append(name) # test for required base types missing = [utype for utype in required_base_types if utype not in _UNIT_LIB.base_types] if missing: raise ValueError('Not all required base type were present in the' ' config file. missing: %s, at least %s required' % (missing, required_base_types)) _update_library(_UNIT_LIB) return _UNIT_LIB
def load_config(): """ Load Geopedia configuration file storing authentication and table IDs """ config_parser = RawConfigParser() config_parser.read(GeopediaConfig.get_config_path()) return dict(config_parser.items('geopedia')), dict( config_parser.items('tables'))
def load_settings(config_file): config = RawConfigParser() if not config.read(config_file): raise Exception('Not a valid config file: {0!r}'.format(config_file)) if config.has_section('app:main_helper'): settings = dict(config.items('app:main_helper')) else: settings = dict(config.items('app:main')) return settings
def get_db_settings(): rcp = RawConfigParser() rcp.read("taobao/config/settings.cfg") section = rcp.sections()[1] host = rcp.items(section)[0][1] port = int(rcp.items(section)[1][1]) user = rcp.items(section)[2][1] passwd = rcp.items(section)[3][1] db = rcp.items(section)[4][1] return host, port, user, passwd, db
def load_config(self, filename): if os.path.exists(filename): log.info("loading {}".format(filename)) tmp = RawConfigParser() tmp.read(u"{}".format(filename)) if tmp.has_section("module-parameters"): for key, value in tmp.items("module-parameters"): self.set(key, value) if tmp.has_section("core"): for key, value in tmp.items("core"): self.set(key, value)
class ConfigReader: def __init__(self, file_path: Union[str, Path]): self.configparser = RawConfigParser() self.configparser.read(file_path) def to_dict(self, section: str = None): if section is None: configs = {} for sec in self.configparser.sections(): configs[sec] = dict(self.configparser.items(sec)) return configs else: configs = self.configparser.items(section) return dict(configs)
def load_rcfile(self): if os.path.exists(self.RCFILE): config = RawConfigParser() config.optionxform = lambda x: x.upper() config.read(self.RCFILE) items = [] if config.has_section('global'): items.extend(config.items('global')) if self.NAME is not None and config.has_section(self.NAME): items.extend(config.items(self.NAME)) self.load_rcvalues(items) self.update_implications()
def parse(data: Data, config: RawConfigParser): """Parses all subjects documents. :param data: data object :type data: Data :param config: config from config file :type config: RawConfigParser """ while True: subject = data.get_not_parsed() if subject is None: # break if no not processed subject exists break for document in subject['documents']: path = document['file'] pdf = parser.from_file(path) try: pdf_content = pdf['content'] pdf_content = re.sub( r'(,\d\d)', r'\1|', pdf_content) # insert separator behind number pdf_content = re.sub( r'(\d)\s+(\d)', r'\1\2', pdf_content) # remove spaces between numbers except TypeError: pdf_content = "" values = {} for items in config.items('parser'): values[items[0]] = _extract(pdf_content, items[1]) data.update_parsed(subject['ico'], path, values)
class ConfigTool(object): section = "" config = None default_file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "config.ini") def __init__(self, file_path=None): if file_path is None: file_path = self.default_file_path self.config = RawConfigParser() self.config.read(file_path) self.file_path = file_path def set_section(self, section): self.section = section def get(self, key): if self.section: try: return self.config.get(self.section, key) except NoOptionError: message_info = [self.section, str(key), "ConfigTool.error", "no_key"] logger.error("|".join(message_info)) return None def get_section_all(self, section): return self.config.items(section)
def load(self, csv): conf = RawConfigParser() with open(csv, 'rb') as f: input_bytes = f.read() decoded = input_bytes.decode( chardet.detect(input_bytes)['encoding']) decoded = '[__global__]\n' + decoded conf.read_string(decoded) for sec in conf.sections(): if not self.conf.has_section(sec): self.conf.add_section(sec) self.crap.add_section(sec) for k, v in conf.items(sec): is_crap = False if '__' in v: is_crap = True if not is_crap: if self.conf.has_option(sec, k): if self.conf.get(sec, k).lower() != v.lower(): print('Overwriting locale %s (%r -> %r)' % (k, self.conf.get(sec, k), v)) self.conf.set(sec, k, v) else: if self.crap.has_option(sec, k): print('Overwriting crap locale %s (%r -> %r)' % (k, self.crap.get(sec, k), v)) self.crap.set(sec, k, v)
def read(self, path): """ Reads the config file specified by 'path' then reads all the files in the directory obtained by adding '.d' to 'path'. The files in the '.d' directory are read in normal sorted order and section entries in these files override entries in the main file. """ if os.path.exists(path): RawConfigParser.readfp(self, codecs.open(path, 'r', 'utf-8')) path_d = path + ".d" files = [] if os.path.exists(path_d): files = [os.path.join(path_d, f) for f in os.listdir(path_d)] files.sort() for fname in files: p = RawConfigParser() p.readfp(codecs.open(fname, 'r', 'utf-8')) for section_name in p.sections(): # New files override old, so remove first to avoid # DuplicateSectionError. self.remove_section(section_name) self.add_section(section_name) for (name, value) in p.items(section_name): self.set(section_name, name, value) # Store the filename this section was read from. self.set(section_name, '_filename_', fname)
def _parse_cfg_file_patterns( cfg_parser: configparser.RawConfigParser, ) -> typ.Iterable[FileRawPatternsItem]: file_pattern_items: typ.List[typ.Tuple[str, str]] if cfg_parser.has_section("pycalver:file_patterns"): file_pattern_items = cfg_parser.items("pycalver:file_patterns") elif cfg_parser.has_section("bumpver:file_patterns"): file_pattern_items = cfg_parser.items("bumpver:file_patterns") else: return for filepath, patterns_str in file_pattern_items: maybe_patterns = (line.strip() for line in patterns_str.splitlines()) patterns = [p for p in maybe_patterns if p] yield filepath, patterns
def read_config(self, config): result = [] stack = [config] while 1: config = stack.pop() src = None if isinstance(config, (str, unicode)): src = os.path.relpath(config) _config = RawConfigParser() _config.optionxform = lambda s: s if getattr(config, 'read', None) is not None: _config.readfp(config) path = self.path else: if not os.path.exists(config): log.error("Config file '%s' doesn't exist.", config) sys.exit(1) _config.read(config) path = os.path.dirname(config) for section in reversed(_config.sections()): for key, value in reversed(_config.items(section)): result.append((src, path, section, key, value)) result.append((src, path, section, None, None)) if _config.has_option('global', 'extends'): extends = _config.get('global', 'extends').split() elif _config.has_option('global:global', 'extends'): extends = _config.get('global:global', 'extends').split() else: break stack[0:0] = [ os.path.abspath(os.path.join(path, x)) for x in reversed(extends)] return reversed(result)
class Config(object): """Manage configuration read from a secrets file.""" DEFAULTS = { 'username': None, 'password': None, 'authurl': None, 'read-only': '0', } def __init__(self, secrets_file): """ Read configuration from the secrets file. A default_authurl can be provided. """ stat = os.stat(secrets_file) if stat.st_mode & 0x004 != 0: log = logging.getLogger(__package__) log.warning( "%s is world readable, please consider changing its permissions to 0600" % secrets_file) self.secrets_file = secrets_file self.conf = RawConfigParser(Config.DEFAULTS) self.conf.read(secrets_file) def items(self): """ Generator that returns pairs of container name and a dictionary with the values associated to that container. See Config.DEFAULTS for the valid values. """ for name in self.list_containers(): yield name, self.get_container(name) def get_container(self, name): """ Get a dictionary with the values associated to a container. See Config.DEFAULTS for the valid values. """ if not self.conf.has_section(name): raise ValueError("%s not found in %s" % (name, self.secrets_file)) data = dict(self.conf.items(name)) # deprecation warning if data.pop("authurl"): log = logging.getLogger(__package__) log.warning( "in container %r: 'authurl' config token is no longer in use and it'll be ignored" % name) return data def list_containers(self): """List all container names.""" return self.conf.sections()
def get_now_settings(): rcp = RawConfigParser() rcp.read("taobao/config/settings.cfg") section = rcp.sections()[0] for item in rcp.items(section): print(item[0] + ':' + item[1])
def load(self, csv): conf = RawConfigParser() # utf-8-sig per https://bugs.python.org/issue7185#msg94346 with open(csv, encoding='utf-8-sig') as f: conf.read_file(f) for sec in conf.sections(): if not self.conf.has_section(sec): self.conf.add_section(sec) self.crap.add_section(sec) for k, v in conf.items(sec): is_crap = False if '__' in v: is_crap = True if not is_crap: if self.conf.has_option(sec, k): if self.conf.get(sec, k).lower() != v.lower(): print('Overwriting locale %s (%r -> %r)' % (k, self.conf.get(sec, k), v)) self.conf.set(sec, k, v) else: if self.crap.has_option(sec, k): print('Overwriting crap locale %s (%r -> %r)' % (k, self.crap.get(sec, k), v)) self.crap.set(sec, k, v)
def _get_config( profile="Default", config_path="~/.pushoverrc", user_key=None, api_token=None, device=None, ): config_path = os.path.expanduser(config_path) config = RawConfigParser() config.read(config_path) params = {"user_key": None, "api_token": None, "device": None} try: params.update(dict(config.items(profile))) except NoSectionError: pass if user_key: params["user_key"] = user_key if api_token: params["api_token"] = api_token if device: params["device"] = device if not TOKEN: init(params["api_token"]) if not TOKEN: raise InitError return params
def read_configfile(): global cfg, DEBUG,DOVECOT,GPGMAILENCRYPT,MAILDIRLOCK cfg=dict() _cfg = RawConfigParser() try: _cfg.read(CONFIGFILE) except: log("Could not read config file '%s'."%CONFIGFILE,"e",ln=lineno()) return for sect in _cfg.sections(): cfg[sect] = dict() for (name, value) in _cfg.items(sect): cfg[sect][name] = value if 'default' in cfg: if 'gpgmailencrypt' in cfg['default']: GPGMAILENCRYPT=cfg['default']['gpgmailencrypt'] if 'mail' in cfg: if 'dovecot' in cfg['mail'] and cfg['mail']['dovecot']=="yes": DOVECOT=True if 'maildirlock' in cfg['mail']: MAILDIRLOCK=cfg['mail']['maildirlock']
def test_imap_config_values_should_be_stored(): am = AccountManager() option_spec = get_mailbox_parameter_specs('imap') options = { 'user': '******', 'password': '', 'server': 'imap.example.org', 'port': '', 'ssl': True, 'imap': True, 'idle': True, 'folders': ['a', 'b'], } config = RawConfigParser() config.add_section('account1') am._set_cfg_options(config, 'account1', options, option_spec) expected_config_items = [ ('user', 'you'), ('password', ''), ('server', 'imap.example.org'), ('port', ''), ('ssl', '1'), ('imap', '1'), ('idle', '1'), ('folder', '["a", "b"]'), ] assert set(expected_config_items) == set(config.items('account1'))
def config(filename='database.ini', section='postgresql'): db = {} if "DB_NAME" in os.environ: print('getting db settings from environment variables') db['host'] = os.environ['DB_HOST'] db['port'] = os.environ['DB_PORT'] db['database'] = os.environ['DB_NAME'] db['user'] = os.environ['DB_USERNAME'] db['password'] = os.environ['DB_PASSWORD'] else: print('getting db settings from config file') # create a parser parser = RawConfigParser() # read config file parser.read(filename) # get section, default to postgresql if parser.has_section(section): params = parser.items(section) for param in params: db[param[0]] = param[1] else: raise Exception('Section {0} not found in the {1} file'.format( section, filename)) return db
def load(self, csv): conf = RawConfigParser() with open(csv, 'rb') as f: input_bytes = f.read() decoded = input_bytes.decode(chardet.detect(input_bytes)['encoding']) decoded = '[__global__]\n' + decoded conf.read_string(decoded) for sec in conf.sections(): if not self.conf.has_section(sec): self.conf.add_section(sec) self.crap.add_section(sec) for k, v in conf.items(sec): is_crap = False if '__' in v: is_crap = True if not is_crap: if self.conf.has_option(sec, k): if self.conf.get(sec, k).lower() != v.lower(): print('Overwriting locale %s (%r -> %r)' % (k, self.conf.get(sec, k), v)) self.conf.set(sec, k, v) else: if self.crap.has_option(sec, k): print('Overwriting crap locale %s (%r -> %r)' % (k, self.crap.get(sec, k), v)) self.crap.set(sec, k, v)
def load_cfg(): defaults = {"name": "cz_conventional_commits"} config = RawConfigParser("") try: home = str(Path.home()) except AttributeError: home = os.path.expanduser("~") config_file = ".cz" global_cfg = os.path.join(home, config_file) # load cfg from current project configs = ["setup.cfg", ".cz.cfg", config_file, global_cfg] for cfg in configs: if os.path.exists(cfg): logger.debug('Reading file "%s"', cfg) config.readfp(io.open(cfg, "rt", encoding="utf-8")) log_config = io.StringIO() config.write(log_config) try: defaults.update(dict(config.items("commitizen"))) break except NoSectionError: # The file does not have commitizen sectioncz continue return defaults
def run(self): try: config_file = DEFAULT_CONF_FILE if not os.path.isfile(config_file): raise ValueError('Configuration file not found: {0}'.format(config_file)) config = RawConfigParser() config.read(config_file) if 'security' in config.sections(): return 'The configuration file is already migrated to the new version' config.add_section('security') config.add_section('database') for item in config.items('paths'): if item[0] == 'database_file': config.set('database', item[0], item[1]) else: config.set('security', item[0], item[1]) config.remove_section('paths') config.set('security', 'crl_file_url', 'None') config.set('logging', 'log_level', 'INFO') with open(config_file, 'w') as file: config.write(file) except Exception as exc: return exc return 'Configuration file migrated'
def load_fp(fp): cfg = RawConfigParser() cfg.read_file(fp) for section in cfg.sections(): for k, v in cfg.items(section): rkey = '.'.join((section, k)) apply_kv(rkey, v)
def __init__(self, config_path=None): if config_path is None: config_path = 'regexbot.ini' config = RawConfigParser() config.read_dict(DEFAULT_CONFIG) config.read(config_path) self.rtm_token = config.get('regexbot', 'rtm_token') self.channel_flood_cooldown = timedelta(seconds=config.getint('regexbot', 'channel_flood_cooldown')) self.global_flood_cooldown = timedelta(seconds=config.getint('regexbot', 'global_flood_cooldown')) self.max_messages = config.getint('regexbot', 'max_messages') self.max_message_size = config.getint('regexbot', 'max_message_size') self.version = str(config.get('regexbot', 'version')) + '; %s' try: self.version = self.version % Popen(["git","branch","-v","--contains"], stdout=PIPE).communicate()[0].strip() except: self.version = self.version % 'unknown' self._last_message_times = {} self._last_message = datetime.utcnow() self._message_buffer = {} self.ignore_list = [] if config.has_section('ignore'): for k,v in config.items('ignore'): try: self.ignore_list.append(regex.compile(str(v), regex.I)) except Exception, ex: print "Error compiling regular expression in ignore list (%s):" % k print " %s" % v print ex exit(1)
def __nested__(func): from configparser import RawConfigParser conf = RawConfigParser() conf.read(conf_file) items = dict(conf.items(section)) return PgConnProxy(items, func)
def read_configfile(): global cfg, DEBUG, DOVECOT, GPGMAILENCRYPT, MAILDIRLOCK cfg = dict() _cfg = RawConfigParser() try: _cfg.read(CONFIGFILE) except: log("Could not read config file '%s'." % CONFIGFILE, "e", ln=lineno()) return for sect in _cfg.sections(): cfg[sect] = dict() for (name, value) in _cfg.items(sect): cfg[sect][name] = value if 'default' in cfg: if 'gpgmailencrypt' in cfg['default']: GPGMAILENCRYPT = cfg['default']['gpgmailencrypt'] if 'mail' in cfg: if 'dovecot' in cfg['mail'] and cfg['mail']['dovecot'] == "yes": DOVECOT = True if 'maildirlock' in cfg['mail']: MAILDIRLOCK = cfg['mail']['maildirlock']
def parse_config(self, filename): #from ConfigParser import RawConfigParser from configparser import RawConfigParser import io config = RawConfigParser() config.readfp(io.open(filename, 'r', encoding='utf_8_sig')) for s in config.sections(): port = int(config.get(s, 'port')) config.remove_option(s, 'port') xsize, ysize = [int(d) for d in config.get(s, 'size').split(",")] config.remove_option(s, 'size') x_off, y_off = [int(d) for d in config.get(s, 'offset').split(",")] config.remove_option(s, 'offset') self.offsets[s] = (x_off, y_off) for device, offset in config.items(s): x_off, y_off = [int(d) for d in offset.split(",")] if device in self.offsets: if (x_off, y_off) != self.offsets[device]: raise RuntimeError( "conflicting offsets for device %s" % device) self.offsets[device] = (x_off, y_off) if s in self.transtbl: self.transtbl[s].append(device) else: self.transtbl[s] = [device] if device in self.transtbl: self.transtbl[device].append(s) else: self.transtbl[device] = [s] self.add_virtual(s, xsize, ysize, port)
def main(): # general app configuration # set root directory for the app (this directory, that is) root = Path.cwd() # setup configuration file path using the APP_ENV environment variable cfg_path = root / 'config' / '{}.ini'.format(os.environ.get('APP_ENV')) cfg_parser = RawConfigParser() # read .ini file for the appropriate app setup (dev, prod or test) cfg = cfg_parser.read(cfg_path) # create a dict with the config cfg_dict = {x: dict(cfg_parser.items(x)) for x in cfg_parser.sections()} # Logger configuration log_path = root / 'logs' # create logs directory if not present log_path.mkdir(mode=0o755, parents=True, exist_ok=True) # load the logger configuration from the configuration file logging_cfg.fileConfig(cfg_path, disable_existing_loggers=False) # initialize logger logger = logging.getLogger(__name__) # create app instance for dev, test or prod app = create_app(cfg_dict) # make a log logger.info("Running app: " + (os.environ.get('APP_NAME') or "Anonymous App")) return True
def test_init_admin_repository(): tmp = maketemp() admin_repository = os.path.join(tmp, 'admin.git') pubkey = ('ssh-somealgo ' + '0123456789ABCDEFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= fakeuser@fakehost') user = '******' init.init_admin_repository( git_dir=admin_repository, pubkey=pubkey, user=user, ) eq(os.listdir(tmp), ['admin.git']) hook = os.path.join( tmp, 'admin.git', 'hooks', 'post-update', ) util.check_mode(hook, 0o755, is_file=True) got = util.readFile(hook).splitlines() assert 'gitosis-run-hook post-update' in got export_dir = os.path.join(tmp, 'export') repository.export(git_dir=admin_repository, path=export_dir) eq(sorted(os.listdir(export_dir)), sorted(['gitosis.conf', 'keydir'])) eq(os.listdir(os.path.join(export_dir, 'keydir')), ['jdoe.pub']) got = util.readFile(os.path.join(export_dir, 'keydir', 'jdoe.pub')) eq(got, pubkey) # the only thing guaranteed of initial config file ordering is # that [gitosis] is first got = util.readFile(os.path.join(export_dir, 'gitosis.conf')) got = got.splitlines()[0] eq(got, '[gitosis]') cfg = RawConfigParser() cfg.read(os.path.join(export_dir, 'gitosis.conf')) eq(sorted(cfg.sections()), sorted([ 'gitosis', 'group gitosis-admin', ])) eq(cfg.items('gitosis'), []) eq(sorted(cfg.items('group gitosis-admin')), sorted([ ('writable', 'gitosis-admin'), ('members', 'jdoe'), ]))
def read_config(self, configfilename): """ Returns all of the needed information from the config file called <name>.cfg. Also checks to make sure all of the config parameters are set based on the configure dictionaries given in the configDictionaryList. """ survey_dict = {} mosaic_dict = {} utils_dict = {} spectral_dict = {} spatial_dict = {} try: self.check_for_files([SURVEY_CONFIG_DIR + configfilename + ".cfg"]) self.logger.info('Reading from config file (' + configfilename + '.cfg)') config = RawConfigParser() config.read(SURVEY_CONFIG_DIR + configfilename + '.cfg') if config.has_section('survey'): survey_dict = dict(config.items('survey')) if config.has_section('mosaic'): mosaic_dict = dict(config.items('mosaic')) if config.has_section('constants'): utils_dict = dict(config.items('constants')) if config.has_section('spectralSearch'): spectral_dict = dict(config.items('spectralSearch')) if config.has_section('spatialSearch'): spatial_dict = dict(config.items('spatialSearch')) list_of_dict = { 'survey': survey_dict, 'mosaic': mosaic_dict, 'constants': utils_dict, 'spectral': spectral_dict, 'spatial': spatial_dict } return list_of_dict except FileNotFound: raise FileNotFound
def read(self, filename): parser = RawConfigParser(dict_type=OrderedDict) read = parser.read(filename) if read != [filename]: raise RuntimeError("Unable to read batch file: %s." % filename) for s in parser.sections(): typ, nam = s.split("::") if typ.lower() == 'arg': self.args[nam.lower()] = OrderedDict(parser.items(s)) elif typ.lower() == 'batch': self.batches[nam.lower()] = OrderedDict(parser.items(s)) elif typ.lower() == 'command': self.commands[nam.lower()] = OrderedDict(parser.items(s)) else: raise RuntimeError("Unknown section type: '%s'." % typ) self.expand_groups()
def _get_mapped_libraries(self): """ Get mapped libraries from modelsim.ini file """ cfg = RawConfigParser() cfg.read(self._modelsim_ini) libraries = dict(cfg.items("Library")) if "others" in libraries: del libraries["others"] return libraries
def readConfig(section): parser = RawConfigParser() if getattr(sys, 'frozen', False): # frozen parser.read(os.path.dirname(os.path.realpath(sys.executable))+'/config') else: # unfrozen parser.read(os.path.dirname(os.path.realpath(__file__))+'/config') return dict(parser.items(section))
def parse_index(self, fp): """ Parse index of a repository :param fp: file descriptor to read :type fp: buffer """ config = RawConfigParser() config.readfp(fp) # Read default parameters items = dict(config.items(DEFAULTSECT)) try: self.name = items['name'] self.update = int(items['update']) self.maintainer = items['maintainer'] self.signed = bool(int(items.get('signed', '0'))) self.key_update = int(items.get('key_update', '0')) except KeyError as e: raise RepositoryUnavailable('Missing global parameters in repository: %s' % e) except ValueError as e: raise RepositoryUnavailable('Incorrect value in repository parameters: %s' % e) if len(self.name) == 0: raise RepositoryUnavailable('Name is empty') if 'url' in items: self.url = items['url'] self.local = self.url.startswith('file://') elif self.local is None: raise RepositoryUnavailable('Missing "url" key in settings') # Load modules self.modules.clear() for section in config.sections(): module = ModuleInfo(section) module.load(dict(config.items(section))) if not self.local: module.url = posixpath.join(self.url, '%s.tar.gz' % module.name) module.repo_url = self.url module.signed = self.signed self.modules[section] = module
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(u'[root]\n'+ini)) return dict(config.items('root')) else: raise ValueError('Unexpected type for ini file %s'%type(ini))
def load_rcfile(self): if self.RCFILE == DEFAULT_SETTINGS['RCFILE'] and \ not os.path.exists(self.RCFILE) and os.path.exists(OLD_RCFILE): sys.stderr.write("Warning: Old rcfile found at %s, please rename to %s.\n" \ % (OLD_RCFILE, self.RCFILE)) self.RCFILE = OLD_RCFILE if os.path.exists(self.RCFILE): config = RawConfigParser() config.optionxform = lambda x: x.upper() config.read(self.RCFILE) items = [] if config.has_section('global'): items.extend(config.items('global')) if self.NAME is not None and config.has_section(self.NAME): items.extend(config.items(self.NAME)) self.load_rcvalues(items) self.update_implications()
def __init__(self, paths): parser = RawConfigParser() parser.read(paths) for section in parser.sections(): options = { k: self.__try_parse(v) for k, v in parser.items(section) } section = section.upper() if hasattr(self, section): getattr(self, section).update(options) else: setattr(self, section, options)
class IniConfig(object): """This class is used to access/read config file, if it exists. :param config_file: the config file name :type config_file: str or None """ def __init__(self, filename=None): self.config_file = filename self.parser = RawConfigParser() self.load() def load(self, encoding="utf-8"): """Load a config file from the list of paths, if it exists.""" config_file = self.config_file if os.path.isfile(config_file) and os.path.getsize(config_file) > 0: try: if is_py3: self.parser.read(config_file, encoding=encoding) else: self.parser.read(config_file) # print(_("DEBUG: Read configuration file %s") % config_file) except UnicodeDecodeError as e: print (_("Error: Cannot decode configuration file '{0}': {1}").format(config_file, e)) sys.exit(1) def items(self, section): """Return the items list of a section.""" return self.parser.items(section) def has_section(self, section): """Return info about the existence of a section.""" return self.parser.has_section(section) def get_option(self, section, option): """Get the float value of an option, if it exists.""" try: value = self.parser.getfloat(section, option) except NoOptionError: return else: return value def get_raw_option(self, section, option): """Get the raw value of an option, if it exists.""" try: value = self.parser.get(section, option) except NoOptionError: return else: return value
def load_rcfile(self): self.process_args() if self.RCFILE == parser.get_default('RCFILE') and \ not os.path.exists(self.RCFILE) and os.path.exists(OLD_RCFILE): sys.stderr.write("Warning: Using old rcfile found at %s, " "please rename to %s.\n" % (OLD_RCFILE, self.RCFILE)) self.RCFILE = OLD_RCFILE if os.path.exists(self.RCFILE): config = RawConfigParser() config.optionxform = lambda x: x.upper() config.read(self.RCFILE) items = [] if config.has_section('global'): items.extend(config.items('global')) if self.NAME is not None and config.has_section(self.NAME): items.extend(config.items(self.NAME)) return self.parse_rcvalues(items) return {}
class Config(object): """Manage configuration read from a secrets file.""" DEFAULTS = { 'username': None, 'password': None, 'authurl': None, 'read-only': '0', } def __init__(self, secrets_file, default_authurl=None): """ Read configuration from the secrets file. A default_authurl can be provided. """ if default_authurl: Config.DEFAULTS['authurl'] = default_authurl stat = os.stat(secrets_file) if stat.st_mode & 0x004 != 0: log = logging.getLogger(__package__) log.warning("%s is world readable, please consider changing its permissions to 0600" % secrets_file) self.secrets_file = secrets_file self.conf = RawConfigParser(Config.DEFAULTS) self.conf.read(secrets_file) def items(self): """ Generator that returns pairs of container name and a dictionary with the values associated to that contaiener. See Config.DEFAULTS for the valid values. """ for name in self.list_containers(): yield name, self.get_container(name) def get_container(self, name): """ Get a dictionary with the values associated to a container. See Config.DEFAULTS for the valid values. """ if not self.conf.has_section(name): raise ValueError("%s not found in %s" % (name, self.secrets_file)) return dict(self.conf.items(name)) def list_containers(self): """List all container names.""" return self.conf.sections()
def __init__(self, path): self.path = path self.versions = {} try: with open(os.path.join(self.path, self.VERSIONS_LIST), 'r') as fp: config = RawConfigParser() config.readfp(fp) # Read default parameters for key, value in config.items(DEFAULTSECT): self.versions[key] = int(value) except IOError: pass
class FactorioLocale: def __init__(self): self.conf = RawConfigParser() self.crap = RawConfigParser() def get_name(self, section, name): return self.conf.get(section, name) or '#%s#%s#' % (section, name) def load(self, csv): conf = RawConfigParser() with open(csv, 'rb') as f: input_bytes = f.read() decoded = input_bytes.decode(chardet.detect(input_bytes)['encoding']) decoded = '[__global__]\n' + decoded conf.read_string(decoded) for sec in conf.sections(): if not self.conf.has_section(sec): self.conf.add_section(sec) self.crap.add_section(sec) for k, v in conf.items(sec): is_crap = False if '__' in v: is_crap = True if not is_crap: if self.conf.has_option(sec, k): if self.conf.get(sec, k).lower() != v.lower(): print('Overwriting locale %s (%r -> %r)' % (k, self.conf.get(sec, k), v)) self.conf.set(sec, k, v) else: if self.crap.has_option(sec, k): print('Overwriting crap locale %s (%r -> %r)' % (k, self.crap.get(sec, k), v)) self.crap.set(sec, k, v) def merge(self): for sec in self.crap.sections(): for k, v in self.crap.items(sec): if not self.conf.has_option(sec, k): print('Using crap locale %s (%r)' % (k, v)) self.conf.set(sec, k, v) def save(self, out): with open(out, 'w') as f: self.conf.write(f)
def get_config(config_file): if not os.path.isabs(config_file): config_file = os.path.abspath(config_file) config = INIParser() with open(config_file) as f: config.readfp(f) def _parse_value(v): if v.lower() == 'true': return True if v.lower() == 'false': return False return v return {k: _parse_value(v) for k, v in config.items('app:main')}
def _setup(self): """Initializes all required attributes, getting the values from the configuration file. """ # On Windows the current path is where PythonService.exe is located. # We change the current path to the location of this script. os.chdir(os.path.dirname(os.path.realpath( __file__ ))) if not os.path.isfile(self.config_file): raise ValueError('Configuration file not found: {}'.format( self.config_file)) config = RawConfigParser(DEFAULTS) config.read((self.config_file,)) self.port = int(config.get('general', 'port')) self.ca_file = config.get('security', 'known_ca') self.cert_key_file = config.get('security', 'cert_key_file') self.crl_file_url = config.get('security', 'crl_file_url') self.db_file = config.get('database', 'database_file') self.logfile = config.get('logging', 'log_file') self.loglevel = config.get('logging', 'log_level') self.logformat = config.get('logging', 'log_format') if not os.path.isfile(self.ca_file): raise ValueError('Known CAs file not found: {}'.format( self.ca_file)) if not os.path.isfile(self.cert_key_file): raise ValueError('Certificate and key file not found: {}'.format( self.cert_key_file)) self.db = Database(self.db_file) if self.db.new_db: for obj in objects.objects: obj.create_table(self.db) conf = dict([(sc, dict(config.items(sc))) for sc in config.sections()]) self.context = dict(conf=conf, db=self.db) for obj in objects.objects: self.context[obj.__name__] = obj logging.basicConfig( filename=self.logfile, format=self.logformat, level=getattr(logging, self.loglevel))
class FactorioLocale: def __init__(self): self.conf = RawConfigParser() self.crap = RawConfigParser() def get_name(self, section, name): return self.conf.get(section, name) or '#%s#%s#' % (section, name) def load(self, csv): conf = RawConfigParser() # utf-8-sig per https://bugs.python.org/issue7185#msg94346 with open(csv, encoding='utf-8-sig') as f: conf.read_file(f) for sec in conf.sections(): if not self.conf.has_section(sec): self.conf.add_section(sec) self.crap.add_section(sec) for k, v in conf.items(sec): is_crap = False if '__' in v: is_crap = True if not is_crap: if self.conf.has_option(sec, k): if self.conf.get(sec, k).lower() != v.lower(): print('Overwriting locale %s (%r -> %r)' % (k, self.conf.get(sec, k), v)) self.conf.set(sec, k, v) else: if self.crap.has_option(sec, k): print('Overwriting crap locale %s (%r -> %r)' % (k, self.crap.get(sec, k), v)) self.crap.set(sec, k, v) def merge(self): for sec in self.crap.sections(): for k, v in self.crap.items(sec): if not self.conf.has_option(sec, k): print('Using crap locale %s (%r)' % (k, v)) self.conf.set(sec, k, v) def save(self, out): with open(out, 'w') as f: self.conf.write(f)
def verify_pki_config_override(cls, filename): """Verify pki config override file * filename must be an absolute path to an existing file * file must be a valid ini file * ini file must not override immutable settings TODO: The checker does not verify config interpolation values, yet. The validator does not have access to all settings. :param filename: path to pki.ini """ if not os.path.isfile(filename): raise ValueError( "Config file '{}' does not exist.".format(filename) ) if not os.path.isabs(filename): raise ValueError( "Config file '{}' is not an absolute path.".format(filename) ) try: cfg = RawConfigParser() with open(filename) as f: cfg.read_file(f) except Exception as e: raise ValueError( "Invalid config '{}': {}".format(filename, e) ) immutable_keys = cls.get_immutable_keys() invalid_keys = set() sections = [cfg.default_section] sections.extend(cls.subsystems) for section in sections: if not cfg.has_section(section): continue for k, _v in cfg.items(section, raw=True): if k in immutable_keys: invalid_keys.add(k) if invalid_keys: raise ValueError( "'{}' overrides immutable options: {}".format( filename, ', '.join(sorted(invalid_keys)) ) )
class ConfigParser(object): __slots__ = '_config', 'schemas' def __init__(self, schemas): self.schemas = schemas self._config = RawConfigParser() def __getitem__(self, key): return self._convert()[key] def __iter__(self): return iter(self._convert()) def __str__(self): return '<%s(%s)>' % (self.__class__.__name__, self._convert()) def _convert(self): ret = {} for section in self._config.sections(): data = {} for key, value in self._config.items(section): data[key] = value section_list = section.split('_') section_base = section_list[-1] if section_base in self.schemas: section_ret = tuple(section_list) data = self._convert_one(self.schemas[section_base], data) else: section_ret = (section, ) ret[section_ret] = data return ret def _convert_one(self, schema, data): ret = {} for key, value in data.items(): if key in schema: value = schema[key](value) ret[key] = value return ret def keys(self): return self._convert().keys() def read(self, data): return self._config.read(data)
def from_file(cls, path, skip_clean=False, **defaults): self = cls() self.update(defaults) parser = ConfigParser() parser.read(path) sections = parser.sections() if not sections: raise ConfigurationError( "Missing or empty configuration file at '{}'".format(path)) for section in sections: for key, value in parser.items(section): if section not in ('DEFAULT', 'bottle'): key = '{}.{}'.format(section, key) if not skip_clean: value = self.clean_value(value) self[key] = value return self
def load_config(configuration_id, seen=None, **overrides): # flake8: noqa if seen is None: seen = [] if configuration_id in seen: raise ValueError( 'Recursive configuration extends: %s (%r)' % (configuration_id, seen)) seen.append(configuration_id) if ':' in configuration_id: configuration_file = resolvePackageReference(configuration_id) else: config_info = configuration_registry.getConfiguration(configuration_id) configuration_file = config_info['configuration'] parser = RawConfigParser() parser.optionxform = str # case sensitive parser.readfp(open(configuration_file)) result = {} includes = None for section in parser.sections(): result[section] = dict(parser.items(section)) if section == 'transmogrifier': includes = result[section].pop('include', includes) if includes: for configuration_id in includes.split()[::-1]: include = load_config(configuration_id, seen) sections = set(include.keys()) | set(result.keys()) for section in sections: result[section] = update_section( result.get(section, {}), include.get(section, {})) seen.pop() for section, options in iteritems(overrides): assert section in result, \ 'Overrides include non-existing section {0:s}'.format(section) for key, value in iteritems(options): assert key in result[section], \ 'Overrides include non-existing key {0:s}:{1:s}'.format( section, key) result[section][key] = value return result