ODFPY 1.2.0 |

Loading...
Searching...
No Matches
userfield.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3# Copyright (C) 2006-2009 Søren Roug, European Environment Agency
4#
5# This is free software. You may redistribute it under the terms
6# of the Apache license and the GNU General Public License Version
7# 2 or at your option any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public
15# License along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17#
18# Contributor(s): Michael Howitz, gocept gmbh & co. kg
19#
20# $Id: userfield.py 447 2008-07-10 20:01:30Z roug $
21
22
23
24 import sys
25 import zipfile
26
27 from odf.text import UserFieldDecl
28 from odf.namespaces import OFFICENS
29 from odf.opendocument import load
30 import io, sys
31
32 if sys.version_info[0]==3:
33 unicode=str
34
35OUTENCODING = "utf-8"
36
37
38# OpenDocument v.1.0 section 6.7.1
39VALUE_TYPES = {
40 u'float': (OFFICENS, u'value'),
41 u'percentage': (OFFICENS, u'value'),
42 u'currency': (OFFICENS, u'value'),
43 u'date': (OFFICENS, u'date-value'),
44 u'time': (OFFICENS, u'time-value'),
45 u'boolean': (OFFICENS, u'boolean-value'),
46 u'string': (OFFICENS, u'string-value'),
47 }
48
49
50
51 class UserFields():
52
53 # these attributes can be a filename or a file like object
54 src_file = None
55 dest_file = None
56
57
64 def __init__(self, src=None, dest=None):
65 assert(src==None or 'rb' in repr(src) or 'BufferedReader' in repr(src) or 'BytesIO' in repr(src) or type(src)==type(u""))
66 assert(dest==None or 'wb' in repr(dest) or 'BufferedWriter' in repr(dest) or 'BytesIO' in repr(dest) or type(dest)==type(u""))
67 self.src_file = src
68 self.dest_file = dest
69 self.document = None
70
71 def loaddoc(self):
72 if (sys.version_info[0]==3 and (isinstance(self.src_file, str) or (isinstance(self.src_file, io.IOBase)))) or (sys.version_info[0]==2 and isinstance(self.src_file, basestring)):
73 # src_file is a filename, check if it is a zip-file
74 if not zipfile.is_zipfile(self.src_file):
75 raise TypeError(u"%s is no odt file." % self.src_file)
76 elif self.src_file is None:
77 # use stdin if no file given
78 self.src_file = sys.stdin
79
80 self.document = load(self.src_file)
81
82 def savedoc(self):
83 # write output
84 if self.dest_file is None:
85 # use stdout if no filename given
86 self.document.save(u'-')
87 else:
88 self.document.save(self.dest_file)
89
90
94 def list_fields(self):
95 return [x[0] for x in self.list_fields_and_values()]
96
97
106 def list_fields_and_values(self, field_names=None):
107 self.loaddoc()
108 found_fields = []
109 all_fields = self.document.getElementsByType(UserFieldDecl)
110 for f in all_fields:
111 value_type = f.getAttribute(u'valuetype')
112 if value_type == u'string':
113 value = f.getAttribute(u'stringvalue')
114 else:
115 value = f.getAttribute(u'value')
116 field_name = f.getAttribute(u'name')
117
118 if field_names is None or field_name in field_names:
119 found_fields.append((field_name,
120 value_type,
121 value))
122 return found_fields
123
124
131 def list_values(self, field_names):
132 return [x[2] for x in self.list_fields_and_values(field_names)]
133
134
139 def get(self, field_name):
140 assert(type(field_name)==type(u""))
141 values = self.list_values([field_name])
142 if not values:
143 return None
144 return values[0]
145
146
152 def get_type_and_value(self, field_name):
153 assert(type(field_name)==type(u""))
154 fields = self.list_fields_and_values([field_name])
155 if not fields:
156 return None
157 field_name, value_type, value = fields[0]
158 return value_type, value
159
160
167 def update(self, data):
168 self.loaddoc()
169 all_fields = self.document.getElementsByType(UserFieldDecl)
170 for f in all_fields:
171 field_name = f.getAttribute(u'name')
172 if field_name in data:
173 value_type = f.getAttribute(u'valuetype')
174 value = data.get(field_name)
175 if value_type == u'string':
176 f.setAttribute(u'stringvalue', value)
177 else:
178 f.setAttribute(u'value', value)
179 self.savedoc()
180
odf.userfield.UserFields
List, view and manipulate user fields.
Definition userfield.py:51
odf.userfield.UserFields.get
get(self, field_name)
Extract the contents of this field from the file.
Definition userfield.py:139
odf.userfield.UserFields.list_values
list_values(self, field_names)
Extract the contents of given field names from the file.
Definition userfield.py:131
odf.userfield.UserFields.list_fields
list_fields(self)
List (extract) all known user-fields.
Definition userfield.py:94
odf.userfield.UserFields.__init__
__init__(self, src=None, dest=None)
Constructor.
Definition userfield.py:64
odf.userfield.UserFields.dest_file
dest_file
Definition userfield.py:55
odf.userfield.UserFields.savedoc
savedoc(self)
Definition userfield.py:82
odf.userfield.UserFields.update
update(self, data)
Set the value of user fields.
Definition userfield.py:167
odf.userfield.UserFields.src_file
src_file
Definition userfield.py:54
odf.userfield.UserFields.list_fields_and_values
list_fields_and_values(self, field_names=None)
List (extract) user-fields with type and value.
Definition userfield.py:106
odf.userfield.UserFields.get_type_and_value
get_type_and_value(self, field_name)
Extract the type and contents of this field from the file.
Definition userfield.py:152
odf.userfield.UserFields.document
document
Definition userfield.py:69
odf.userfield.UserFields.loaddoc
loaddoc(self)
Definition userfield.py:71
odf.namespaces
Definition namespaces.py:1
odf.opendocument
Definition opendocument.py:1
odf.text
Definition text.py:1