Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
DEFAULT_MIMETYPE = "application/octet-stream"
FORMAT_WORD97 = "doc"
FORMAT_WORD2003 = "wordml"
FORMAT_PDF = "pdf"
FORMAT_DOCBOOK = "docbook"
FORMAT_HTML = "html"
FORMAT_ODT = "odt"
FORMAT_ODS = "ods"
FORMAT_XLS = "xls"
FORMAT_DOCX = "docx"
class UnkownFormatException(Exception):
pass
class Format(object):
"""A format representation that contains:
a name we use in our applications
an ODF name (like: 'MS Word 2003 XML') which is the name you must
use as a filter to call a renderserver or a LibreOffice server
a mimetype that corresponds to the mimetype of the produced file
if you ask LibreOffice to convert to the corresponding format
and a simple flag that indicates if the format is Native or if it is
produced by calling a LibreOffice filter to convert the native
document to an "external format"
"""
def __init__(self, name, odfname, mimetype=DEFAULT_MIMETYPE, native=False):
self.name = name
self.odfname = odfname
self.mimetype = mimetype
self.native = native
class Formats(object):
def __init__(self):
self._formats = {
FORMAT_WORD97: Format(
FORMAT_WORD97, "MS Word 97", "application/msword"
),
FORMAT_WORD2003: Format(
FORMAT_WORD2003,
"MS Word 2003 XML",
"application/vnd.openxmlformats-officedocument"
".wordprocessingml.document",
),
FORMAT_PDF: Format(
FORMAT_PDF, "writer_pdf_Export", "application/pdf"
),
FORMAT_DOCBOOK: Format(
FORMAT_DOCBOOK, "DocBook File", "application/xml"
),
FORMAT_HTML: Format(FORMAT_HTML, "HTML", "text/html"),
FORMAT_ODT: Format(
FORMAT_ODT,
"writer8",
"application/vnd.oasis.opendocument.text",
native=True,
),
FORMAT_ODS: Format(
FORMAT_ODS,
"calc8",
"application/vnd.oasis.opendocument.spreadsheet",
native=True,
),
FORMAT_XLS: Format(
FORMAT_XLS, "MS Excel 97", "application/msexcel"
),
FORMAT_DOCX: Format(
FORMAT_DOCX,
"Office Open XML Text",
"application/vnd.openxmlformats-officedocument"
".wordprocessingml.document",
),
}
def get_format(self, name):
"""an accessor that will return a redner.formats.Format instance that can
be used to know the LibreOffice filter name, the mimetype etc.
:param name: the name of the LibreOffice filter (ie: format) you want
to obtain
:type name: string
:returns: a redner.formats.Format instance.
:raises: redner.formats.UnkownFormatException if the provided name does
not correspond to a known format named
"""
f = self._formats.get(name)
if not f:
raise UnkownFormatException("Format {} is unkown".format(name))
return f
def get_known_format_names(self, nativeonly=False):
"""return the a list of names that can be used as format names in
redner.template.
:param nativeonly: a boolean flag. If set to True will only return
native formats.
:type nativeonly: bool
:return: list of chars
:returntype: list
:raises: nothing
"""
if nativeonly:
return [f for f in self._formats if self.get_format(f).native]
else:
return [f for f in self._formats]