Coverage for configman.config_manager : 97%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# ***** BEGIN LICENSE BLOCK ***** # Version: MPL 1.1/GPL 2.0/LGPL 2.1 # # The contents of this file are subject to the Mozilla Public License Version # 1.1 (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # http://www.mozilla.org/MPL/ # # Software distributed under the License is distributed on an "AS IS" basis, # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License # for the specific language governing rights and limitations under the # License. # # The Original Code is configman # # The Initial Developer of the Original Code is # Mozilla Foundation # Portions created by the Initial Developer are Copyright (C) 2011 # the Initial Developer. All Rights Reserved. # # Contributor(s): # K Lars Lohn, lars@mozilla.com # Peter Bengtsson, peterbe@mozilla.com # # Alternatively, the contents of this file may be used under the terms of # either the GNU General Public License Version 2 or later (the "GPL"), or # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), # in which case the provisions of the GPL or the LGPL are applicable instead # of those above. If you wish to allow use of your version of this file only # under the terms of either the GPL or the LGPL, and not to allow others to # use your version of this file under the terms of the MPL, indicate your # decision by deleting the provisions above and replace them with the notice # and other provisions required by the GPL or the LGPL. If you do not delete # the provisions above, a recipient may use your version of this file under # the terms of any one of the MPL, the GPL or the LGPL. # # ***** END LICENSE BLOCK *****
#============================================================================== # for convenience define some external symbols here
#============================================================================== #-------------------------------------------------------------------------- def get_required_config(cls):
#-------------------------------------------------------------------------- '%s missing from config' % a_parameter
#==============================================================================
#-------------------------------------------------------------------------- definition_source=None, values_source_list=None, argv_source=None, #use_config_files=True, use_auto_help=True, use_admin_controls=True, quit_after_admin=True, options_banned_from_help=None, app_name='', app_version='', app_description='', config_pathname='.', ): """create and initialize a configman object.
parameters: definition_source - a namespace or list of namespaces from which configman is to fetch the definitions of the configuration parameters. values_source_list - (optional) a hierarchical list of sources for values for the configuration parameters. As values are copied from these sources, conficting values are resolved with sources on the right getting preference over sources on the left. argv_source - if the values_source_list contains a commandline source, this value is an alternative source for actual command line arguments. Useful for testing or preprocessing command line arguments. use_auto_help - set to True if configman is to automatically set up help output for command line invocations. use_admin_controls - configman can add command line flags that it interprets independently of the app defined arguments. True enables this capability, while, False supresses it. quit_after_admin - if True and admin controls are enabled and used, call sys.exit to end the app. This is useful to stop the app from running if all that was done was to write a config file or stop after help. options_banned_from_help - a list of strings that will censor the output of help to prevent specified options from being listed in the help output. This is useful for hiding debug or secret command line arguments. app_name - assigns a name to the app. This is used in help output and as a default basename for config files. app_version - assigns a version for the app used help output. app_description - assigns a description for the app to be used in the help output. config_pathname - a hard coded path to the directory of or the full path and name of the configuration file.""" # instead of allowing mutables as default keyword argument values... not isinstance(definition_source, basestring)): else:
# will be stored here.
cm.environment, cm.command_line) else: cm.command_line)
'admin.conf', 'admin.dump_conf', 'admin.print_conf', 'admin.application']
# iterate through the option definitions to create the nested dict # hierarchy of all the options called 'option_definitions' self.option_definitions)
# some admin options need to be loaded from the command line # prior to processing the rest of the command line options. self) # integrate the admin_options with 'option_definitions' ignore_mismatches=True)
self)
# first pass to get classes & config path - ignore bad options
# walk tree expanding class options
# the app_name, app_version and app_description are to come from # if 'admin.application' option if it is present. If it is not present, # get the app_name,et al, from parameters passed into the constructor. # if those are empty, set app_name, et al, to empty strings 'app_description', '') # there is no 'admin.application' option, continue to use the # 'app_name' from the parameters passed in, if they exist.
# second pass to include config file values - ignore bad options
# walk tree expanding class options
# third pass to get values - complain about bad options
and self._get_option('admin.print_conf').value):
and self._get_option('admin.dump_conf').value):
#-------------------------------------------------------------------------- def context(self): """return a config as a context that calls close on every item when it goes out of scope""" except Exception: raise finally:
#-------------------------------------------------------------------------- # state changed, must regenerate else:
#-------------------------------------------------------------------------- output_stream=sys.stdout, block_password=True): """outputs a usage tip and the list of acceptable commands. This is useful as the output of the 'help' option.
parameters: output_stream - an open file-like object suitable for use as the target of a print statement block_password - a boolean driving the use of a string of * in place of the value for any object containing the substring 'passowrd' """
# don't bother with certain dead obvious ones
#-------------------------------------------------------------------------- """write a config file to the pathname specified in the parameter. The file extention determines the type of file written and must match a registered type.
parameters: config_pathname - the full path and filename of the target config file."""
def stdout_opener():
#-------------------------------------------------------------------------- """write a config file to the pathname specified in the parameter. The file extention determines the type of file written and must match a registered type.
parameters: config_pathname - the full path and filename of the target config file."""
#-------------------------------------------------------------------------- """write a configuration file to a file-like object.
parameters: config_file_type - a string containing a registered file type. Passing in an unregistered string will result in a KeyError exception opener - a callable object or function that returns a file like object that works as a context in a with statement."""
blocked_keys=self.admin_controls_list) option_iterator, config_fp)
#-------------------------------------------------------------------------- """write out the current configuration to a log-like object.
parameters: logger - a object that implements a method called 'info' with the same semantics as the call to 'logger.info'"""
self._walk_config(self.option_definitions) if qkey not in self.admin_controls_list and not isinstance(val, Namespace)] else: conv.to_string_converters[type(key)](val)) except KeyError: logger.info('%s: %s', key, val)
#-------------------------------------------------------------------------- """returns a list of fully qualified option names.
parameters: source - a sequence of Namespace of Options, usually not specified, If not specified, the function will default to using the internal list of Option definitions. names - a list to start with for appending the lsit Option names. If ommited, the function will start with an empty list.
returns: a list of strings representing the Options in the source Namespace list. Each item will be fully qualified with dot delimited Namespace names. """ # skip aggregations, we want only Options
#-------------------------------------------------------------------------- def _walk_and_close(a_dict):
#-------------------------------------------------------------------------- """This routine generates a copy of the DotDict based config"""
#-------------------------------------------------------------------------- parent_namespace=None): # can't use iteritems in loop, we're changing the dict parent_namespace=source_namespace) (inspect.isclass(val.value) or inspect.ismodule(val.value))): else: val.value.get_required_config().iteritems(): else:
#--------------------------------------------------------------------------
#-------------------------------------------------------------------------- # we've got a path with no file name at the end # use the appname as the file name and default to an 'ini' # config file type '%s.ini' % self.app_name) else: # there is no app_name yet # we'll punt and use 'config'
#-------------------------------------------------------------------------- default=None, doc='write current config to stdout ' '(conf, ini, json)', ) default='', doc='a pathname to which to write the current config', ) # only offer the config file admin options if they've been requested in # the values source list default=default_config_pathname, doc='the pathname of the config file ' '(path/filename)', )
#-------------------------------------------------------------------------- a_settings_source.always_ignore_mismatches) # the settings source doesn't have the concept of always # ignoring mismatches, so the original value of # ignore_mismatches stands ignore_mismatches=this_source_ignore_mismatches) ignore_mismatches=this_source_ignore_mismatches)
#-------------------------------------------------------------------------- prefix='', ignore_mismatches=True): raise exc.NotAnOptionError('%s is not an option' % key) (key, subkey)) except TypeError: pass prefix=('%s.%s' % (prefix, key))) elif isinstance(sub_destination, Aggregation): # there is nothing to do for Aggregations at this time # it appears here anyway as a marker for future enhancements pass
#--------------------------------------------------------------------------
#-------------------------------------------------------------------------- self._aggregate(val, base_namespace, local_namespace[key])) # skip Options, we're only dealing with Aggregations
#-------------------------------------------------------------------------- def _option_sort(x_tuple): else:
#--------------------------------------------------------------------------
#-------------------------------------------------------------------------- block_password=False): continue block_password) new_prefix, blocked_keys, block_password):
#-------------------------------------------------------------------------- else: # of a key error or execution falling through the loop
#-------------------------------------------------------------------------- else: |