def __init__(self, source, config_manager=None, top_level_section_name=''):
     self.delayed_parser_instantiation = False
     self.top_level_section_name = top_level_section_name
     if source is configobj.ConfigObj:
         try:
             app = config_manager._get_option('admin.application')
             source = "%s.%s" % (app.value.app_name, file_name_extension)
         except AttributeError:
             # we likely don't have the admin.application object set up yet.
             # we need to delay the instantiation of the config parser
             # until later.
             if source is None:
                 raise NotEnoughInformationException(
                     "Can't setup an ini file without knowing the file name"
                 )
             self.delayed_parser_instantiation = True
             return
         if not os.path.exists(source) and config_manager.config_optional:
             return
     if isinstance(source, (six.binary_type, six.text_type)):
         source = to_str(source)
     if (isinstance(source, six.string_types)
             and source.endswith(file_name_extension)):
         try:
             self.config_obj = ConfigObjWithIncludes(source)
         except Exception as x:
             raise LoadingIniFileFailsException(
                 "ConfigObj cannot load ini: %s" % str(x))
     else:
         raise CantHandleTypeException()
 def __init__(self, source, the_config_manager=None):
     if source is getopt:
         self.argv_source = the_config_manager.argv_source
     elif isinstance(source, collections.Sequence):
         self.argv_source = source
     else:
         raise CantHandleTypeException()
Exemple #3
0
 def __init__(self, candidate, the_config_manager=None):
     if (isinstance(candidate, basestring)
             and candidate.endswith(file_name_extension)):
         # we're trusting the string represents a filename
         opener = functools.partial(open, candidate)
     elif isinstance(candidate, function_type):
         # we're trusting that the function when called with no parameters
         # will return a Context Manager Type.
         opener = candidate
     else:
         raise CantHandleTypeException()
     self.values = {}
     try:
         with opener() as f:
             previous_key = None
             for line in f:
                 if line.strip().startswith('#') or not line.strip():
                     continue
                 if line[0] in ' \t' and previous_key:
                     line = line[1:]
                     self.values[previous_key] = (
                         '%s%s' %
                         (self.values[previous_key], line.rstrip()))
                     continue
                 try:
                     key, value = line.split("=", 1)
                     self.values[key.strip()] = value.strip()
                     previous_key = key
                 except ValueError:
                     self.values[line] = ''
     except Exception, x:
         raise NotAConfigFileError(
             "Conf couldn't interpret %s as a config file: %s" %
             (candidate, str(x)))
Exemple #4
0
 def __init__(self, source, the_config_manager=None):
     self.values = None
     if source is json:
         try:
             app = the_config_manager._get_option('admin.application')
             source = "%s.%s" % (app.value.app_name, file_name_extension)
         except (AttributeError, KeyError):
             raise NotEnoughInformationException(
                 "Can't setup an json file without knowing the file name")
     if isinstance(source, (six.binary_type, six.text_type)):
         source = to_str(source)
     if (isinstance(source, six.string_types)
             and source.endswith(file_name_extension)):
         try:
             with open(source) as fp:
                 self.values = json.load(fp)
         except IOError as x:
             # The file doesn't exist.  That's ok, we'll give warning
             # but this isn't a fatal error
             import warnings
             warnings.warn("%s doesn't exist" % source)
             self.values = {}
         except ValueError:
             raise LoadingJsonFileFailsException("Cannot load json: %s" %
                                                 str(x))
     else:
         raise CantHandleTypeException()
Exemple #5
0
 def __init__(self, source, the_config_manager=None):
     if source is os.environ:
         self.always_ignore_mismatches = True
     elif isinstance(source, collections.Mapping):
         if "always_ignore_mismatches" in source:
             self.always_ignore_mismatches = \
                 bool(source["always_ignore_mismatches"])
         else:
             self.always_ignore_mismatches = False
     else:
         raise CantHandleTypeException()
     self.source = source
Exemple #6
0
        if (isinstance(source, basestring)
                and source.endswith(file_name_extension)):
            try:
                with open(source) as fp:
                    self.values = json.load(fp)
            except IOError, x:
                # The file doesn't exist.  That's ok, we'll give warning
                # but this isn't a fatal error
                import warnings
                warnings.warn("%s doesn't exist" % source)
                self.values = {}
            except ValueError:
                raise LoadingJsonFileFailsException("Cannot load json: %s" %
                                                    str(x))
        else:
            raise CantHandleTypeException()

    #--------------------------------------------------------------------------
    @memoize()
    def get_values(self, config_manager, ignore_mismatches, obj_hook=DotDict):
        if isinstance(self.values, obj_hook):
            return self.values
        return obj_hook(self.values)

    #--------------------------------------------------------------------------
    @staticmethod
    def recursive_default_dict():
        return collections.defaultdict(ValueSource.recursive_default_dict)

    #--------------------------------------------------------------------------
    @staticmethod