Beispiel #1
0
 def get_value(key, ldict):
     '''Returns the key from the given dictionary.
        If this is not the last part of the key, this method
        is called recursively.'''
     assert(type(ldict) == DictType)
     assert(len(key) > 0)
     if key[0] not in ldict:
         raise CfgEx("(Sub-)Key [%s] not found." % key[0])
     val = ldict[key[0]]
     # No more keys to go for.
     if len(key) == 1:
         return val
     if type(val) != DictType:
         raise CfgEx("(Sub-)Type of configuration for key [%s] not a "
                     "dictionary " % key[0])
     return InternalCfg.get_value(key[1:], val)
Beispiel #2
0
def get_raw(ldict, key):
    """Returns the value of the given key.

    If the key is not found a CfgEx is raised.
    """
    ckey = cfg_key(key)
    try:
        rval = reduce(dict.get, ckey, ldict)
    except TypeError:
        # This is raised when dict.get finds a None instead of a dict.
        raise CfgEx("Key [%s] not found" % ckey)

    if rval is None:
        raise CfgEx("Key [%s] not found" % ckey)
    # This is the tricky part: With this construct each
    # sub-configuration is again a configuration.
    return rval
Beispiel #3
0
 def create_cfg_from_str(self, url):
     """Create config from JSON string"""
     if url.startswith("json:"):
         url = url[5:]
     jdict = json.loads(url)
     if not isinstance(jdict, dict):
         raise CfgEx("Given JSON string encodes no dictionary.")
     return jdict
Beispiel #4
0
 def create_cfg_from_str(self, url):
     """Create config from YAML string"""
     if url.startswith("yaml:"):
         url = url[5:]
     ydict = yaml.safe_load(url)
     if not isinstance(ydict, dict):
         raise CfgEx("Given YAML string encodes no dictionary.")
     return ydict
Beispiel #5
0
 def merge_json_str(self, jstr):
     '''Adds all the values from the given JSON string to
        the existing configuration.'''
     if jstr.startswith("json:"):
         jstr = jstr[5:]
     jdict = json.loads(jstr)
     if type(jdict) != DictType:
         raise CfgEx("Given JSON string encodes no dictionary.")
     self.__merge_dictionary(jdict)
Beispiel #6
0
 def create_cfg_from_file(self, url):
     """Creates dict from JSON file"""
     if url.startswith("file://"):
         url = url[7:]
     with open(url, "r") as jfd:
         jdict = json.load(jfd)
     if not isinstance(jdict, dict):
         raise CfgEx("Given JSON string encodes no dictionary.")
     return jdict
Beispiel #7
0
 def create_cfg_from_file(self, url):
     """Creates dict from YAML file"""
     if url.startswith("file://"):
         url = url[7:]
     with open(url, "r") as yfd:
         ydict = yaml.safe_load(yfd)
     if not isinstance(ydict, dict):
         raise CfgEx("Given YAML string encodes no dictionary.")
     return ydict
Beispiel #8
0
 def __merge_json_file(self, jfile):
     '''Adds all the values from the given JSON file to
        the existing configuration.'''
     if jfile.startswith("file://"):
         jfile = jfile[7:]
     jfd = file(jfile, "r")
     jdict = json.load(jfd)
     jfd.close()
     if type(jdict) != DictType:
         raise CfgEx("Given JSON string encodes no dictionary.")
     self.__merge_dictionary(jdict)