lib.config

This library does the handling and parsing of the configuration of SmartHomeNG.

Warning:This library is part of the core of SmartHomeNG. It should not be called directly from plugins!
lib.config.merge(source, destination)[source]

Merges an OrderedDict Tree into another one

Parameters:
  • source (OrderedDict) – source tree to merge into another one
  • destination (OrderedDict) – destination tree to merge into
Returns:

Merged configuration tree

Return type:

OrderedDict

Example:

Run me with nosetests –with-doctest file.py

>>> a = { 'first' : { 'all_rows' : { 'pass' : 'dog', 'number' : '1' } } }
>>> b = { 'first' : { 'all_rows' : { 'fail' : 'cat', 'number' : '5' } } }
>>> merge(b, a) == { 'first' : { 'all_rows' : { 'pass' : 'dog', 'fail' : 'cat', 'number' : '5' } } }
True
lib.config.parse(filename, config=None, addfilenames=False)[source]

Load and parse a configuration file and merge it to the configuration tree Depending on the extension of the filename, the apropriate parser is called

Parameters:
  • filename (str) – Name of the configuration file
  • config (OrderedDict) – Optional OrderedDict tree, into which the configuration should be merged
Returns:

The resulting merged OrderedDict tree

Return type:

OrderedDict

lib.config.parse_basename(basename, configtype='')[source]

Load and parse a single configuration and merge it to the configuration tree The configuration is only specified by the basename. At the moment it looks for a .yaml file or a .conf file .yaml files take preference

Parameters:
  • basename (str) – Name of the configuration
  • configtype (str) – Optional string with config type (only used for log output)
Returns:

The resulting merged OrderedDict tree

Return type:

OrderedDict

lib.config.parse_conf(filename, config=None)[source]

Load and parse a configuration file which is in the old .conf format of smarthome.py and merge it to the configuration tree

Parameters:
  • filename (str) – Name of the configuration file
  • config (bool) – Optional OrderedDict tree, into which the configuration should be merged
Returns:

The resulting merged OrderedDict tree

Return type:

OrderedDict

The config file should stick to the following setup:

[firstlevel]
    attribute1 = xyz
    attribute2 = foo
    attribute3 = bar
    
    [[secondlevel]]
        attribute1 = abc
        attribute2 = bar
        attribute3 = foo
        
        [[[thirdlevel]]]
            attribute1 = def
            attribute2 = barfoo
            attribute3 = foobar
            
    [[anothersecondlevel]]
        attribute1 = and so on

where firstlevel, secondlevel, thirdlevel and anothersecondlevel are defined as items and attribute are their respective attribute - value pairs

Valid characters for the items are a-z and A-Z plus any digit and underscore as second or further characters. Valid characters for the attributes are the same as for an item plus @ and *

lib.config.parse_itemsdir(itemsdir, item_conf, addfilenames=False)[source]

Load and parse item configurations and merge it to the configuration tree The configuration is only specified by the name of the directory. At the moment it looks for .yaml files and a .conf files Both filetypes are read, even if they have the same basename

Parameters:
  • itemsdir (str) – Name of folder containing the configuration files
  • item_conf (OrderedDict) – Optional OrderedDict tree, into which the configuration should be merged
Returns:

The resulting merged OrderedDict tree

Return type:

OrderedDict

lib.config.parse_yaml(filename, config=None, addfilenames=False)[source]

Load and parse a yaml configuration file and merge it to the configuration tree

Parameters:
  • filename (str) – Name of the configuration file
  • config (bool) – Optional OrderedDict tree, into which the configuration should be merged
Returns:

The resulting merged OrderedDict tree

Return type:

OrderedDict

The config file should stick to the following setup:

firstlevel:
    attribute1: xyz
    attribute2: foo
    attribute3: bar
    
    secondlevel:
        attribute1: abc
        attribute2: bar
        attribute3: foo
        
        thirdlevel:
            attribute1: def
            attribute2: barfoo
            attribute3: foobar
            
    anothersecondlevel:
        attribute1: and so on

where firstlevel, secondlevel, thirdlevel and anothersecondlevel are defined as items and attribute are their respective attribute - value pairs

Valid characters for the items are a-z and A-Z plus any digit and underscore as second or further characters. Valid characters for the attributes are the same as for an item plus @ and *

lib.config.remove_comments(ydata)[source]

Removes comments from a dict or OrderedDict structure

Parameters:ydata (OrderedDict) – configuration (sub)tree to work on
lib.config.remove_digits(ydata)[source]

Removes keys starting with digits from a dict or OrderedDict structure

Parameters:ydata (OrderedDict) – configuration (sub)tree to work on
lib.config.remove_invalid(ydata)[source]

Removes invalid chars in item from a dict or OrderedDict structure

Parameters:ydata (OrderedDict) – configuration (sub)tree to work on
lib.config.remove_keys(ydata, func, remove=['attr'], level=0, msg=None, key_prefix='')[source]

Removes given keys from a dict or OrderedDict structure

Parameters:
  • ydata (OrderedDict) – configuration (sub)tree to work on
  • func (function) – the function to call to check for removal (Example: lambda k: k.startswith(‘comment’))
  • level (int) – optional subtree level (used for recursion)
lib.config.remove_keyword(ydata)[source]

Removes keys that are reserved Python keywords from a dict or OrderedDict structure

Parameters:ydata (OrderedDict) – configuration (sub)tree to work on
lib.config.remove_reserved(ydata)[source]

Removes keys that are reserved keywords from a dict or OrderedDict structure

Parameters:ydata (OrderedDict) – configuration (sub)tree to work on
lib.config.strip_quotes(string)[source]

Strip single-quotes or double-quotes from string beggining and end

Parameters:string (str) – String to strip the quotes from
Returns:Stripped string
Return type:str