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 DateTimeImpl(object):
58 def __init__(self
, date
, time
):
65 result
+= str(self
.date
)
69 result
+= str(self
.time
)
74 return DateTimeImpl(DateImpl
.parse(val
), TimeImpl
.parse(val
))
76 class DateTimePrinter(object):
77 '''Prints date and time'''
79 def __init__(self
, typename
, val
):
83 return str(DateTimeImpl
.parse(self
.val
))
85 class DateImpl(DateTimeImpl
):
87 def __init__(self
, year
, month
, day
):
88 super(DateImpl
, self
).__init
__(self
, None)
94 return "%d-%d-%d" % (self
.year
, self
.month
, self
.day
)
102 m
= (date
/ 100) % 100
104 return DateImpl(y
, m
, d
)
106 class DatePrinter(object):
109 def __init__(self
, typename
, val
):
113 return str(DateImpl
.parse(self
.val
))
115 class TimeImpl(DateTimeImpl
):
117 def __init__(self
, hour
, minute
, second
, nanosecond
= 0):
118 super(TimeImpl
, self
).__init
__(None, self
)
122 self
.nanosecond
= nanosecond
126 if self
.nanosecond
!= 0:
127 decimal
= '.%09d' % self
.nanosecond
128 return "%02d:%02d:%02d%s" % (self
.hour
, self
.minute
, self
.second
, decimal
)
133 h
= time
/ 10000000000000
134 m
= (time
/ 100000000000) % 100
135 s
= (time
/ 1000000000) % 100
136 ns
= time
% 1000000000
137 return TimeImpl(h
, m
, s
, ns
)
139 class TimePrinter(object):
142 def __init__(self
, typename
, val
):
146 return str(TimeImpl
.parse(self
.val
))
148 class PointPrinter(object):
149 '''Prints a Point.'''
151 def __init__(self
, typename
, value
):
152 self
.typename
= typename
156 return "%s" % (self
.typename
)
161 children
= [('x', x
), ('y', y
)]
162 return children
.__iter
__()
164 class SizePrinter(object):
167 def __init__(self
, typename
, value
):
168 self
.typename
= typename
172 return "%s" % (self
.typename
)
175 width
= self
.value
['nA']
176 height
= self
.value
['nB']
177 children
= [('width', width
), ('height', height
)]
178 return children
.__iter
__()
180 class RectanglePrinter(object):
181 '''Prints a Rectangle.'''
183 def __init__(self
, typename
, value
):
184 self
.typename
= typename
188 return "%s" % (self
.typename
)
191 left
= self
.value
['nLeft']
192 top
= self
.value
['nTop']
193 right
= self
.value
['nRight']
194 bottom
= self
.value
['nBottom']
195 children
= [('left', left
), ('top', top
), ('right', right
), ('bottom', bottom
)]
196 return children
.__iter
__()
200 def build_pretty_printers():
203 printer
= printing
.Printer('libreoffice/tl')
206 printer
.add('BigInt', BigIntPrinter
)
207 printer
.add('Color', ColorPrinter
)
208 printer
.add('DateTime', DateTimePrinter
)
209 printer
.add('Date', DatePrinter
)
210 printer
.add('Time', TimePrinter
)
211 printer
.add('Point', PointPrinter
)
212 printer
.add('Size', SizePrinter
)
213 printer
.add('Rectangle', RectanglePrinter
)
215 def register_pretty_printers(obj
):
216 printing
.register_pretty_printer(printer
, obj
)
218 build_pretty_printers()
220 # vim:set shiftwidth=4 softtabstop=4 expandtab: