Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens...

34
Heracles Documentation Release 0.0.6 Jorge Monforte Sep 27, 2017

Transcript of Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens...

Page 1: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles DocumentationRelease 0.0.6

Jorge Monforte

Sep 27, 2017

Page 2: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python
Page 3: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Contents

1 Status 3

2 Installing 5

3 Testing 7

4 How it works 9

5 Author 13

6 Table of contents 156.1 Parser objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156.2 Data structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

7 Indices and tables 25

Python Module Index 27

i

Page 4: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

ii

Page 5: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

A Python lens configuration file parser.

It uses libheracles to parse configuration files into Python objects. libheracles is a fork of augeas that allows straightaccess to its parser functions. This provides Heracles with a huge collection of already built configuration file parsers,you can view the full list of builtin parsers at augeas.net.

A lens parser is a kind of parser developed for the Armony Project with the idea to parse a file modify the extracteddata and regenerate the file from the data without destroying other information like comments stored in the originalfile.

A lens parser normally works with two methods get and put, the first parses the file creating a tree structure easilymodifiable with any program, then the put method regenerates the file with the modifications keeping the data discardedin the get parse because it simultaneously parses the original file at the same time it regenerates the modified one.

Heracles is open source, you can download it at http://github.com/llou/heracles

You can find an alredy compiled version of the documentation at https://heracles.readthedocs.org/en/latest/index.html

Contents 1

Page 6: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

2 Contents

Page 7: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

CHAPTER 1

Status

Heracles is now in alpha, so you can play with it but its use in production is completely discouraged.

I have tested it on Python 2.6 and 2.7 running in Debian and MacOs. It should work also with 2.5 but I haven’t testedyet.

3

Page 8: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

4 Chapter 1. Status

Page 9: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

CHAPTER 2

Installing

To install it from repository:

$ git clone https://github.com/llou/heracles.git$ cd heracles$ sudo python setup.py install

Should make the thing work.

I have uploaded it to PIP, so the easiest way to get it working is through:

$ sudo pip install heracles

5

Page 10: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

6 Chapter 2. Installing

Page 11: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

CHAPTER 3

Testing

To test if heracles runs in your computer before installing from the repository you have to build libheracles first:

$ python setup.py build_libheracles --inplace

Then you can run the test suites by:

$ python setup.py test

7

Page 12: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

8 Chapter 3. Testing

Page 13: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

CHAPTER 4

How it works

First of all you have to instantiate an Heracles object::

>>> from heracles import Heracles>>> h = Heracles()

This should load all the default lenses. Now if you want to load a lens parser you have two ways of doing it:

• If you know the lens name::

>>> l = h.lenses['Aptsources']

• Given the file you want to parse::

>>> l = h.get_lens_by_path('/etc/apt/sources.list')

Now with the lens parser loaded you can start parsing::

>>> text = file('/etc/apt/sources.list').read()>>> print textdeb http://ftp.es.debian.org/debian/ squeeze main contrib non-free...>>> t = l.get(text)

You get a ListTree object, that you can modify the values using standar Python methods. ListTree objects behave insome ways like list of TreeNode objects.

A ListTree is returned when heracles detects some TreeNode labels are correlative numbers starting at 1. This allowsstraight access to indexed nodes using standar Python syntax. For example lets get the first indexed entry of theconfiguration file::

>>> e = t[0]>>> print e<Heracles.TreeNode label:'1' value:'' children:6>

9

Page 14: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

TreeNodes have three main properties, label and value are strings and children can be a Tree or a ListTree object.If you look close you will see the entry has 6 children, each one is a different TreeNode with its label , value andchildren.

>>> c = e.children>>> for n in c: print n<Heracles.TreeNode label:'type' value:'deb' children:0><Heracles.TreeNode label:'uri' value:'http://ftp.es.debian.org/debian/' children:0><Heracles.TreeNode label:'distribution' value:'squeeze' children:0><Heracles.TreeNode label:'component' value:'main' children:0><Heracles.TreeNode label:'component' value:'contrib' children:0><Heracles.TreeNode label:'component' value:'non-free' children:0>

In this case e.children is Tree objects instead of a ListTree. Tree objects behave someways like dicts, you can accessTreeNodes by their label.:

>>> print c['type'].value'deb'

There is a caveat working with trees as dicts due the support of multiple items with the same label. In this case heraclesreturns an iterable with all nodes with this label.:

