1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
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.
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.
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.
19 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
21 """Basic functions for on-the-fly arnied cnf-var generator.
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`
28 from .cnfline import CnfLine
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
37 def __getattr__(self, name):
38 # Redirect all unknown attributes to "parent" cnfline
39 return getattr(self._parent, name)
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:
50 def find_free_line_no(self):
51 """Find highest currently in use line number"""
52 highest_line_no = self.line_no
54 for line_no, unused in self._children.items():
55 if line_no > highest_line_no:
56 highest_line_no = line_no
58 return highest_line_no+1
60 def find_free_child_instance(self, name):
61 """Find next free child instance of type NAME"""
64 for unused, cnfline in self._children.items():
65 if cnfline.name == name and cnfline.instance > highest_instance:
66 highest_instance = cnfline.instance
68 return highest_instance+1
74 different_parent_line_no=0,
76 """Update existing cnfline or create new one"""
78 child_line_no = self.find_child_line_no(name)
82 if child_line_no == 0:
83 child_line_no = self.find_free_line_no()
86 instance = self.find_free_child_instance(name)
88 parent_line_no = self._parent.line_no
89 if different_parent_line_no:
90 parent_line_no = different_parent_line_no
92 new_line = CnfLine(name,
98 self._children[child_line_no] = new_line
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
111 different_parent_line_no=0):
112 return self.update_cnf(name,
115 different_parent_line_no,
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]
124 def add_defaults(self, defaults=None):
125 """Add default values from a simple dictionary"""
129 for name, value in defaults.items():
130 self.update_cnf(name, 0, value)
133 rtn = str(self._parent) + '\n'
134 for unused, value in self._children.items():
135 rtn = rtn + str(value) + '\n'
139 def save(self, filename):
140 """Save string representation to disk."""
141 with open(filename, 'w') as out: