Beispiel #1
0
    def subthing_names(self):
        """
        Represent all section names from ``.ini`` file reverse sorted by length

        :raises IOError: On failing to read ``.ini`` file or if no sections
        """
        if self._subthing_names is None:
            # Using different parser here, helps validate job performed
            # in this class is correct (at least for section names)
            parser = RawConfigParser()
            if self.ini_filename is not None:
                parser.read([self.ini_filename])
            else:
                parser.readfp(StringIO(self._ini_string))
            section_names = parser.sections()  # Could be empty!
            if len(section_names) < 1:
                if self.ini_filename is not None:
                    raise IOError("No sections found in ini file: %s"
                                  % self.ini_filename)
                else:
                    raise IOError("No sections found in ini string: '%s'"
                                  % self._ini_string)
            section_names.sort(lambda x, y: cmp(len(x), len(y)), reverse=True)
            self._subthing_names = section_names
        return tuple(self._subthing_names)
Beispiel #2
0
    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)
Beispiel #3
0
    def _get_autoreload_programs(self, cfg_file):
        """Get the set of programs to auto-reload when code changes.

        Such programs will have autoreload=true in their config section.
        This can be affected by config file sections or command-line
        arguments, so we need to read it out of the merged config.
        """
        cfg = RawConfigParser()
        cfg.readfp(cfg_file)
        reload_progs = {}
        graceful_reload_progs = {}
        for section in cfg.sections():
            if section.startswith("program:"):
                try:
                    patterns = (
                        cfg.get(section, "autoreload_patterns").split(",")
                        if cfg.has_option(section, "autoreload_patterns")
                        else AUTORELOAD_PATTERNS
                    )
                    if cfg.getboolean(section, "autoreload"):
                        if cfg.getboolean(section, "autoreload_graceful"):
                            graceful_reload_progs[section.split(":", 1)[1]] = patterns
                        else:
                            reload_progs[section.split(":", 1)[1]] = patterns
                except NoOptionError:
                    pass
        return (reload_progs, graceful_reload_progs)
 def edit_profilevars(self):
     config = ProfileVariablesConfig(self.conn, self.current.profile)
     tmp, path = tempfile.mkstemp('variable', 'paella')
     tmp = file(path, 'w')
     config.write(tmp)
     tmp.close()
     os.system('$EDITOR %s' %path)
     tmp = file(path, 'r')
     newconfig = RawConfigParser()
     newconfig.readfp(tmp)
     tmp.close()
     os.remove(path)
     cursor = self.variables.env.cursor
     pclause = Eq('profile', self.current.profile)
     for trait in config.sections():
         tclause = pclause & Eq('trait', trait)
         if not newconfig.has_section(trait):
             cursor.delete(clause=tclause)
         else:
             for name, value in newconfig.items(trait):
                 nclause = tclause & Eq('name', name)
                 if config.has_option(trait, name):
                     if value != config.get(trait, name):
                         cursor.update(data={'value' : value}, clause=nclause)
                 else:
                     idata = { 'profile' : self.current.profile,
                               'trait' : trait,
                               'name' : name,
                               'value' : value}
                     cursor.insert(data=idata)
                 if config.has_section(trait):
                     for name, value in config.items(trait):
                         if not newconfig.has_option(trait, name):
                             cursor.delete(clause=tclause & Eq('name', name))
     self.select_profile(self.current.profile)
    def load_config(self):
        """
        Reads a configuration from reality
        """
        contents = EtcdConfiguration.get(ArakoonClusterConfig.ETCD_CONFIG_KEY.format(self.cluster_id), raw=True)
        parser = RawConfigParser()
        parser.readfp(StringIO(contents))

        self.nodes = []
        self._extra_globals = {}
        for key in parser.options('global'):
            if key == 'plugins':
                self._plugins = [plugin.strip() for plugin in parser.get('global', 'plugins').split(',')]
            elif key in ['cluster_id', 'cluster']:
                pass  # Ignore these
            else:
                self._extra_globals[key] = parser.get('global', key)
        for node in parser.get('global', 'cluster').split(','):
            node = node.strip()
            self.nodes.append(ArakoonNodeConfig(name=node,
                                                ip=parser.get(node, 'ip'),
                                                client_port=parser.get(node, 'client_port'),
                                                messaging_port=parser.get(node, 'messaging_port'),
                                                log_dir=parser.get(node, 'log_dir'),
                                                home=parser.get(node, 'home'),
                                                tlog_dir=parser.get(node, 'tlog_dir')))
def _read_config(filename):
    """Returns a RawConfigParser that has parsed the config file specified by
       the passed filename."""

    # check for bad config files
    p = RawConfigParser()
    fp = None
    try:
        fp = open(filename)
    except IOError:
        pass

    if fp is not None:
        try:
            p.readfp(fp, filename=filename)
        except MissingSectionHeaderError:
            fp.close()
            del fp
            bad_config(filename)
        except ParsingError:
            fp.close()
            del fp
            bad_config(filename)
        else:
            fp.close()

    return p
Beispiel #7
0
 def __read_config(self):
     """
     Obtiene la configuración de la persona de su archivo .person y devuelve True si es válida
     """
     parser = RawConfigParser()
     with codecs.open("config/" + self.person + '.person', 'r', encoding='utf-8') as f:
         parser.readfp(f)
     if parser.has_section("metadata_crawling"):
         if parser.has_option('metadata_crawling', 'files'):
             self.__files =\
                 [e.strip() for e in parser.get('metadata_crawling', 'files').split(',')]
             if self.__files == ['']:
                 return False
         else:
             return False
         if parser.has_option('metadata_crawling', 'weight'):
             self.__weight = parser.getint('metadata_crawling', 'weight')
         if parser.has_option('metadata_crawling', 'access_log'):
             self.__access_log = parser.get('metadata_crawling', 'access_log')
             if not self.__access_log:
                 return False
         else:
             return False
         if parser.has_option('metadata_crawling', 'access_log_format'):
             self.__access_log_format = parser.get('metadata_crawling', 'access_log_format')
             if not self.__access_log_format:
                 return False
         else:
             return False
     else:
         return False
     return True
Beispiel #8
0
def parse_locators(file):
    """Parses locators configuration file and
    returns dictionary of locators.

    Arguments:
    file = locators file object (opened with open() method)

    Return:
    Dictionary of parsed locators."""
    # parse file
    parser = RawConfigParser()
    parser.readfp(file)
    # get sections from file
    sections = parser.sections()
    # prepare locators dictionary
    locators = {}
    # don't add sections name
    # when only one section exists
    if len(sections) is 1:
        section = sections[0]
        for name in parser.options(section):
            locators[name] = parser.get(section, name)
    # add section name as a key
    # when more than one section exists
    else:
        for section in sections:
            locators[section] = {}
            for name in parser.options(section):
                locators[section][name] = parser.get(section, name)
    # return dictionary of parsed locators
    return locators
Beispiel #9
0
 def parse(self):
     _config = RawConfigParser()
     _config.optionxform = lambda s: s
     if getattr(self.config, 'read', None) is not None:
         _config.readfp(self.config)
     else:
         _config.read(self.config)
         self.path = os.path.dirname(self.config)
     for section in _config.sections():
         if ':' in section:
             sectiongroupname, sectionname = section.split(':')
         else:
             sectiongroupname, sectionname = 'global', section
         items = dict(_config.items(section))
         sectiongroup = self.setdefault(sectiongroupname, {})
         sectiongroup.setdefault(sectionname, {}).update(items)
     self._load_plugins()
     seen = set()
     for sectiongroupname in self:
         sectiongroup = self[sectiongroupname]
         for sectionname in sectiongroup:
             section = sectiongroup[sectionname]
             if '<' in section:
                 self._expand(sectiongroupname, sectionname, section, seen)
             for key in section:
                 massage = self.massagers.get((sectiongroupname, key))
                 if callable(massage):
                     section[key] = massage(self, sectionname)
     return self
Beispiel #10
0
def pms_readConf(filename):
	class multiKeyDict(DictMixin):
		def __init__(self, d=None):
			self._data = d or {}
		def __setitem__(self, key, value):
			if key in self._data and isinstance(self._data[key], list) and isinstance(value, list):
				self._data[key].extend(value)
			else:
				self._data[key] = value
		def __getitem__(self, key):
			return self._data[key]
		def __delitem__(self, key):
			del self._data[key]
		def keys(self):
			return self._data.keys()
		def copy(self):
			return multiKeyDict(self._data.copy())
	conf = RawConfigParser(dict_type=multiKeyDict)
	for c in [filename, os.path.join(pms.getProfileDir(), os.path.basename(filename))]:
		if os.path.exists(c):
			conf.readfp(StringIO('[.]\n'+open(c).read()))
	try:
		conf = dict(conf.items('.'))
		print '%s: %s' % (filename, conf)
		return conf
	except:
		return {}
    def get_option(self, key, default=NoDefault, asbool=False, section="options"):
        config = RawConfigParser()
        fp = StringIO(self.config)
        try:
            config.readfp(fp)
        except MissingSectionHeaderError:
            if default is NoDefault:
                raise
            return default
        try:
            value = config.get(section, key)
        except (NoOptionError, NoSectionError):
            if default is NoDefault:
                raise
            return default

        if not asbool:
            return value.strip()

        value = value.lower()
        if value in ("1", "true", "t", "yes", "y", "on"):
            return True
        elif value in ("0", "false", "f", "no", "n", "off"):
            return False
        else:
            raise TypeError("Cannot convert to bool: %s" % value)
Beispiel #12
0
 def __init__(self, config, path=None):
     _config = RawConfigParser()
     _config.optionxform = lambda s: s
     if getattr(config, 'read', None) is not None:
         _config.readfp(config)
         self.path = path
     else:
         _config.read(config)
         self.path = os.path.dirname(config)
     for section in _config.sections():
         if ':' in section:
             sectiongroupname, sectionname = section.split(':')
         else:
             sectiongroupname, sectionname = 'global', section
         items = dict(_config.items(section))
         sectiongroup = self.setdefault(sectiongroupname, {})
         sectiongroup.setdefault(sectionname, {}).update(items)
     seen = set()
     for sectiongroupname in self:
         sectiongroup = self[sectiongroupname]
         for sectionname in sectiongroup:
             section = sectiongroup[sectionname]
             if '<' in section:
                 self._expand(sectiongroupname, sectionname, section, seen)
             for key in section:
                 fname = 'massage_%s_%s' % (sectiongroupname, key.replace('-', '_'))
                 massage = getattr(self, fname, None)
                 if callable(massage):
                     section[key] = massage(section[key])
Beispiel #13
0
 def __read_config(self):
     """
     Obtiene la configuración de la persona de su archivo .person y devuelve True si es válida
     """
     pparser = RawConfigParser()
     with codecs.open("config/" + self.person + '.person', 'r', encoding='utf-8') as cf:
         pparser.readfp(cf)
     if pparser.has_section("general"):
         if pparser.has_option('general', 'name'):
             self.name = pparser.get('general', 'name')
             if self.name == '':
                 return False
         else:
             return False
         if pparser.has_option('general', 'notify'):
             self.notify = pparser.getboolean('general', 'notify')
         if pparser.has_option('general', 'alarm_threshold'):
             self.alarm_threshold = pparser.getint('general', 'alarm_threshold')
         if pparser.has_option('general', 'email'):
             self.email = pparser.get('general', 'email')
         if self.email == '' and self.notify:
                 return False
     else:
         return False
     return True
Beispiel #14
0
    def get_items(self):
        parameters = {}
        mysql_config = RawConfigParser()

        sections = []

        try:
            contents = file(path(MY_CNF))
            lines = contents.readlines()

            # Workaround for `allow_no_value` (which is available in only Python 2.7+).
            body = ''
            for line in lines:
                stripped_line = line.strip()
                if not stripped_line or stripped_line[0] in ('#', ';'):
                    pass

                elif line[0] != '[' and line[-1] != ']' and \
                   '=' not in line:
                    line = '%s=\n' % line.rstrip('\n')

                body += line

            mysql_config.readfp(cStringIO.StringIO(body))

            sections = mysql_config.sections()
        except IOError:
            #sys.exit("Notice: Cannot open MySQL configuration file '%s'" % MY_CNF)
            print "Notice: Cannot open MySQL configuration file '%s'" % MY_CNF
        except ParsingError, error:
            print "Notice: Cannot parse PHP configuration file '%s'\n%s" % (PHP_INI, error)