>>> for i in c['component']: print i<Heracles.TreeNode label:'component' value:'main' children:0><Heracles.TreeNode label:'component' value:'contrib' children:0><Heracles.TreeNode label:'component' value:'non-free' children:0>

To select which node you use it’s index::

>>> n = c['component'][2]>>> print n.value'non-free'

Tree objects also allow straight access through the index of the node. So if you want to get the first children::

>>> print c[0]<Heracles.TreeNode label:'type' value:'deb' children:0>

You can modify the tree using standar methods:

>>> c.remove(n)>>> for i in c['component']: print i<Heracles.TreeNode label:'component' value:'main' children:0><Heracles.TreeNode label:'component' value:'contrib' children:0>

If you want to modify an existing entry through it’s label you have to remember to set the index of list of nodes withthat label. If you don’t do it, it will append a new entry:

>>> c['uri'][0] = 'http://ftp.uk.debian.org/debian/'>>> for n in c: print n<Heracles.TreeNode label:'type' value:'deb' children:0><Heracles.TreeNode label:'uri' value:'http://ftp.uk.debian.org/debian/' children:0><Heracles.TreeNode label:'distribution' value:'squeeze' children:0><Heracles.TreeNode label:'component' value:'main' children:0><Heracles.TreeNode label:'component' value:'contrib' children:0>

Now with the updated tree object we can regenerate the file.:

10 Chapter 4. How it works

Page 15: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

>>> text = l.put(t, '')>>> print textdeb http://ftp.uk.debian.org/debian/ squeeze main contrib...

As you can see it updated the file.

11

Page 16: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

12 Chapter 4. How it works

Page 17: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

CHAPTER 5

Author

Heracles is developed by Jorge Monforte <[email protected]> to provide his remote automation tool Panop-ticon of a configuration file parser.

Copyright 2013 Jorge Monforte. Distributed under the LGPL license.

13

Page 18: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

14 Chapter 5. Author

Page 19: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

CHAPTER 6

Table of contents

Parser objects

This module includes the core of the Heracles package

Heracles main object

This is the base class of the Heracles package. It is recomended to be instantiated once for app as it is pretty memoryhungry as it loads all the lenses at the beginning and it takes its time doing it.

class heracles.base.Heracles(loadpath=None, flags=0)Main heracles object. Loads lenses into memory and makes them accessible to the user.

Attribute: lenses - stores the Lens instances of the instance. It is an instance of HeraclesLenses.

Can be instantiatd with these parameters:

Parameters

• loadpath (list of strings) – Paths to search for aditional lenses.

• flags (int) – Flags to pass to de libheracles init function, mostly useless.

lensesReturns a HeraclesLensesDescriptor to allow access to the lens database.

get_lens_by_path(path)Return the propper lens to parse a file given its path.

Parameters path (str) – Path of the file which its lens we want to edit.

parse_file_from_path(path)Returns a Tree object parsing the file given its path.

The Tree object stores the path so it can be saved using its heracles.tree.Tree.save() method.

Parameters path (str) – Path of the file we want to parse.

15

Page 20: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

Lens descriptor

class heracles.base.HeraclesLensesDescriptorA descriptor object to access loaded lenses by the Heracles instance.

It allows straight access to the lens by its name using standard dict syntax. So if you want to load the lens toparse the sources file of the Debian/Ubuntu package management system APT you would do:

>> h = Heracles()>> parser = h.lenses['Aptsources']

The names of the lenses can be found at augeas.net

Lens parser object

Lens objects can be obtained by its name or the path of the file you want to parse. One instantiated, the parser instancecomes with two methods, get and put.

• The Lens.get() methods reads the text file and creates a python data structure with its contents.

• The Lens.put() method allows regenerating the file with from the python data structure, allowing to re-cover discarded information in the original parse of the file if its contents are provided in the optional keywordparameter text.

class heracles.base.Lens(heracles, module)Object that stores an augeas lens parser ready to do get and put operations

Note: This object is not intended to be instantiated manually but from the internal methods of the Heraclesobject instance

Parameters

• heracles (Heracles) – An heracles object instance

• module (struct_module) – A ctypes reference to a module struct.

get(text)Returns a tree from applying the lens parser to text.

Parameters text (str) – The text to parse.

Return type heracles.tree.Tree

put(tree, text=’‘)Returns the dumped data from the tree after applying the inverse lens parser to the lens.

If text is given it uses it to merge with the data of the tree.

