Commit d32669a0 authored by Jim Fulton's avatar Jim Fulton

Formatting and minor refactoring of defau;t globals arg.

parent c39cdf2a
...@@ -111,7 +111,7 @@ option_start = re.compile( ...@@ -111,7 +111,7 @@ option_start = re.compile(
leading_blank_lines = re.compile(r"^(\s*\n)+") leading_blank_lines = re.compile(r"^(\s*\n)+")
def parse(fp, fpname, exp_globals=None): def parse(fp, fpname, exp_globals=dict):
"""Parse a sectioned setup file. """Parse a sectioned setup file.
The sections in setup files contain a title line at the top, The sections in setup files contain a title line at the top,
...@@ -121,15 +121,15 @@ def parse(fp, fpname, exp_globals=None): ...@@ -121,15 +121,15 @@ def parse(fp, fpname, exp_globals=None):
leading whitespace. Blank lines, lines beginning with a '#', leading whitespace. Blank lines, lines beginning with a '#',
and just about everything else are ignored. and just about everything else are ignored.
The title line is in the form [name] followed by an optional trailing The title line is in the form [name] followed by an optional trailing
comment separated by a semicolon `;' or a hash `#' character. comment separated by a semicolon `;' or a hash `#' character.
Optionally the title line can have the form `[name:expression]' where Optionally the title line can have the form `[name:expression]' where
expression is an arbitrary Python expression. Sections with an expression expression is an arbitrary Python expression. Sections with an expression
that evaluates to False are ignored. Semicolon `;' an hash `#' characters that evaluates to False are ignored. Semicolon `;' an hash `#' characters
mustr be string-escaped in expression literals. mustr be string-escaped in expression literals.
exp_globals is a callable returning a mapping of defaults used as globals exp_globals is a callable returning a mapping of defaults used as globals
during the evaluation of a section conditional expression. during the evaluation of a section conditional expression.
""" """
sections = {} sections = {}
...@@ -167,7 +167,7 @@ def parse(fp, fpname, exp_globals=None): ...@@ -167,7 +167,7 @@ def parse(fp, fpname, exp_globals=None):
header = section_header(line) header = section_header(line)
if header: if header:
# reset to True when starting a new section # reset to True when starting a new section
section_condition = True section_condition = True
sectname = header.group('name') sectname = header.group('name')
head = header.group('head') # the starting [ head = header.group('head') # the starting [
...@@ -176,18 +176,22 @@ def parse(fp, fpname, exp_globals=None): ...@@ -176,18 +176,22 @@ def parse(fp, fpname, exp_globals=None):
if expression: if expression:
# normalize tail comments to Python style # normalize tail comments to Python style
tail = tail.replace(';', '#') if tail else '' tail = tail.replace(';', '#') if tail else ''
# un-escape literal # and ; . Do not use a string-escape decode # un-escape literal # and ; . Do not use a
expr = expression.replace(r'\x23','#').replace(r'x3b', ';') # string-escape decode
expr = expression.replace(r'\x23','#').replace(r'x3b', ';')
# rebuild a valid Python expression wrapped in a list # rebuild a valid Python expression wrapped in a list
expr = head + expr + tail expr = head + expr + tail
# lazily populate context only expression # lazily populate context only expression
if not context: if not context:
context = exp_globals() if exp_globals else {} context = exp_globals()
# evaluated expression is in list: get first element # evaluated expression is in list: get first element
section_condition = eval(expr, context)[0] section_condition = eval(expr, context)[0]
# finally, ignore section when an expression evaluates to false # finally, ignore section when an expression
# evaluates to false
if not section_condition: if not section_condition:
logger.debug('Ignoring section %(sectname)r with [expression]: %(expression)r' % locals()) logger.debug(
'Ignoring section %(sectname)r with [expression]:'
' %(expression)r' % locals())
continue continue
if sectname in sections: if sectname in sections:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment