NodeData   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 95
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __str__() 0 2 1
B __init__() 0 17 5
1
# Copyright (c) 2016 Fabian Kochem
2
3
4
class NodeData(object):
5
    """
6
    Immutable data-holding object which represents tree node data. Its
7
    attributes are identical to the columns in the ``nodes`` table
8
    (see :ref:`db_model`).
9
10
    Since the object is immutable, you must retrieve a new instance
11
    of the same node using :func:`libtree.core.query.get_node` to get
12
    updated values.
13
14
    To manipulate the values, you must use one of the following
15
    functions:
16
17
    * :func:`libtree.core.tree.change_parent`
18
    * :ref:`core-positioning`
19
    * :ref:`core-properties`
20
21
    Most ``libtree`` functions need a database ID in order to know on
22
    which data they should operate, but also accept ``Node`` objects
23
    to make handling with them easier.
24
25
    All parameters are optional and default to ``None``.
26
27
    :param int id: ID of the node as returned from the database
28
    :param parent: Reference to a parent node
29
    :type parent: Node or int
30
    :param int position: Position in between siblings
31
                         (see :ref:`core-positioning`)
32
    :param dict properties: Inheritable key/value pairs
33
                            (see :ref:`core-properties`)
34
    """
35
    __slots__ = [
36
        '_NodeData__id',
37
        '_NodeData__parent',
38
        '_NodeData__position',
39
        '_NodeData__properties',
40
    ]
41
42
    def __init__(self, id=None, parent=None, position=None, properties=None):
43
        self.__id = None
44
        if id is not None:
45
            self.__id = id
46
47
        self.__parent = None
48
        if parent is not None:
49
            self.__parent = parent
50
51
        self.__position = None
52
        if position is not None:
53
            self.__position = int(position)
54
55
        if isinstance(properties, dict):
56
            self.__properties = properties
57
        else:
58
            self.__properties = {}
59
60
    def __str__(self):
61
        return self.id
62
63
    def to_dict(self):
64
        """ Return dictionary containing all values of the object. """
65
        return {
66
            'id': self.id,
67
            'parent': self.parent,
68
            'position': self.position,
69
            'properties': self.properties
70
        }
71
72
    def __repr__(self):
73
        if 'title' in self.properties:
74
            ret = "<NodeData id={!r}, title='{!s}'>"
75
            return ret.format(self.id, self.properties['title'])
76
        else:
77
            ret = '<NodeData id={!r}>'
78
            return ret.format(self.id, self.parent)
79
80
    @property
81
    def id(self):
82
        """ Node ID """
83
        return self.__id
84
85
    @property
86
    def parent(self):
87
        """ Parent ID """
88
        return self.__parent
89
90
    @property
91
    def position(self):
92
        """ Position in between its siblings """
93
        return self.__position
94
95
    @property
96
    def properties(self):
97
        """ Node properties """
98
        return self.__properties
99