ia64/xen-unstable

view tools/python/xen/xend/PrettyPrint.py @ 4947:6556b9a0ffa4

bitkeeper revision 1.1421.1.1 (4289b3a0n7JmXiz6fiz0QAfqJ_M1Lg)

XendDomainInfo.py:
Redirect debug output to log.
PrettyPrint.py:
Add prettyprintstring.
XendDomainInfo.py:
Remove debug print.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Tue May 17 09:04:32 2005 +0000 (2005-05-17)
parents 236a9f2698a3
children 5b730cb3857a
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 """General pretty-printer, including support for SXP.
5 """
6 import sys
7 import types
8 import StringIO
9 import sxp
11 class PrettyItem:
13 def __init__(self, width):
14 self.width = width
16 def insert(self, block):
17 block.addtoline(self)
19 def get_width(self):
20 return self.width
22 def output(self, out):
23 print '***PrettyItem>output>', self
24 pass
26 def prettyprint(self, out, width):
27 print '***PrettyItem>prettyprint>', self
28 return width
30 class PrettyString(PrettyItem):
32 def __init__(self, x):
33 PrettyItem.__init__(self, len(x))
34 self.value = x
36 def output(self, out):
37 out.write(self.value)
39 def prettyprint(self, line):
40 line.output(self)
42 def show(self, out):
43 print >> out, ("(string (width %d) '%s')" % (self.width, self.value))
45 class PrettySpace(PrettyItem):
47 def output(self, out):
48 out.write(' ' * self.width)
50 def prettyprint(self, line):
51 line.output(self)
53 def show(self, out):
54 print >> out, ("(space (width %d))" % self.width)
56 class PrettyBreak(PrettyItem):
58 def __init__(self, width, indent):
59 PrettyItem.__init__(self, width)
60 self.indent = indent
61 self.space = 0
62 self.active = 0
64 def output(self, out):
65 out.write(' ' * self.width)
67 def prettyprint(self, line):
68 if line.breaks(self.space):
69 self.active = 1
70 line.newline(self.indent)
71 else:
72 line.output(self)
74 def show(self, out):
75 print >> out, ("(break (width %d) (indent %d) (space %d) (active %d))"
76 % (self.width, self.indent, self.space, self.lspace, self.active))
78 class PrettyNewline(PrettySpace):
80 def __init__(self, indent):
81 PrettySpace.__init__(self, indent)
83 def insert(self, block):
84 block.newline()
85 block.addtoline(self)
87 def output(self, out):
88 out.write(' ' * self.width)
90 def prettyprint(self, line):
91 line.newline(0)
92 line.output(self)
94 def show(self, out):
95 print >> out, ("(nl (indent %d))" % self.indent)
97 class PrettyLine(PrettyItem):
98 def __init__(self):
99 PrettyItem.__init__(self, 0)
100 self.content = []
102 def write(self, x):
103 self.content.append(x)
105 def end(self):
106 width = 0
107 lastwidth = 0
108 lastbreak = None
109 for x in self.content:
110 if isinstance(x, PrettyBreak):
111 if lastbreak:
112 lastbreak.space = (width - lastwidth)
113 lastbreak = x
114 lastwidth = width
115 width += x.get_width()
116 if lastbreak:
117 lastbreak.space = (width - lastwidth)
118 self.width = width
120 def prettyprint(self, line):
121 for x in self.content:
122 x.prettyprint(line)
124 def show(self, out):
125 print >> out, '(LINE (width %d)' % self.width
126 for x in self.content:
127 x.show(out)
128 print >> out, ')'
130 class PrettyBlock(PrettyItem):
132 def __init__(self, all=0, parent=None):
133 self.width = 0
134 self.lines = []
135 self.parent = parent
136 self.indent = 0
137 self.all = all
138 self.broken = 0
139 self.newline()
141 def add(self, item):
142 item.insert(self)
144 def end(self):
145 self.width = 0
146 for l in self.lines:
147 l.end()
148 if self.width < l.width:
149 self.width = l.width
151 def breaks(self, n):
152 return self.all and self.broken
154 def newline(self):
155 self.lines.append(PrettyLine())
157 def addtoline(self, x):
158 self.lines[-1].write(x)
160 def prettyprint(self, line):
161 self.indent = line.used
162 line.block = self
163 if not line.fits(self.width):
164 self.broken = 1
165 for l in self.lines:
166 l.prettyprint(line)
167 line.block = self.parent
169 def show(self, out):
170 print >> out, ('(BLOCK (width %d) (indent %d) (all %d) (broken %d)' %
171 (self.width, self.indent, self.all, self.broken))
172 for l in self.lines:
173 l.show(out)
174 print >> out, ')'
176 class Line:
178 def __init__(self, out, width):
179 self.out = out
180 self.width = width
181 self.used = 0
182 self.space = self.width
184 def newline(self, indent):
185 indent += self.block.indent
186 self.out.write('\n')
187 self.out.write(' ' * indent)
188 self.used = indent
189 self.space = self.width - self.used
191 def fits(self, n):
192 return self.space - n >= 0
194 def breaks(self, n):
195 return self.block.breaks(n) or not self.fits(n)
197 def output(self, x):
198 n = x.get_width()
199 self.space -= n
200 self.used += n
201 if self.space < 0:
202 self.space = 0
203 x.output(self.out)
205 class PrettyPrinter:
206 """A prettyprinter based on what I remember of Derek Oppen's
207 prettyprint algorithm from TOPLAS way back.
208 """
210 def __init__(self, width=40):
211 self.width = width
212 self.block = None
213 self.top = None
215 def write(self, x):
216 self.block.add(PrettyString(x))
218 def add(self, item):
219 self.block.add(item)
221 def addbreak(self, width=1, indent=4):
222 self.add(PrettyBreak(width, indent))
224 def addspace(self, width=1):
225 self.add(PrettySpace(width))
227 def addnl(self, indent=0):
228 self.add(PrettyNewline(indent))
230 def begin(self, all=0):
231 block = PrettyBlock(all=all, parent=self.block)
232 self.block = block
234 def end(self):
235 self.block.end()
236 if self.block.parent:
237 self.block.parent.add(self.block)
238 else:
239 self.top = self.block
240 self.block = self.block.parent
242 def prettyprint(self, out=sys.stdout):
243 line = Line(out, self.width)
244 self.top.prettyprint(line)
246 class SXPPrettyPrinter(PrettyPrinter):
247 """An SXP prettyprinter.
248 """
250 def pstring(self, x):
251 io = StringIO.StringIO()
252 sxp.show(x, out=io)
253 io.seek(0)
254 val = io.getvalue()
255 io.close()
256 return val
258 def pprint(self, l):
259 if isinstance(l, types.ListType):
260 self.begin(all=1)
261 self.write('(')
262 i = 0
263 for x in l:
264 if(i): self.addbreak()
265 self.pprint(x)
266 i += 1
267 self.addbreak(width=0, indent=0)
268 self.write(')')
269 self.end()
270 else:
271 self.write(self.pstring(l))
273 def prettyprint(sxpr, out=sys.stdout, width=80):
274 """Prettyprint an SXP form.
276 sxpr s-expression
277 out destination
278 width maximum output width
279 """
280 if isinstance(sxpr, types.ListType):
281 pp = SXPPrettyPrinter(width=width)
282 pp.pprint(sxpr)
283 pp.prettyprint(out=out)
284 else:
285 sxp.show(sxpr, out=out)
286 print >> out
288 def prettyprintstring(sxp):
289 class tmpstr:
290 def __init__(self):
291 self.str = ""
292 def write(self, str):
293 self.str = self.str + str
294 tmp = tmpstr()
295 prettyprint(sxp, out=tmp)
296 return tmp.str
298 def main():
299 pin = sxp.Parser()
300 while 1:
301 buf = sys.stdin.read(100)
302 pin.input(buf)
303 if buf == '': break
304 l = pin.get_val()
305 prettyprint(l, width=80)
307 if __name__ == "__main__":
308 main()