コード例 #1
0
ファイル: ini.py プロジェクト: 0101011/translate-1
    def parse(self, input):
        """Parse the given file or file source string."""
        if hasattr(input, 'name'):
            self.filename = input.name
        elif not getattr(self, 'filename', ''):
            self.filename = ''
        if hasattr(input, "read"):
            inisrc = input.read()
            input.close()
            input = inisrc

        if isinstance(input, bytes):
            if sys.version_info[0] == 3:
                input = StringIO(input.decode('utf-8'))
            else:
                input = BytesIO(input)
            self._inifile = INIConfig(input, optionxformvalue=None)
        else:
            self._inifile = INIConfig(open(input), optionxformvalue=None)

        for section in self._inifile:
            for entry in self._inifile[section]:
                source = self._dialect.unescape(self._inifile[section][entry])
                newunit = self.addsourceunit(source)
                newunit.addlocation("[%s]%s" % (section, entry))
コード例 #2
0
def iniopen(name=None):
    '''
    Produces an INIConfig from the given filename, but also taking care of
    UTF-16LE decoding for UTF-16LE files.
    '''
    if name is None:
        return INIConfig()
    with smartopen(name) as f:
        return INIConfig(f)
コード例 #3
0
def parse_cfg(cfg):
    """
    Parses a MySQL config file looking for connection info for this plugin
    and returns a dict containing the parsed connection information.

    Args:
        cfg(str): String containing the path to the config file

    Returns:
        dict
    """
    mysqlcfg = {}
    try:
        cfgdata = INIConfig(open(cfg))
    except IOError as ioe:
        print "status err I/O error({0}): {1} {2}".format(ioe.errno,
                                                          ioe.strerror,
                                                          cfg)
        sys.exit(1)
    except Exception as e:
        raise
    for key in ['user', 'password', 'unix_socket', 'host', 'port']:
        try:
            if cfgdata.raxmon[key] != iniparseUndefType:
                mysqlcfg[key] = cfgdata.raxmon[key]
        except KeyError:
            pass
    return mysqlcfg
コード例 #4
0
def writeRawRepoFile(repo,only=None):
    """
    Writes changes in a repo object back to a .repo file.
    @param repo: Repo Object
    @param only: List of attributes to work on (None = All)
    It work by reading the repo file, changes the values there shall be changed and write it back to disk.
    """

    if not _use_iniparse:
        return

    ini = INIConfig(open(repo.repofile))
    # b/c repoids can have $values in them we need to map both ways to figure
    # out which one is which
    section_id = repo.id
    if repo.id not in ini._sections:
        for sect in ini._sections.keys():
            if varReplace(sect, repo.yumvar) == repo.id:
                section_id = sect
    
    # Updated the ConfigParser with the changed values    
    cfgOptions = repo.cfg.options(repo.id)
    for name,value in repo.iteritems():
        option = repo.optionobj(name)
        if option.default != value or name in cfgOptions :
            if only == None or name in only:
                ini[section_id][name] = option.tostring(value)
    fp =file(repo.repofile,"w")               
    fp.write(str(ini))
    fp.close()
コード例 #5
0
 def newini(fp=None):
     try:
         # TODO: optionxformvalue isn't used by INIConfig ?
         return INIConfig(fp=fp, optionxformvalue=None)
     except ConfigParser.MissingSectionHeaderError, err:
         raise error.ParseError(err.message.splitlines()[0],
                                '%s:%d' % (err.filename, err.lineno))
コード例 #6
0
ファイル: test_agent.py プロジェクト: tomlanyon/pulp
 def cfg(self):
     conf = INIConfig()
     conf.heartbeat.seconds = 0
     conf.rest.host = ''
     conf.rest.port = 0
     conf.rest.clientcert = ''
     return conf
