class VarEnv(object): """ Allow to load the available variables in a file """ def __init__(self, file): """ 'file' to read """ self._cp = SafeConfigParser() self._cp.read(file) def defaults(self): """ Return a dictionary containing the instance-wide defaults. """ return self._cp.defaults() def has_section(self, section): """ Indicate whether the named section is present in the configuration. """ return self._cp.has_section(section) def items(self, section): """ Return a list of tuples with (name, value) for each option in the section. """ return self._cp.items(section) def sections(self): """ Return a list of section names, excluding [DEFAULT] section. """ return self._cp.sections() def write(self, dest_file): """ Write an ini-format representation of the configuration """ self._cp.write(dest_file) def set_var(self, section, name, value): """ Set an option """ self._cp.set(section, option, value) def get_var(self, var_name, default='', section='DEFAULT'): """ Get a value for given section. The default section will be 'DEFAULT'. """ try: value = dict(self._cp.items(section))[var_name.lower()] if value.startswith('"') and value.endswith('"'): value = value[1:-1] return value except (KeyError, IOError): return default
def _loadini(self, baseini, defaultsett): """Parse master ini configuration file ``baseini`` and ini files refered by `baseini`. Construct a dictionary of settings for special sections and plugin sections.""" from pluggdapps.plugin import pluginnames, plugin_info if not baseini or (not isfile(baseini)): return deepcopy(defaultsett) # Initialize return dictionary. settings = {} # context for parsing ini files. _vars = {"here": abspath(dirname(baseini))} # Read master ini file. cp = SafeConfigParser() cp.read(baseini) # [DEFAULT] overriding global def. s = deepcopy(defaultsett["DEFAULT"]) s.update(dict(cp.defaults())) settings["DEFAULT"] = normalize_defaults(s) # [pluggdapps] s = deepcopy(defaultsett["pluggdapps"]) if cp.has_section("pluggdapps"): s.update(dict(cp.items("pluggdapps", vars=_vars))) s.pop("here", None) # TODO : how `here` gets populated ?? settings["pluggdapps"] = normalize_pluggdapps(s) # Override plugin's package default settings with [DEFAULT] settings. for pluginsec, sett in defaultsett.items(): if not pluginsec.startswith("plugin:"): continue sett = h.mergedict(sett, settings["DEFAULT"]) if cp.has_section(pluginsec): sett.update(dict(cp.items(pluginsec, vars=_vars))) sett.pop("here", None) # TODO : how `here` ?? cls = plugin_info(h.sec2plugin(pluginsec))["cls"] for b in reversed(cls.mro()): if hasattr(b, "normalize_settings"): sett = b.normalize_settings(sett) settings[pluginsec] = sett return settings
def _loadini(self, baseini, defaultsett): """Parse master ini configuration file ``baseini`` and ini files refered by `baseini`. Construct a dictionary of settings for special sections and plugin sections.""" from pluggdapps.plugin import pluginnames, plugin_info if not baseini or (not isfile(baseini)): return deepcopy(defaultsett) # Initialize return dictionary. settings = {} # context for parsing ini files. _vars = {'here': abspath(dirname(baseini))} # Read master ini file. cp = SafeConfigParser() cp.read(baseini) # [DEFAULT] overriding global def. s = deepcopy(defaultsett['DEFAULT']) s.update(dict(cp.defaults())) settings['DEFAULT'] = normalize_defaults(s) # [pluggdapps] s = deepcopy(defaultsett['pluggdapps']) if cp.has_section('pluggdapps'): s.update(dict(cp.items('pluggdapps', vars=_vars))) s.pop('here', None) # TODO : how `here` gets populated ?? settings['pluggdapps'] = normalize_pluggdapps(s) # Override plugin's package default settings with [DEFAULT] settings. for pluginsec, sett in defaultsett.items(): if not pluginsec.startswith('plugin:'): continue sett = h.mergedict(sett, settings['DEFAULT']) if cp.has_section(pluginsec): sett.update(dict(cp.items(pluginsec, vars=_vars))) sett.pop('here', None) # TODO : how `here` ?? cls = plugin_info(h.sec2plugin(pluginsec))['cls'] for b in reversed(cls.mro()): if hasattr(b, 'normalize_settings'): sett = b.normalize_settings(sett) settings[pluginsec] = sett return settings
def _loadinstance(self, appsett, instanceini): """Load configuration settings for a web application's instance.""" from pluggdapps.plugin import plugin_info _vars = {'here': abspath(dirname(instanceini))} cp = SafeConfigParser() cp.read(instanceini) # Update appsett with [DEFAULT] section of instanceini defaultsett = normalize_defaults(dict(cp.defaults())) appsett['DEFAULT'].update(defaultsett) [sett.update(appsett['DEFAULT']) for key, sett in appsett.items()] # Update plugin sections in appsett from instanceini for sec in cp.sections(): if not sec.startswith('plugin:'): continue sett = dict(cp.items(sec, vars=_vars)) sett.pop('here', None) # TODO : how `here` gets populated ?? appsett[sec].update(sett) cls = plugin_info(h.sec2plugin(sec))['cls'] for b in reversed(cls.mro()): if hasattr(b, 'normalize_settings'): appsett[sec] = b.normalize_settings(appsett[sec])
def _loadinstance(self, appsett, instanceini): """Load configuration settings for a web application's instance.""" from pluggdapps.plugin import plugin_info _vars = {"here": abspath(dirname(instanceini))} cp = SafeConfigParser() cp.read(instanceini) # Update appsett with [DEFAULT] section of instanceini defaultsett = normalize_defaults(dict(cp.defaults())) appsett["DEFAULT"].update(defaultsett) [sett.update(appsett["DEFAULT"]) for key, sett in appsett.items()] # Update plugin sections in appsett from instanceini for sec in cp.sections(): if not sec.startswith("plugin:"): continue sett = dict(cp.items(sec, vars=_vars)) sett.pop("here", None) # TODO : how `here` gets populated ?? appsett[sec].update(sett) cls = plugin_info(h.sec2plugin(sec))["cls"] for b in reversed(cls.mro()): if hasattr(b, "normalize_settings"): appsett[sec] = b.normalize_settings(appsett[sec])
def merge_configs(cfgs, forprint=True, raw=True): """Merge configurations Options of the last config overrides options of the other ones, except for config file names (values then end with .ini or .cfg) that are store as a tuple. .. warning:: This makes output config incompatible with :meth:`~ConfigParser.ConfigParser.getint`, etc. """ # Init if not isinstance(cfgs, (list, tuple)): cfgs = [cfgs] cfg = SafeConfigParser() # Loop on configs kwget = get_dir_dict() kwget['mod_dir'] = '%%(mod_dir)s' pending = {} for cfgi in cfgs: # print '-'*50 # S = StringIO.StringIO() # cfgi.write(S) # s = S.getvalue() # S.close() # print s if not isinstance(cfgi, SafeConfigParser): cfgfiles = cfgi cfgi = SafeConfigParser() cfgi.read(cfgfiles) cfg._defaults.update(cfgi.defaults()) for section in cfgi.sections(): if section not in cfg.sections(): cfg.add_section(section) pending.setdefault(section, {}) for option, rvali in cfgi.items(section, raw=raw): if option in pending[ section]: # already identified as an option of config files if rvali not in pending[section][option]: pending[section][option] = tuple( pending[section][option]) + (rvali, ) elif _re_cfgfile(cfgi.get(section, option, vars=kwget)): pending[section][option] = rvali, else: cfg.set(section, option, rvali) # Convert pending options for section, options in list(pending.items()): for option, value in list(options.items()): if forprint: if len(value) == 1: value = value[0] else: value = '(%s)' % ', '.join(value) else: value = '(%s)' % '|'.join(value) # print section, option, value cfg.set(section, option, value) return cfg
if len(args.secret) == 0 : print("Enter valid text and try again") print("ABORTING") sys.exit(-1) if len(args.key) == 0: print("Enter valid key/passphrase") print("ABORTING") sys.exit(-1) conf_file = SafeConfigParser() conf_file.read("/etc/hpedockerplugin/hpe.conf") CONF = conf_file.defaults() if len(CONF) == 0: print("please Check the hpe.conf file on /etc/hpedockerplugin/ path") sys.exit(-1) host_etcd_ip_address = CONF.get('host_etcd_ip_address') host_etcd_port_number = int(CONF.get('host_etcd_port_number')) host_etcd_client_cert = CONF.get('host_etcd_client_cert') host_etcd_client_key = CONF.get('host_etcd_client_key') if host_etcd_ip_address == None or host_etcd_port_number == None: print("Please check hpe.conf for host_etcd_ip_address or host_etcd_port_number") sys.exit(-1)
class ConfigSection(object): """ Wraps SafeConfigParser with static section handling :param defaults: dict-like containing default keys/values :param section: name of section to initially bind to :note: Not an exact interface reproduction, some functionality left out! """ def __init__(self, defaults, section): self._section = section # SafeConfigParser is old-style, and we're changing method parameters self._scp = SafeConfigParser(defaults) self._scp.add_section(self._section) def defaults(self): """ Returns dictionary of default options """ return self._scp.defaults() def sections(self): """ Returns a list containing this instances section-name """ return [self._section] def add_section(self, section): """ Not written, do not use! :raises NotImplementedError: DO NOT USE! """ raise NotImplementedError() def has_section(self, section): """ Returns True if instance-section == ``section`` :param section: Name of section to check. :returns: True/False if section exists. """ return section == self._section def options(self): """ Returns dictionary of all options keys/values """ return self._scp.options(self._section) def has_option(self, option): """ Returns True if key-named ``option`` exists :param option: Name of the option (key) to check. :returns: True/False if option (key) exists. """ return self._scp.has_option(self._section, option) # Private method doesn't need docstring def _prune_sections(self): # pylint: disable=C0111 for section in self._scp.sections(): if section != self._section: self._scp.remove_section(section) def read(self, filenames): """ Replace current contents with content from filename(s)/list :param filenames: Same as for ``SafeConfigParser.read`` method :return: List of successfully parsed filenames. """ result = self._scp.read(filenames) # Changes self._scp self._prune_sections() return result # Short name 'fp' mirrors use in ConfigParser module def readfp(self, fp, filename=None): # pylint: disable=C0103 """ Replace current contents with content from file :param fp: Same as for ``SafeConfigParser.readfp`` method :param filename: Same as for ``SafeConfigParser.readfp`` method :return: Same as for ``SafeConfigParser.readfp`` method """ result = self._scp.readfp(fp, filename) # Changes self._scp self._prune_sections() return result def get(self, option): """ Return value assigned to key named ``option`` :param option: Name of the ``option`` (key) to check. :returns: The value assigned to ``option`` """ return self._scp.get(self._section, option) def getint(self, option): """ Convert/Return value assigned to key named ``option`` :param option: Name of the ``option`` (key) to check. :return: Value assigned to ``option`` converted to an integer. """ return self._scp.getint(self._section, option) def getfloat(self, option): """ Convert/Return value assigned to key named ``option`` :param option: Name of the ``option`` (key) to check. :return: Value assigned to ``option`` converted to a float. """ return self._scp.getfloat(self._section, option) def getboolean(self, option): """ Convert/Return value assigned to key named ``option`` :param option: Name of the ``option`` (key) to check. :return: ``True``: if value is ``yes``, ``true``. ``False`` if ``no`` or ``false``. """ try: value = self._scp.get(self._section, option).lower().strip() positives = ("yes", "true") negatives = ("no", "false") if value in positives: return True if value in negatives: return False # try regular way except AttributeError: pass # try regular way return self._scp.getboolean(self._section, option) def set(self, option, value): """ Set value assigned to key named ``option`` :param option: Name of the ``option`` (key) to set. :param value: Content to assign to ``option``. :return: Same as for ``SafeConfigParser.set`` method. """ return self._scp.set(self._section, option, str(value)) def write(self, fileobject): """ Overwrite current contents of ``fileobject.name`` """ return self._scp.write(open(fileobject.name, "w")) def merge_write(self, fileobject): """ Update section contents of ``fileobject.name`` by section only. """ scp = SafeConfigParser() # Safe if file doesn't exist scp.read(fileobject.name) # N/B: This won't work with DEFAULTS if not scp.has_section(self._section): scp.add_section(self._section) for key, value in list(self.items()): scp.set(self._section, key, value) scp.write(open(fileobject.name, "w+b")) # truncates file first def remove_option(self, option): """ Remove option-key ``option`` """ return self._scp.remove_option(self._section, option) def remove_section(self): """ Not implemented, do not use! :raises NotImplementedError: DO NOT USE! """ raise NotImplementedError() def items(self): """ Return list of ``key``/``value`` tuples for contents """ return self._scp.items(self._section)
class XYZBathyBank(object): """Bank of XYZ bathymetry infos .. seealso:: :class:`XYZBathyBankClient` :Usage: >>> xyzb = XYZBathyBank() """ def __init__(self, cfgfile=None): # Guess file name if cfgfile is None: f = ConfigParser() f.read(get_config_value(__name__, 'cfgfile_xyz')) if f.has_option('banks', 'main'): cfgfile = f.get('banks', 'main') #else: # raise Exception, 'Cannot read config file to guess bank file name' # Read file self.cfg = SafeConfigParser() self.cfgfile = cfgfile if cfgfile and os.path.exists(cfgfile): self.cfg.read(cfgfile) self._clients = {} for id in self.cfg.sections(): self._clients[id] = XYZBathyBankClient(self, id) self._order = None def ids(self): """Return the list of bathy ids""" return self.cfg.sections() def __str__(self): if not len(self): return 'No bathymetry' infos=[] for id in self.ids(): infos.append(str(self._clients[id])) return '\n'.join(infos) def __len__(self): return len(self.cfg.sections()) def add(self, xyzfile, id=None, force=False, **kwargs): """Add a bathy to the bank - **xyzfile**: xyz file. - *id*: Id of the bank. If ``None``, it is guessed from the file name: ``xyzfile="/path/toto.xyz"`` -> ``id="toto"`` """ if id is None: id = os.path.basename(xyzfile) if id.endswith('.xyz'): id = id[:-4] if self.cfg.has_section(id): if force: self.cfg.remove_section(id) else: raise KeyError('Bathymetry %s already exists'%id) self.cfg.add_section(id) self.cfg.set(id, 'xyzfile', xyzfile) self._clients[id] = XYZBathyBankClient(self, id, **kwargs) # self._save_() self._update_order_() def __iadd__(self, d): self.add(d) return self def remove(self, id): """Remove a bathy from the bank *id*: A bathy id or :class:`XYZBathyBankClient` instance. """ if isinstance(id, XYZBathyBankClient): id = id.id if not self.cfg.has_section(id): warn('No such bathymetry %s'%id) return self.cfg.remove_section(id) del self._clients[id] self._update_order_() # self._save_() def __isub__(self, d): self.remove(id) return self def copy(self, id1, id2): """Copy bathy infos to another""" assert self.cfg.has_section(id1), 'No such bathymetry %s'%id1 if not self.cfg.has_section(id2): self.cfg.add_section(id2) for opt in self.cfg.options(id1): self.cfg.set(id2, self.cfg.get(id1, opt)) self._clients[id2] = XYZBathyBankClient(self, id2) self._update_order_() ## self._save_() def __getitem__(self, id): assert self.cfg.has_section(id), 'No such bathymetry %s'%id return self._clients[id] def __setitem__(self, id, xyzfile): self.add(id, xyzfile) def __deltiem__(self, id): self.remove(id) def load(self, id): """Load a bathymetry as a :class:`XYZBathy` instance""" assert self.cfg.has_section(id), 'No such bathymetry '+id return self._clients[id].load() def list(self): """Get all clients of the bank as a list""" return list(self._clients.values()) def select(self, xmin=None, xmax=None, ymin=None, ymax=None, load=True, margin=None, ordered=None): """Return a list of bathymetries relevant to the specified region - *margin*: Margin for loading xyz bathies: - if None, the whole bathy is loaded from the bank, - else it should be a value relative to the approximative resolution (see :meth:`XYZBathy.resol`) """ res = [] for id in self.ids(): b = self._clients[id] if (None not in (xmin, b.xmax) and xmin > b.xmax) or \ (None not in (xmax, b.xmin) and xmax < b.xmin) or \ (None not in (ymin, b.ymax) and ymin > b.ymax) or \ (None not in (ymax, b.ymin) and ymax > b.ymin): continue if load: b = b.load(zone=(xmin, ymin, xmax, ymax), margin=margin) res.append(b) if self.get_order() is not None and ordered != False: ids = [b.id for b in res] order = [id for id in self.get_order() if id in ids] res.sort(cmp=lambda b1, b2: cmp(self.get_order().index(b1), self.get_order().index(b2))) return res def set_order(self, order): """Set a list of ids to define the order""" if order is None: del self.cfg.defaults()['order'] else: self.cfg.defaults()['order'] = str([id for id in order if id in self.ids()]) def get_order(self,): """Get a list of ids that define the order""" return eval(self.cfg.defaults().get('order', 'None')) def _update_order_(self): if self.get_order() is None: return self.cfg.defaults()['order'] = str([id for id in self.get_order() if id in self.ids()]) def save(self, cfgfile=None): if cfgfile is None: cfgfile = self.cfgfile else: self.cfgfile = cfgfile if cfgfile is None: raise VACUMMError('You must provide a valid file name') f = open(cfgfile, 'w') self.cfg.write(f) f.close() def merger(self, **kwargs): """Return a :class:`XYZBathyMerger` instance using the current bank Keywords are passed to :meth:`select` """ merger = XYZBathyMerger() merger += self.select(**kwargs) return merger def plot(self, **kwargs): """Plot current bathies using a :class:`XYZBathyMerger` instance""" return self.merger(**kwfilter(kwargs, 'merger')).plot(**kwargs)