gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / reporters / ureports / nodes.py @ 745
History | View | Annotate | Download (5.12 KB)
1 |
# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
|
---|---|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
|
3 |
#
|
4 |
# This file is part of pylint.
|
5 |
#
|
6 |
# pylint is free software: you can redistribute it and/or modify it under
|
7 |
# the terms of the GNU Lesser General Public License as published by the Free
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) any
|
9 |
# later version.
|
10 |
#
|
11 |
# pylint is distributed in the hope that it will be useful, but WITHOUT
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
14 |
# details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU Lesser General Public License along
|
17 |
# with pylint. If not, see <http://www.gnu.org/licenses/>.
|
18 |
"""Micro reports objects.
|
19 |
|
20 |
A micro report is a tree of layout and content objects.
|
21 |
"""
|
22 |
|
23 |
from six import string_types |
24 |
|
25 |
|
26 |
class VNode(object): |
27 |
|
28 |
def __init__(self, nid=None): |
29 |
self.id = nid
|
30 |
# navigation
|
31 |
self.parent = None |
32 |
self.children = []
|
33 |
|
34 |
def __iter__(self): |
35 |
return iter(self.children) |
36 |
|
37 |
def append(self, child): |
38 |
"""add a node to children"""
|
39 |
self.children.append(child)
|
40 |
child.parent = self
|
41 |
|
42 |
def insert(self, index, child): |
43 |
"""insert a child node"""
|
44 |
self.children.insert(index, child)
|
45 |
child.parent = self
|
46 |
|
47 |
def _get_visit_name(self): |
48 |
"""
|
49 |
return the visit name for the mixed class. When calling 'accept', the
|
50 |
method <'visit_' + name returned by this method> will be called on the
|
51 |
visitor
|
52 |
"""
|
53 |
try:
|
54 |
return self.TYPE.replace('-', '_') |
55 |
except Exception: |
56 |
return self.__class__.__name__.lower() |
57 |
|
58 |
def accept(self, visitor, *args, **kwargs): |
59 |
func = getattr(visitor, 'visit_%s' % self._get_visit_name()) |
60 |
return func(self, *args, **kwargs) |
61 |
|
62 |
def leave(self, visitor, *args, **kwargs): |
63 |
func = getattr(visitor, 'leave_%s' % self._get_visit_name()) |
64 |
return func(self, *args, **kwargs) |
65 |
|
66 |
|
67 |
class BaseLayout(VNode): |
68 |
"""base container node
|
69 |
|
70 |
attributes
|
71 |
* children : components in this table (i.e. the table's cells)
|
72 |
"""
|
73 |
def __init__(self, children=(), **kwargs): |
74 |
super(BaseLayout, self).__init__(**kwargs) |
75 |
for child in children: |
76 |
if isinstance(child, VNode): |
77 |
self.append(child)
|
78 |
else:
|
79 |
self.add_text(child)
|
80 |
|
81 |
def append(self, child): |
82 |
"""overridden to detect problems easily"""
|
83 |
assert child not in self.parents() |
84 |
VNode.append(self, child)
|
85 |
|
86 |
def parents(self): |
87 |
"""return the ancestor nodes"""
|
88 |
assert self.parent is not self |
89 |
if self.parent is None: |
90 |
return []
|
91 |
return [self.parent] + self.parent.parents() |
92 |
|
93 |
def add_text(self, text): |
94 |
"""shortcut to add text data"""
|
95 |
self.children.append(Text(text))
|
96 |
|
97 |
|
98 |
# non container nodes #########################################################
|
99 |
|
100 |
class Text(VNode): |
101 |
"""a text portion
|
102 |
|
103 |
attributes :
|
104 |
* data : the text value as an encoded or unicode string
|
105 |
"""
|
106 |
def __init__(self, data, escaped=True, **kwargs): |
107 |
super(Text, self).__init__(**kwargs) |
108 |
#if isinstance(data, unicode):
|
109 |
# data = data.encode('ascii')
|
110 |
assert isinstance(data, string_types), data.__class__ |
111 |
self.escaped = escaped
|
112 |
self.data = data
|
113 |
|
114 |
|
115 |
class VerbatimText(Text): |
116 |
"""a verbatim text, display the raw data
|
117 |
|
118 |
attributes :
|
119 |
* data : the text value as an encoded or unicode string
|
120 |
"""
|
121 |
|
122 |
# container nodes #############################################################
|
123 |
|
124 |
class Section(BaseLayout): |
125 |
"""a section
|
126 |
|
127 |
attributes :
|
128 |
* BaseLayout attributes
|
129 |
|
130 |
a title may also be given to the constructor, it'll be added
|
131 |
as a first element
|
132 |
a description may also be given to the constructor, it'll be added
|
133 |
as a first paragraph
|
134 |
"""
|
135 |
def __init__(self, title=None, description=None, **kwargs): |
136 |
super(Section, self).__init__(**kwargs) |
137 |
if description:
|
138 |
self.insert(0, Paragraph([Text(description)])) |
139 |
if title:
|
140 |
self.insert(0, Title(children=(title,))) |
141 |
|
142 |
|
143 |
class Title(BaseLayout): |
144 |
"""a title
|
145 |
|
146 |
attributes :
|
147 |
* BaseLayout attributes
|
148 |
|
149 |
A title must not contains a section nor a paragraph!
|
150 |
"""
|
151 |
|
152 |
|
153 |
class Paragraph(BaseLayout): |
154 |
"""a simple text paragraph
|
155 |
|
156 |
attributes :
|
157 |
* BaseLayout attributes
|
158 |
|
159 |
A paragraph must not contains a section !
|
160 |
"""
|
161 |
|
162 |
|
163 |
class Table(BaseLayout): |
164 |
"""some tabular data
|
165 |
|
166 |
attributes :
|
167 |
* BaseLayout attributes
|
168 |
* cols : the number of columns of the table (REQUIRED)
|
169 |
* rheaders : the first row's elements are table's header
|
170 |
* cheaders : the first col's elements are table's header
|
171 |
* title : the table's optional title
|
172 |
"""
|
173 |
def __init__(self, cols, title=None, |
174 |
rheaders=0, cheaders=0, |
175 |
**kwargs): |
176 |
super(Table, self).__init__(**kwargs) |
177 |
assert isinstance(cols, int) |
178 |
self.cols = cols
|
179 |
self.title = title
|
180 |
self.rheaders = rheaders
|
181 |
self.cheaders = cheaders
|