1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 from libreoffice
.util
import printing
14 class BigIntPrinter(object):
15 '''Prints big integer'''
17 def __init__(self
, typename
, val
):
21 if self
.val
['bIsSet']:
22 if self
.val
['bIsBig']:
25 return self
.val
['nVal']
27 return "unset %s" % self
.typename
30 len = self
.val
['nLen']
31 digits
= self
.val
['nNum']
32 dsize
= digits
.dereference().type.sizeof
* 8
34 # The least significant byte is on index 0
35 for i
in reversed(range(0, len)):
40 class ColorPrinter(object):
41 '''Prints color as rgb(r, g, b) or rgba(r, g, b, a)'''
43 def __init__(self
, typename
, val
):
52 return "rgba(%d, %d, %d, %d)" % (r
, g
, b
, a
)
54 return "rgb(%d, %d, %d)" % (r
, g
, b
)
56 class FractionPrinter(object):
59 def __init__(self
, typename
, val
):
60 self
.typename
= typename
64 # Workaround gdb bug <https://sourceware.org/bugzilla/show_bug.cgi?id=22968> "ptype does not
65 # find inner C++ class type without -readnow"
66 gdb
.lookup_type('Fraction')
67 # This would be simpler and more reliable if we could call the operator* on mpImpl to get the internal Impl.
68 # Different libc have different structures. Some have one _M_t, some have two nested.
69 tmp
= self
.val
['mpImpl']['_M_t']
70 if tmp
.type.fields()[0].name
== '_M_t': tmp
= tmp
['_M_t']
71 impl
= tmp
['_M_head_impl'].dereference().cast(gdb
.lookup_type('Fraction::Impl'))
72 numerator
= impl
['value']['num']
73 denominator
= impl
['value']['den']
75 return "%d/%d" % (numerator
, denominator
)
77 return "invalid %s %d/%d" % (self
.typename
, numerator
, denominator
)
79 class DateTimeImpl(object):
81 def __init__(self
, date
, time
):
88 result
+= str(self
.date
)
92 result
+= str(self
.time
)
97 return DateTimeImpl(DateImpl
.parse(val
), TimeImpl
.parse(val
))
99 class DateTimePrinter(object):
100 '''Prints date and time'''
102 def __init__(self
, typename
, val
):
106 return str(DateTimeImpl
.parse(self
.val
))
108 class DateImpl(DateTimeImpl
):
110 def __init__(self
, year
, month
, day
):
111 super(DateImpl
, self
).__init
__(self
, None)
117 return "%d-%d-%d" % (self
.year
, self
.month
, self
.day
)
125 m
= (date
/ 100) % 100
127 return DateImpl(y
, m
, d
)
129 class DatePrinter(object):
132 def __init__(self
, typename
, val
):
136 return str(DateImpl
.parse(self
.val
))
138 class TimeImpl(DateTimeImpl
):
140 def __init__(self
, hour
, minute
, second
, nanosecond
= 0):
141 super(TimeImpl
, self
).__init
__(None, self
)
145 self
.nanosecond
= nanosecond
149 if self
.nanosecond
!= 0:
150 decimal
= '.%09d' % self
.nanosecond
151 return "%02d:%02d:%02d%s" % (self
.hour
, self
.minute
, self
.second
, decimal
)
156 h
= time
/ 10000000000000
157 m
= (time
/ 100000000000) % 100
158 s
= (time
/ 1000000000) % 100
159 ns
= time
% 1000000000
160 return TimeImpl(h
, m
, s
, ns
)
162 class TimePrinter(object):
165 def __init__(self
, typename
, val
):
169 return str(TimeImpl
.parse(self
.val
))
171 class PointPrinter(object):
172 '''Prints a Point.'''
174 def __init__(self
, typename
, value
):
175 self
.typename
= typename
179 return "%s" % (self
.typename
)
184 children
= [('x', x
), ('y', y
)]
185 return children
.__iter
__()
187 class SizePrinter(object):
190 def __init__(self
, typename
, value
):
191 self
.typename
= typename
195 return "%s" % (self
.typename
)
198 width
= self
.value
['nA']
199 height
= self
.value
['nB']
200 children
= [('width', width
), ('height', height
)]
201 return children
.__iter
__()
203 class RectanglePrinter(object):
204 '''Prints a Rectangle.'''
206 def __init__(self
, typename
, value
):
207 self
.typename
= typename
211 return "%s" % (self
.typename
)
214 left
= self
.value
['nLeft']
215 top
= self
.value
['nTop']
216 right
= self
.value
['nRight']
217 bottom
= self
.value
['nBottom']
218 children
= [('left', left
), ('top', top
), ('right', right
), ('bottom', bottom
)]
219 return children
.__iter
__()
223 def build_pretty_printers():
226 printer
= printing
.Printer('libreoffice/tl')
229 printer
.add('BigInt', BigIntPrinter
)
230 printer
.add('Color', ColorPrinter
)
231 printer
.add('Fraction', FractionPrinter
)
232 printer
.add('DateTime', DateTimePrinter
)
233 printer
.add('Date', DatePrinter
)
234 printer
.add('Time', TimePrinter
)
235 printer
.add('Point', PointPrinter
)
236 printer
.add('Size', SizePrinter
)
237 printer
.add('Rectangle', RectanglePrinter
)
239 def register_pretty_printers(obj
):
240 printing
.register_pretty_printer(printer
, obj
)
242 build_pretty_printers()
244 # vim:set shiftwidth=4 softtabstop=4 expandtab: