def check_web_config(config_path): ''' Try to load the Django settings. If this does not work, than settings file does not exist. ''' WEB_CONFIG_FILE = config_path + '/settings.ini' print("Looking for config file at {0} ...".format(WEB_CONFIG_FILE)) config = RawConfigParser() try: config.readfp(open(WEB_CONFIG_FILE)) return config except IOError: print("ERROR: Seems like the config file does not exist.") print( " I am creating a new one. Please edit it and re-run this command." ) # Create fresh config file try: check_path(config_path) f = open(WEB_CONFIG_FILE, 'wt') f.write(DEFAULT_CONFIG) f.close() check_file(WEB_CONFIG_FILE) return None # Manual editing is needed before further proceeding with the fresh file except FileNotFoundError: print( "ERROR: Could not create config file at {0}. Please use sudo or become root." .format(WEB_CONFIG_FILE)) return None
def send_old_submits(): submits = filter(lambda s: s.startswith("submit-"), os.listdir(os.getenv("PINGUINO_USER_PATH"))) for submit in submits: parser = RawConfigParser() filename = os.path.join(os.getenv("PINGUINO_USER_PATH"), submit) parser.readfp(open(filename, "r")) summary = parser.get("SUBMIT", "summary") details = parser.get("SUBMIT", "details") repo = parser.get("SUBMIT", "repo") environ = parser.get("SUBMIT", "environ") username = parser.get("SUBMIT", "username") password = parser.get("SUBMIT", "password") try: url = 'https://api.github.com/repos/{}/{}/issues'.form session = requests.Session() session.auth = (username, password) issue = { 'title': summary, 'body': "{}\n\n{}".format(details, environ), 'labels': [ 'submitted-from-ide', 'bug', 'v{}'.format(os.environ["PINGUINO_VERSION"][:2]), ], } r = session.post(url, json.dumps(issue)) os.remove(filename) except: pass
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 __init__(self, filename, file_object=None): """Load a configuration schema from the provided filename. :param filename: The name of the file to load from, or if `file_object` is given, to pretend to load from. :type filename: string :param file_object: If given, optional file-like object to read from instead of actually opening the named file. :type file_object: An object with a readline() method. :raise `UnicodeDecodeError`: if the string contains non-ascii characters. :raise `RedefinedSectionError`: if a SectionSchema name is redefined. :raise `InvalidSectionNameError`: if a SectionSchema name is ill-formed. """ # XXX sinzui 2007-12-13: # RawConfigParser permits redefinition and non-ascii characters. # The raw schema data is examined before creating a config. self.filename = filename self.name = basename(filename) self._section_schemas = {} self._category_names = [] if file_object is None: raw_schema = self._getRawSchema(filename) else: raw_schema = file_object parser = RawConfigParser() parser.readfp(raw_schema, filename) self._setSectionSchemasAndCategoryNames(parser)
def _getExtendedConfs(self, conf_filename, conf_data, confs=None): """Return a list of tuple (conf_name, parser, encoding_errors). :param conf_filename: The path and name of the conf file. :param conf_data: Unparsed config data. :param confs: A list of confs that extend filename. :return: A list of confs ordered from extender to extendee. :raises IOError: If filename cannot be read. This method parses the config data and checks for encoding errors. It checks parsed config data for the extends key in the meta section. It reads the unparsed config_data from the extended filename. It passes filename, data, and the working list to itself. """ if confs is None: confs = [] encoding_errors = self._verifyEncoding(conf_data) parser = RawConfigParser() parser.readfp(StringIO(conf_data), conf_filename) confs.append((conf_filename, parser, encoding_errors)) if parser.has_option('meta', 'extends'): base_path = dirname(conf_filename) extends_name = parser.get('meta', 'extends') extends_filename = abspath('%s/%s' % (base_path, extends_name)) extends_data = read_content(extends_filename) self._getExtendedConfs(extends_filename, extends_data, confs) return confs
def set_environ_vars(cls): if not os.path.exists(os.path.join(os.getenv("PINGUINO_DATA"), "paths.cfg")): logging.error("Missing: "+os.path.join(os.getenv("PINGUINO_DATA"), "paths.cfg")) sys.exit() config_paths = RawConfigParser() config_paths.readfp(open(os.path.join(os.getenv("PINGUINO_DATA"), "paths.cfg"), "r")) #RB20141116 : get the “bitness” of the current OS bitness, linkage = platform.architecture() os.environ["PINGUINO_OS_ARCH"] = bitness if os.name == "posix": #GNU/Linux os.environ["PINGUINO_OS_NAME"] = "linux" #Mac could return posix :/ elif os.name == "os2": #Mac OS X os.environ["PINGUINO_OS_NAME"] = "macosx" elif os.name == "nt": #Windows os.environ["PINGUINO_OS_NAME"] = "windows" #load path from paths.conf os.environ["PINGUINO_USER_PATH"] = os.path.expanduser(config_paths.get("paths-%s"%os.getenv("PINGUINO_OS_NAME"), "user_path")) os.environ["PINGUINO_INSTALL_PATH"] = os.path.expanduser(config_paths.get("paths-%s"%os.getenv("PINGUINO_OS_NAME"), "install_path")) os.environ["PINGUINO_USERLIBS_PATH"] = os.path.join(os.getenv("PINGUINO_USER_PATH"), "library_manager")
def run_generator(self, expect_error=False): '''Run sysv-generator. Fail if stderr contains any "Fail", unless expect_error is True. Return (stderr, filename -> ConfigParser) pair with ouput to stderr and parsed generated units. ''' env = os.environ.copy() env['SYSTEMD_LOG_LEVEL'] = 'debug' env['SYSTEMD_SYSVINIT_PATH'] = self.init_d_dir env['SYSTEMD_SYSVRCND_PATH'] = self.rcnd_dir env['SYSTEMD_UNIT_PATH'] = self.unit_dir gen = subprocess.Popen( [sysv_generator, 'ignored', 'ignored', self.out_dir], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, env=env) (out, err) = gen.communicate() if not expect_error: self.assertFalse('Fail' in err, err) self.assertEqual(gen.returncode, 0, err) results = {} for service in glob(self.out_dir + '/*.service'): if os.path.islink(service): continue cp = RawConfigParser() cp.optionxform = lambda o: o # don't lower-case option names with open(service) as f: cp.readfp(f) results[os.path.basename(service)] = cp return (err, results)
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)
def set_environ_vars(cls): if not os.path.exists(os.path.join(os.getenv("PINGUINO_DATA"), "paths.cfg")): logging.error("Missing: "+os.path.join(os.getenv("PINGUINO_DATA"), "paths.cfg")) sys.exit() config_paths = RawConfigParser() config_paths.readfp(open(os.path.join(os.getenv("PINGUINO_DATA"), "paths.cfg"), "r")) #RB20141116 : get the “bitness” of the current OS bitness, linkage = platform.architecture() os.environ["PINGUINO_OS_ARCH"] = bitness if os.name == "posix": #GNU/Linux os.environ["PINGUINO_OS_NAME"] = "linux" #Mac could return posix :/ elif os.name == "os2": #Mac OS X os.environ["PINGUINO_OS_NAME"] = "macosx" elif os.name == "nt": #Windows os.environ["PINGUINO_OS_NAME"] = "windows" #load path from paths.conf os.environ["PINGUINO_USER_PATH"] = os.path.expandvars(os.path.expanduser(config_paths.get("paths-%s"%os.getenv("PINGUINO_OS_NAME"), "user_path"))) os.environ["PINGUINO_INSTALL_PATH"] = os.path.expandvars(os.path.expanduser(config_paths.get("paths-%s"%os.getenv("PINGUINO_OS_NAME"), "install_path"))) os.environ["PINGUINO_USERLIBS_PATH"] = os.path.expandvars(os.path.join(os.getenv("PINGUINO_USER_PATH"), "library_manager"))
def parse_cmake_module(s_in): s_out = [] is_rst_line = False for line in s_in.split('\n'): if is_rst_line: if len(line) > 0: if line[0] != '#': is_rst_line = False else: is_rst_line = False if is_rst_line: s_out.append(line[2:]) if '#.rst:' in line: is_rst_line = True autocmake_entry = '\n'.join(s_out).split('Example autocmake.cfg entry::')[1] autocmake_entry = autocmake_entry.replace('\n ', '\n') buf = StringIO(autocmake_entry) config = RawConfigParser(dict_type=OrderedDict) config.readfp(buf) config_docopt = None config_define = None config_export = None for section in config.sections(): if config.has_option(section, 'docopt'): config_docopt = config.get(section, 'docopt') if config.has_option(section, 'define'): config_define = config.get(section, 'define') if config.has_option(section, 'export'): config_export = config.get(section, 'export') return config_docopt, config_define, config_export
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 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 send_old_submits(): submits = filter(lambda s:s.startswith("submit-"), os.listdir(os.getenv("PINGUINO_USER_PATH"))) for submit in submits: parser = RawConfigParser() filename = os.path.join(os.getenv("PINGUINO_USER_PATH"), submit) parser.readfp(open(filename, "r")) summary = parser.get("SUBMIT", "summary") details = parser.get("SUBMIT", "details") repo = parser.get("SUBMIT", "repo") environ = parser.get("SUBMIT", "environ") username = parser.get("SUBMIT", "username") password = parser.get("SUBMIT", "password") try: url = 'https://api.github.com/repos/{}/{}/issues'.form session = requests.Session() session.auth = (username, password) issue = {'title': summary, 'body': "{}\n\n{}".format(details, environ), 'labels': ['submitted-from-ide', 'bug', 'v{}'.format(os.environ["PINGUINO_VERSION"][:2]), ], } r = session.post(url, json.dumps(issue)) os.remove(filename) except: pass
def load_config(fp, reopen=False): config = RawConfigParser() if reopen: fp = open(fp.name) config.readfp(fp) # TODO: Deprecated since python 3.2 fp.close() return config
def update_library(filename): """ Update units in current library from `filename`. Parameters ---------- filename : str or file Source of units configuration data. """ if isinstance(filename, basestring): inp = open(filename, 'rU') else: inp = filename try: cfg = ConfigParser() cfg.optionxform = _do_nothing # New in Python 3.2: read_file() replaces readfp(). if sys.version_info >= (3, 2): cfg.read_file(inp) else: cfg.readfp(inp) _update_library(cfg) finally: inp.close()
def load_config(fp, reopen=False): config = RawConfigParser() if reopen: fp = open(fp.name) config.readfp(fp) fp.close() return config
def read_raw_parser(self, filename): blocks_set = [] file_parser = RawConfigParser() if os.getenv("PINGUINO_PYTHON") is "2": if type(filename) in [str, unicode]: file_parser.readfp(codecs.open(filename, "r", encoding="utf-8")) else: file_parser = filename elif os.getenv("PINGUINO_PYTHON") is "3": if type(filename) == str: file_parser.readfp(codecs.open(filename, "r", encoding="utf-8")) else: file_parser = filename sections = file_parser.sections() for section in sections: options = file_parser.options(section) block = {} for option in options: file_parser.get(section, option) value = file_parser.get(section, option) if os.getenv("PINGUINO_PYTHON") is "2": if (type(value) in [str, unicode]) and (value[0] in ["[", "("]): block[option] = eval(file_parser.get(section, option)) else: block[option] = file_parser.get(section, option) elif os.getenv("PINGUINO_PYTHON") is "3": if (type(value) == str) and (value[0] in ["[", "("]): block[option] = eval(file_parser.get(section, option)) else: block[option] = file_parser.get(section, option) blocks_set.append(block) return blocks_set
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 readConfig(fileName): cp = RawConfigParser() fp = open(fileName, 'r') if sys.version_info < (3, 2): cp.readfp(fp) # pylint: disable=W1505 else: cp.read_file(fp, fileName) return cp
def read(self, pathfilename): with codecs.open(pathfilename, 'r', encoding='utf-8') as input_file: config_pairs = input_file.read() with _closing(_StringIO("[{0}]{1}{2}".format(self._default_section, os.linesep, config_pairs))) \ as default_section: _RawConfigParser.readfp(self, default_section)
def read(self, pathfilename): with _codecs.open(pathfilename, 'r', encoding='utf-8') as input_file: config_pairs = input_file.read() with _closing(_StringIO("[{0}]{1}{2}".format(self._default_section, _os.linesep, config_pairs))) \ as default_section: _RawConfigParser.readfp(self, default_section)
def version(self, root): """Retrieve the release version of the installed browser.""" platform_info = RawConfigParser() with open(os.path.join(root, self.platform_ini), "r") as fp: platform_info.readfp(BytesIO(fp.read())) return "BuildID %s; SourceStamp %s" % ( platform_info.get("Build", "BuildID"), platform_info.get("Build", "SourceStamp"))
class BuildPartConfigs(object): def __init__(self, parts, template, prefix): self.prefix = prefix self.parts = RawConfigParser() # Loaded the part data file try: with io.open(parts) as parts_file: self.parts.readfp(parts_file) LOG.info("{} loaded.".format(parts)) except IOError: logging.error( "Couldn't read the part data file: {}.".format(parts)) raise except ValueError: logging.error( "Couldn't parse the part data file: {}.".format(parts)) raise # Load the part template file try: LOG.debug("Reading {}.".format(template)) with io.open(template, 'r') as template_cfg: # Read the template into memory self.template = template_cfg.readlines() LOG.info("{} loaded.".format(template)) except IOError: LOG.error("Couldn't read part template file: {}".format(template)) raise def build_all(self): LOG.debug("Building all part configurations.") for name in self.parts.sections(): self.build_config(name) LOG.info("Done.") def build_config(self, name): LOG.debug("Starting build for {}.".format(name)) part = dict(self.parts.items(name)) part_cfg_path = os.path.join(self.prefix, 'Parts/{}.cfg'.format(name)) try: LOG.debug("Reading {}.".format(part_cfg_path)) with io.open(part_cfg_path, 'w+') as part_cfg: # Use python's built-in string formatting to customize the part config and write it to the file. for template_line in self.template: part_cfg.write(template_line.format(name=name, **part)) except IOError: LOG.error("Unable to write to {}.".format(part_cfg_path)) raise LOG.info("Built {}".format(part_cfg_path))
class CertificateAuthorityConfig(object): """ Attempt to parse CA-s from openssl.cnf """ def __init__(self, *args): self._config = RawConfigParser() for arg in args: self._config.readfp(itertools.chain(["[global]"], open(os.path.expanduser(arg)))) def get(self, section, key, default=""): if self._config.has_option(section, key): return self._config.get(section, key) else: return default def instantiate_authority(self, slug): section = "CA_" + slug dirs = dict([(key, self.get(section, key)) for key in ("dir", "certificate", "crl", "certs", "new_certs_dir", "private_key", "revoked_certs_dir", "autosign_whitelist")]) # Variable expansion, eg $dir for key, value in dirs.items(): if "$" in value: dirs[key] = re.sub(r'\$([a-z]+)', lambda m:dirs[m.groups()[0]], value) dirs.pop("dir") dirs["email_address"] = self.get(section, "emailAddress") dirs["inbox"] = self.get(section, "inbox") dirs["outbox"] = self.get(section, "outbox") dirs["lifetime"] = int(self.get(section, "default_days", "1825")) extensions_section = self.get(section, "x509_extensions") if extensions_section: dirs["basic_constraints"] = self.get(extensions_section, "basicConstraints") dirs["key_usage"] = self.get(extensions_section, "keyUsage") dirs["extended_key_usage"] = self.get(extensions_section, "extendedKeyUsage") authority = CertificateAuthority(slug, **dirs) return authority def all_authorities(self): for section in self._config: if section.startswith("CA_"): try: yield self.instantiate_authority(section[3:]) except FileNotFoundError: pass def pop_certificate_authority(self): def wrapper(func): def wrapped(*args, **kwargs): slug = kwargs.pop("ca") kwargs["ca"] = self.instantiate_authority(slug) return func(*args, **kwargs) return wrapped return wrapper
def _check_submodule_no_git(self): """ Like ``_check_submodule_using_git``, but simply parses the .gitmodules file to determine if the supplied path is a git submodule, and does not exec any subprocesses. This can only determine if a path is a submodule--it does not perform updates, etc. This function may need to be updated if the format of the .gitmodules file is changed between git versions. """ gitmodules_path = os.path.abspath('.gitmodules') if not os.path.isfile(gitmodules_path): return False # This is a minimal reader for gitconfig-style files. It handles a few of # the quirks that make gitconfig files incompatible with ConfigParser-style # files, but does not support the full gitconfig syntax (just enough # needed to read a .gitmodules file). gitmodules_fileobj = io.StringIO() # Must use io.open for cross-Python-compatible behavior wrt unicode with io.open(gitmodules_path) as f: for line in f: # gitconfig files are more flexible with leading whitespace; just # go ahead and remove it line = line.lstrip() # comments can start with either # or ; if line and line[0] in (':', ';'): continue gitmodules_fileobj.write(line) gitmodules_fileobj.seek(0) cfg = RawConfigParser() try: cfg.readfp(gitmodules_fileobj) except Exception as exc: log.warn('Malformatted .gitmodules file: {0}\n' '{1} cannot be assumed to be a git submodule.'.format( exc, self.path)) return False for section in cfg.sections(): if not cfg.has_option(section, 'path'): continue submodule_path = cfg.get(section, 'path').rstrip(os.sep) if submodule_path == self.path.rstrip(os.sep): return True return False
def has_config(config_fname): ''' Determine if the given config file exists. ''' config = RawConfigParser() try: config.readfp(open(config_fname)) return True except IOError: return False
def load_ini_file(filename): parser = RawConfigParser() parser.readfp(open(filename)) ini = IniFile() for section_name in parser.sections(): section = Section(section_name) ini.add_section(section) for key, value in parser.items(section_name): section[key] = value return ini
def _load_ini(path): """Load an INI file from *path*.""" cfg = RawConfigParser() with codecs.open(path, mode="r", encoding="utf-8") as f: try: cfg.read_file(f) except AttributeError: cfg.readfp(f) return cfg
def config_load(): config = RawConfigParser() config.optionxform = str try: with open(settings.CONFIG_NAME) as f: config.readfp(f) except IOError: return None return config
def read(self, pathfilename): #seb: expand path to allow using homedir and relative paths pathfilename = os.path.realpath(os.path.expanduser(pathfilename)) with codecs.open(pathfilename, 'r', encoding=ServiceDefault.CHAR_CODEC) as input_file: config_pairs = input_file.read() with _closing(_StringIO("[{0}]{1}{2}".format(self._default_section, os.linesep, config_pairs))) \ as default_section: _RawConfigParser.readfp(self, default_section)
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 get_profile(profile): """Using the provided Read the configuration file and get the connection parameters. """ config = "Settings" params = { "host": None, # Host to connect "group": None, # Group used in connection profile "login": None, # Username "password": None, "post_cmd": None, # Script to execute after connection is established } if profile == "-": LOG.debug("Reading profile from stdin") profile = "STDIN" data = sys.stdin.read() # Convert input to file like object configdata = StringIO(data) else: LOG.debug("Reading profile from password store: %s", profile) # Retrieve data from the password store p = Popen(["pass", profile], stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() if p.returncode != 0: sys.stderr.write( "Error while reading profile from pass: {!r}\n".format(stderr)) sys.exit(1) # Convert output to file like object configdata = StringIO(stdout.decode(sys.stdout.encoding)) # Parse the config data conf = RawConfigParser() conf.readfp(configdata) # Check if the required section is available in the config file if not conf.has_section(config): raise Exit(Exit.BADCONFIG, "Settings section not found in {}".format(profile)) # Read all necessary parameters for param in params: try: params[param] = conf.get(config, param) except NoOptionError: raise Exit( Exit.BADCONFIG, "Missing parameter {} for account {}".format(param, profile)) return params
def set_environ_vars(cls): """Configure some environ variables about OS and architecture. """ #if not os.path.exists(os.path.join(os.getenv("PINGUINO_LIB"), "paths.cfg")): #logging.error("Missing: "+os.path.join(os.getenv("PINGUINO_LIB"), "paths.cfg")) #sys.exit() #config_paths = RawConfigParser() #config_paths.readfp(open(os.path.join(os.getenv("PINGUINO_LIB"), "paths.cfg"), "r") #RB20141116 : get the “bitness” of the current OS bitness, linkage = platform.architecture() os.environ["PINGUINO_OS_ARCH"] = bitness if os.name == "posix": #GNU/Linux os.environ["PINGUINO_OS_NAME"] = "linux" #Mac could return posix :/ elif os.name == "os2": #Mac OS X os.environ["PINGUINO_OS_NAME"] = "macosx" elif os.name == "nt": #Windows os.environ["PINGUINO_OS_NAME"] = "windows" config_paths = RawConfigParser() config_paths.readfp( open( os.path.join( os.getenv("PINGUINO_LIB"), "qtgui", "config", "pinguino.{PINGUINO_OS_NAME}.conf".format(**os.environ)), "r")) #load path from paths.conf os.environ["PINGUINO_USER_PATH"] = os.path.expandvars( os.path.expanduser(config_paths.get("Paths", "user_path"))) os.environ["PINGUINO_INSTALL_PATH"] = os.path.expandvars( os.path.expanduser(config_paths.get("Paths", "install_path"))) os.environ["PINGUINO_USERLIBS_PATH"] = os.path.expandvars( os.path.expanduser(config_paths.get("Paths", "user_libs"))) os.environ["PINGUINO_LIBS_PATH"] = os.path.expandvars( os.path.expanduser( os.path.dirname(config_paths.get("Paths", "pinguino_8_libs")))) os.environ["PINGUINO_8_LIBS_PATH"] = os.path.expandvars( os.path.expanduser(config_paths.get("Paths", "pinguino_8_libs"))) os.environ["PINGUINO_32_LIBS_PATH"] = os.path.expandvars( os.path.expanduser(config_paths.get("Paths", "pinguino_32_libs"))) # os.environ["PINGUINO_USERLIBS_PATH"] = os.path.expandvars(os.path.join(os.getenv("PINGUINO_USER_PATH"), "libraries")) os.environ["PINGUINO_DEFAULT_FILES"] = os.path.join( os.getenv("PINGUINO_USER_PATH"), "local")
def load_mc_config(self): """Load the configuration from minecraft-control.""" log.debug("Loading minecraft-control settings from {}".format(args.config)) parser = RawConfigParser() with open(self.args.config, "r") as fh: parser.readfp(fh) self.config["host"] = parser.get("tcp-server", "address") self.config["port"] = parser.get("tcp-server", "port") self.config["password"] = parser.get("auth", "password") self.config["method"] = parser.get("auth", "method")
def _get_value(section, key, path=None): if path is None: path = DEFAULT_PATH config = ConfigParser(allow_no_value=True) with open(path, encoding='utf8') as configfile: config.readfp(configfile) try: return config.get(section, key) except NoSectionError: raise EnvironmentError("Config not set for {} {} in {}".format( section, key, path))
def get_console_scripts(wheel: zipfile.ZipFile): for name in wheel.namelist(): if name.endswith("dist-info/entry_points.txt"): config = RawConfigParser() # No clue some entry_points.txt files have leading space data = [l.decode("utf-8").strip() for l in wheel.open(name)] config.readfp(StringIO("\n".join(data))) if config.has_section("console_scripts"): return dict(config.items("console_scripts")) else: return {} return {}
def load_mc_config(self): """Load the configuration from minecraft-control.""" log.debug("Loading minecraft-control settings from {}".format( args.config)) parser = RawConfigParser() with open(self.args.config, "r") as fh: parser.readfp(fh) self.config["host"] = parser.get("tcp-server", "address") self.config["port"] = parser.get("tcp-server", "port") self.config["password"] = parser.get("auth", "password") self.config["method"] = parser.get("auth", "method")
def open_ini(path): encodings = ["utf-16", "utf-8", None] for i, encoding in enumerate(encodings): try: parse = RawConfigParser() with io.open(path, encoding=encoding) as fp: parse.readfp(fp) # parse.read(path, encoding=encoding) return parse except (UnicodeDecodeError, UnicodeError) as e: if i == len(encodings) - 1: raise Exception("can't load {0}\n{1}".format(path, e)) continue
def get_auth(self): """""" if not os.path.exists(os.path.join(os.getenv("PINGUINO_USER_PATH"), "submit-auth")): return '', '' parser = RawConfigParser() filename = os.path.join(os.getenv("PINGUINO_USER_PATH"), "submit-auth") parser.readfp(open(filename, "r")) username = parser.get("AUTH", "username") password = parser.get("AUTH", "password") return username, password
def get_auth(self): """""" if not os.path.exists( os.path.join(os.getenv("PINGUINO_USER_PATH"), "submit-auth")): return '', '' parser = RawConfigParser() filename = os.path.join(os.getenv("PINGUINO_USER_PATH"), "submit-auth") parser.readfp(open(filename, "r")) username = parser.get("AUTH", "username") password = parser.get("AUTH", "password") return username, password
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
def load_language(language): """ Load up a language as a dictionary. """ langdict = dict() if language.find("..") >= 0 or language.find("?") >= 0 or \ language.find("!") >= 0 or language.find("\\") >= 0 or \ language.find("/") >= 0 or language.find("`") >= 0: raise ValueError("language identifier has invalid characters") parser = RawConfigParser() parser.readfp(codecs.open(os.path.abspath(os.path.join("lang", language + ".cfg")), "r", "utf8")) for section in parser.sections(): for option in parser.options(section): langdict[(section + "_" + option).lower()] = \ parser.get(section, option) return langdict
def load_config(config_file, config_type=None, log=logging): """Read and parse configuration data from the file. Args: config_file (str or Path): Absolute path to the configuration file. config_type (str): The type the configuration is in (config or yaml). If not set (or set to None) the file extension is used for automatic detection. log (optional): A log handler to use. Returns: Configuration dictionary. """ config_file = Path(config_file) # Auto-detection if config_type is None: file_type = _detect_config_type(config_file, log) else: file_type = config_type try: if file_type in [".conf", "conf"]: configparser = RawConfigParser() try: # pylint: disable=deprecated-method configparser.readfp(_FakeSecHead(config_file.open('r'))) # TODO why was this necessary? except Exception: with config_file.open('r') as f: config_string = '[asection]\n' + f.read() configparser.read_string(config_string) config = parse_parameters(configparser)["asection"] elif file_type in [".yaml", "yaml"]: with config_file.open('r') as f: config = yaml.safe_load(f) # check for "None" entries _fix_none_entries(dictionary=config) else: raise Exception() except Exception: log.error("Could not load config file %s", config_file) raise return config
class IniFile(object): def __init__(self): self.keys = {} def __repr__(self): return self.keys.__repr__() def get(self, key, default=None): return self.keys.get(key, default) def parse(self, path): with open(path, "r") as file: self.cfg = RawConfigParser() self.cfg.readfp(file) self.parseKeys()
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')}
class IniFile(object): def __init__(self): self.sections = {} def __repr__(self): return self.sections.__repr__() def get(self, section, key, default=None): return self.sections[section].get(key, default) def parse(self, path): with open(path, "r", encoding="utf-8") as file: self.cfg = RawConfigParser() self.cfg.readfp(file) self.parseKeys()
def parse_modelsimini(file_name): """ Parse a modelsim.ini file :returns: A RawConfigParser object """ cfg = RawConfigParser() if sys.version_info.major == 2: # For Python 2 read modelsim.ini as binary file since ConfigParser # does not support unicode with io.open(file_name, "rb") as fptr: cfg.readfp(fptr) # pylint: disable=deprecated-method else: with io.open(file_name, "r", encoding="utf-8") as fptr: cfg.read_file(fptr) return cfg
def __initialize_config_info_inner(config_fh): """ Return a RawConfigParser for the platform. If config_filename is None, then an attempt is made to load the user-specific configuration file. If this is not found, then the fall back is the contents provided by the default configurations, supplied by configure_parser(config_info). """ global __config_info __config_info = RawConfigParser() configure_parser(__config_info) # try to read in a configuration if config_fh is not None: __config_info.readfp(config_fh)
def _parse_username(self): """ Get the username from the session file of rhythmbox' audioscrobbler plugin as per http://mail.gnome.org/archives/rhythmbox-devel/2011-December/msg00029.html """ username_config_parser = RawConfigParser() # Expanduser expands '~' into '/home/<username>/' as_session = open(path.expanduser('~/.local/share/rhythmbox/audioscrobbler/sessions'), 'r') username_config_parser.readfp(as_session) try: username = username_config_parser.get('Last.fm', 'username') print("Parsed Last.fm username: %s" % username) self._username = username except: print("Error: last.fm sessions file could not be parsed. Username set to 'None'") self._username = None
def gen_pdf(sender, **kwargs): statement = kwargs.get('instance') project = Project.objects.get(pk=statement.project.id) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) config = RawConfigParser() if(os.path.isfile(os.path.join(BASE_DIR, 'config.overrides.ini'))): config.readfp(open(os.path.join(BASE_DIR, 'config.overrides.ini'))) else: config.readfp(open(os.path.join(BASE_DIR, 'config.ini'))) host = config.get('hostname', 'SITE_HOST') invoiceUrl = "http://"+host+"/invoice?statement="+str(statement.id) invoicePath = os.path.abspath(os.path.dirname(__name__)) invoiceFilename = statement.url mkdirCmd = "mkdir -p %s"%(invoicePath+"/static/uploads/invoices/"+str(project.user)+"/"+str(project.id)) os.system(mkdirCmd) pdfCmd = "wkhtmltopdf.sh %s %s%s"%(invoiceUrl,invoicePath,invoiceFilename) os.system(pdfCmd)
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
def parse_cmake_module(s_in): config_docopt = None config_define = None config_export = None config_fetch = None if 'autocmake.cfg configuration::' not in s_in: return config_docopt, config_define, config_export, config_fetch s_out = [] is_rst_line = False for line in s_in.split('\n'): if is_rst_line: if len(line) > 0: if line[0] != '#': is_rst_line = False else: is_rst_line = False if is_rst_line: s_out.append(line[2:]) if '#.rst:' in line: is_rst_line = True autocmake_entry = '\n'.join(s_out).split('autocmake.cfg configuration::')[1] autocmake_entry = autocmake_entry.replace('\n ', '\n') # we prepend a fake section heading so that we can parse it with configparser autocmake_entry = '[foo]\n' + autocmake_entry buf = StringIO(autocmake_entry) config = RawConfigParser(dict_type=OrderedDict) config.readfp(buf) for section in config.sections(): if config.has_option(section, 'docopt'): config_docopt = config.get(section, 'docopt') if config.has_option(section, 'define'): config_define = config.get(section, 'define') if config.has_option(section, 'export'): config_export = config.get(section, 'export') if config.has_option(section, 'fetch'): config_fetch = config.get(section, 'fetch') return config_docopt, config_define, config_export, config_fetch
def getCfg(): ''' Loads the config from disk or rebuilds it from user input. Return: the dictionary of user inputs ''' try: #Attempt to load the file from the config with open('setup.cfg') as fp: cfgParse = RawConfigParser() cfgParse['DEFAULT'] = defaults cfgParse.readfp(fp) return dict(cfgParse['DEFAULT']) except: #If we failed we'll rebuild the config. print(I18N['CONFIG_MISSING_MSG']) return rebuildCfg()
def list_languages(): """ Get a list of (language id, language name) tuples for all available languages """ languages = list() for file in os.listdir("lang/"): if file.endswith(".cfg"): # extract id from name: langid = os.path.basename(file) if langid.find(".") >= 0: langid = langid.split(".")[0] # get the actual name for that language: parser = RawConfigParser() parser.readfp(codecs.open(os.path.abspath(os.path.join("lang", file)), "r", "utf8")) langname = parser.get("language", "name") # append resulting tuple: languages.append((langid, langname)) return languages