scripts/settings.py
author Todd Larsen <tlarsen@google.com>
Fri, 16 May 2008 19:46:16 +0000
changeset 29 64b3e323210f
child 43 fdb9a6d839ae
permissions -rw-r--r--
settings.py module (and associated tests) for reading a settings file and combining it with command-line options, for SoC utility and tool scripts. Patch by: Todd Larsen Review by: Sverre Rabbelier Review issue: 145 Review URL: http://codereviews.googleopensourceprograms.com/145
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Custom optparse OptionParser and functions for reading Python settings files.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
  Option:  class derived from optparse.Option that adds a 'required' parameter
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
  OptionParser:  class derived from optparse.OptionParser for use with Option
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  readPythonSettings():  interprets a valid Python file as a settings file
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
"""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
__authors__ = [
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
  # alphabetical order by last name, please
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
  '"Todd Larsen" <tlarsen@google.com>',
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
]
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
import os
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
import optparse
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
import sys
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
DEF_SETTINGS_FILE_DIR = "~"
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
DEF_SETTINGS_FILE_NAME = '.soc_scripts_settings'
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
class Error(Exception):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
  """Base exception class for all exceptions in the settings module."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
  pass
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
class Option(optparse.Option):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
  """Class derived from optparse.Option that adds a 'required' parameter."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
  ATTRS = optparse.Option.ATTRS + ['required']
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
  def _check_required(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
    """Insures that 'required' option can accept a value."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
    if self.required and (not self.takes_value()):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
      raise optparse.OptionError(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
          "'required' parameter set for option that does not take a value",
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
          self)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
  # Make sure _check_required() is called from the constructor!
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
  CHECK_METHODS = optparse.Option.CHECK_METHODS + [_check_required]
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
  def process(self, opt, value, values, parser):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
    optparse.Option.process(self, opt, value, values, parser)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
    parser.option_seen[self] = 1
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
class OptionParser(optparse.OptionParser):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
  """Class derived from optparse.OptionParser for use with Option."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
  def _init_parsing_state(self):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
    """Sets up dict to track options seen so far."""
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
    optparse.OptionParser._init_parsing_state(self)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
    self.option_seen = {}
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
  def error(self, *args):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
    """Convert errors reported by optparse.OptionParser to Error exceptions.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
    Args:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
      *args:  passed through to the Error exception __init__() constructor,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
        usually a list of strings
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
    Raises:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
      Error with the supplied *args
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
    raise Error(*args)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
  def check_values(self, values, args):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
    """Checks to make sure all required=True options were supplied.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
    Args:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
      values, args:  passed through unchanged (see Returns:)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
    Returns:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
      (values, args) unchanged.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
    Raises:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
      Error if an option was not supplied that had required=True;  exception
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
      positional arguments are the error message strings.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
    """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
    errors = []
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
    for option in self.option_list:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
      if (isinstance(option, Option)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
          and option.required
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
          and (not self.option_seen.has_key(option))):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
        errors.append(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
            'required %s option not supplied' % option)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
    if errors:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   108
      self.error(*errors)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
    return values, args
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   111
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   112
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   113
def readPythonSettings(defaults={},  # {} OK since defaults is always copied
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   114
                       settings_dir=DEF_SETTINGS_FILE_DIR,
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   115
                       settings_file=DEF_SETTINGS_FILE_NAME):
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   116
  """Executes a Python-syntax settings file and returns the local variables.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   117
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   118
  Args:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   119
    defaults:  dict of default values to use when settings are not present
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   120
      in the settings file (or if no settings file is present at all);  this
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   121
      dict is *copied* and is not altered at all
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   122
    settings_dir:  optional directory containing settings_file
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   123
    settings_file:  optional settings file name found in settings_dir
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   124
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   125
  Returns:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   126
    dict of setting name/value pairs (possibly including some values from the
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   127
    defaults parameter).  Since the settings file is full-fledged Python
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   128
    source, the values could be any valid Python object.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   129
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   130
  Raises:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   131
    Error if some error occurred parsing the present settings file;  exception
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   132
    positional arguments are the error message strings.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   133
  """
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   134
  # do not let the original defaults be altered
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   135
  defaults = defaults.copy()
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   136
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   137
  # form absolute path to the settings file, expanding any environment
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   138
  # variables and "~", then removing excess . and .. path elements
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   139
  path = os.path.abspath(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   140
      os.path.normpath(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   141
          os.path.expanduser(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   142
              os.path.expandvars(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   143
                  os.path.join(settings_dir, settings_file)))))
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   144
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   145
  # empty dict to capture the local variables in the settings file
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   146
  settings_locals = {}
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   147
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   148
  try:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   149
    # execute the Python source file and recover the local variables as settings
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   150
    execfile(path, {}, settings_locals)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   151
  except IOError:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   152
    # If the settings file is not present, there are no defaults.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   153
    pass
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   154
  except Exception, error:
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   155
    # Other exceptions usually mean a faulty settings file.
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   156
    raise Error(
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   157
        'faulty settings file:',
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   158
        ('  %s: %s' % (error.__class__.__name__, str(error))),
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   159
        ('  %s' % path))
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   160
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   161
  # overwrite defaults copy with values from the (possibly empty) settings file
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   162
  defaults.update(settings_locals)
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   163
64b3e323210f settings.py module (and associated tests) for reading a settings file and
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   164
  return defaults