Beispiel #15
0
 def __read_config(self):
     """
     Obtiene la configuración de la persona de su archivo .person y devuelve True si es válida
     """
     parser = RawConfigParser()
     with codecs.open("config/" + self.person + '.person', 'r', encoding='utf-8') as f:
         parser.readfp(f)
     if parser.has_section("web_bug"):
         if parser.has_option("web_bug", 'search_terms'):
             self.__search_terms = parser.get("web_bug", 'search_terms')
             if self.__search_terms == '':
                 return False
         else:
             return False
         if parser.has_option("web_bug", 'weight'):
             self.__weight = parser.getint("web_bug", 'weight')
         if parser.has_option("web_bug", 'weight_no_search_terms'):
             self.__weight_no_search_terms = parser.getint("web_bug", 'weight_no_search_terms')
         if parser.has_option("web_bug", 'weight_visit'):
             self.__weight_visit = parser.getint("web_bug", 'weight_visit')
         if parser.has_option("web_bug", 'webbug_log'):
             self.__webbug_log =\
                 [e.strip() for e in parser.get("web_bug", 'webbug_log').split(',')]
             if self.__webbug_log == ['']:
                 return False
         else:
             return False
     else:
         return False
     return True
Beispiel #16
0
    def collapse_arakoon():
        """
        Collapse Arakoon's Tlogs
        :return: None
        """
        logger.info('Starting arakoon collapse')
        arakoon_clusters = {}
        for service in ServiceList.get_services():
            if service.type.name in ('Arakoon', 'NamespaceManager', 'AlbaManager'):
                arakoon_clusters[service.name.replace('arakoon-', '')] = service.storagerouter

        for cluster, storagerouter in arakoon_clusters.iteritems():
            logger.info('  Collapsing cluster {0}'.format(cluster))
            contents = EtcdConfiguration.get(ArakoonClusterConfig.ETCD_CONFIG_KEY.format(cluster), raw=True)
            parser = RawConfigParser()
            parser.readfp(StringIO(contents))
            nodes = {}
            for node in parser.get('global', 'cluster').split(','):
                node = node.strip()
                nodes[node] = ([parser.get(node, 'ip')], parser.get(node, 'client_port'))
            config = ArakoonClientConfig(str(cluster), nodes)
            for node in nodes.keys():
                logger.info('    Collapsing node: {0}'.format(node))
                client = ArakoonAdminClient(node, config)
                try:
                    client.collapse_tlogs(2)
                except:
                    logger.exception('Error during collapsing cluster {0} node {1}'.format(cluster, node))

        logger.info('Arakoon collapse finished')
Beispiel #17
0
class ApplicationConfig(object):
    """A thin wrapper around ConfigParser that remembers what we read.

    The remembered settings can then be written out to a minimal config file
    when building the Elastic Beanstalk zipfile.

    """
    def __init__(self):
        self.input = RawConfigParser()
        with open("production.ini") as f:
            self.input.readfp(f)
        self.output = RawConfigParser()

    def get(self, section, key):
        value = self.input.get(section, key)

        # remember that we needed this configuration value
        if (section.upper() != "DEFAULT" and
            not self.output.has_section(section)):
            self.output.add_section(section)
        self.output.set(section, key, value)

        return value

    def to_config(self):
        io = cStringIO.StringIO()
        self.output.write(io)
        return io.getvalue()
Beispiel #18
0
def next_serial(serial_file=CA_SERIALNO):
    """
    Get the next serial number if we're using an NSS-based self-signed CA.

    The file is an ini-like file with following properties:
       lastvalue = the last serial number handed out
       nextreplica = the serial number the next replica should start with
       replicainterval = the number to add to nextreplica the next time a
                         replica is created

    File locking is attempted so we have unique serial numbers.
    """
    fp = None
    parser = RawConfigParser()
    if ipautil.file_exists(serial_file):
        try:
            fp = open(serial_file, "r+")
            fcntl.flock(fp.fileno(), fcntl.LOCK_EX)
            parser.readfp(fp)
            serial = parser.getint('selfsign', 'lastvalue')
            cur_serial = serial + 1
        except IOError, e:
            raise RuntimeError("Unable to determine serial number: %s" % str(e))
        except MissingSectionHeaderError:
            fcntl.flock(fp.fileno(), fcntl.LOCK_UN)
            fp.close()
            f=open(serial_file,"r")
            r = f.readline()
            f.close()
            cur_serial = int(r) + 1
            fp = open(serial_file, "w")
            fcntl.flock(fp.fileno(), fcntl.LOCK_EX)
            parser.add_section('selfsign')
            parser.set('selfsign', 'nextreplica', 500000)
            parser.set('selfsign', 'replicainterval', 500000)
def getAccount(account_key):
    from ConfigParser import RawConfigParser, NoOptionError, NoSectionError

    account_file = '.account'

    config = RawConfigParser()
    with open(account_file, 'r') as fp:
        config.readfp(fp)

    account = config.get('account', account_key)
    password = None
    password_section = 'password'
    try:
        password = config.get(password_section, account_key)
    except NoSectionError:
        config.add_section(password_section)
    except NoOptionError:
        pass

    aes = AESCipher(account)
    if password:
        return account, aes.decrypt(password).encode('UTF-8')

    from getpass import getpass
    password = getpass(account_key + ' of ' +account + "'s password: ")

    config.set(password_section, account_key, aes.encrypt(password))
    with open(account_file, 'w') as fp:
        config.write(fp)

    return account, password
Beispiel #20
0
 def test_config_before_parse_is_plain(self):
     rawConfig = RawConfigParser()
     rawConfig.readfp(StringIO(self.config_string))
     self.assertEqual([(section, sorted(self.config.items(section)))
                       for section in self.config.sections()],
                      [(section, sorted(rawConfig.items(section)))
                       for section in rawConfig.sections()])
Beispiel #21
0
 def parse_config(self, filename):
     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 self.offsets.has_key(device):
                 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)