コード例 #7
0
    def set_save_repo_attr(self, repo, attribute, value):
        """Set the priority for the given RHN repo

        Arguments:
        repo -- rhnplugin.RhnRepo object representing the
                repository to be updated
        attribute -- str representing repository configuration
                     attribute to be updated (e.g. 'priority')
        value -- updated value for specified attribute
        """
        repo = self._resolve_repoid(repo)
        repo.setAttribute(attribute, value)
        if self.repo_is_rhn(repo):
            if hasattr(value, '__iter__'):
                value = ' '.join(value)
            self.backup_config(RHNPLUGINCONF)
            cfg = INIConfig(file(RHNPLUGINCONF))
            repocfg = getattr(cfg, repo.id)
            setattr(repocfg, attribute, value)
            cfg_file = open(RHNPLUGINCONF, 'w')
            print >> cfg_file, cfg
            cfg_file.close()
        else:
            self.backup_config(repo.repofile)
            config.writeRawRepoFile(repo, only=[attribute])
コード例 #8
0
ファイル: test_config.py プロジェクト: aweiteka/pulp
    def test_validation_option(self, mock_config, mock_validate):
        mock_config.return_value = INIConfig()

        # test
        read_config(validate=False)

        # validation
        self.assertFalse(mock_validate.called)
コード例 #9
0
def main():
    if not os.path.isdir(PACKAGES_ROOT):
        print 'Packages root %r does not exist, unable to verify categories.' % PACKAGES_ROOT

    iniconfig = INIConfig(
        urllib2.urlopen('http://portableapps.com/updater/update.ini'))
    if CONCURRENCY_TECHNIQUE in ('multiprocessing', 'threading'):
        main_threaded(iniconfig)
    else:
        main_unthreaded(iniconfig)
コード例 #10
0
ファイル: test_config.py プロジェクト: aweiteka/pulp
    def test_valid(self, mock_config):
        mock_config.return_value = INIConfig(fp=StringIO(VALID))

        # test
        cfg = read_config()

        # validation
        self.assertTrue(isinstance(cfg, Config))
        self.assertEqual(len(cfg), len(SCHEMA))
        self.assertEqual(sorted(cfg.keys()), sorted([s[0] for s in SCHEMA]))
コード例 #11
0
def pullINI(pFile):
    result = {}

    with codecs.open(pFile["file"], "r", "utf-8") as fp:
        ini = INIConfig(fp)

        for (section, entry) in pFile["strings"]:
            result["%s_%s" % (section, entry)] = ini[section][entry]

    return result
コード例 #12
0
ファイル: vcconfig.py プロジェクト: tirthatrb/videocache
    def read(self):
        config = INIConfig(open(self.config_file))
        vcconf = VideocacheConf()

        # Pick up options' values from videocache.conf or set default if they are
        # not defined in videocache.conf .
        for option in vcconf.iterkeys():
            if isinstance(getattr(config.main, option, None), Undefined):
                setattr(config.main, option,
                        getattr(vcconf, option).default_value)
        return config.main
コード例 #13
0
ファイル: freqwatch.py プロジェクト: tomasgeci/freqwatch
def main():
    config = INIConfig(open(CONF_FILE))

    scanners = config.rtl.scanners
    if type(scanners) != type(str()):
        print("No scanners defined in the configuration file.")
        sys.exit(ERR)

    if int(config.gps.gpsd) == 1 and gps_imported == True:
        gpsp = GpsPoller(config.gps.gpsd_ip, config.gps.gpsd_port)
        gpsp.start()
    else:
        gpsp = None

    threads = list()
    devs = list()
    for s in scanners.split(','):
        devid = s.strip()
        if len(devid) == 0:
            continue

        if int(devid) in devs:
            print("You already have assigned the device id {}".format(devid))
            sys.exit(ERR)
        devs.append(int(devid))

        a = 'config.rtl.scanner' + s
        tmp = eval(a)

        freqs, squelch, ppm = tmp.split('/')
        if freqs == None:
            print("Is there a frequency entry for each device you've " \
                    "specified in 'scanners' in the configuration?")
            sys.exit(ERR)

        freqs = freqs.strip()
        squelch = int(squelch.strip())
        ppm = ppm.strip()
        if len(ppm) == 0:
            ppm = 0
        else:
            ppm = int(ppm)

        clientid = config.rtl.clientid

        sworker = Scanner(devid, freqs, squelch, ppm, gpsp, config)
        thread = threading.Thread(target=sworker.worker)
        threads.append(thread)
        thread.start()

    for t in threads:
        t.join()
コード例 #14
0
def repo_status(repos, enable=True):
    for repo in sorted(repos):
        ini = INIConfig(open(repo.repofile))
        if enable:
            repo.enable()
            ini[section_id]['enabled'] = 1
        else:
            repo.disable()
            ini[section_id]['enabled'] = 0

        fp = file(filename, "w")
        fp.write(str(ini))
        fp.close()
コード例 #15
0
ファイル: ini.py プロジェクト: slide333333/translate
    def parse(self, input):
        """Parse the given file or file source string."""
        if hasattr(input, "name"):
            self.filename = input.name
        elif not getattr(self, "filename", ""):
            self.filename = ""
        if hasattr(input, "read"):
            inisrc = input.read()
            input.close()
            input = inisrc

        if isinstance(input, bytes):
            input = StringIO(input.decode("utf-8"))
            self._inifile = INIConfig(input, optionxformvalue=None)
        else:
            self._inifile = INIConfig(open(input), optionxformvalue=None)

        for section in self._inifile:
            for entry in self._inifile[section]:
                source = self._dialect.unescape(self._inifile[section][entry])
                newunit = self.addsourceunit(source)
                newunit.addlocation(f"[{section}]{entry}")
コード例 #16
0
def _set_releasever(cfg, log):
    """Set the yum releasever to the value of repo_releasever."""
    releasever = util.get_cfg_option_str(cfg, 'repo_releasever')
    if not releasever:
        log.info("No releasever provided, leaving yum.conf unchanged.")
        return
    log.info('Setting yum releasever to %s', releasever)

    statinfo = os.stat(YUMCONF)

    with open(YUMCONF) as conf:
        cfg = INIConfig(conf)
    cfg.main.releasever = releasever
    util.write_file(YUMCONF, str(cfg))
コード例 #17
0
def read_config():
    """
    Read the local configuration file & return an INIConfig object.

    :rtype: INIConfig
  """

    try:
        return INIConfig(open(INI_FILE))
    except IOError:

        print "Creating default config file at: {0}".format(INI_FILE)
        print "Please edit before continuing."

        config = INIConfig()
        config.brickset.username = '******'
        config.brickset.password = '******'
        config.brickset.papersize = DEFAULT_PAPER_SIZE
        config.download.path = '~/Documents/LEGO'

        with open(INI_FILE, 'w') as fh:
            print >> fh.write(str(config))

        sys.exit(1)
コード例 #18
0
    def __init__(self):
        """
        Initializes a RawConfigParser and reads the configuration file into the object
        """
        if Config.values:
            return

        Config.values = INIConfig()

        # Set the defaults
        Config.values.server.url = ''
        Config.values.server.username = ''
        Config.values.server.password = ''
        Config.values.export.directory = ''
        Config.values.export.outputformat = 'json'
        Config.values.activationkey.environment = 'Dev'
        Config.values.activationkey.includedisabled = 'False'
        Config.values.mapping.roles = ''
        Config.values.mapping.orgs = ''


        if os.path.exists(Config.USER):
            user_settings = INIConfig(open(Config.USER))
            update_config(Config.values, user_settings)
コード例 #19
0
def pushINI(pFile, pofiles):
    with codecs.open(pFile["file"], "r", "utf-8") as fp:
        ini = INIConfig(fp)

        for (section, entry) in pFile["strings"]:
            value = ini[section][entry]

            for lang, po in pofiles.iteritems():
                poEntry = po.find(value)

                if (poEntry and not "fuzzy" in poEntry.flags
                        and poEntry.msgstr != ""):
                    ini[section]["%s[%s]" % (entry, lang)] = poEntry.msgstr

        with codecs.open(pFile["file"], "w+b", "utf-8") as wFp:
            wFp.write(unicode(ini))
