def process(self): nm_config = NetworkManagerConfig() try: # Use 'NM --print-config' to read the configurationo so # that the main configuration file and other files in # various directories get merged in the right way. r = run(['NetworkManager', '--print-config'], split=False)['stdout'] except (OSError, CalledProcessError) as e: self.log.warning( 'Error reading NetworkManager configuration: {}'.format(e)) return parser = ConfigParser() try: if hasattr(parser, 'read_string'): # Python 3 parser.read_string(r) else: # Python 2 from cStringIO import StringIO parser.readfp(StringIO(r)) except ParsingError as e: self.log.warning( 'Error parsing NetworkManager configuration: {}'.format(e)) return if parser.has_option('main', 'dhcp'): nm_config.dhcp = parser.get("main", "dhcp") self.produce(nm_config)
def get_config(p): """Read a config file. :return: dict of ('section.option', value) pairs. """ cfg = {} parser = ConfigParser() if hasattr(parser, 'read_file'): parser.read_file(Path(p).open(encoding='utf8')) else: # pragma: no cover assert PY2 # The `read_file` method is not available on ConfigParser in py2.7! parser.readfp(Path(p).open(encoding='utf8')) for section in parser.sections(): getters = { 'int': partial(parser.getint, section), 'boolean': partial(parser.getboolean, section), 'float': partial(parser.getfloat, section), 'list': lambda option: parser.get(section, option).split(), } default = partial(parser.get, section) for option in parser.options(section): type_ = option.rpartition('_')[2] if '_' in option else None value = getters.get(type_, default)(option) cfg['{0}.{1}'.format(section, option)] = value return cfg
def _get_attach_points(self, info, size_request): has_attach_points_, attach_points = info.get_attach_points() attach_x = attach_y = 0 if attach_points: # this works only for Gtk < 3.14 # https://developer.gnome.org/gtk3/stable/GtkIconTheme.html # #gtk-icon-info-get-attach-points attach_x = float(attach_points[0].x) / size_request attach_y = float(attach_points[0].y) / size_request elif info.get_filename(): # try read from the .icon file icon_filename = info.get_filename().replace('.svg', '.icon') if icon_filename != info.get_filename() and \ os.path.exists(icon_filename): try: with open(icon_filename) as config_file: cp = ConfigParser() cp.readfp(config_file) attach_points_str = cp.get('Icon Data', 'AttachPoints') attach_points = attach_points_str.split(',') attach_x = float(attach_points[0].strip()) / 1000 attach_y = float(attach_points[1].strip()) / 1000 except Exception as e: logging.exception('Exception reading icon info: %s', e) return attach_x, attach_y
def func(): with self._lock: # First, see if user has defined a token using set_token if self._credentials is not None: return self._credentials # If not, look for config file in intern file location config_path = '~/.intern/intern.cfg' if os.path.isfile(os.path.expanduser(config_path)): with open(os.path.expanduser(config_path), 'r') as config_file_handle: config_parser = ConfigParser() config_parser.readfp(config_file_handle) # Try Default section first try: self._credentials = config_parser["Default"][ "token"] print("Using token from intern config file") return dict(tokenType=u'Token', accessToken=self._credentials) except: pass # Try Volume Service section second try: self._credentials = config_parser[ "Volume Service"]["token"] print("Using token from intern config file") return dict(tokenType=u'Token', accessToken=self._credentials) except: pass # Else, use "public" print("Accessing Boss data using token 'public'") return dict(tokenType=u'Token', accessToken='public')
class RepoConfiguration(object): """ Read configuration from repository. """ DEFAULT_CONFIG = dedent("""\ [autorebuild] enabled = false """) def __init__(self, dir_path='', file_name=REPO_CONFIG_FILE): self._config_parser = ConfigParser() self.container = {} # Set default options self._config_parser.readfp(StringIO(self.DEFAULT_CONFIG)) config_path = os.path.join(dir_path, file_name) if os.path.exists(config_path): self._config_parser.read(config_path) file_path = os.path.join(dir_path, REPO_CONTAINER_CONFIG) if os.path.exists(file_path): with open(file_path) as f: self.container = (yaml.load(f) or {}) def is_autorebuild_enabled(self): return self._config_parser.getboolean('autorebuild', 'enabled')
def read_mcf(mcf): """returns dict of ConfigParser object""" mcf_list = [] def makelist(mcf2): """recursive function for MCF by reference inclusion""" c = ConfigParser() LOGGER.debug('reading {}'.format(mcf2)) with codecs.open(mcf2, encoding='utf-8') as fh: c.readfp(fh) mcf_dict = c.__dict__['_sections'] for section in mcf_dict.keys(): if 'base_mcf' in mcf_dict[section]: base_mcf_path = get_abspath(mcf, mcf_dict[section]['base_mcf']) makelist(base_mcf_path) mcf_list.append(mcf2) else: # leaf mcf_list.append(mcf2) makelist(mcf) c = ConfigParser() for mcf_file in mcf_list: LOGGER.debug('reading {}'.format(mcf)) with codecs.open(mcf_file, encoding='utf-8') as fh: c.readfp(fh) mcf_dict = c.__dict__['_sections'] return mcf_dict
def handleSection(self, section, items): locales = items['locales'] if locales == 'all': inipath = '/'.join(( items['repo'], items['mozilla'], 'raw-file', 'default', items['l10n.ini'] )) ini = ConfigParser() ini.readfp(urlopen(inipath)) allpath = urljoin( urljoin(inipath, ini.get('general', 'depth')), ini.get('general', 'all')) locales = urlopen(allpath).read() locales = locales.split() obs = (Active.objects .filter(run__tree__code=section) .exclude(run__locale__code__in=locales) .order_by('run__locale__code')) obslocs = ' '.join(obs.values_list('run__locale__code', flat=True)) if not obslocs: self.stdout.write(' OK\n') return s = input('Remove %s? [Y/n] ' % obslocs) if s.lower() == 'y' or s == '': obs.delete()
def get_configuration(configuration=None): """ Parse a configuration file Parameters ---------- configuration : str or list, optional A configuration file or list of configuration files to parse, defaults to the deploy_default.conf file in the package and deploy.conf in the current working directory. Returns ------- configparser The parsed configuration """ if not configuration: # pragma: no cover configuration = [ # Config file that is part of the package # PACKAGE_DEFAULT_CONFIG, # Any deploy.conf files in the current directory 'deploy.conf' ] config = ConfigParser() # Set the config defaults try: config.read_string(config_defaults()) except AttributeError: config.readfp(io.BytesIO(config_defaults())) logger.debug('Working with default dict: %r', config_defaults()) config.read(configuration) return config
def get_config(p): """Read a config file. :return: dict of ('section.option', value) pairs. """ if not isinstance(p, Path): p = Path(p) cfg = {} parser = ConfigParser() parser.readfp(p.open(encoding='utf8')) for section in parser.sections(): getters = { 'int': partial(parser.getint, section), 'boolean': partial(parser.getboolean, section), 'float': partial(parser.getfloat, section), 'list': lambda option: parser.get(section, option).split(), } default = partial(parser.get, section) for option in parser.options(section): type_ = option.rpartition('_')[2] if '_' in option else None value = getters.get(type_, default)(option) cfg['{0}.{1}'.format(section, option)] = value return cfg
def test_export_empty_site(self): self._setUpAdapters() site = _makeFolder('site', site_folder=True) site.title = 'test_export_empty_site' site.description = 'Testing export of an empty site.' context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 0) filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) self.assertEqual(parser.get('DEFAULT', 'Title'), site.title) self.assertEqual(parser.get('DEFAULT', 'Description'), site.description)
def test_export_site_with_non_exportable_simple_items(self): self._setUpAdapters() ITEM_IDS = ('foo', 'bar', 'baz') site = _makeFolder('site', site_folder=True) site.title = 'AAA' site.description = 'DESCRIPTION' for id in ITEM_IDS: site._setObject(id, _makeItem(id)) context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 3) for index in range(len(ITEM_IDS)): self.assertEqual(objects[index][0], ITEM_IDS[index]) self.assertEqual(objects[index][1], TEST_CONTENT) filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) self.assertEqual(parser.get('DEFAULT', 'title'), 'AAA') self.assertEqual(parser.get('DEFAULT', 'description'), 'DESCRIPTION')
def _get_config_parser(self, text): parser = ConfigParser() try: parser.read_file(StringIO(text)) except AttributeError: # Python 2 parser.readfp(StringIO(text)) return parser
def read_config_file(cfgfile, options): config = ConfigParser() config.readfp(open(cfgfile)) if config.has_option('testflo', 'skip_dirs'): skips = config.get('testflo', 'skip_dirs') options.skip_dirs = [s.strip() for s in skips.split(',') if s.strip()]
def test_shared_part_list(self): # this slapos used shared_part_list cfg_parser = ConfigParser() with open( os.path.join(self.computer_partition_root_path, 'etc', 'slapos.cfg')) as f: cfg_parser.readfp(f) shared_part_list = cfg_parser.get('slapos', 'shared_part_list').splitlines() # web runner own shared parts. Note that there is intentionnaly a double # slash in this path, because slaprunner has double slash in paths since # early releases, including for the path of slapos repository that will be # used to develop and install software. If we fix this duplication, then # the URL of installed software will be different and it will get a different # hash and be reinstalled. To prevent this, we keep that // between srv and runner. self.assertEqual( '{}/srv//runner//shared'.format( self.computer_partition_root_path.rstrip('/')), shared_part_list[-1]) # shared parts from outer slapos outer_shared_part_list = os.getenv('SLAPOS_TEST_SHARED_PART_LIST', '').split(os.pathsep) for outer_shared_part in outer_shared_part_list: self.assertIn(outer_shared_part, shared_part_list)
def load_ini(self, ini_config): """ Read the provided ini contents arguments and merge the data in the ini config into the config object. ini_config is assumed to be a string of the ini file contents. """ parser = ConfigParser() parser.readfp(StringIO(ini_config)) data = { 'linters': {}, 'files': {}, 'branches': {}, } if parser.has_section('files'): ignore = parser.get('files', 'ignore') data['files']['ignore'] = newline_value(ignore) if parser.has_section('branches'): ignore = parser.get('branches', 'ignore') data['branches']['ignore'] = comma_value(ignore) linters = [] if parser.has_section('tools'): linters = comma_value(parser.get('tools', 'linters')) # Setup empty config sections for linter in linters: data['linters'][linter] = {} for section in parser.sections(): if not section.startswith('tool_'): continue # Strip off tool_ linter = section[5:] data['linters'][linter] = dict(parser.items(section)) self.update(data)
def load_config(): """Load configuration. :returns: Current configuration based on configuration file and environment variables. :rtype: dict """ defaults = {"api_key": ""} config_parser = ConfigParser(defaults) config_parser.add_section("greynoise") if os.path.isfile(CONFIG_FILE): LOGGER.debug("Parsing configuration file: %s...", CONFIG_FILE) with open(CONFIG_FILE) as config_file: config_parser.readfp(config_file) else: LOGGER.debug("Configuration file not found: %s", CONFIG_FILE) if "GREYNOISE_API_KEY" in os.environ: api_key = os.environ["GREYNOISE_API_KEY"] LOGGER.debug("API key found in environment variable: %s", api_key) # Environment variable takes precedence over configuration file content config_parser.set("greynoise", "api_key", api_key) return {"api_key": config_parser.get("greynoise", "api_key")}
def parse_color_map_from_file(f): color_map_config = ConfigParser() if isinstance(f, string_types): with open(f, 'r') as fp: color_map_config.readfp(fp) else: color_map_config.readfp(f) return parse_color_map_from_configparser(color_map_config)
def read_systemini(self): """read group info from system.ini this is part of the connection process """ self.ftpconn.connect(**self.ftpargs) self.ftpconn.cwd(posixpath.join(self.ftphome, 'Config')) lines = self.ftpconn.getlines('system.ini') self.ftpconn.close() pvtgroups = [] self.stages = OrderedDict() self.groups = OrderedDict() sconf = ConfigParser() sconf.readfp(StringIO('\n'.join(lines))) # read and populate lists of groups first for gtype, glist in sconf.items('GROUPS'): # ].items(): if len(glist) > 0: for gname in glist.split(','): gname = gname.strip() self.groups[gname] = OrderedDict() self.groups[gname]['category'] = gtype.strip() self.groups[gname]['positioners'] = [] if gtype.lower().startswith('multiple'): pvtgroups.append(gname) for section in sconf.sections(): if section in ('DEFAULT', 'GENERAL', 'GROUPS'): continue items = sconf.options(section) if section in self.groups: # this is a Group Section! poslist = sconf.get(section, 'positionerinuse') posnames = [a.strip() for a in poslist.split(',')] self.groups[section]['positioners'] = posnames elif 'plugnumber' in items: # this is a stage self.stages[section] = { 'stagetype': sconf.get(section, 'stagename') } if len(pvtgroups) == 1: self.set_trajectory_group(pvtgroups[0]) for sname in self.stages: ret = self._xps.PositionerMaximumVelocityAndAccelerationGet( self._sid, sname) try: self.stages[sname]['max_velo'] = ret[1] self.stages[sname]['max_accel'] = ret[2] / 3.0 except: print("could not set max velo/accel for %s" % sname) ret = self._xps.PositionerUserTravelLimitsGet(self._sid, sname) try: self.stages[sname]['low_limit'] = ret[1] self.stages[sname]['high_limit'] = ret[2] except: print("could not set limits for %s" % sname) return self.groups
def _parseINI(text): from six.moves.configparser import ConfigParser from six.moves import cStringIO parser = ConfigParser() try: parser.read_file(cStringIO(text)) except AttributeError: # Python 2 parser.readfp(cStringIO(text)) return parser
def handleApps(self, **kwargs): l10nbuilds = urlopen( 'https://raw.githubusercontent.com/Pike/master-ball/' 'master/l10n-master/l10nbuilds.ini') cp = ConfigParser() cp.readfp(l10nbuilds) for section in cp.sections(): self.stdout.write(section + '\n') self.handleSection(section, dict(cp.items(section)))
class INIReader(object): """ConfigParser wrapper able to cast value when reading INI options.""" # Helper casters cast_boolean = casts.Boolean() cast_dict = casts.Dict() cast_list = casts.List() cast_logging_level = casts.LoggingLevel() cast_tuple = casts.Tuple() cast_webdriver_desired_capabilities = casts.WebdriverDesiredCapabilities() def __init__(self, path): self.config_parser = ConfigParser() with open(path) as handler: self.config_parser.readfp(handler) if sys.version_info[0] < 3: # ConfigParser.readfp is deprecated on Python3, read_file # replaces it self.config_parser.readfp(handler) else: self.config_parser.read_file(handler) def get(self, section, option, default=None, cast=None): """Read an option from a section of a INI file. The default value will return if the look up option is not available. The value will be cast using a callable if specified otherwise a string will be returned. :param section: Section to look for. :param option: Option to look for. :param default: The value that should be used if the option is not defined. :param cast: If provided the value will be cast using the cast provided. """ try: value = self.config_parser.get(section, option) if cast is not None: if cast is bool: value = self.cast_boolean(value) elif cast is dict: value = self.cast_dict(value) elif cast is list: value = self.cast_list(value) elif cast is tuple: value = self.cast_tuple(value) else: value = cast(value) except (NoSectionError, NoOptionError): value = default return value def has_section(self, section): """Check if section is available.""" return self.config_parser.has_section(section)
def get_configuration_dict(configuration=None, value_types=None): """ Parse the configuration files Parameters ---------- configuration : str or list, optional A configuration file or list of configuration files to parse, defaults to the deploy_default.conf file in the package and deploy.conf in the current working directory. value_types : dict, optional Dictionary containing classes to apply to specific items Returns ------- dict Configuration dictionary """ if not value_types: # pragma: no cover value_types = config_types() if configuration is None or configuration is '': # pragma: no cover configuration = [ # Config file that is part of the package # PACKAGE_DEFAULT_CONFIG, # Any deploy.conf files in the current directory 'deploy.conf' ] config = ConfigParser() # Set the config defaults try: config.read_string(config_defaults()) except AttributeError: config.readfp(io.BytesIO(config_defaults())) logger.debug('Working with default dict: %r', config_defaults()) config.read(configuration) result_dict = {} for section in config.sections(): result_dict[section] = {} for key, val in config.items(section): result_dict[section][key] = str_format_env(val) config_update(result_dict) if 'locations' not in result_dict.keys(): result_dict['locations'] = {} result_dict['locations']['package_scripts'] = package_scripts_directory() if not result_dict['global'].get('virtualenv_dir', None): result_dict['global']['virtualenv_dir'] = \ default_virtualenv_directory() cast_types(result_dict) return result_dict
def _parseINI(text): from six.moves import cStringIO from six.moves.configparser import ConfigParser parser = ConfigParser() try: parser.read_file(cStringIO(text)) except AttributeError: # Python 2 parser.readfp(cStringIO(text)) return parser
def loadManifest(data): if six.PY2: parser = ConfigParser(None, multidict) parser.readfp(six.StringIO(data)) else: if isinstance(data, six.binary_type): data = data.decode() parser = ConfigParser(dict_type=multidict, strict=False) parser.read_string(data) return parser
def save_config(config): """Save configuration. :param config: Data to be written to the configuration file. :type config: dict """ config_parser = ConfigParser() config_parser.add_section("sublime") if len(config) == 0: click.echo( 'Error: no options provided. Try "sublime setup -h" for help.') click.get_current_context().exit(-1) # If either value was not specified, load the existing values saved # to ensure we don't overwrite their values to null here saved_config = load_config() if 'api_key' not in config or not config['api_key']: config['api_key'] = saved_config['api_key'] if 'save_dir' not in config or not config['save_dir']: config['save_dir'] = saved_config['save_dir'] if 'permission' not in config or not config['permission']: config['permission'] = saved_config['permission'] if config["save_dir"] and not os.path.isdir(config["save_dir"]): click.echo("Error: save directory is not a valid directory") click.get_current_context().exit(-1) config_parser.set("sublime", "api_key", config["api_key"]) config_parser.set("sublime", "save_dir", config["save_dir"]) config_parser.set("sublime", "permission", config["permission"]) config_parser_existing = ConfigParser() if os.path.isfile(CONFIG_FILE): # LOGGER.debug("Reading configuration file: %s...", CONFIG_FILE, path=CONFIG_FILE) with open(CONFIG_FILE) as config_file: config_parser_existing.readfp(config_file) # if an emailrep key exists, ensure we don't overwrite it try: emailrep_key = config_parser_existing.get("emailrep", "key") if emailrep_key: config_parser.add_section("emailrep") config_parser.set("emailrep", "key", emailrep_key) except: pass config_dir = os.path.dirname(CONFIG_FILE) if not os.path.isdir(config_dir): os.makedirs(config_dir) with open(CONFIG_FILE, "w") as config_file: config_parser.write(config_file)
def parse_xml_mapping(xml_mapping_filename): with open(xml_mapping_filename, 'r') as f: config = ConfigParser() if six.PY3: config.read_file(f) # pylint: disable=no-member else: config.readfp(f) return { k: dict(config.items(k)) for k in config.sections() }
def test_export_site_with_subfolders(self): self._setUpAdapters() FOLDER_IDS = ('foo', 'bar', 'baz') site = _makeFolder('site', site_folder=True) site.title = 'AAA' site.description = 'DESCRIPTION' for id in FOLDER_IDS: folder = _makeFolder(id) folder.title = 'Title: %s' % id folder.description = 'xyzzy' site._setObject(id, folder) context = DummyExportContext(site) exporter = self._getExporter() exporter(context) self.assertEqual(len(context._wrote), 2 + (2 * len(FOLDER_IDS))) filename, text, content_type = context._wrote[0] self.assertEqual(filename, 'structure/.objects') self.assertEqual(content_type, 'text/comma-separated-values') objects = [x for x in reader(StringIO(text))] self.assertEqual(len(objects), 3) for index in range(len(FOLDER_IDS)): id = FOLDER_IDS[index] self.assertEqual(objects[index][0], id) self.assertEqual(objects[index][1], TEST_FOLDER) filename, text, content_type = context._wrote[2 + (2 * index)] self.assertEqual(filename, '/'.join(('structure', id, '.objects'))) self.assertEqual(content_type, 'text/comma-separated-values') subobjects = [x for x in reader(StringIO(text))] self.assertEqual(len(subobjects), 0) filename, text, content_type = context._wrote[2 + (2 * index) + 1] self.assertEqual(filename, '/'.join( ('structure', id, '.properties'))) self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) self.assertEqual(parser.get('DEFAULT', 'Title'), 'Title: %s' % id) filename, text, content_type = context._wrote[1] self.assertEqual(filename, 'structure/.properties') self.assertEqual(content_type, 'text/plain') parser = ConfigParser() parser.readfp(StringIO(text)) self.assertEqual(parser.get('DEFAULT', 'title'), 'AAA') self.assertEqual(parser.get('DEFAULT', 'description'), 'DESCRIPTION')
def _load(self): self.files = self.options config = ConfigParser() for filepath in self.files: with codecs.open(filepath, 'r', encoding='utf-8') as stream: fakefile = StringIO("[top]\n" + stream.read()) config.readfp(fakefile) return [self._l10n2rec(key, config.get('top', key)) for key in config.options('top')]
def put_ini(self, text): """ """ context = self.context parser = ConfigParser() parser.readfp(cStringIO(text)) for option, value in parser.defaults().items(): prop_type = context.getPropertyType(option) if prop_type is None: context._setProperty(option, value, 'string') else: context._updateProperty(option, value)
def main(): ############################################################################# # # ARGUMENT PARSING # ############################################################################# parser = OptionParser(usage) parser.add_option( "-e", "--example", default=False, dest="example", action="store_true", help="Create example config.ini and an example sourcefile") (opts, args) = parser.parse_args() if opts.example: with open("omega_config.ini", "w") as f: f.write(ExampleConfig) with open("omegascanslist.txt", "w") as f: f.write(ExampleSourceFile) print( "Example files \"omega_config.ini\" and \"omegascanslist.txt\" are created" ) sys.exit(0) if len(args) != 1: parser.print_help() sys.exit("ERROR: Must provide one config.ini") cp = ConfigParser() cp.optionxform = str cp.readfp(open(args[0])) dag = OmegaScansDAG(cp) dag.write_sub_files() dag.write_dag() dag.write_script() #fix the sub and sh files #This is required because pipeline.py does not yet have the ability to add #a specific argument before all other arguments and options ('scan' in this case) fix_subfile(dag.submitFile) fix_scriptfile(cp.get('paths', 'basedir'), dag.get_dag_file(), cp.get('omegapipe', 'executable')) print('Successfully created DAG file.') fulldagpath = os.path.join(cp.get('paths', 'basedir'), dag.get_dag_file()) print('Now run condor_submit_dag %s\n' % (fulldagpath))
class Config(object): """A ConfigParser wrapper to support defaults when calling instance methods, and also tied to a single section""" SECTION = 'server' _loaded_files = [] def __init__(self, values=None, extra_sources=()): if values is None: sources = self._getsources() self.cp = ConfigParser() if __package__: default_config = ensure_str( get_data(__package__, 'scrapydd.default.conf')) self.cp.readfp(StringIO(default_config)) for source in sources + list(extra_sources): if os.path.exists(source): #self._load_config_file(open(source)) with open(source, 'r') as f: self.cp.readfp(f) else: self.cp = ConfigParser(values) self.cp.add_section(self.SECTION) def _getsources(self): sources = ['/etc/scrapydd/scrapydd.conf', r'c:\scrapydd\scrapydd.conf'] sources += sorted(glob.glob('/etc/scrapyd/conf.d/*')) sources += ['scrapydd.conf'] sources += ['conf/scrapydd.conf'] sources += [expanduser('~/.scrapydd.conf')] return sources def get(self, option, default=None): env_key = 'SCRAPYDD_' + option.replace('.', '_').upper() try: return os.getenv(env_key) or self.cp.get(self.SECTION, option) except (NoSectionError, NoOptionError): if default is not None: return default raise def _get(self, option, conv, default=None): return conv(self.get(option, default)) def getint(self, option, default=None): return self._get(option, int, default) def getboolean(self, option, default=None): return self._get(option, str2bool, default) def getfloat(self, option, default=None): return self._get(option, float, default)
class RepoConfiguration(object): """ Read configuration from repository. """ DEFAULT_CONFIG = dedent("""\ [autorebuild] enabled = false """) def __init__(self, dir_path='', file_name=REPO_CONFIG_FILE, depth=None): self._config_parser = ConfigParser() self.container = {} self.depth = depth or 0 self.autorebuild = {} # Set default options self._config_parser.readfp(StringIO(self.DEFAULT_CONFIG)) # pylint: disable=W1505; py2 config_path = os.path.join(dir_path, file_name) if os.path.exists(config_path): self._config_parser.read(config_path) file_path = os.path.join(dir_path, REPO_CONTAINER_CONFIG) if os.path.exists(file_path): with open(file_path) as f: try: self.container = yaml.safe_load(f) or {} except yaml.scanner.ScannerError as e: msg = ('Failed to parse YAML file "{file}": {reason}' .format(file=REPO_CONTAINER_CONFIG, reason=e)) raise OsbsException(msg) # container values may be set to None container_compose = self.container.get('compose') or {} modules = container_compose.get('modules') or [] self.autorebuild = self.container.get('autorebuild') or {} self.container_module_specs = [] value_errors = [] for module in modules: try: self.container_module_specs.append(ModuleSpec.from_str(module)) except ValueError as e: value_errors.append(e) if value_errors: raise ValueError(value_errors) def is_autorebuild_enabled(self): return self._config_parser.getboolean('autorebuild', 'enabled')
def load_config(): """Load configuration. :returns: Current configuration based on configuration file and environment variables. :rtype: dict """ config_parser = ConfigParser( {key: str(value) for key, value in DEFAULT_CONFIG.items()} ) config_parser.add_section("greynoise") if os.path.isfile(CONFIG_FILE): LOGGER.debug("Parsing configuration file: %s...", CONFIG_FILE, path=CONFIG_FILE) with open(CONFIG_FILE) as config_file: config_parser.readfp(config_file) else: LOGGER.warning( "Configuration file not found: %s", CONFIG_FILE, path=CONFIG_FILE ) if "GREYNOISE_API_KEY" in os.environ: api_key = os.environ["GREYNOISE_API_KEY"] LOGGER.debug( "API key found in environment variable: %s", api_key, api_key=api_key ) # Environment variable takes precedence over configuration file content config_parser.set("greynoise", "api_key", api_key) if "GREYNOISE_TIMEOUT" in os.environ: timeout = os.environ["GREYNOISE_TIMEOUT"] try: int(timeout) except ValueError: LOGGER.error( "GREYNOISE_TIMEOUT environment variable " "cannot be converted to an integer: %r", timeout, timeout=timeout, ) else: LOGGER.debug( "Timeout found in environment variable: %s", timeout, timeout=timeout ) # Environment variable takes precedence over configuration file content config_parser.set("greynoise", "timeout", timeout) return { "api_key": config_parser.get("greynoise", "api_key"), "timeout": config_parser.getint("greynoise", "timeout"), }
def load_config(filename): section = "root" try: config_text = "[%s]\n%s" % (section, open(filename).read()) except IOError as e: sys.stderr.write("load_config: %s\n" % e) config_text = "[%s]\n" % section config = ConfigParser() config.readfp(StringIO(config_text)) return config.items(section)
def read_config_file(cfgfile, options): config = ConfigParser() config.readfp(open(cfgfile)) if config.has_option('testflo', 'skip_dirs'): skips = config.get('testflo', 'skip_dirs') options.skip_dirs = [s.strip() for s in skips.split(',') if s.strip()] if config.has_option('testflo', 'num_procs'): options.num_procs = int(config.get('testflo', 'num_procs')) if config.has_option('testflo', 'noreport'): options.noreport = bool(config.get('testflo', 'noreport'))
def load_theme(struct, path, colors, default_colors): theme = ConfigParser() with open(path, 'r') as f: theme.readfp(f) for k, v in chain(theme.items('syntax'), theme.items('interface')): if theme.has_option('syntax', k): colors[k] = theme.get('syntax', k) else: colors[k] = theme.get('interface', k) # Check against default theme to see if all values are defined for k, v in iteritems(default_colors): if k not in colors: colors[k] = v
def put_ini(self, text): """ """ context = self.context parser = ConfigParser() try: parser.read_file(cStringIO(text)) except AttributeError: # Python 2 parser.readfp(cStringIO(text)) for option, value in parser.defaults().items(): prop_type = context.getPropertyType(option) if prop_type is None: context._setProperty(option, value, 'string') else: context._updateProperty(option, value)
def _load_config(no_cfgfile=False): config = ConfigParser() config.optionxform = str # make it preserve case # defaults if not six.PY3: config.readfp(BytesIO(_DEFAULT_CONFIG)) else: config.read_file(StringIO(_DEFAULT_CONFIG)) # update from config file if not no_cfgfile: config.read(os.path.join(_STASH_ROOT, f) for f in _STASH_CONFIG_FILES) return config
def _parse_linfo(self, linfo_file): cp = ConfigParser() try: cp.readfp(linfo_file) section = 'Activity' if cp.has_option(section, 'name'): self._name = cp.get(section, 'name') if cp.has_option(section, 'summary'): self._summary = cp.get(section, 'summary') if cp.has_option(section, 'tags'): tag_list = cp.get(section, 'tags').strip(';') self._tags = [tag.strip() for tag in tag_list.split(';')] except ParsingError as e: logging.exception('Exception reading linfo file: %s', e)
def main(): ############################################################################# # # ARGUMENT PARSING # ############################################################################# parser=OptionParser(usage) parser.add_option("-e","--example",default=False,dest="example",action="store_true",help="Create example config.ini and an example sourcefile") (opts,args) = parser.parse_args() if opts.example: with open("omega_config.ini","w") as f: f.write(ExampleConfig) with open("omegascanslist.txt","w") as f: f.write(ExampleSourceFile) print("Example files \"omega_config.ini\" and \"omegascanslist.txt\" are created") sys.exit(0) if len(args) != 1: parser.print_help() sys.exit("ERROR: Must provide one config.ini") cp=ConfigParser() cp.optionxform = str cp.readfp(open(args[0])) dag=OmegaScansDAG(cp) dag.write_sub_files() dag.write_dag() dag.write_script() #fix the sub and sh files #This is required because pipeline.py does not yet have the ability to add #a specific argument before all other arguments and options ('scan' in this case) fix_subfile(dag.submitFile) fix_scriptfile(cp.get('paths','basedir'),dag.get_dag_file(),cp.get('omegapipe','executable')) print('Successfully created DAG file.') fulldagpath=os.path.join(cp.get('paths','basedir'),dag.get_dag_file()) print('Now run condor_submit_dag %s\n'%(fulldagpath))
def _makeInstance(self, id, portal_type, subdir, import_context): context = self.context subdir = '%s/%s' % (subdir, id) properties = self.read_data_file(import_context, '.properties', subdir) tool = getUtility(ITypesTool) try: tool.constructContent(portal_type, context, id) except ValueError: # invalid type return None content = context._getOb(id) if properties is not None: if '[DEFAULT]' not in properties: try: adp = FolderishDAVAwareFileAdapter adp(content).import_(import_context, subdir) return content except (AttributeError, MethodNotAllowed): # Fall through to old implemenatation below pass lines = properties.splitlines() stream = StringIO('\n'.join(lines)) parser = ConfigParser(defaults={'title': '', 'description': 'NONE'}) try: parser.read_file(stream) except AttributeError: # Python 2 parser.readfp(stream) title = parser.get('DEFAULT', 'title') description = parser.get('DEFAULT', 'description') content.setTitle(title) content.setDescription(description) return content
def _parse_info(self, info_file): cp = ConfigParser() cp.readfp(info_file) section = 'Library' if cp.has_option(section, 'name'): self._name = cp.get(section, 'name') else: raise MalformedBundleException( 'Content bundle %s does not specify a name' % self._path) if cp.has_option(section, 'library_version'): version = cp.get(section, 'library_version') try: NormalizedVersion(version) except InvalidVersionError: raise MalformedBundleException( 'Content bundle %s has invalid version number %s' % (self._path, version)) self._library_version = version if cp.has_option(section, 'locale'): self._locale = cp.get(section, 'locale') if cp.has_option(section, 'global_name'): self._global_name = cp.get(section, 'global_name') if cp.has_option(section, 'icon'): self._icon = cp.get(section, 'icon') # Compatibility with old content bundles if self._global_name is not None \ and cp.has_option(section, 'bundle_class'): self._global_name = cp.get(section, 'bundle_class') if cp.has_option(section, 'activity_start'): self._activity_start = cp.get(section, 'activity_start') if self._global_name is None: raise MalformedBundleException( 'Content bundle %s must specify global_name' % self._path)
def parse_config_file(filename): logger.debug("reading configuration file: %s", filename) cp = ConfigParser() with open(filename) as f: cp.readfp(f, filename=filename) if cp.has_option('include', 'files'): for fn in glob(cp.get('include', 'files')): cp.read(fn) cp.redis = cp.get('sup_broadcast', 'redis') cp.url = cp.get('sup_broadcast', 'supervisor_url') try: cp.ident = cp.get('supervisord', 'identifier') except Error: cp.ident = _url2name(cp.url) cp.config_file = filename return cp
def load_ini(self, ini_config): """ Read the provided ini contents arguments and merge the data in the ini config into the config object. ini_config is assumed to be a string of the ini file contents. """ parser = ConfigParser() parser.readfp(StringIO(ini_config)) data = { 'linters': {}, 'files': {}, 'branches': {}, 'fixers': {}, 'review': {} } if parser.has_section('files'): ignore = parser.get('files', 'ignore') data['files']['ignore'] = newline_value(ignore) if parser.has_section('branches'): ignore = parser.get('branches', 'ignore') data['branches']['ignore'] = comma_value(ignore) linters = [] if parser.has_section('tools'): linters = comma_value(parser.get('tools', 'linters')) if parser.has_section('fixers'): data['fixers'] = dict(parser.items('fixers')) if parser.has_section('review'): data['review'] = dict(parser.items('review')) # Setup empty config sections for linter in linters: data['linters'][linter] = {} for section in parser.sections(): if not section.startswith('tool_'): continue # Strip off tool_ linter = section[5:] data['linters'][linter] = dict(parser.items(section)) self.update(data)
def parse(self): """ Parse the config and return a dict with the parsed values. :rtype: ``dict`` """ result = defaultdict(dict) if not os.path.isfile(self.config_file_path): # Config doesn't exist, return the default values return CONFIG_DEFAULT_VALUES config = ConfigParser() with open(self.config_file_path, 'r') as fp: config.readfp(fp) for section, keys in six.iteritems(CONFIG_FILE_OPTIONS): for key, options in six.iteritems(keys): key_type = options['type'] key_default_value = options['default'] if config.has_option(section, key): if key_type in ['str', 'string']: get_func = config.get elif key_type in ['int', 'integer']: get_func = config.getint elif key_type in ['float']: get_func = config.getfloat elif key_type in ['bool', 'boolean']: get_func = config.getboolean else: msg = 'Invalid type "%s" for option "%s"' % (key_type, key) raise ValueError(msg) value = get_func(section, key) result[section][key] = value else: result[section][key] = key_default_value return dict(result)
def loadSubs(self, filename): """Load a substitutions file. The file must be in the Windows-style INI format (see the standard ConfigParser module docs for information on this format). Each section of the file is loaded into its own substituter. """ inFile = file(filename) parser = ConfigParser() parser.readfp(inFile, filename) inFile.close() for s in parser.sections(): # Add a new WordSub instance for this section. If one already # exists, delete it. if s in self._subbers: del(self._subbers[s]) self._subbers[s] = WordSub() # iterate over the key,value pairs and add them to the subber for k, v in parser.items(s): self._subbers[s][k] = v
def getLayoutsFromManifest(fp, _format, directory_name): # support multiple sections with the same name in manifest.cfg if six.PY2: parser = ConfigParser(None, multidict) parser.readfp(fp) else: data = fp.read() if isinstance(data, six.binary_type): data = data.decode() parser = ConfigParser(dict_type=multidict, strict=False) parser.read_string(data) layouts = {} for section in parser.sections(): if not section.startswith(_format.resourceType) or \ ':variants' in section: continue # id is a combination of directory name + filename if parser.has_option(section, 'file'): filename = parser.get(section, 'file') else: filename = '' # this should not happen... _id = directory_name + '/' + filename if _id in layouts: # because TTW resources are created first, we consider layouts # with same id in a TTW to be taken before other resources continue data = { 'directory': directory_name } for key in _format.keys: if parser.has_option(section, key): data[key] = parser.get(section, key) else: data[key] = _format.defaults.get(key, None) layouts[_id] = data return layouts
def from_string(cls, source, prefix=""): """Create an `EventManager` from a string. Parameters ---------- source : str Ini-formatted string. prefix : str Prefix for section. Returns ------- EventManager A newly-created `EventManager`. See Also -------- :meth:`from_path` : Alternate constructor that uses a path name. """ config = ConfigParser() config.readfp(StringIO(source)) return cls._from_config(config, prefix=prefix)
def __to_configparser(mcf_object): """normalize mcf input into ConfigParser object""" cp_obj = None if isinstance(mcf_object, ConfigParser): LOGGER.debug("mcf object is already a ConfigParser object") cp_obj = mcf_object elif "[metadata]" in mcf_object: LOGGER.debug("mcf object is a string") s = StringIO(mcf_object) c = ConfigParser() c.readfp(s) cp_obj = c else: LOGGER.debug("mcf object is likely a filepath") c = ConfigParser() with codecs.open(mcf_object, encoding="utf-8") as fh: c.readfp(fh) cp_obj = c return cp_obj
def handle(self, *args, **options): # type: (*Any, **Any) -> None config_file = os.path.join(os.environ["HOME"], ".zuliprc") if not os.path.exists(config_file): raise RuntimeError("No ~/.zuliprc found") config = ConfigParser() with open(config_file, 'r') as f: # Apparently, six.moves.configparser.ConfigParser is not # consistent between Python 2 and 3! if hasattr(config, 'read_file'): config.read_file(f, config_file) else: config.readfp(f, config_file) api_key = config.get("api", "key") email = config.get("api", "email") try: realm = get_realm("zulip") user_profile = get_user(email, realm) user_profile.api_key = api_key user_profile.save(update_fields=["api_key"]) except UserProfile.DoesNotExist: print("User %s does not exist; not syncing API key" % (email,))
class RepoConfiguration(object): """ Read configuration from repository. """ DEFAULT_CONFIG = dedent("""\ [autorebuild] enabled = false """) def __init__(self, dir_path='', file_name=REPO_CONFIG_FILE): self._config_parser = ConfigParser() # Set default options self._config_parser.readfp(StringIO(self.DEFAULT_CONFIG)) config_path = os.path.join(dir_path, file_name) if os.path.exists(config_path): self._config_parser.read(config_path) def is_autorebuild_enabled(self): return self._config_parser.getboolean('autorebuild', 'enabled')
def test_read_mcf(self): """Test reading MCF files, strings or ConfigParser objects""" # test as file with self.assertRaises(IOError): mcf = read_mcf(get_abspath('../404.mcf')) mcf = read_mcf(get_abspath('../sample.mcf')) self.assertIsInstance(mcf, dict, 'Expected dict') self.assertTrue('metadata' in mcf, 'Expected metadata section') # test as string with open(get_abspath('../sample.mcf')) as fh: mcf_string = fh.read() mcf = read_mcf(mcf_string) self.assertTrue('metadata' in mcf, 'Expected metadata section') # test as ConfigParser object mcf_cp = ConfigParser() mcf_cp.readfp(StringIO(mcf_string)) mcf = read_mcf(mcf_cp) self.assertTrue('metadata' in mcf, 'Expected metadata section')