Beispiel #22
0
    def collapse_arakoon():
        """
        Collapse Arakoon's Tlogs
        :return: None
        """
        ScheduledTaskController._logger.info('Starting arakoon collapse')
        arakoon_clusters = []
        for service in ServiceList.get_services():
            if service.is_internal is True and \
               service.type.name in (ServiceType.SERVICE_TYPES.ARAKOON,
                                     ServiceType.SERVICE_TYPES.NS_MGR,
                                     ServiceType.SERVICE_TYPES.ALBA_MGR):
                arakoon_clusters.append(service.name.replace('arakoon-', ''))

        for cluster in arakoon_clusters:
            ScheduledTaskController._logger.info('  Collapsing cluster {0}'.format(cluster))
            contents = EtcdConfiguration.get(ArakoonClusterConfig.ETCD_CONFIG_KEY.format(cluster), raw=True)
            parser = RawConfigParser()
            parser.readfp(StringIO(contents))
            nodes = {}
            for node in parser.get('global', 'cluster').split(','):
                node = node.strip()
                nodes[node] = ([str(parser.get(node, 'ip'))], int(parser.get(node, 'client_port')))
            config = ArakoonClientConfig(str(cluster), nodes)
            for node in nodes.keys():
                ScheduledTaskController._logger.info('    Collapsing node: {0}'.format(node))
                client = ArakoonAdmin(config)
                try:
                    client.collapse(str(node), 2)
                except:
                    ScheduledTaskController._logger.exception('Error during collapsing cluster {0} node {1}'.format(cluster, node))

        ScheduledTaskController._logger.info('Arakoon collapse finished')
Beispiel #23
0
 def setFilenameInConfigFile(self, root_path, name, filename):
     """Add new filename association in config file
     
     @root_path: Path where config file is stored
     @param name: Field name
     @param filename: Filename of value stored in field
     """
     
     path = os.path.join(root_path, self.cfg_filename)
     
     # Update file
     config = RawConfigParser()
     
     if os.path.exists(path):
         # Read file
         fd = open(path, 'r')
         try:
             config.readfp(fd)
         finally:
             fd.close()
     
     # Create section if it doesn't exist
     if not config.has_section(self.cfg_filename_section):
         config.add_section(self.cfg_filename_section)
     config.set(self.cfg_filename_section, name, filename)
     
     fd = open(path, 'w')
     try:
         # Write file
         config.write(fd)
     finally:
         fd.close()
Beispiel #24
0
def parse_package_metadata():
    """
    Read the 'metadata' section of 'setup.cfg' to calculate the package
    metadata (at least those parts that can be configured staticly).
    """
    try:
        from ConfigParser import RawConfigParser
    except ImportError:
        from configparser import RawConfigParser

    cfg = RawConfigParser()
    with open('setup.cfg') as fp:
        cfg.readfp(fp)

    cfg.optionxform = lambda x: x

    metadata = {}
    for opt in cfg.options('metadata'):
        val = cfg.get('metadata', opt)
        if opt in ('classifiers',):
            metadata[opt] = [x for x in val.splitlines() if x]
        elif opt in ('long_description',):
            metadata[opt] = val[1:]
        elif opt in ('packages', 'namespace_packages', 'platforms', 'keywords'):
            metadata[opt] = [x.strip() for x in val.split(',')]

        elif opt in ['zip-safe']:
            metadata['zip_safe'] = int(val)
        else:
            metadata[opt] = val

    metadata['version'] = package_version()

    return metadata
Beispiel #25
0
def load_config(path):

    config = {}

    defaults = resource_stream("tiktok.config", "defaults.cfg")
    parser = RawConfigParser(allow_no_value=True)
    parser.readfp(defaults, "defaults.cfg")

    if os.path.exists(path):
        parser.read(path)

    sections = [x for x in parser.sections() if x != "general"]

    config.update(dict(parser.items("general")))
    for section in sections:
        config[section] = dict(parser.items(section))

    config["config_dir"] = CONFIG_DIR

    task_properties = zip(
        [int(x) for x in config["task"]["property_ids"].split(",")],
        [int(x) for x in config["task"]["property_values"].split(",")],
    )

    config["task_properties"] = task_properties

    return config
Beispiel #26
0
 def getFilenameInConfigFile(self, root_path, name):
     """Get filename field association in config file
     
     @root_path: Path where config file is stored
     @param name: Field name
     """
     
     path = os.path.join(root_path, self.cfg_filename)
     
     # Config file doesn't exists
     if not os.path.exists(path):
         return None
     
     # If it exists parse it and get value
     fd = open(path, 'r')
     value = None
     try:
         config = RawConfigParser()
         config.readfp(fd)
         if config.has_section(self.cfg_filename_section) and \
            config.has_option(self.cfg_filename_section, name):
             value = config.get(self.cfg_filename_section, name)
     finally:
         fd.close()
     
     return value
Beispiel #27
0
def makeService(twisted_config):
    from ConfigParser import RawConfigParser
    from starpy import fastagi
    from twisted.application import internet

    # Read configuration
    f = twisted_config['config']
    config = RawConfigParser()
    config.readfp(open(f), f)

    # Set Redmine REST API connection parameters
    Issue.set_site(config.get('Astminer', 'RedmineSite'))
    Issue.set_user(config.get('Astminer', 'RedmineUser'))
    Issue.set_password(config.get('Astminer', 'RedminePassword'))

    app = Application(config)

    # Connect to asterisk manager interface
    f = MyAMIFactory(
            config.get('Astminer', 'ManagerUser'),
            config.get('Astminer', 'ManagerPassword'),
            app)
    reactor.connectTCP(
            config.get('Astminer', 'ManagerHost'),
            int(config.get('Astminer', 'ManagerPort')),
            f)

    # Setup FastAGI listener
    f = fastagi.FastAGIFactory(app.agiRequestReceived)
    service = internet.TCPServer(
            int(config.get('Astminer', 'AgiPort')), f)

    return service
Beispiel #28
0
def config_from_filepath(path):
    config = Parser()
    config.readfp(open(path))
    config.get('credentials', 'user')
    config.get('credentials', 'password')
    config.get('site', 'remote')
    return config
Beispiel #29
0
    def parse(self, argv=None, needsconfig=False):
        """
        Load configuration from the configuration file and from command-line arguments.

        :param argv: The argument vector, or None to use sys.argv
        :type argv: [str]
        :param needsconfig: True if the config file must be present for the application to function.
        :type needsconfig: bool
        :returns: A :class:`Namespace` object with the parsed settings
        :rtype: :class:`Namespace`
        """
        stack = list()
        options = RawConfigParser()
        args = list()
        overrides = RawConfigParser()
        try:
            argv = argv if argv is not None else sys.argv
            overrides.add_section(self._section)
            overrides.set(self._section, 'config file', os.path.join(self._confbase, "%s.conf" % self.appname))
            # parse command line arguments
            stack,args = self._parse(argv[1:], overrides)
            # load configuration file
            config_file = overrides.get(self._section, 'config file')
            path = os.path.normpath(os.path.join(self._cwd, config_file))
            with open(path, 'r') as f:
                options.readfp(f, path)
            logger.debug("loaded settings from %s" % path)
        except getopt.GetoptError, e:
            raise ConfigureError(str(e))
 def parser_to_dict(self, filename):
     parser = RawConfigParser()
     parser.readfp(open(filename, "r"))
     dict_ = {}
     options = parser.options("LIB")
     for option in options: dict_[option] = parser.get("LIB", option)
     return dict_
Beispiel #31
0
    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"))
Beispiel #32
0
 def read(self, pathfilename):
     with codecs.open(pathfilename, 'r',
                      encoding=ServiceDefault.CHAR_CODEC) as input_file:
         config_pairs = input_file.read()
     with _closing(_StringIO(u"[{0}]{1}{2}".format(self._default_section,
                                                   os.linesep,
                                                   config_pairs))) \
             as default_section:
         _RawConfigParser.readfp(self, default_section)
Beispiel #33
0
    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
Beispiel #34
0
def pms_readConf(filename):
    conf = RawConfigParser()
    for c in [
            filename,
            os.path.join(pms.getProfileDir(), os.path.basename(filename))
    ]:
        if os.path.exists(c):
            conf.readfp(StringIO('[.]\n' + open(c).read()))
    return dict(conf.items('.'))
Beispiel #35
0
def main(argv=sys.argv):
    argparser = ArgumentParser()

    argparser.add_argument('--config', help="nextgisweb configuration file")
    argparser.add_argument('--logging',
                           help="logging library configuration file")

    config = None
    logging = None

    i = 1

    while i < len(argv):
        if argv[i] == '--config' and (i < len(argv) - 1):
            config = argv[i + 1]
        if argv[i] == '--logging' and (i < len(argv) - 1):
            logging = argv[i + 1]

        i += 2 if argv[i].startswith('--') else 1

    if config is None:
        config = os.environ.get('NEXTGISWEB_CONFIG')

    if logging is None:
        logging = os.environ.get('NEXTGISWEB_LOGGING')

    if logging:
        setup_logging(logging)

    cfg = RawConfigParser()

    if config:
        cfg.readfp(codecs.open(config, 'r', 'utf-8'))

    for section in cfg.sections():
        for item, value in cfg.items(section):
            cfg.set(section, item, value % os.environ)

    env = Env(cfg=cfg)
    env.initialize()

    setenv(env)

    subparsers = argparser.add_subparsers()

    for cmd in Command.registry:
        subparser = subparsers.add_parser(cmd.identity)
        cmd.argparser_setup(subparser, env)
        subparser.set_defaults(command=cmd)

    args = argparser.parse_args(argv[1:])

    if args.logging:
        setup_logging(args.logging)

    args.command.execute(args, env)
Beispiel #36
0
 def reset_value(self, option):
     default = RawConfigParser()
     default.readfp(
         open(
             os.path.join(
                 os.getenv("PINGUINO_HOME"), "qtgui", "config",
                 "pinguino." + os.getenv("PINGUINO_OS_NAME") + ".conf"),
             "r"))
     getattr(self.set_paths,
             "lineEdit_" + option).setText(default.get("Paths", option))
    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()
        self.config_data = config_data % dict(tmpdir=self.tmpdir)

        config = RawConfigParser()
        config.readfp(StringIO(self.config_data))

        passphrases = {"gpg": "foobar"}

        self.server = ss.SigningServer(config, passphrases)
Beispiel #38
0
Datei: lib.py Projekt: semtle/wfw
def get_user_data():
    if isfile(USER_DATA):
        configparser = RawConfigParser()
        user_data = {}
        with open(USER_DATA) as config:
            configparser.readfp(config)
        user_data['email'] = configparser.get('user', 'email')
        user_data['password'] = configparser.get('user', 'password')
        user_data['aux'] = configparser.get('tree', 'shared')
        return user_data
 def initialize_from_config(self):
     """
     Initializes the scene using parameters set in the config file
     """
     cfg = RawConfigParser()
     cfg.readfp(open('./Config/generation.conf'))
     self.height = cfg.getint("scene", "height")
     self.width = cfg.getint("scene", "width")
     self.num_objects = cfg.getint("scene", "num_objects")
     self.save()
Beispiel #40
0
def _parse_submodule(repository, submodule_obj):
    # get ref
    #submodule_conf_raw = _read_blob(repository, submodule_obj.hex)
    submodule_conf_raw = submodule_obj.data
    submodule_conf_raw = _format_submodule_conf(submodule_conf_raw)

    config = RawConfigParser(allow_no_value=True)
    config.readfp(StringIO(submodule_conf_raw))

    return config
Beispiel #41
0
class Config(object):
    """
    Manage configuration - a simple wrapper around RawConfigParser.
    Upon initialization, the loaded file is updated with the default values.
    config.save() will save the current state.
    """
    def __init__(self):
        self.filename = get_config_fn()
        try:
            self.parser = RawConfigParser(dict_type=OrderedDict)
        except TypeError:
            # Python versions < 2.6 don't support dict_type
            self.parser = RawConfigParser()
        f = StringIO(default_config)
        self.parser.readfp(f)
        self.parser.read(self.filename)
        self.save()

    def get(self, key, section='DreamPie'):
        return self.parser.get(section, key)

    def get_bool(self, key, section='DreamPie'):
        return self.parser.getboolean(section, key)

    def get_int(self, key, section='DreamPie'):
        return self.parser.getint(section, key)

    def set(self, key, value, section='DreamPie'):
        self.parser.set(section, key, value)

    def set_bool(self, key, value, section='DreamPie'):
        value_str = 'True' if value else 'False'
        self.set(key, value_str, section)

    def set_int(self, key, value, section='DreamPie'):
        if value != int(value):
            raise ValueError("Expected an int, got %r" % value)
        self.set(key, '%d' % value, section)

    def sections(self):
        return self.parser.sections()

    def has_section(self, section):
        return self.parser.has_section(section)

    def add_section(self, section):
        return self.parser.add_section(section)

    def remove_section(self, section):
        return self.parser.remove_section(section)

    def save(self):
        f = open(self.filename, 'w')
        self.parser.write(f)
        f.close()