Parameters tree (heracles.tree.Tree) – The tree from to generate the text.

Key text Optional text to merge in the generation.

Return type str

check_path(path)Tests if the given path matches the criteria of the lens.

Parameters path (str) – A path to search its lens

Return type bool

16 Chapter 6. Table of contents

Page 21: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

Data structures

Heracles data structures are a replica in python of the original augeas ones. The parsed data in augeas is stored in itstree structure that is defined in the file src/internal.h

struct tree {struct tree *next;struct tree *parent;char *label;struct tree *children;char *value;int dirty;struct span *span;

};

This creates an ordered tree structure:

• tree : Each node has a parent field that relates it to an higher level of the tree and a children that referes tothe first node of a lower level of the tree.

• ordered : Each node of a subtree level1 is related with the following one through the next field.

As you can see there are two string fields:

• label: is used to store a text to index the data. Some times it stores the string form of an integer to create a listof ordered nodes starting at the value of 1.

• value: the content stored in the node.

In heracles threre is no use of the last two definitions dirty and span.

This structure is implemented in the form of the class TreeNode and its sibling ListTreeNode, both contains alabel and a value attribute to store the info, a parent attribute to relate to its parent in case of it has one and a childrenattribute in form of a Tree or ListTree to store the subnodes of this one.

To keep the order between the nodes of the same level of a tree branch heracles implements the class Tree thatbasically store the node in a list (in the hidden attribute ._nodes) and provides several helping methods to assist in themanipulation of the sequence of nodes.

For assisting in the manipulation tree levels1 that contains enumeration of nodes, those who’s labels are a sequence ofintegers starting at one, heracles implements its classes ListTreeNode and ListTree. This allows treating thesenodes like an standar Python list, providing extra functionallity to access other non-enumerated nodes that can been the same tree level1.

Trees

Heracles stores parsed data in trees and can be of two different classes:

class heracles.tree.Tree(parent=None, nodes=None, lens=None, path=None, de-fault_node_class=None)

The basic object to store data returned from the lens parser. A Tree object stores a list of TreeNode andprovides several methods to manage them using standar python syntax.

Tree nodes behave someways like dict objects but as the augeas parser data structure allow multiple objectswith the same label, when accessing them through the container syntax instead of returning a single object, itreturns a LabelNodeList instance, which is basically an object to manage the sequence of objects with thesame label.

1 by tree level and subtree level I mean the nodes that share the same parent.

6.2. Data structures 17

Page 22: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

The Tree objects and their subclasses are also used to handle the heracles.tree.TreeNode.childrenattribute of the TreeNode instances.

Parameters

• parent – If it is a children tree, the parent node.

• nodes (list of TreeNode) – The list of nodes that the tree manages

• lens (heracles.base.Lens) – If it is a master tree, it stores the lens object to allowthe put method to render back the text file.

• path (str) – If it is a master tree, it stores the path of the parsed file to allow the savemethod to store the changes.

• default_node_class (TreeNode or ListTreeNode) – The default node class tobe used when creating automaticaly new nodes.

Examples:

#Create a Tree object:>>> t = Tree()#Add a new node to the tree:>>> t.add_new_node(label="new_node", value="value")#Get the first node of the tree:>>> t[0]<TreeNode label:'new_node' value:'value' children:0>#Get the LabelNodeList instance of nodes with label 'new_node':>>> t['new_node']<LabelNodeList label:'new_node' values:'value'>#Get the first node of the LabelNodeList:>>> t['new_node'][0]<TreeNode label:'new_node' value:'value' children:0>

classmethod build_from_parent(parent, nodes, default_node_class)Builds the children Tree of a node given the parent and its nodes.

Parameters

• parent (TreeNode) – The parent node to create the children class.

• nodes (list of TreeNode or ListTreeNode.) – The children nodes of the parentclass.

• default_node_class (TreeNode or ListTreeNode) – The default node class tobe used when creating automaticaly new nodes.

insert(index, node)Insert node into index place.

Parameters

• node – The instance of TreeNode or :class:’ListTreeNode’ to insert.

• index (int) – The position where to insert the node.

append(node)Appends node to tree.

Parameters node – The instance of TreeNode or :class:’ListTreeNode’ to append.

has_key(name)Checks if the tree has any node with label name.

Parameters name (str) – The label to search for.

18 Chapter 6. Table of contents

Page 23: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

Return type bool

index(node)If the tree stores node returns its position in the tree.

