8dda1fb3973516c463120859c437c9dbb9ec2597
[pyi2ncommon] / src / cnfline / cnfline.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 class CnfLine(object):
22     """Represents an arnied cnfline"""
23
24     def __init__(self,
25                  name='',
26                  instance=0,
27                  data='',
28                  line_no=1,
29                  parent_line_no=0):
30         self.name = name
31         self.instance = instance
32         self.data = data
33         self.line_no = line_no
34         self.parent_line_no = parent_line_no
35
36         if len(self.name) == 0:
37             raise ValueError("You can't leave the cnfvar name empty")
38
39         if line_no == 0:
40             raise ValueError('Zero is not a valid line number')
41
42     def __str__(self):
43         """Build cnfline string representation"""
44
45         # Sanity checks
46         if len(self.name) == 0:
47             raise ValueError("Can't display empty cnfvar name")
48         if self.line_no == 0:
49             raise ValueError('Zero is not a valid line number')
50
51         if self.parent_line_no:
52             rtn = '{0} ({1})'.format(self.line_no, self.parent_line_no)
53         else:
54             rtn = '{0}'.format(self.line_no)
55
56         rtn += ' {0},{1}: "{2}"'.format(self.name, self.instance, self.data)
57
58         return rtn