Beispiel #42
0
    def expand_macro(self, formatter, name, text, args):
        if not text:
            raw_actions = self.config.options('ticket-workflow')
        else:
            if args is None:
                text = '\n'.join([line.lstrip() for line in text.split(';')])
            if '[ticket-workflow]' not in text:
                text = '[ticket-workflow]\n' + text
            parser = RawConfigParser()
            try:
                parser.readfp(StringIO(text))
            except ParsingError as e:
                return system_message(_("Error parsing workflow."), unicode(e))
            raw_actions = list(parser.items('ticket-workflow'))
        actions = parse_workflow_config(raw_actions)
        states = list(
            set([
                state for action in actions.itervalues()
                for state in action['oldstates']
            ] + [action['newstate'] for action in actions.itervalues()]))
        action_labels = [attrs['label'] for attrs in actions.values()]
        action_names = actions.keys()
        edges = []
        for name, action in actions.items():
            new_index = states.index(action['newstate'])
            name_index = action_names.index(name)
            for old_state in action['oldstates']:
                old_index = states.index(old_state)
                edges.append((old_index, new_index, name_index))

        args = args or {}
        width = args.get('width', 800)
        height = args.get('height', 600)
        graph = {
            'nodes': states,
            'actions': action_labels,
            'edges': edges,
            'width': width,
            'height': height
        }
        graph_id = '%012x' % id(graph)
        req = formatter.req
        add_script(req, 'common/js/excanvas.js', ie_if='IE')
        add_script(req, 'common/js/workflow_graph.js')
        add_script_data(req, {'graph_%s' % graph_id: graph})
        return tag(
            tag.div('',
                    class_='trac-workflow-graph trac-noscript',
                    id='trac-workflow-graph-%s' % graph_id,
                    style="display:inline-block;width:%spx;height:%spx" %
                    (width, height)),
            tag.noscript(
                tag.div(_("Enable JavaScript to display the workflow graph."),
                        class_='system-message')))
Beispiel #43
0
def read(where):

    # TODO: Allow the user the option of just choosing a single checkout to
    # process - this makes it easier to recover if some patches worked, and
    # others didn't, but the user has figured out why and fixed things...

    where = canonical_path(where)

    manifest = os.path.join(where, 'MANIFEST.txt')
    config = RawConfigParser()
    with open(manifest) as fd:
        config.readfp(fd)

    sections = config.sections()
    for section in sections:
        print 'Section %s' % section
        if section.startswith("BZR"):
            co_leaf = config.get(section, 'co_leaf')
            co_dir = config.get(section, 'co_dir')
            if co_dir == 'None':
                co_dir = None
            filename = config.get(section, 'patch')
            print '  co_leaf %s, co_dir %s, filename %s' % (co_leaf, co_dir,
                                                            filename)
            bzr_merge_from_send(co_leaf, co_dir, os.path.join(where, filename))
        elif section.startswith("SVN"):
            co_leaf = config.get(section, 'co_leaf')
            co_dir = config.get(section, 'co_dir')
            if co_dir == 'None':
                co_dir = None
            filename = config.get(section, 'patch')
            print '  co_leaf %s, co_dir %s, filename %s' % (co_leaf, co_dir,
                                                            filename)
            svn_patch(co_leaf, co_dir, os.path.join(where, filename))
        elif section.startswith("GIT"):
            co_leaf = config.get(section, 'co_leaf')
            co_dir = config.get(section, 'co_dir')
            if co_dir == 'None':
                co_dir = None
            patch_dir = config.get(section, 'patch')
            print '  co_leaf %s, co_dir %s, patch_dir %s' % (co_leaf, co_dir,
                                                             patch_dir)
            git_am(co_leaf, co_dir, os.path.join(where, patch_dir))
        elif section.startswith("TAR"):
            co_leaf = config.get(section, 'co_leaf')
            co_dir = config.get(section, 'co_dir')
            if co_dir == 'None':
                co_dir = None
            filename = config.get(section, 'patch')
            print '  co_leaf %s, co_dir %s, filename %s' % (co_leaf, co_dir,
                                                            filename)
            tar_unpack(co_leaf, co_dir, os.path.join(where, filename))
        else:
            print 'No support for %s yet - ignoring...' % section.split()[0]
Beispiel #44
0
def readconf(conf_path,
             section_name=None,
             log_name=None,
             defaults=None,
             raw=False):
    """
    Read config file(s) and return config items as a dict

    :param conf_path: path to config file/directory, or a file-like object
                     (hasattr readline)
    :param section_name: config section to read (will return all sections if
                     not defined)
    :param log_name: name to be used with logging (will use section_name if
                     not defined)
    :param defaults: dict of default values to pre-populate the config with
    :returns: dict of config items
    """
    if defaults is None:
        defaults = {}
    if raw:
        c = RawConfigParser(defaults)
    else:
        c = ConfigParser(defaults)
    if hasattr(conf_path, 'readline'):
        c.readfp(conf_path)
    else:
        if os.path.isdir(conf_path):
            # read all configs in directory
            success = read_conf_dir(c, conf_path)
        else:
            success = c.read(conf_path)
        if not success:
            print _("Unable to read config from %s") % conf_path
            sys.exit(1)
    if section_name:
        if c.has_section(section_name):
            conf = dict(c.items(section_name))
        else:
            print _("Unable to find %s config section in %s") % \
                (section_name, conf_path)
            sys.exit(1)
        if "log_name" not in conf:
            if log_name is not None:
                conf['log_name'] = log_name
            else:
                conf['log_name'] = section_name
    else:
        conf = {}
        for s in c.sections():
            conf.update({s: dict(c.items(s))})
        if 'log_name' not in conf:
            conf['log_name'] = log_name
    conf['__file__'] = conf_path
    return conf
Beispiel #45
0
 def reset_value(self, option):
     default = RawConfigParser()
     default.readfp(
         open(
             os.path.join(
                 os.getenv("PINGUINO_HOME"), "tkgui", "config",
                 "pinguino." + os.getenv("PINGUINO_OS_NAME") + ".conf"),
             "r"))
     getattr(self, "label_" + option).delete(0, END)
     getattr(self, "label_" + option).insert(INSERT,
                                             default.get("Paths", option))
