def __init__(self): path = '/etc/pulp/consumer/consumer.conf' fp = open(path) try: INIConfig.__init__(self, fp) finally: fp.close()
def SaveTuningVariables(): """Saves the current tuning variables back to their .ini file.""" if not (os.path.exists(TUNING_FILE_PATH)): raise IOError, "Couldn't find " + TUNING_FILE_PATH tuning_file = open(TUNING_FILE_PATH, "r") config = INIConfig(tuning_file) tuning_file.close() for var_name in theTuning.GetVariables(): if var_name not in config: # add the variable (this syntax is a workaround for a bug in # the iniparse library) config._new_namespace(var_name) # Turn strings into proper Python values var = theTuning.GetString(var_name) try: val = eval(var) except: val = var if isinstance(val, list): config[var_name].type = "vector" elif isinstance(val, float): config[var_name].type = "float" elif isinstance(val, int): config[var_name].type = "int" else: config[var_name].type = "string" config[var_name].value = val else: truth = ["true", "1", "one", "yes", "on", "t"] if str(config[var_name].readonly.split()[0]).lower() in truth: continue config[var_name].value = theTuning.GetString(var_name) tuning_file = open(TUNING_FILE_PATH, "w") print >>tuning_file, config tuning_file.close()
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))
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)
def __init__(self, path): """ @param path: The path to an INI file. @type path: str """ fp = Reader(path) try: INIConfig.__init__(self, fp) finally: fp.close()
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()
def cfg(self): conf = INIConfig() conf.heartbeat.seconds = 0 conf.rest.host = '' conf.rest.port = 0 conf.rest.clientcert = '' return conf
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))
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
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])
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)
def load(self, conf_file=None, template=None, validate_against_template=True): if template is not None: self.template_map = INIConfig(open(template)) if conf_file and os.path.exists(conf_file): self.conf_map = INIConfig(open(conf_file)) if validate_against_template: self.validate()
def __init__(self, conf_file=None, template=None, exclude={}, validate_against_template=True): self.conf_file = conf_file self.template_file = template self.validate_against_template = validate_against_template self.exclude = exclude self.conf_map = INIConfig() self.template_map = INIConfig() self.load(conf_file, template, validate_against_template)
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]))
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)
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
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
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()
def stringify(self, tree): cp = INIConfig() for section in tree.children: if self.sectionless and section.name is None: sectionname = self.nullsection else: sectionname = section.name cp._new_namespace(sectionname) for option in section.children: if not isinstance(option, PropertyNode): raise TypeError('Third level nodes should be PropertyNodes') cp[sectionname][option.name] = option.value if option.comment: self._set_comment(cp[sectionname]._options[option.name], option.comment) if hasattr(cp[sectionname], '_lines'): self._set_comment(cp[sectionname]._lines[0], section.comment) data = str(cp) + '\n' if self.sectionless: data = data.replace('[' + self.nullsection + ']\n', '') return data
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}")
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()
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))
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)
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)
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))
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.')
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()
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)
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
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 _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()
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()
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)
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 ''
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
def process(self, output_dir, project_name, params): """.""" eggsnames = [] devnames = [] src_dir = os.path.join(output_dir, 'src') for path in os.listdir(src_dir): p = os.path.join(src_dir, path) if os.path.isdir(p): if os.path.exists(os.path.join(p, 'setup.py')): eggsnames.append(path) devnames.append(os.path.join('src', path)) zcmlnames = [n for n in eggsnames if (('policy' in n) or (n==project_name) or (n=='%s.core' % project_name) )] f = os.path.join(output_dir, 'buildout.cfg') pf = os.path.join(output_dir, 'etc', 'project', '%s.cfg' % project_name ) if os.path.exists(pf): f = pf else: for i in ['django', 'plone', 'pyramid']: pf = os.path.join(output_dir, 'etc', 'project', '%s.cfg' % i ) if os.path.exists(pf): f = pf cfg = INIConfig(open(f)) extdevoption_re = re.compile('develop\s*\+\s*', re_flags) devoption_re = re.compile('develop\s*', re_flags) exteggsoption_re = re.compile('eggs\s*\+\s*', re_flags) eggsoption_re = re.compile('eggs\s*', re_flags) eggsoption_re = re.compile('eggs\s*', re_flags) devoption, eggsoption= 'develop+', 'eggs+' devfound, eggsfound = False, False for optionre in [extdevoption_re, devoption_re, ]: if 'buildout' in cfg: for option in cfg.buildout: if optionre.match(option): devfound = True devoption = option break for optionre in [exteggsoption_re, eggsoption_re, ]: for option in cfg.buildout: if optionre.match(option): eggsfound = True eggsoption = option break if eggsfound: for eggn in eggsnames: if not (eggn in cfg.buildout[eggsoption]): cfg.buildout[eggsoption] = '%s \n%s' % ( eggn, cfg.buildout[eggsoption].strip() ) else: cfg.buildout[eggsoption] = '' for eggn in eggsnames: cfg.buildout[eggsoption] = '%s \n%s' % ( eggn, cfg.buildout[eggsoption].strip() ) if devfound: for eggn in devnames: if not (eggn in cfg.buildout[devoption]): cfg.buildout[devoption] = '%s \n%s' % ( eggn, cfg.buildout[devoption].strip() ) else: cfg.buildout[devoption] = '' for eggn in devnames: cfg.buildout[devoption] = '%s \n%s' % ( eggn, cfg.buildout[devoption].strip() ) # zcml are now handled via collective.generic.skel extzcmloption_re = re.compile('zcml\s*\+\s*', re_flags) zcmloption_re = re.compile('zcml\s*', re_flags) zcmlfound = False for optionre in [extzcmloption_re, zcmloption_re, ]: if 'buildout' in cfg: for option in cfg.buildout: if optionre.match(option): zcmlfound = True zcmloption = option break if zcmlfound: for eggn in zcmlnames: if 'buildout' in cfg: if not (eggn in cfg.buildout[zcmloption]): if (('policy' in eggn ) or ('tma' in eggn) or ('.core' in eggn) or (project_name == eggn) ): cfg.buildout[zcmloption] = '%s \n %s' % ( cfg.buildout[zcmloption].strip(), eggn, ) else: zcmloption = '' for eggn in zcmlnames: if 'instance' in cfg: if 'policy' in eggn: cfg.buildout[zcmloption] = '%s \n%s' % ( eggn, cfg.buildout[zcmloption].strip() ) f = open(f, 'w') cfg = '%s'%cfg cfg = cfg.replace('+ =', ' +=') f.write(cfg) f.close()
class UWSAConfiguration(): def __init__(self, conf_file=None, template=None, exclude={}, validate_against_template=True): self.conf_file = conf_file self.template_file = template self.validate_against_template = validate_against_template self.exclude = exclude self.conf_map = INIConfig() self.template_map = INIConfig() self.load(conf_file, template, validate_against_template) def load(self, conf_file=None, template=None, validate_against_template=True): if template is not None: self.template_map = INIConfig(open(template)) if conf_file and os.path.exists(conf_file): self.conf_map = INIConfig(open(conf_file)) if validate_against_template: self.validate() def validate(self): for section in self.template_map: for key in self.template_map[section]: if section not in self.conf_map: self.conf_map._new_namespace(section) cmap = self.conf_map[section] if key not in cmap: cmap[key] = self.template_map[section][key] def merge_exclude(self, new_dict): for section in new_dict: for key in new_dict[section]: if section not in self.exclude: self.exclude[section] = [] self.exclude[section].append(key) def get(self, section=None, key=None): value = None if section is None and key is None: value = self.conf_map else: if self.is_set(section, key): value = self.conf_map[section][key] if value.__class__ == str: if value.lower() in ['true','false']: value = True if value.lower() == 'true' else False elif value.lower() == "none": value = None return value def get_section(self, section): value = None if self.is_set(section): value = self.conf_map[section] return value def mod(self, section, key, value, override=True): self.set(section, key, value, override) def set(self, section, key, value, override=True): if section not in self.conf_map: self.conf_map._new_namespace(section) cmap = self.conf_map[section] if (key in cmap and override) or key not in cmap: cmap[key] = value def is_set(self, *value): is_set = True c = self.conf_map for p in value: if c[p].__class__ == iniparse.config.Undefined: is_set = False break c = c[p] return is_set def save(self): self.save_to_file(self.conf_file) def save_to_file(self, filename): path = os.path.dirname(filename) if os.path.exists(path): import copy conf_copy = copy.deepcopy(self.conf_map) for section in self.exclude: if section in conf_copy: for key in self.exclude[section]: if key in conf_copy[section]: conf_copy[section][key] = None del conf_copy[section][key] with open(filename, 'w+') as f: f.write(str(conf_copy))
def __init__(self, path=MAIN): fp = open(path) try: INIConfig.__init__(self, fp) finally: fp.close()
class test_tidy(unittest.TestCase): def setUp(self): self.cfg = INIConfig() def test_empty_file(self): self.assertEqual(str(self.cfg), '') tidy(self.cfg) self.assertEqual(str(self.cfg), '') def test_last_line(self): self.cfg.newsection.newproperty = "Ok" self.assertEqual(str(self.cfg), dedent("""\ [newsection] newproperty = Ok""")) tidy(self.cfg) self.assertEqual(str(self.cfg), dedent("""\ [newsection] newproperty = Ok """)) def test_first_line(self): s = dedent("""\ [newsection] newproperty = Ok """) self.cfg._readfp(StringIO(s)) tidy(self.cfg) self.assertEqual(str(self.cfg), dedent("""\ [newsection] newproperty = Ok """)) def test_remove_newlines(self): s = dedent("""\ [newsection] newproperty = Ok [newsection2] newproperty2 = Ok newproperty3 = yup [newsection4] # remove blank lines, but leave continuation lines unharmed a = 1 b = l1 l2 # asdf l5 c = 2 """) self.cfg._readfp(StringIO(s)) tidy(self.cfg) self.assertEqual(str(self.cfg), dedent("""\ [newsection] newproperty = Ok [newsection2] newproperty2 = Ok newproperty3 = yup [newsection4] # remove blank lines, but leave continuation lines unharmed a = 1 b = l1 l2 # asdf l5 c = 2 """)) def test_compat(self): s = dedent(""" [sec1] a=1 [sec2] b=2 c=3 """) cfg = ConfigParser() cfg.readfp(StringIO(s)) tidy(cfg) self.assertEqual(str(cfg.data), dedent("""\ [sec1] a=1 [sec2] b=2 c=3 """))
def setUp(self): self.cfg = INIConfig()
def process(self, output_dir, project_name, params): """.""" eggsnames = [] devnames = [] src_dir = os.path.join(output_dir, 'src') for path in os.listdir(src_dir): p = os.path.join(src_dir, path) if os.path.isdir(p): if os.path.exists(os.path.join(p, 'setup.py')): eggsnames.append(path) devnames.append(os.path.join('src', path)) zcmlnames = [n for n in eggsnames if ( ('policy' in n) or (n == project_name) or (n == '%s.core' % project_name) )] f = os.path.join(output_dir, 'buildout.cfg') sf = os.path.join( output_dir, 'etc', 'project', 'sources.cfg') pf = os.path.join( output_dir, 'etc', 'project', '%s.cfg' % project_name) if os.path.exists(pf): f = pf else: for i in ['django', 'plone', 'pyramid']: pf = os.path.join( output_dir, 'etc', 'project', '%s.cfg' % i) if os.path.exists(pf): f = pf cfg = INIConfig(open(f)) srccfg = None if os.path.exists(sf): srccfg = INIConfig(open(sf)) else: srccfg = cfg extdevoption_re = re.compile('develop\s*\+\s*', re_flags) devoption_re = re.compile('develop\s*', re_flags) autocheckout_option_re = re.compile( 'auto-checkout\s*', re_flags) ext_autocheckout_option_re = re.compile( 'auto-checkout\s*\+\s*', re_flags) exteggsoption_re = re.compile('eggs\s*\+\s*', re_flags) eggsoption_re = re.compile('eggs\s*', re_flags) eggsoption_re = re.compile('eggs\s*', re_flags) devoption, eggsoption = 'develop+', 'eggs+' devfound, eggsfound, acfound = False, False, False for optionre in [ext_autocheckout_option_re, autocheckout_option_re, ]: if 'buildout' in cfg: for option in cfg.buildout: if optionre.match(option): acfound = True autocheckout = option break for optionre in [extdevoption_re, devoption_re, ]: if 'buildout' in cfg: for option in cfg.buildout: if optionre.match(option): devfound = True devoption = option break for optionre in [exteggsoption_re, eggsoption_re, ]: for option in cfg.buildout: if optionre.match(option): eggsfound = True eggsoption = option break if eggsfound: for eggn in eggsnames: if not (eggn in cfg.buildout[eggsoption]): cfg.buildout[eggsoption] = '\n '.join( [a for a in eggn, cfg.buildout[eggsoption].strip() if a.strip()]) else: cfg.buildout[eggsoption] = '' for eggn in eggsnames: cfg.buildout[eggsoption] = '\n '.join( [a for a in eggn, cfg.buildout[eggsoption].strip() if a.strip()]) if srccfg: if acfound: for eggn in eggsnames: if 'sources-dir' in cfg.buildout: del cfg.buildout['sources-dir'] if not (eggn in cfg.buildout[autocheckout]): cfg.buildout[autocheckout] = '\n '.join( [a for a in eggn, cfg.buildout[autocheckout].strip() if a.strip()]) if not (eggn in cfg.sources): cfg.sources[eggn] = 'fs %s' % (eggn,) cfg.sources._lines[0].contents.insert( 1, cfg.sources._lines[0].contents.pop(-1)) else: if devfound: for eggn in devnames: if not (eggn in cfg.buildout[devoption]): cfg.buildout[devoption] = '\n '.join( [a for a in eggn, cfg.buildout[devoption].strip() if a.strip()]) else: cfg.buildout[devoption] = '' for eggn in devnames: cfg.buildout[devoption] = '\n '.join( [a for a in eggn, cfg.buildout[devoption].strip() if a.strip()]) # zcml are now handled via collective.generic.skel extzcmloption_re = re.compile('zcml\s*\+\s*', re_flags) zcmloption_re = re.compile('zcml\s*', re_flags) zcmlfound = False for optionre in [extzcmloption_re, zcmloption_re, ]: if 'buildout' in cfg: for option in cfg.buildout: if optionre.match(option): zcmlfound = True zcmloption = option break if zcmlfound: for eggn in zcmlnames: if 'buildout' in cfg: if not (eggn in cfg.buildout[zcmloption]): if ( ('policy' in eggn) or ('tma' in eggn) or ('.core' in eggn) or (project_name == eggn) ): cfg.buildout[zcmloption] = '\n '.join([ a for a in cfg.buildout[zcmloption].strip(), eggn if a.strip()]) else: for eggn in zcmlnames: if 'instance' in cfg: if 'policy' in eggn: oldzcml = '' if 'zcml' in cfg.buildout: oldzcml = cfg.buildout['zcml'].strip() cfg.buildout['zcml'] = '\n ' % ( [a for a in eggn, oldzcml if a.strip()]) f = open(f, 'w') cfg = '%s' % cfg cfg = cfg.replace('+ =', ' +=') f.write(cfg) f.close()