コード例 #20
0
ファイル: main.py プロジェクト: joelspadin/OperaSkinTweak
	def run(self):
		#remove the working directory if it exists
		if os.path.isdir(self.working):
			self.update_msg('Cleaning...')
			shutil.rmtree(self.working, True)
		
		if not self.custom_skin:
			self.update_msg('Extracting...')
			self.extract()
		
			#open and parse the skin's skin.ini
			self.update_msg('Opening skin.ini...')
			ini = INIConfig(open(self.working + 'skin.ini'))
		else:
			ini = self.setup_custom_skin()
		
		current_name = str(ini.Info.Name)
		ini.Info.Name = self.skin_name.get().replace(self.original_skin_tag, current_name);
		
		#run the selected tweaks
		list = self.tweak_list.curselection()
		for i in list:
			tweak = self.tweak_modules[int(i)]
			print 'Running ' + tweak.name
			self.update_msg('Running ' + tweak.name + '...')
			tweak.run(self.tweaks, self.working, ini)
		
		#save skin.ini
		self.update_msg('Saving skin.ini...')
		f = open(self.working + 'skin.ini', 'w')
		print >>f, ini
		f.close()
		
		self.update_msg('Compressing...')
		self.compress()
			
		#move the temporary skin.zip to the output location
		if os.path.realpath(self.tempskin) != os.path.realpath(self.output.get()):
			self.update_msg('Moving to output file...')
			if os.path.isfile(self.output.get()):
				os.remove(self.output.get())
			os.rename(os.path.realpath(self.tempskin), os.path.realpath(self.output.get()))
		
		#remove the working directory
		self.update_msg('Cleaning...')
		shutil.rmtree(self.working, True)
		self.update_msg('Done.')
コード例 #21
0
ファイル: test_config.py プロジェクト: aweiteka/pulp
    def test_explicit_paths(self, mock_config, mock_validate, mock_open):
        mock_config.return_value = INIConfig()

        mock_fp = Mock()
        mock_fp.__enter__ = Mock(return_value=mock_fp)
        mock_fp.__exit__ = Mock()
        mock_open.return_value = mock_fp

        paths = ['/tmp/a.conf', '/tmp/b.conf']

        # test
        read_config(paths, validate=False)

        # validation

        mock_open.assert_any(paths[0])
        mock_open.assert_any(paths[1])
        self.assertFalse(mock_validate.called)
コード例 #22
0
ファイル: config.py プロジェクト: rossonet/Strumenti-RCloud
def writeRawRepoFile(repo, only=None):
    """
    Writes changes in a repo object back to a .repo file.
    @param repo: Repo Object
    @param only: List of attributes to work on (None = All)
    It work by reading the repo file, changes the values there shall be changed and write it back to disk.
    """
    ini = INIConfig(open(repo.repofile))
    # Updated the ConfigParser with the changed values
    cfgOptions = repo.cfg.options(repo.id)
    for name, value in repo.iteritems():
        option = repo.optionobj(name)
        if option.default != value or name in cfgOptions:
            if only == None or name in only:
                ini[repo.id][name] = option.tostring(value)
    fp = file(repo.repofile, "w")
    fp.write(str(ini))
    fp.close()
コード例 #23
0
ファイル: config.py プロジェクト: zhunzhun1988/mydocker
def _writeRawConfigFile(filename,
                        section_id,
                        yumvar,
                        cfgoptions,
                        items,
                        optionobj,
                        only=None):
    """
    From writeRawRepoFile, but so we can alter [main] too.
    """
    ini = INIConfig(open(filename))

    osection_id = section_id
    # b/c repoids can have $values in them we need to map both ways to figure
    # out which one is which
    if section_id not in ini._sections:
        for sect in ini._sections.keys():
            if varReplace(sect, yumvar) == section_id:
                section_id = sect

    # Updated the ConfigParser with the changed values
    cfgOptions = cfgoptions(osection_id)
    for name, value in items():
        if value is None:  # Proxy
            continue

        if only is not None and name not in only:
            continue

        option = optionobj(name)
        ovalue = option.tostring(value)
        #  If the value is the same, but just interpreted ... when we don't want
        # to keep the interpreted values.
        if (name in ini[section_id]
                and ovalue == varReplace(ini[section_id][name], yumvar)):
            ovalue = ini[section_id][name]

        if name not in cfgOptions and option.default == value:
            continue

        ini[section_id][name] = ovalue
    fp = file(filename, "w")
    fp.write(str(ini))
    fp.close()