Parameters node – The instance of TreeNode or :class:’ListTreeNode’ to get its index.

Return type int

remove(node)If the tree stores node removes the node from the tree.

Parameters node – The instance of TreeNode or :class:’ListTreeNode’ to remove from tree.

add_new_node(label=’‘, value=’‘, node_class=None)Appends a new node with the given label and value. If not node_class is given it uses the tree’sdefault_node_class.

Parameters

• label (str) – The label of the new node.

• value (str) – The value of the new node.

• node_class – The class of the node to create either TreeNode or ListTreeNode

Return type TreeNode

insert_new_node(index, label=’‘, value=’‘, node_class=None)Inserts a new node with the given label and value in the position given by index. If notnode_class is given it uses the tree’s default_node_class.

Parameters

• index (int) – The position where to insert the node.

• label (str) – The label of the new node.

• value (str) – The value of the new node.

• node_class – The class of the node to create either TreeNode or ListTreeNode

Return type TreeNode

add_new_list_node(label=’‘, value=’‘)Appends a new ListTreeNode instance with the given label and value.

Parameters

• label (str) – The label of the new node.

• value (str) – The value of the new node.

Return type ListTreeNode

insert_new_list_node(index, label=’‘, value=’‘)Inserts a new ListTreeNode instance with the given label and value in the position given byindex.

Parameters

• index (int) – The position where to insert the node.

• label (str) – The label of the new node.

• value (str) – The value of the new node.

Return type ListTreeNode

6.2. Data structures 19

Page 24: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

put(text=’‘)If it is a master node it renders back to text applying the lens parser.

Parameters text (str) – If given it uses it to merge with the data of the tree.

Return type str

save(text=’‘)If it is a master node it renders back to text applying the lens parser and saves the result to the file in path.

Parameters text (str) – If given it uses it to merge with the data of the tree.

__iter__()Iterates over all nodes.

__getitem__(name)If name is str returns a LabelNodeList of the nodes with label name. If it is integer returns self._nodes[name]

__setitem__(name, value)The behaviour depends of the types of the arguments.

If name is string:

•if value is instance of TreeNode appends the node setting its label to name

•if value is string appends a new node with label name and value value.

Danger: It doesn’t change existing nodes with that label.

If name is int:

•if value is instance of TreeNode replaces existing node at self._nodes[name] with value

•if value is string replaces self._nodes[name].value with value.

__contains__(value)If value is TreeNode check if it is in self._nodes, if it is string checks if there is any node withvalue as label.

__len__()Returns the number of all nodes.

class heracles.tree.ListTree(parent=None, nodes=None, lens=None, path=None, de-fault_node_class=None)

This is a subclass of Tree to handle list of nodes wich uses counter as label, so it can be handled like a regularpython list object.

Parameters

• parent – If it is a children tree, the parent node.

• nodes (list of TreeNode) – The list of nodes that the tree manages

• lens (heracles.base.Lens) – If it is a master tree, it stores the lens object to allowthe put method to render back the text file.

• path (str) – If it is a master tree, it stores the path of the parsed file to allow the savemethod to store the changes.

• default_node_class (TreeNode or ListTreeNode) – The default node class tobe used when creating automaticaly new nodes.

20 Chapter 6. Table of contents

Page 25: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

Examples:

# Create ListTree object>>> from heracles import ListTree, TreeNode>>> t = ListTree()# Add new node>>> t.add_new_node("first")>>> t[0]<TreeNode label:'1' value:'first' children:0># You can see label index is always 1 + python index# Now lets add a non indexed node>>> t['#comment'] = "None"# The node is not accesible from numeric index>>> t[1]...IndexError: Index out of range# But it is accessible though its label.>>> t['#comment']<LabelNodeList label:'#comment' values:'None'># Lets add another node>>> t.add_new_node("second")>>> t[2]<TreeNode label:'2' value:'second' children:0># The comment is hidden and the second item is the third# on the list of nodes

insert(index, tree_node)Insert node into index place.

Parameters node – The instance of TreeNode to get its index.

Return type int

append(tree_node)Appends node to tree.

Parameters node – The instance of TreeNode or ListTreeNode to append.

index(tree_node)If the tree stores node returns its position in the tree.

Parameters node – The instance of TreeNode or ListTreeNode to get its index.

Return type int

remove(tree_node)If the tree stores node removes the node from the tree.

Parameters node – The TreeNode or ListTreeNode to remove from tree.