Beispiel #46
0
def read_config(f):
    c = RawConfigParser()
    c.readfp(f)

    def get_options(s):
        return dict(parse_options(c.items(s), section=s))

    general = {}
    pairs = {}
    storages = {}

    def handle_storage(storage_name, options):
        storages.setdefault(storage_name, {}).update(options)
        storages[storage_name]['instance_name'] = storage_name

    def handle_pair(pair_name, options):
        _validate_pair_section(options)
        a, b = options.pop('a'), options.pop('b')
        pairs[pair_name] = a, b, options

    def handle_general(_, options):
        if general:
            raise exceptions.UserError(
                'More than one general section in config file.')
        general.update(options)

    def bad_section(name, options):
        cli_logger.error('Unknown section: {}'.format(name))

    handlers = {
        'storage': handle_storage,
        'pair': handle_pair,
        'general': handle_general
    }

    for section in c.sections():
        if ' ' in section:
            section_type, name = section.split(' ', 1)
        else:
            section_type = name = section

        try:
            validate_section_name(name, section_type)
            f = handlers.get(section_type, bad_section)
            f(name, get_options(section))
        except ValueError as e:
            raise exceptions.UserError('Section `{}`: {}'.format(
                section, str(e)))

    _validate_general_section(general)
    if getattr(f, 'name', None):
        general['status_path'] = os.path.join(
            os.path.dirname(f.name), expand_path(general['status_path']))
    return general, pairs, storages
Beispiel #47
0
    def walkOnStorageDirectory(self, **kwargs):
        """Walk on storage directory"""

        # Get all paths to walk on
        paths = []
        root_path = os.path.abspath(self.storage_path)
        for root, dirs, files in os.walk(root_path):
            if dirs:
                # We have reached last level
                continue
            paths.append(root)

        items = []

        for path in paths:
            # Get config files
            cfg_path = os.path.join(path, self.cfg_filename)

            if not os.path.exists(cfg_path):
                continue

            # Update file
            config = RawConfigParser()

            # Read file
            fd = open(cfg_path, 'r')
            try:
                config.readfp(fd)
            finally:
                fd.close()

            obj_path = path[(len(root_path) + 1):]
            if sys.platform == 'win32':
                obj_path = obj_path.replace('\\', '/')

            names = config.options(self.cfg_filename_section)

            # Get filenames
            for name in names:
                filename = config.get(self.cfg_filename_section, name)
                file_path = os.path.join(path, filename)
                modified = os.path.getmtime(file_path)
                size = os.path.getsize(file_path)

                # Store item
                item = {}
                item['path'] = obj_path
                item['name'] = name
                item['fs_path'] = file_path
                item['modified'] = DateTime(modified)
                item['size'] = size
                items.append(item)

        return items
Beispiel #48
0
class TestPositiveCase(TestCase):
    def test_micro_app(self):
        self.config = RawConfigParser()
        with open("%s/config" % path, "r") as configfile:
            self.config.readfp(configfile)
        micro_app_addr = self.config.get("Network", "addr")
        self.driver.get(micro_app_addr)
        go_button = self.driver.find_element_by_xpath("//input[2]")
        go_button.click()

    def test_error(self):
        raise Exception('some client exception')
Beispiel #49
0
class HaroldConfiguration(object):
    def __init__(self, filename):
        self.parser = RawConfigParser()
        with open(filename, "r") as config_file:
            self.parser.readfp(config_file)

    def plugin_names(self):
        for section in self.parser.sections():
            if not section.startswith(plugin_prefix):
                continue

            yield section[len(plugin_prefix):]
Beispiel #50
0
def get_console_scripts(wheel):
    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 {}
Beispiel #51
0
 def __init__(self, cluster, configuration):
     """
     Initializes the client
     """
     parser = RawConfigParser()
     parser.readfp(StringIO(configuration))
     nodes = {}
     for node in parser.get('global', 'cluster').split(','):
         node = node.strip()
         nodes[node] = ([parser.get(node,
                                    'ip')], parser.get(node, 'client_port'))
     self._client = PyrakoonClient(cluster, nodes)
Beispiel #52
0
class preferences:
	config = []
	parse = None
	lcdconnection = None
	
	def __init__(self):
		if (DEBUG == True): 
			self.writetolog("Init Prefs")
		self.loadprefs(None, None, None)
		if (DEBUG == True): 
			self.writetolog("Done")
			
	def loadprefs(self, word, word_eol, userdata):
		if (self.lcdconnection != None):
			self.lcdconnection.offlights(None, None, None)
		prefs = open(module_dir + "config.txt")
		self.parse = RawConfigParser()
		self.parse.readfp(prefs)
		prefs.close()
		print "Prefrences Loaded"
				
	def value(self,option):
		if (DEBUG == True): 
			self.writetolog("Prefs -> Value") 
		return self.parse.get("config",option)
		if (DEBUG == True): 
			self.writetolog("Done")
			
	def floatvalue(self,option):
		if (DEBUG == True): 
			self.writetolog("Prefs -> FloatValue") 
		return self.parse.getfloat("config",option)
		if (DEBUG == True): 
			self.writetolog("Done")
	
	def intvalue(self,option):
		if (DEBUG == True): 
			self.writetolog("Prefs -> IntValue") 
		return self.parse.getint("config",option)
		if (DEBUG == True): 
			self.writetolog("Done")
			
	def boolvalue(self,option):
		if (DEBUG == True): 
			self.writetolog("Prefs -> IntValue") 
		return self.parse.getboolean("config",option)
		if (DEBUG == True): 
			self.writetolog("Done")
			
	def writetolog(self, message):
		debuglog = open(module_dir + "log.txt","a")
		debuglog.write(message + "\n")
		debuglog.close
Beispiel #53
0
 def read_config(filename, client=None):
     if client is None:
         cp = RawConfigParser()
         with open(filename, 'r') as config_file:
             cfg = config_file.read()
         cp.readfp(StringIO(cfg))
         return cp
     else:
         contents = client.file_read(filename)
         cp = RawConfigParser()
         cp.readfp(StringIO(contents))
         return cp
Beispiel #54
0
def get_node_config(portal, targetIQN):
    ''' Using iscsadm to get the current configuration of a iscsi node.
    The output is parsed in ini format, and returned as a dictionary.'''
    failuremessage = "Failed to get node configurations"
    cmd = ["iscsiadm", "-m", "node", "-p", portal, "-T", targetIQN, "-S"]
    (stdout, stderr) = exn_on_failure(cmd, failuremessage)
    ini_sec = "root"
    str_fp = StringIO.StringIO("[%s]\n%s" % (ini_sec, stdout))
    parser = RawConfigParser()
    parser.readfp(str_fp)
    str_fp.close()
    return dict(parser.items(ini_sec))