コード例 #24
0
def extendMemberAdaptor(list):
    from iniparse import INIConfig
    f = popen('forge_get_config config_path')
    path = f.read().strip()
    cfg = INIConfig(open(path + '/config.ini.d/mailman.ini'))
    dbparam = {}
    #Config to connect to database
    dbparam['dbhost'] = cfg['mailman']['dbhost']
    dbparam['dbuser'] = cfg['mailman']['dbuser']
    dbparam['dbpassword'] = cfg['mailman']['dbpassword']
    dbparam['database'] = cfg['mailman']['database']
    dbparam['refresh'] = 360

    #table where mailman stores memeberships info
    dbparam['mailman_table'] = 'plugin_mailman'

    ######################
    # Session Management #
    ######################
    #Forge default session
    dbparam['cookiename'] = 'session_ser'
    dbparam[
        'queryCookieMail'] = "SELECT email FROM user_session,users WHERE users.user_id=user_session.user_id AND session_hash = substring('%s','.*-%%2A-(.*)');"
    dbparam[
        'queryCookieId'] = "SELECT user_id FROM user_session WHERE session_hash = substring('%s','.*-%%2A-(.*)');"

    dbparam[
        'queryIsAdmin'] = "SELECT COUNT(*) FROM mail_group_list WHERE list_admin=%s AND list_name='%s';"
    dbparam['queryIsMonitoring'] = "SELECT COUNT(*) FROM " + dbparam[
        'mailman_table'] + ", users " + " WHERE users.email = " + dbparam[
            'mailman_table'] + ".address" + " AND users.user_id=%s AND listname='%s';"
    dbparam[
        'queryIsSiteAdmin'] = "SELECT count(*) AS count FROM user_group WHERE user_id=%s AND group_id=1 AND admin_flags='A';"

    #Forge ZendSession
    #dbparam['cookiename']='zend_cookie_session'
    #dbparam['queryCookieMail']="""select substring(session_data,'email";s:[0-9]*?:"(.*)";s') from plugin_zendsession where session_hash='%s';"""
    #dbparam['queryCookieId']="""SELECT substring(session_data,'user_id";i:([0-9]{1,})') FROM plugin_zendsession WHERE session_hash='%s';"""

    ######################
    # Type of connection #
    ######################
    db = PsycopgConnector(list, dbparam)
    list._memberadaptor = db
def pretty_format_ini(argv: typing.Optional[typing.List[str]] = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--autofix",
        action="store_true",
        dest="autofix",
        help="Automatically fixes encountered not-pretty-formatted files",
    )

    parser.add_argument("filenames", nargs="*", help="Filenames to fix")
    args = parser.parse_args(argv)

    status = 0

    for ini_file in set(args.filenames):
        with open(ini_file) as input_file:
            string_content = "".join(input_file.readlines())

        try:
            # INIConfig only supports strict mode for throwing errors
            config_parser = ConfigParser()
            config_parser.read_string(string_content)

            ini_config = INIConfig(io.StringIO(str(string_content)),
                                   parse_exc=False)

            pretty_content_str = remove_trailing_whitespaces_and_set_new_line_ending(
                str(ini_config), )

            if string_content != pretty_content_str:
                print("File {} is not pretty-formatted".format(ini_file))

                if args.autofix:
                    print("Fixing file {}".format(ini_file))
                    with io.open(ini_file, "w",
                                 encoding="UTF-8") as output_file:
                        output_file.write(str(pretty_content_str))

                status = 1
        except Error:
            print("Input File {} is not a valid INI file".format(ini_file))
            return 1

    return status
