MultiOrderedConfigParser Class Reference


Public Member Functions

def __init__
def add_default
def add_include
def add_section
def default
def defaults
def find_value
def get
def get_defaults
def get_sections
def includes
def multi_get
def read
def section
def sections
def set
def write

Detailed Description

Definition at line 268 of file astconfigparser.py.


Member Function Documentation

def __init__ (   self,
  parent = None 
)

Definition at line 269 of file astconfigparser.py.

00269                                    :
00270         self._parent = parent
00271         self._defaults = MultiOrderedDict()
00272         self._sections = MultiOrderedDict()
00273         self._includes = OrderedDict()
00274 
    def find_value(self, sections, key):

def add_default (   self,
  key,
  template_keys = None 
)

Adds a default section to defaults, returning the
default Section object.

Definition at line 304 of file astconfigparser.py.

00304                                                   :
00305         """
00306         Adds a default section to defaults, returning the
00307         default Section object.
00308         """
00309         if template_keys is None:
00310             template_keys = []
00311         return self.add_section(key, template_keys, self._defaults)
00312 
    def sections(self):

def add_include (   self,
  filename,
  parser = None 
)

Add a new #include file to the configuration.

Definition at line 367 of file astconfigparser.py.

00367                                                 :
00368         """
00369         Add a new #include file to the configuration.
00370         """
00371         if filename in self._includes:
00372             return self._includes[filename]
00373 
00374         self._includes[filename] = res = \
00375             MultiOrderedConfigParser(self) if parser is None else parser
00376         return res
00377 
    def get(self, section, key):

def add_section (   self,
  key,
  template_keys = None,
  mdicts = None 
)

Create a new section in the configuration. The name of the
new section is the 'key' parameter.

Definition at line 348 of file astconfigparser.py.

00348                                                                :
00349         """
00350         Create a new section in the configuration. The name of the
00351         new section is the 'key' parameter.
00352         """
00353         if template_keys is None:
00354             template_keys = []
00355         if mdicts is None:
00356             mdicts = self._sections
00357         res = Section()
00358         for t in template_keys:
00359             res.add_templates(self.get_defaults(t))
00360         res.add_defaults(self.get_defaults(DEFAULTSECT))
00361         mdicts.insert(0, key, res)
00362         return res
00363 
    def includes(self):

def default (   self,
  key 
)

Retrieves a list of dictionaries for a default section.

Definition at line 300 of file astconfigparser.py.

00300                           :
00301         """Retrieves a list of dictionaries for a default section."""
00302         return self.get_defaults(key)
00303 
    def add_default(self, key, template_keys=None):

def defaults (   self  ) 

Definition at line 297 of file astconfigparser.py.

00297                       :
00298         return self._defaults
00299 
    def default(self, key):

def find_value (   self,
  sections,
  key 
)

Given a list of sections, try to find value(s) for the given key.

Definition at line 275 of file astconfigparser.py.

00275                                        :
00276         """Given a list of sections, try to find value(s) for the given key."""
00277         # always start looking in the last one added
00278         sections.sort(reverse=True)
00279         for s in sections:
00280             try:
00281                 # try to find in section and section's templates
00282                 return s.get(key, from_defaults=False)
00283             except KeyError:
00284                 pass
00285 
00286         # wasn't found in sections or a section's templates so check in
00287         # defaults
00288         for s in sections:
00289             try:
00290                 # try to find in section's defaultsects
00291                 return s.get(key, from_self=False, from_templates=False)
00292             except KeyError:
00293                 pass
00294 
00295         raise KeyError(key)
00296 
    def defaults(self):

def get (   self,
  section,
  key 
)

Retrieves the list of values from a section for a key.

Definition at line 378 of file astconfigparser.py.

00378                                :
00379         """Retrieves the list of values from a section for a key."""
00380         try:
00381             # search for the value in the list of sections
00382             return self.find_value(self.section(section), key)
00383         except KeyError:
00384             pass
00385 
00386         try:
00387             # section may be a default section so, search
00388             # for the value in the list of defaults
00389             return self.find_value(self.default(section), key)
00390         except KeyError:
00391             raise LookupError("key %r not found for section %r"
00392                               % (key, section))
00393 
    def multi_get(self, section, key_list):

def get_defaults (   self,
  key 
)

Retrieve a list of defaults that have values for the given key.

Definition at line 342 of file astconfigparser.py.

00342                                :
00343         """
00344         Retrieve a list of defaults that have values for the given key.
00345         """
00346         return self.get_sections(key, '_defaults')
00347 
    def add_section(self, key, template_keys=None, mdicts=None):

def get_sections (   self,
  key,
  attr = '_sections',
  searched = None 
)

Retrieve a list of sections that have values for the given key.
The attr parameter can be used to control what part of the parser
to retrieve values from.

Definition at line 320 of file astconfigparser.py.

00320                                                                 :
00321         """
00322         Retrieve a list of sections that have values for the given key.
00323         The attr parameter can be used to control what part of the parser
00324         to retrieve values from.
00325         """
00326         if searched is None:
00327             searched = []
00328         if self in searched:
00329             return []
00330 
00331         sections = getattr(self, attr)
00332         res = sections[key] if key in sections else []
00333         searched.append(self)
00334         if self._includes:
00335             res.extend(list(itertools.chain(*[
00336                 incl.get_sections(key, attr, searched)
00337                 for incl in self._includes.itervalues()])))
00338         if self._parent:
00339             res += self._parent.get_sections(key, attr, searched)
00340         return res
00341 
    def get_defaults(self, key):

def includes (   self  ) 

Definition at line 364 of file astconfigparser.py.

00364                       :
00365         return self._includes
00366 
    def add_include(self, filename, parser=None):

def multi_get (   self,
  section,
  key_list 
)

Retrieves the list of values from a section for a list of keys.
This method is intended to be used for equivalent keys. Thus, as soon
as any match is found for any key in the key_list, the match is
returned. This does not concatenate the lookups of all of the keys
together.

Definition at line 394 of file astconfigparser.py.

00394                                           :
00395         """
00396         Retrieves the list of values from a section for a list of keys.
00397         This method is intended to be used for equivalent keys. Thus, as soon
00398         as any match is found for any key in the key_list, the match is
00399         returned. This does not concatenate the lookups of all of the keys
00400         together.
00401         """
00402         for i in key_list:
00403             try:
00404                 return self.get(section, i)
00405             except LookupError:
00406                 pass
00407 
00408         # Making it here means all lookups failed.
00409         raise LookupError("keys %r not found for section %r" %
00410                           (key_list, section))
00411 
    def set(self, section, key, val):

def read (   self,
  filename,
  sect = None 
)

Parse configuration information from a file

Definition at line 421 of file astconfigparser.py.

00421                                        :
00422         """Parse configuration information from a file"""
00423         try:
00424             with open(filename, 'rt') as config_file:
00425                 self._read(config_file, sect)
00426         except IOError:
00427             print "Could not open file ", filename, " for reading"
00428 
    def _read(self, config_file, sect):

def section (   self,
  key 
)

Retrieves a list of dictionaries for a section.

Definition at line 316 of file astconfigparser.py.

00316                           :
00317         """Retrieves a list of dictionaries for a section."""
00318         return self.get_sections(key)
00319 
    def get_sections(self, key, attr='_sections', searched=None):

def sections (   self  ) 

Definition at line 313 of file astconfigparser.py.

00313                       :
00314         return self._sections
00315 
    def section(self, key):

def set (   self,
  section,
  key,
  val 
)

Sets an option in the given section.

Definition at line 412 of file astconfigparser.py.

00412                                     :
00413         """Sets an option in the given section."""
00414         # TODO - set in multiple sections? (for now set in first)
00415         # TODO - set in both sections and defaults?
00416         if section in self._sections:
00417             self.section(section)[0][key] = val
00418         else:
00419             self.defaults(section)[0][key] = val
00420 
    def read(self, filename, sect=None):

def write (   self,
  config_file 
)

Write configuration information out to a file

Definition at line 457 of file astconfigparser.py.

00457                                 :
00458         """Write configuration information out to a file"""
00459         try:
00460             for key, val in self._includes.iteritems():
00461                 val.write(key)
00462                 config_file.write('#include "%s"\n' % key)
00463 
00464             config_file.write('\n')
00465             write_dicts(config_file, self._defaults)
00466             write_dicts(config_file, self._sections)
00467         except:
00468             try:
00469                 with open(config_file, 'wt') as fp:
00470                     self.write(fp)
00471             except IOError:
00472                 print "Could not open file ", config_file, " for writing"
                print "Could not open file ", config_file, " for writing"


The documentation for this class was generated from the following file:

Generated on Thu Apr 16 06:41:37 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6