Beispiel #55
0
def parse_package_metadata():
    """
    Read the 'metadata' section of 'setup.cfg' to calculate the package
    metadata (at least those parts that can be configured staticly).
    """
    try:
        from ConfigParser import RawConfigParser
    except ImportError:
        from configparser import RawConfigParser

    cfg = RawConfigParser()
    cfg.optionxform = lambda x: x

    if sys.version_info.major == 2:
        with open('setup.cfg') as fp:
            cfg.readfp(fp)
    else:
        with open('setup.cfg') as fp:
            cfg.read_file(fp)


    metadata = {}
    for opt in cfg.options('x-metadata'):
        val = cfg.get('x-metadata', opt)
        if opt in ('classifiers',):
            metadata[opt] = [x for x in val.splitlines() if x]
        elif opt in ('long_description',):
            # In python 2.7 empty lines in the long description are handled incorrectly,
            # therefore setup.cfg uses '$' at the start of empty lines. Remove that
            # character from the description
            val = val[1:]
            val = val.replace('$', '')
            metadata[opt] = val

            # Add links to interesting location to the long_description
            metadata[opt] += "\n\nProject links\n"
            metadata[opt] += "-------------\n"
            metadata[opt] += "\n"
            metadata[opt] += "* `Documentation <https://pyobjc.readthedocs.io/en/latest/>`_\n\n"
            metadata[opt] += "* `Issue Tracker <https://bitbucket.org/ronaldoussoren/pyobjc/issues?status=new&status=open>`_\n\n"
            metadata[opt] += "* `Repository <https://bitbucket.org/ronaldoussoren/pyobjc/>`_\n\n"

        elif opt in ('packages', 'namespace_packages', 'platforms', 'keywords'):
            metadata[opt] = [x.strip() for x in val.split(',')]

        elif opt in ['zip-safe']:
            metadata['zip_safe'] = int(val)
        else:
            metadata[opt] = val

    metadata['version'] = package_version()

    return metadata
Beispiel #56
0
 def edit(self):
     tmp, path = tempfile.mkstemp('variables', 'config')
     tmp = file(path, 'w')
     self.write(tmp)
     tmp.close()
     os.system('$EDITOR %s' % path)
     tmp = file(path, 'r')
     newconfig = RawConfigParser()
     newconfig.readfp(tmp)
     tmp.close()
     os.remove(path)
     return newconfig
Beispiel #57
0
def readconf(
    conffile,
    section_name=None,
    log_name=None,
    defaults=None,
    raw=False,
):
    """
    Read config file and return config items as a dict

    :param conffile: path to config file, or a file-like object (hasattr
                     readline)
    :param section_name: config section to read (will return all sections if
                     not defined)
    :param log_name: name to be used with logging (will use section_name if
                     not defined)
    :param defaults: dict of default values to pre-populate the config with
    :returns: dict of config items
    """

    if defaults is None:
        defaults = {}
    if raw:
        c = RawConfigParser(defaults)
    else:
        c = ConfigParser(defaults)
    if hasattr(conffile, 'readline'):
        c.readfp(conffile)
    else:
        if not c.read(conffile):
            print _('Unable to read config file %s') % conffile
            sys.exit(1)
    if section_name:
        if c.has_section(section_name):
            conf = dict(c.items(section_name))
        else:
            print _('Unable to find %s config section in %s') \
                % (section_name, conffile)
            sys.exit(1)
        if 'log_name' not in conf:
            if log_name is not None:
                conf['log_name'] = log_name
            else:
                conf['log_name'] = section_name
    else:
        conf = {}
        for s in c.sections():
            conf.update({s: dict(c.items(s))})
        if 'log_name' not in conf:
            conf['log_name'] = log_name
    conf['__file__'] = conffile
    return conf
Beispiel #58
0
class Settings(Parser):
    """
    Contains configuration loaded from the configuration file and
    parsed from command-line arguments.
    """
    def __init__(self, usage='', description=''):
        """
        :param usage: The usage string, displayed in --help output
        :type usage: str
        :param description: A short application description, displayed in --help output
        :type description: str
        """
        self.appname = os.path.basename(sys.argv[0])
        Parser.__init__(self, self.appname, usage, description)
        self._config = RawConfigParser()
        self._overrides = RawConfigParser()
        self._cwd = os.getcwd()
        self._overrides.add_section('settings')
        self._overrides.set('settings', 'config file',
                            "/etc/terane/%s.conf" % self.appname)
        self.addOption('c',
                       'config-file',
                       'settings',
                       'config file',
                       help="Load configuration from FILE",
                       metavar="FILE")

    def load(self, needsconfig=False):
        """
        Load configuration from the configuration file and from command-line arguments.

        :param needsconfig: True if the config file must be present for the application to function.
        :type needsconfig: bool
        """
        try:
            # parse command line arguments
            self._parser, self._args = self._parse(sys.argv[1:],
                                                   self._overrides)
            # load configuration file
            config_file = self._overrides.get('settings', 'config file')
            path = os.path.normpath(os.path.join(self._cwd, config_file))
            with open(path, 'r') as f:
                self._config.readfp(f, path)
            logger.debug("loaded settings from %s" % path)
        except getopt.GetoptError, e:
            raise ConfigureError(str(e))
        except EnvironmentError, e:
            if needsconfig:
                raise ConfigureError("failed to read configuration %s: %s" %
                                     (path, e.strerror))
            logger.info("didn't load configuration %s: %s" %
                        (path, e.strerror))
Beispiel #59
0
def parseConfig(ctx):
    '''
   Parse config.ini file
   '''
    config = RawConfigParser()
    try:
        printDebug("Parsing config file = %s" % (ctx._config_file))
        config.readfp(open(ctx._config_file, 'r'))
        # If _scidb_name has not been assigned, use the first section name as the dbname.
        if ctx._scidb_name == '':
            ctx._scidb_name = config.sections()[0]
    except Exception, e:
        raise AppError("Cannot read config file: %s" % e)
Beispiel #60
0
def createdrivers(numdrivers, sourcename, pubrate, endpoints, archiverip, apikey):
    s = StringIO(configfile.format(uuid=str(uuid.uuid1()),driver=sourcename, rate=pubrate, number=endpoints, archiverip=archiverip, apikey=apikey))

    source = RawConfigParser()
    source.optionxform=str
    source.readfp(s)

    for i in xrange(numdrivers):
        print 'creating source at', source.get('server','port')
        port = int(source.get('server','port'))
        source.set('server', 'port', port+1)
        source.set('/','uuid', str(uuid.uuid1()))
        source.write(open('{0}{1}.ini'.format(sourcename.replace('.','_'), i), 'wb'))