コード例 #26
0
ファイル: test_config.py プロジェクト: aweiteka/pulp
    def test_default_paths(self, mock_config, mock_validate, mock_open, *unused):
        mock_config.return_value = INIConfig()

        mock_fp = Mock()
        mock_fp.__enter__ = Mock(return_value=mock_fp)
        mock_fp.__exit__ = Mock()
        mock_open.return_value = mock_fp

        # test
        read_config(validate=False)

        # validation
        paths = [
            '/etc/pulp/consumer/consumer.conf',
            os.path.expanduser('~/.pulp/consumer.conf')
        ]
        mock_open.assert_any(paths[0])
        mock_open.assert_any(paths[1])
        self.assertFalse(mock_validate.called)
コード例 #27
0
def writeRawRepoFile(repo, only=None):
    """Write changes in a repo object back to a .repo file.

    :param repo: the Repo Object to write back out
    :param only: list of attributes to work on. If *only* is None, all
       options will be written out
    """
    if not _use_iniparse:
        return

    ini = INIConfig(open(repo.repofile))
    # b/c repoids can have $values in them we need to map both ways to figure
    # out which one is which
    section_id = repo.id
    if repo.id not in ini._sections:
        for sect in ini._sections.keys():
            if varReplace(sect, repo.yumvar) == repo.id:
                section_id = sect

    # Updated the ConfigParser with the changed values
    cfgOptions = repo.cfg.options(repo.id)
    for name, value in repo.iteritems():
        if value is None:  # Proxy
            continue

        if only is not None and name not in only:
            continue

        option = repo.optionobj(name)
        ovalue = option.tostring(value)
        #  If the value is the same, but just interpreted ... when we don't want
        # to keep the interpreted values.
        if (name in ini[section_id]
                and ovalue == varReplace(ini[section_id][name], repo.yumvar)):
            ovalue = ini[section_id][name]

        if name not in cfgOptions and option.default == value:
            continue

        ini[section_id][name] = ovalue
    fp = file(repo.repofile, "w")
    fp.write(str(ini))
    fp.close()
コード例 #28
0
ファイル: ds-backup.py プロジェクト: XSCE/ds-backup
def get_backup_url():

    bu = gconf_get_string('/desktop/sugar/backup_url')
    if bu:
        return bu
    try:  # pre-gconf
        from iniparse import INIConfig
        conf = INIConfig(
            open(os.path.expanduser('~') + '/.sugar/default/config'))
        # this access mode throws an exception if the value
        # does not exist
        bu = conf['Server']['backup1']
    except:
        pass
    if bu:
        return bu
    bu = identifier_get_string('backup_url')
    if bu:
        return bu
    return ''
コード例 #29
0
def _readRawRepoFile(repo):
    if not _use_iniparse:
        return None

    if not hasattr(repo, 'repofile') or not repo.repofile:
        return None

    try:
        ini = INIConfig(open(repo.repofile))
    except:
        return None

    # b/c repoids can have $values in them we need to map both ways to figure
    # out which one is which
    section_id = repo.id
    if repo.id not in ini._sections:
        for sect in ini._sections.keys():
            if varReplace(sect, repo.yumvar) == repo.id:
                section_id = sect
    return ini, section_id
コード例 #30
0
ファイル: ini.py プロジェクト: Cloudhedge/staticBuilds
    def parse(self, content):
        content = '\n'.join(filter(None, [x.strip() for x in content.splitlines()]))
        if self.sectionless:
            content = '[' + self.nullsection + ']\n' + content
        data = StringIO(content)
        cp = INIConfig(data, optionxformvalue=lambda x: x)

        root = RootNode()
        for section in cp:
            name = section
            if self.sectionless and section == self.nullsection:
                name = None
            section_node = Node(name)
            section_node.comment = self._get_comment(cp[section]._lines[0])
            for option in cp[section]:
                if option in cp[section]._options:
                    node = PropertyNode(option, cp[section][option])
                    node.comment = self._get_comment(cp[section]._options[option])
                    section_node.children.append(node)
            root.children.append(section_node)
        return root