lib.shyaml

This library does the handling of the configuration files of SmartHomeNG in yaml format. All file i/o from and to these configuration files goes through the functions which are implemented in this library.

Warning:This library is part of the core of SmartHomeNG. It should not be called directly from plugins!
lib.shyaml.convert_linenumber(s, occ=1)[source]
lib.shyaml.editing_is_enabled()[source]
lib.shyaml.get_commentedseq(l)[source]

Convert a list to a commented sequence

lib.shyaml.get_emptynode()[source]

Return an empty node

lib.shyaml.get_key(path)[source]
lib.shyaml.get_parent(path)[source]
lib.shyaml.setInDict(dataDict, path, value)[source]
lib.shyaml.writeBackToFile(filename, itempath, itemattr, value)[source]

write the value of an item’s attribute back to the yaml-file

Parameters:
  • filename – name of the yaml-file (without the .yaml extension!)
  • itempath – path of the item to modify
  • itemattr – name of the item’s attribute to modify
  • value – new value for the attribute
Returns:

formatted string

lib.shyaml.yaml_dump_roundtrip(data)[source]

Dump yaml to a string using the RoundtripDumper and correct linespacing in output file

Parameters:data – data structure to save
lib.shyaml.yaml_load(filename, ordered=False, ignore_notfound=False)[source]

Load contents of a configuration file into an dict/OrderedDict structure. The configuration file has to be a valid yaml file

Parameters:
  • filename (str) – name of the yaml file to load
  • ordered (bool) – load to an OrderedDict? Default=False
Returns:

configuration data loaded from the file (or None if an error occured)

Return type:

Dict | OrderedDict | None

lib.shyaml.yaml_load_fromstring(string, ordered=False)[source]

Load contents of a string into an dict/OrderedDict structure. The string has to be valid yaml

Parameters:
  • string (str) – name of the yaml file to load
  • ordered (bool) – load to an OrderedDict? Default=False
Returns:

configuration data loaded from the file (or None if an error occured)

Return type:

Dict | OrderedDict | None

lib.shyaml.yaml_load_roundtrip(filename)[source]

Load contents of a yaml file into an dict structure for editing (using Roundtrip Loader)

Parameters:filename – name of the yaml file to load
Returns:data structure loaded from file
lib.shyaml.yaml_save(filename, data)[source]

Save contents of an OrderedDict structure to a yaml file

Parameters:
  • filename (str) – name of the yaml file to save to
  • data (OrderedDict) – configuration data to to save
Returns:

Nothing

lib.shyaml.yaml_save_roundtrip(filename, data, create_backup=False)[source]

Dump yaml using the RoundtripDumper and correct linespacing in output file

Parameters:
  • filename – name of the yaml file to save to
  • data – data structure to save
class lib.shyaml.yamlfile(filename, filename_write='', create_bak=False)[source]

Bases: object

data = None
filename = ''
getnode(path)[source]

get the contents of a node (branch or leaf)

Parameters:path – path of the node to return
Returns:content of the node
getnodetype(path)[source]

get the type of a node

Parameters:path – path of the node to return
Returns:node type (‘branch’, ‘leaf’ or ‘none’)
getvalue(path)[source]

get the value of a leaf-node

Parameters:path – path of the node to return
Returns:value of the leaf (or None, if the node is no leaf-node)
getvaluetype(path)[source]

get the valuetype of a node

Parameters:path – path of the node to return
Returns:node valuetype
load()[source]

load the contents of the yaml-file to the data-structure

save()[source]

save the contents of the data-structure to the yaml-file

setleafvalue(branch, leaf, value)[source]

set the value of a leaf, specified by branch-path and attribute name

Parameters:
  • branch – path of the branch-node which contains th attribute
  • attr – name of the attribute to modify
  • value – new value of the attribute
setvalue(path, value)[source]

set the value of a leaf, specified by leaf-path

Parameters:
  • path – path of the leaf-node to modify
  • value – new value of the leaf-node