Update deprecation warnings
[pyi2ncommon] / src / cnfline / build_cnfvar.py
1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
3 #
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
6 #
7 # As a special exception, if other files instantiate templates or use macros
8 # or inline functions from this file, or you compile this file and link it
9 # with other works to produce a work based on this file, this file
10 # does not by itself cause the resulting work to be covered
11 # by the GNU General Public License.
12 #
13 # However the source code for this file must still be made available
14 # in accordance with section (3) of the GNU General Public License.
15 #
16 # This exception does not invalidate any other reasons why a work based
17 # on this file might be covered by the GNU General Public License.
18 #
19 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
20
21 """Basic functions for on-the-fly arnied cnf-var generator.
22
23 .. note:: DEPRECATED! Please do not extend this or add new uses of this module,
24           use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar`
25           instead!
26 """
27
28 from .cnfline import CnfLine
29
30
31 class BuildCnfVar(object):
32     """Basic one the fly arnied cnfvar generator"""
33     def __init__(self, name, instance=0, data='', line_no=1):
34         self._parent = CnfLine(name, instance, data, line_no)
35         self._children = {}            # key: lineno, value: CnfLine
36
37     def __getattr__(self, name):
38         # Redirect all unknown attributes to "parent" cnfline
39         return getattr(self._parent, name)
40
41     def find_child_line_no(self, name):
42         """Look for child line number of child NAME"""
43         for lineno, cnfline in self._children.items():
44             if cnfline.name == name:
45                 return lineno
46
47         # Not found
48         return 0
49
50     def find_free_line_no(self):
51         """Find highest currently in use line number"""
52         highest_line_no = self.line_no
53
54         for line_no, unused in self._children.items():
55             if line_no > highest_line_no:
56                 highest_line_no = line_no
57
58         return highest_line_no+1
59
60     def find_free_child_instance(self, name):
61         """Find next free child instance of type NAME"""
62         highest_instance = -1
63
64         for unused, cnfline in self._children.items():
65             if cnfline.name == name and cnfline.instance > highest_instance:
66                 highest_instance = cnfline.instance
67
68         return highest_instance+1
69
70     def update_cnf(self,
71                    name,
72                    instance,
73                    data,
74                    different_parent_line_no=0,
75                    force_append=False):
76         """Update existing cnfline or create new one"""
77         if not force_append:
78             child_line_no = self.find_child_line_no(name)
79         else:
80             child_line_no = 0
81
82         if child_line_no == 0:
83             child_line_no = self.find_free_line_no()
84
85         if instance == -1:
86             instance = self.find_free_child_instance(name)
87
88         parent_line_no = self._parent.line_no
89         if different_parent_line_no:
90             parent_line_no = different_parent_line_no
91
92         new_line = CnfLine(name,
93                            instance,
94                            data,
95                            child_line_no,
96                            parent_line_no)
97
98         self._children[child_line_no] = new_line
99         return child_line_no
100
101     def mark_as_own_parent(self, child_line_no):
102         """Remove parent <-> child relationship for
103            a given cnf line. We use this heavily
104            for the *configure_xxx.py* files"""
105         self._children[child_line_no].parent_line_no = 0
106
107     def add_cnf(self,
108                 name,
109                 instance,
110                 data,
111                 different_parent_line_no=0):
112         return self.update_cnf(name,
113                                instance,
114                                data,
115                                different_parent_line_no,
116                                force_append=True)
117
118     def del_cnf(self, name):
119         """Delete cnfline with name"""
120         for lineno, cnfline in list(self._children.items()):
121             if cnfline.name == name:
122                 del self._children[lineno]
123
124     def add_defaults(self, defaults=None):
125         """Add default values from a simple dictionary"""
126         if defaults is None:
127             return
128
129         for name, value in defaults.items():
130             self.update_cnf(name, 0, value)
131
132     def __str__(self):
133         rtn = str(self._parent) + '\n'
134         for unused, value in self._children.items():
135             rtn = rtn + str(value) + '\n'
136
137         return rtn
138
139     def save(self, filename):
140         """Save string representation to disk."""
141         with open(filename, 'w') as out:
142             out.write(str(self))