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 / __init__.py @ 745
History | View | Annotate | Download (3.47 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 |
"""Universal report objects and some formatting drivers.
|
19 |
|
20 |
A way to create simple reports using python objects, primarily designed to be
|
21 |
formatted as text and html.
|
22 |
"""
|
23 |
import os |
24 |
import sys |
25 |
|
26 |
import six |
27 |
|
28 |
|
29 |
class BaseWriter(object): |
30 |
"""base class for ureport writers"""
|
31 |
|
32 |
def format(self, layout, stream=None, encoding=None): |
33 |
"""format and write the given layout into the stream object
|
34 |
|
35 |
unicode policy: unicode strings may be found in the layout;
|
36 |
try to call stream.write with it, but give it back encoded using
|
37 |
the given encoding if it fails
|
38 |
"""
|
39 |
if stream is None: |
40 |
stream = sys.stdout |
41 |
if not encoding: |
42 |
encoding = getattr(stream, 'encoding', 'UTF-8') |
43 |
self.encoding = encoding or 'UTF-8' |
44 |
self.out = stream
|
45 |
self.begin_format()
|
46 |
layout.accept(self)
|
47 |
self.end_format()
|
48 |
|
49 |
def format_children(self, layout): |
50 |
"""recurse on the layout children and call their accept method
|
51 |
(see the Visitor pattern)
|
52 |
"""
|
53 |
for child in getattr(layout, 'children', ()): |
54 |
child.accept(self)
|
55 |
|
56 |
def writeln(self, string=u''): |
57 |
"""write a line in the output buffer"""
|
58 |
self.write(string + os.linesep)
|
59 |
|
60 |
def write(self, string): |
61 |
"""write a string in the output buffer"""
|
62 |
self.out.write(string)
|
63 |
|
64 |
def begin_format(self): |
65 |
"""begin to format a layout"""
|
66 |
self.section = 0 |
67 |
|
68 |
def end_format(self): |
69 |
"""finished to format a layout"""
|
70 |
|
71 |
def get_table_content(self, table): |
72 |
"""trick to get table content without actually writing it
|
73 |
|
74 |
return an aligned list of lists containing table cells values as string
|
75 |
"""
|
76 |
result = [[]] |
77 |
cols = table.cols |
78 |
for cell in self.compute_content(table): |
79 |
if cols == 0: |
80 |
result.append([]) |
81 |
cols = table.cols |
82 |
cols -= 1
|
83 |
result[-1].append(cell)
|
84 |
# fill missing cells
|
85 |
while len(result[-1]) < cols: |
86 |
result[-1].append(u'') |
87 |
return result
|
88 |
|
89 |
def compute_content(self, layout): |
90 |
"""trick to compute the formatting of children layout before actually
|
91 |
writing it
|
92 |
|
93 |
return an iterator on strings (one for each child element)
|
94 |
"""
|
95 |
# Patch the underlying output stream with a fresh-generated stream,
|
96 |
# which is used to store a temporary representation of a child
|
97 |
# node.
|
98 |
out = self.out
|
99 |
try:
|
100 |
for child in layout.children: |
101 |
stream = six.StringIO() |
102 |
self.out = stream
|
103 |
child.accept(self)
|
104 |
yield stream.getvalue()
|
105 |
finally:
|
106 |
self.out = out
|