add_new_node(value, node_class=None)Appends a new node with the given label and value. If not node_class is given it uses the tree’sdefault_node_class.

Return type TreeNode

insert_new_node(index, value, node_class=None)Inserts a new node with the given label and value in the position given by index. If notnode_class is given it uses the tree’s default_node_class.

Return type TreeNode

6.2. Data structures 21

Page 26: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

add_new_list_node(value)Appends a new ListTreeNode instance with the given label and value.

Return type ListTreeNode

insert_new_list_node(index, value)Inserts a new ListTreeNode instance with the given label and value in the position given byindex.

Return type ListTreeNode

search(**key_values)As list trees behaves most of the time like database table: a list of key-values, this method allows you toquery the table that have certain key-value pairs expressed as the keywords.

A keyword named __value referes to the value of the ListTree node itself.

__getitem__(name)If name is str returns a LabelNodeList of the nodes with label name. If it is integer returns theindexed node at position name.

__setitem__(name, value)The behaviour depends of the types of the arguments.

If name is string:

•if value is instance of TreeNode appends the node setting its label to name

•if value is string appends a new node with label name and value value.

Danger: It doesn’t change existing nodes with that label.

If name is int:

•if value is instance of TreeNode replaces existing indexed node at position name with value

•if value is string replaces indexed node at position name with value.

__contains__(value)If value is TreeNode check if it is among indexed nodes , if it is string checks if there is any indexednode with value as label.

__iter__()Iterates over indexed nodes.

__len__()Returns the number of indexed nodes in the tree.

Nodes

To store each branch of the tree Heracles uses node objects:

class heracles.tree.TreeNode(label=’‘, value=’‘, parent=None, children=None, de-fault_children_class=None)

Basic storage unit of Heracles. Each Tree or ListTree instance contains a list of TreeNode instance or itssubclasses.

The TreeNode.children is a Tree or ListTree instance.

Parameters

22 Chapter 6. Table of contents

Page 27: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

• label (str) – A value that classifies the node.

• value (str) – The value stored in the node.

• parent (TreeNode or ListTreeNode) – If it is a children node, its parent.

• children (class:Tree) – The list of nodes that are its children.

• default_children_class (type) – Default node class to instantiate children, canbe TreeNode or ListTreeNode.

children_classalias of Tree

class heracles.tree.ListTreeNode(label=’‘, value=’‘, parent=None, children=None, de-fault_children_class=None)

This is a TreeNode subclass to store its children in a ListTree instance.

Parameters

• label (str) – A value that classifies the node.

• value (str) – The value stored in the node.

• parent (TreeNode or ListTreeNode) – If it is a children node, its parent.

• children (class:ListTree) – The list of nodes that are its children.

• default_children_class (type) – Default node class to instantiate children, canbe TreeNode or ListTreeNode.

children_classalias of ListTree

LabelNodeList object

As the augeas tree structure allows multiple nodes with the same label a class to manage them it is required.

To allow the common case of an unique label, the instance behaves like a node implementing label, value get-ters and setters to access the single node. In case of multiple nodes it raises. heracles.exceptions.HeraclesTreeLabelError

class heracles.tree.LabelNodeList(tree, label)A helper class that allows access to several nodes with the same name.

Nodes can be accessed with a mixture of dict and list methods, so you can get the container methods to accessnumerically returning or setting according to the order in the list, or by an string key that represents the value ofthe node.

If there is a single node in the list you can use the value and children of the LabelNodeList instanceto access its contents.

classmethod build(tree, label)Checks if there are nodes with label in tree before instantiating.

Parameters

• tree (Tree or ListTree) – The tree that contains the nodes.

• label (str) – The label that it is looking to build the LabelNodeList

Return type LabelNodeList

valueIf there is a single node in the list returns its value.

6.2. Data structures 23

Page 28: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

childrenIf there is a single node in the list returns its children.

parentReturns the parent of the tree instance.

insert(index, item)Insert node into index place, the order is keept inside the tree.

Parameters

• index (int) – The position where to insert the node.

• item – It can be a node or a value to build a new node.

remove(item)If the tree stores node removes the node from the tree.

Parameters item – The node to remove.

append(item)Adds a node to the tree setting its label to the instance label.

Parameters item – It can be a node or a value to build a new node.

Auxiliary functions

heracles.tree.check_list_nodes(nodes)Utility function that detects if nodes can be propertly managed by a ListTree object.

Parameters nodes – A list of TreeNode.

Return type bool

heracles.tree.get_tree_from_nodes(nodes, lens=None)Function that selects the best kind of tree object to manage some nodes. This function is intended to build theroot tree

Parameters

• nodes – A list of TreeNode.

• lens (heracles.base.Lens) – The lens where the tree is generated from.

Return type Tree or ListTree.

heracles.tree.get_node(children, label=’‘, value=’‘, parent=None)Given a sequence of children nodes returns the right parent instance to handle them

Parameters

• children – The list of TreeNode of we build its parent. TreeNode.

• label (str) – The label of the node.

• value (str) – The value of the node.

• parent (Tree or ListTree.) – The parent of the node.

Return type TreeNode or ListTreeNode.

24 Chapter 6. Table of contents

Page 29: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

CHAPTER 7

Indices and tables

• genindex

• modindex

• search

25

Page 30: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

26 Chapter 7. Indices and tables

Page 31: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Python Module Index

hheracles.base, 15heracles.tree, 17

27

Page 32: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

28 Python Module Index

Page 33: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Index

Symbols__contains__() (heracles.tree.ListTree method), 22__contains__() (heracles.tree.Tree method), 20__getitem__() (heracles.tree.ListTree method), 22__getitem__() (heracles.tree.Tree method), 20__iter__() (heracles.tree.ListTree method), 22__iter__() (heracles.tree.Tree method), 20__len__() (heracles.tree.ListTree method), 22__len__() (heracles.tree.Tree method), 20__setitem__() (heracles.tree.ListTree method), 22__setitem__() (heracles.tree.Tree method), 20

Aadd_new_list_node() (heracles.tree.ListTree method), 21add_new_list_node() (heracles.tree.Tree method), 19add_new_node() (heracles.tree.ListTree method), 21add_new_node() (heracles.tree.Tree method), 19append() (heracles.tree.LabelNodeList method), 24append() (heracles.tree.ListTree method), 21append() (heracles.tree.Tree method), 18

Bbuild() (heracles.tree.LabelNodeList class method), 23build_from_parent() (heracles.tree.Tree class method), 18

Ccheck_list_nodes() (in module heracles.tree), 24check_path() (heracles.base.Lens method), 16children (heracles.tree.LabelNodeList attribute), 23children_class (heracles.tree.ListTreeNode attribute), 23children_class (heracles.tree.TreeNode attribute), 23

Gget() (heracles.base.Lens method), 16get_lens_by_path() (heracles.base.Heracles method), 15get_node() (in module heracles.tree), 24get_tree_from_nodes() (in module heracles.tree), 24

Hhas_key() (heracles.tree.Tree method), 18Heracles (class in heracles.base), 15heracles.base (module), 15heracles.tree (module), 17HeraclesLensesDescriptor (class in heracles.base), 16

Iindex() (heracles.tree.ListTree method), 21index() (heracles.tree.Tree method), 19insert() (heracles.tree.LabelNodeList method), 24insert() (heracles.tree.ListTree method), 21insert() (heracles.tree.Tree method), 18insert_new_list_node() (heracles.tree.ListTree method),

22insert_new_list_node() (heracles.tree.Tree method), 19insert_new_node() (heracles.tree.ListTree method), 21insert_new_node() (heracles.tree.Tree method), 19

LLabelNodeList (class in heracles.tree), 23Lens (class in heracles.base), 16lenses (heracles.base.Heracles attribute), 15ListTree (class in heracles.tree), 20ListTreeNode (class in heracles.tree), 23

Pparent (heracles.tree.LabelNodeList attribute), 24parse_file_from_path() (heracles.base.Heracles method),

15put() (heracles.base.Lens method), 16put() (heracles.tree.Tree method), 19

Rremove() (heracles.tree.LabelNodeList method), 24remove() (heracles.tree.ListTree method), 21remove() (heracles.tree.Tree method), 19

Ssave() (heracles.tree.Tree method), 20

29

Page 34: Heracles Documentation - Read the Docs · Heracles Documentation, Release 0.0.6 A Python lens configuration file parser. It useslibheraclesto parse configuration files into Python

Heracles Documentation, Release 0.0.6

search() (heracles.tree.ListTree method), 22

TTree (class in heracles.tree), 17TreeNode (class in heracles.tree), 22

Vvalue (heracles.tree.LabelNodeList attribute), 23

30 Index