Browse Source

wrap parse_config() to catch ConfigParseExceptions and report them to the user cleanly; some minor doc changes.

tags/v0.1^2
Ben Kurtovic 13 years ago
parent
commit
94848ab0bc
1 changed files with 13 additions and 3 deletions
  1. +13
    -3
      core/config.py

+ 13
- 3
core/config.py View File

@@ -118,7 +118,7 @@ def attribute_to_bool(element, attribute, default=None):


def get_first_element(parent, tag_name): def get_first_element(parent, tag_name):
"""Return the first child of the parent element with the given tag name, or """Return the first child of the parent element with the given tag name, or
return None."""
return None if no child of that name exists."""
try: try:
return parent.getElementsByTagName(tag_name)[0] return parent.getElementsByTagName(tag_name)[0]
except IndexError: except IndexError:
@@ -129,8 +129,8 @@ def get_required_element(parent, tag_name):
raise MissingElementException() if no child of that name exists.""" raise MissingElementException() if no child of that name exists."""
element = get_first_element(parent, tag_name) element = get_first_element(parent, tag_name)
if not element: if not element:
e = "<{0}> is missing a required <{1}> tag.".format(parent.tagName,
tag_name)
e = "A <{0}> tag is missing a required <{1}> child tag.".format(
parent.tagName, tag_name)
raise MissingElementException(e) raise MissingElementException(e)
return element return element


@@ -145,6 +145,16 @@ def get_required_attribute(element, attr_name):
return attribute return attribute


def parse_config(key): def parse_config(key):
"""A thin wrapper for the actual config parser in _parse_config(): catch
parsing exceptions and report them to the user cleanly."""
try:
_parse_config(key)
except ConfigParseException as e:
print "\nError parsing config file:"
print e
exit(1)

def _parse_config(key):
"""Parse config data from a DOM object into the 'config' global variable. """Parse config data from a DOM object into the 'config' global variable.
The key is used to unencrypt passwords stored in the XML config file.""" The key is used to unencrypt passwords stored in the XML config file."""
_load_config() # we might be re-loading unnecessarily here, but no harm in _load_config() # we might be re-loading unnecessarily here, but no harm in


Loading…
Cancel
Save