Back to index

enigmail  1.4.3
Public Member Functions | Public Attributes | Private Member Functions | Static Private Attributes
configobj.InterpolationEngine Class Reference
Inheritance diagram for configobj.InterpolationEngine:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def interpolate

Public Attributes

 section

Private Member Functions

def _fetch
def _parse_match

Static Private Attributes

tuple _KEYCRE = re.compile(r"%\(([^)]*)\)s")

Detailed Description

A helper class to help perform string interpolation.

This class is an abstract base class; its descendants perform
the actual work.

Definition at line 296 of file configobj.py.


Constructor & Destructor Documentation

def configobj.InterpolationEngine.__init__ (   self,
  section 
)

Definition at line 307 of file configobj.py.

00307 
00308     def __init__(self, section):
00309         # the Section instance that "owns" this engine
00310         self.section = section


Member Function Documentation

def configobj.InterpolationEngine._fetch (   self,
  key 
) [private]
Helper function to fetch values from owning section.

Returns a 2-tuple: the value, and the section where it was found.

Definition at line 359 of file configobj.py.

00359 
00360     def _fetch(self, key):
00361         """Helper function to fetch values from owning section.
00362 
00363         Returns a 2-tuple: the value, and the section where it was found.
00364         """
00365         # switch off interpolation before we try and fetch anything !
00366         save_interp = self.section.main.interpolation
00367         self.section.main.interpolation = False
00368 
00369         # Start at section that "owns" this InterpolationEngine
00370         current_section = self.section
00371         while True:
00372             # try the current section first
00373             val = current_section.get(key)
00374             if val is not None:
00375                 break
00376             # try "DEFAULT" next
00377             val = current_section.get('DEFAULT', {}).get(key)
00378             if val is not None:
00379                 break
00380             # move up to parent and try again
00381             # top-level's parent is itself
00382             if current_section.parent is current_section:
00383                 # reached top level, time to give up
00384                 break
00385             current_section = current_section.parent
00386 
00387         # restore interpolation to previous value before returning
00388         self.section.main.interpolation = save_interp
00389         if val is None:
00390             raise MissingInterpolationOption(key)
00391         return val, current_section

Here is the caller graph for this function:

def configobj.InterpolationEngine._parse_match (   self,
  match 
) [private]
Implementation-dependent helper function.

Will be passed a match object corresponding to the interpolation
key we just found (e.g., "%(foo)s" or "$foo"). Should look up that
key in the appropriate config file section (using the ``_fetch()``
helper function) and return a 3-tuple: (key, value, section)

``key`` is the name of the key we're looking for
``value`` is the value found for that key
``section`` is a reference to the section where it was found

``key`` and ``section`` should be None if no further
interpolation should be performed on the resulting value
(e.g., if we interpolated "$$" and returned "$").

Reimplemented in configobj.TemplateInterpolation, and configobj.ConfigParserInterpolation.

Definition at line 392 of file configobj.py.

00392 
00393     def _parse_match(self, match):
00394         """Implementation-dependent helper function.
00395 
00396         Will be passed a match object corresponding to the interpolation
00397         key we just found (e.g., "%(foo)s" or "$foo"). Should look up that
00398         key in the appropriate config file section (using the ``_fetch()``
00399         helper function) and return a 3-tuple: (key, value, section)
00400 
00401         ``key`` is the name of the key we're looking for
00402         ``value`` is the value found for that key
00403         ``section`` is a reference to the section where it was found
00404 
00405         ``key`` and ``section`` should be None if no further
00406         interpolation should be performed on the resulting value
00407         (e.g., if we interpolated "$$" and returned "$").
00408         """
00409         raise NotImplementedError
00410     

Here is the caller graph for this function:

def configobj.InterpolationEngine.interpolate (   self,
  key,
  value 
)

Definition at line 311 of file configobj.py.

00311 
00312     def interpolate(self, key, value):
00313         def recursive_interpolate(key, value, section, backtrail):
00314             """The function that does the actual work.
00315 
00316             ``value``: the string we're trying to interpolate.
00317             ``section``: the section in which that string was found
00318             ``backtrail``: a dict to keep track of where we've been,
00319             to detect and prevent infinite recursion loops
00320 
00321             This is similar to a depth-first-search algorithm.
00322             """
00323             # Have we been here already?
00324             if backtrail.has_key((key, section.name)):
00325                 # Yes - infinite loop detected
00326                 raise InterpolationLoopError(key)
00327             # Place a marker on our backtrail so we won't come back here again
00328             backtrail[(key, section.name)] = 1
00329 
00330             # Now start the actual work
00331             match = self._KEYCRE.search(value)
00332             while match:
00333                 # The actual parsing of the match is implementation-dependent,
00334                 # so delegate to our helper function
00335                 k, v, s = self._parse_match(match)
00336                 if k is None:
00337                     # That's the signal that no further interpolation is needed
00338                     replacement = v
00339                 else:
00340                     # Further interpolation may be needed to obtain final value
00341                     replacement = recursive_interpolate(k, v, s, backtrail)
00342                 # Replace the matched string with its final value
00343                 start, end = match.span()
00344                 value = ''.join((value[:start], replacement, value[end:]))
00345                 new_search_start = start + len(replacement)
00346                 # Pick up the next interpolation key, if any, for next time
00347                 # through the while loop
00348                 match = self._KEYCRE.search(value, new_search_start)
00349 
00350             # Now safe to come back here again; remove marker from backtrail
00351             del backtrail[(key, section.name)]
00352 
00353             return value
00354 
00355         # Back in interpolate(), all we have to do is kick off the recursive
00356         # function with appropriate starting values
00357         value = recursive_interpolate(key, value, self.section, {})
00358         return value

Here is the call graph for this function:


Member Data Documentation

tuple configobj.InterpolationEngine._KEYCRE = re.compile(r"%\(([^)]*)\)s") [static, private]

Reimplemented in configobj.TemplateInterpolation, and configobj.ConfigParserInterpolation.

Definition at line 305 of file configobj.py.

Definition at line 309 of file configobj.py.


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