07f0976fb3662e8aae7ef6f24fb0c6787ff9b8ac
[auf_rh_dae.git] / project / rh / ods.py
1 # encoding: utf-8
2
3 from decimal import Decimal
4
5 import odf.opendocument
6 import odf.style
7 import odf.table
8 from odf.style import Style, MasterPage, PageLayout, PageLayoutProperties, \
9 TextProperties, GraphicProperties, ParagraphProperties, \
10 DrawingPageProperties
11
12
13 class Separator():
14
15 def __unicode__(self):
16 return u""
17
18 def __str__(self):
19 return ""
20
21
22 def valuetype(val):
23 valuetype = "string"
24 if isinstance(val, str):
25 valuetype = "string"
26 if isinstance(val, (int, float, Decimal)):
27 valuetype = "float"
28 if isinstance(val, bool):
29 valuetype = "boolean"
30
31 return valuetype
32
33
34 class Wrapper(object):
35
36 def __init__(self, *args, **kwargs):
37 self.__wrapped = self._wrapper_constructor(*args, **kwargs)
38
39 def __getattr__(self, attr):
40 return getattr(self.__wrapped, attr)
41
42
43 class OpenDocumentSpreadsheet(Wrapper):
44 _wrapper_constructor = staticmethod(
45 odf.opendocument.OpenDocumentSpreadsheet
46 )
47
48 def __init__(self, *args, **kwargs):
49 super(OpenDocumentSpreadsheet, self).__init__(*args, **kwargs)
50 self._automatic_style_idx = 0
51
52 def add_table(self, **kwargs):
53 table = Table(**kwargs)
54 table._doc = self
55 self.spreadsheet.addElement(table)
56 return table
57
58 def add_automatic_style(self, **kwargs):
59 name = 'auto_style_%d' % self._automatic_style_idx
60 style = odf.style.Style(name=name, **kwargs)
61 self.automaticstyles.addElement(style)
62 self._automatic_style_idx += 1
63 return style
64
65
66 class Table(Wrapper):
67 _wrapper_constructor = staticmethod(odf.table.Table)
68
69 def add_row(self, values=[], **kwargs):
70 # attributs appartenant à table-column-poperties
71 # props = {}
72 # for attr in ['rowheight']:
73 # if attr in kwargs:
74 # props[attr] = kwargs.pop(attr)
75
76 style = self._doc.add_automatic_style(family='table-row')
77 if 'rowheight' in kwargs:
78 style.addElement(odf.style.TableRowProperties(
79 rowheight=kwargs['rowheight']))
80 kwargs['stylename'] = style.getAttribute('name')
81 del kwargs['rowheight']
82
83 style = {}
84
85 row = TableRow(**kwargs)
86 row._doc = self._doc
87 for value in values:
88 row.add_cell(value, verticalalign='middle', **style)
89 self.addElement(row)
90 return row
91
92 def add_column(self, **kwargs):
93
94 # attributs appartenant à table-column-poperties
95 props = {}
96 for attr in ['columnwidth']:
97 if attr in kwargs:
98 props[attr] = kwargs.pop(attr)
99
100 if props:
101 style = self._doc.add_automatic_style(family='table-column')
102 style.addElement(odf.style.TableColumnProperties(**props))
103 kwargs['stylename'] = style.getAttribute('name')
104 col = odf.table.TableColumn(**kwargs)
105 self.addElement(col)
106 return col
107
108
109 class TableRow(Wrapper):
110 _wrapper_constructor = staticmethod(odf.table.TableRow)
111
112 def add_cell(self, value=None, **kwargs):
113 if value:
114 if isinstance(value, (basestring, unicode)):
115 kwargs['stringvalue'] = unicode(value)
116 elif isinstance(value, (int, float, Decimal)):
117 kwargs['valuetype'] = "float"
118 kwargs['value'] = float(value)
119 elif type(value) == type(None) or isinstance(value, Separator):
120 kwargs['stringvalue'] = u""
121 else:
122 kwargs['stringvalue'] = unicode(value)
123
124 style = self._doc.add_automatic_style(family='table-cell')
125 if 'verticalalign' in kwargs:
126 style.addElement(odf.style.TableCellProperties(
127 verticalalign=kwargs['verticalalign'], wrapoption='wrap'))
128 del kwargs['verticalalign']
129
130 if isinstance(value, Separator) or type(value) == type(Separator()):
131 style.addElement(odf.style.TableCellProperties(
132 backgroundcolor='#D3D3D3'))
133
134 kwargs['stylename'] = style.getAttribute('name')
135 # props = {}
136 # if 'fontweight' in kwargs:
137 # props['fontweight'] = kwargs.pop('fontweight')
138 # if 'stringvalue' in kwargs:
139 # props['stringvalue'] = kwargs.pop('stringvalue')
140
141 cell = odf.table.TableCell(**kwargs)
142 # if 'fontweight' in props:
143 # tablecontents = Style(name="Bold", family="paragraph")
144 # tablecontents.addElement(TextProperties(fontweight="bold"))
145 # self._doc.styles.addElement(tablecontents)
146
147 # if 'stringvalue' in props:
148 # p = P(stylename='Bold',text=props['stringvalue'])
149 # cell.addElement(p)
150
151 self.addElement(cell)
152 return cell