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
):
47 color
= self
.val
['mnColor']
49 g
= (color
>> 8) & 0xff
50 r
= (color
>> 16) & 0xff
51 a
= (color
>> 24) & 0xff
53 return "rgba(%d, %d, %d, %d)" % (r
, g
, b
, a
)
55 return "rgb(%d, %d, %d)" % (r
, g
, b
)
57 class FractionPrinter(object):
60 def __init__(self
, typename
, val
):
61 self
.typename
= typename
65 impl
= self
.val
['mpImpl'].dereference()
66 numerator
= impl
['value']['num']
67 denominator
= impl
['value']['den']
69 return "%d/%d" % (numerator
, denominator
)
71 return "invalid %s %d/%d" % (self
.typename
, numerator
, denominator
)
73 class DateTimeImpl(object):
75 def __init__(self
, date
, time
):
82 result
+= str(self
.date
)
86 result
+= str(self
.time
)
91 return DateTimeImpl(DateImpl
.parse(val
), TimeImpl
.parse(val
))
93 class DateTimePrinter(object):
94 '''Prints date and time'''
96 def __init__(self
, typename
, val
):
100 return str(DateTimeImpl
.parse(self
.val
))
102 class DateImpl(DateTimeImpl
):
104 def __init__(self
, year
, month
, day
):
105 super(DateImpl
, self
).__init
__(self
, None)
111 return "%d-%d-%d" % (self
.year
, self
.month
, self
.day
)
117 m
= (date
/ 100) % 100
119 return DateImpl(y
, m
, d
)
121 class DatePrinter(object):
124 def __init__(self
, typename
, val
):
128 return str(DateImpl
.parse(self
.val
))
130 class TimeImpl(DateTimeImpl
):
132 def __init__(self
, hour
, minute
, second
, nanosecond
= 0):
133 super(TimeImpl
, self
).__init
__(None, self
)
137 self
.nanosecond
= nanosecond
141 if self
.nanosecond
!= 0:
142 decimal
= '.%09d' % self
.nanosecond
143 return "%02d:%02d:%02d%s" % (self
.hour
, self
.minute
, self
.second
, decimal
)
148 h
= time
/ 10000000000000
149 m
= (time
/ 100000000000) % 100
150 s
= (time
/ 1000000000) % 100
151 ns
= time
% 1000000000
152 return TimeImpl(h
, m
, s
, ns
)
154 class TimePrinter(object):
157 def __init__(self
, typename
, val
):
161 return str(TimeImpl
.parse(self
.val
))
163 class PointPrinter(object):
164 '''Prints a Point.'''
166 def __init__(self
, typename
, value
):
167 self
.typename
= typename
171 return "%s" % (self
.typename
)
176 children
= [('x', x
), ('y', y
)]
177 return children
.__iter
__()
179 class SizePrinter(object):
182 def __init__(self
, typename
, value
):
183 self
.typename
= typename
187 return "%s" % (self
.typename
)
190 width
= self
.value
['nA']
191 height
= self
.value
['nB']
192 children
= [('width', width
), ('height', height
)]
193 return children
.__iter
__()
195 class RectanglePrinter(object):
196 '''Prints a Rectangle.'''
198 def __init__(self
, typename
, value
):
199 self
.typename
= typename
203 return "%s" % (self
.typename
)
206 left
= self
.value
['nLeft']
207 top
= self
.value
['nTop']
208 right
= self
.value
['nRight']
209 bottom
= self
.value
['nBottom']
210 children
= [('left', left
), ('top', top
), ('right', right
), ('bottom', bottom
)]
211 return children
.__iter
__()
215 def build_pretty_printers():
218 printer
= printing
.Printer('libreoffice/tl')
221 printer
.add('BigInt', BigIntPrinter
)
222 printer
.add('Color', ColorPrinter
)
223 printer
.add('Fraction', FractionPrinter
)
224 printer
.add('DateTime', DateTimePrinter
)
225 printer
.add('Date', DatePrinter
)
226 printer
.add('Time', TimePrinter
)
227 printer
.add('Point', PointPrinter
)
228 printer
.add('Size', SizePrinter
)
229 printer
.add('Rectangle', RectanglePrinter
)
231 def register_pretty_printers(obj
):
232 printing
.register_pretty_printer(printer
, obj
)
234 build_pretty_printers()
236 # vim:set shiftwidth=4 softtabstop=4 expandtab: