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