Commit 056a2158 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

modify cloudooo/handler/ooo/helper to support LibreOffice 4.

* use getDocumentProperties() and getUserDefinedProperties() instead of deprecated getDocumentInfo().
* use python3 syntaxes.
parent f281ea03
...@@ -29,12 +29,16 @@ ...@@ -29,12 +29,16 @@
import sys import sys
import helper_util import helper_util
from types import UnicodeType, InstanceType
from os.path import dirname from os.path import dirname
from tempfile import mktemp from tempfile import mktemp
from base64 import decodestring, encodestring from base64 import decodestring, encodestring
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
try:
unicode
except NameError:
unicode = str
__doc__ = """ __doc__ = """
usage: unoconverter [options] usage: unoconverter [options]
...@@ -184,23 +188,28 @@ class UnoConverter(object): ...@@ -184,23 +188,28 @@ class UnoConverter(object):
def getMetadata(self): def getMetadata(self):
"""Extract all metadata of the document""" """Extract all metadata of the document"""
metadata = {} metadata = {}
document_info = self.document_loaded.getDocumentInfo() document_properties = self.document_loaded.getDocumentProperties()
property_list = [prop.Name for prop in document_info.getPropertyValues() \ user_defined_properties = document_properties.getUserDefinedProperties()
if prop.Value] for container in [document_properties, user_defined_properties]:
for property_name in iter(property_list): for property_name in dir(container):
property = document_info.getPropertyValue(property_name) if property_name in ('SupportedServiceNames',):
if type(property) == UnicodeType: continue
metadata[property_name] = property property_value = getattr(container, property_name, '')
elif type(property) == InstanceType: if property_value:
if property.typeName == 'com.sun.star.util.DateTime': if isinstance(property_value, unicode):
datetime = "%s/%s/%s %s:%s:%s" % (property.Day, property.Month, metadata[property_name] = property_value
property.Year, property.Hours, property.Minutes, property.Seconds) elif isinstance(property_value, tuple) and isinstance(property_value[0], unicode):
metadata[property_name] = datetime metadata[property_name] = property_value
for number in range(document_info.getUserFieldCount()): else:
field_value_str = document_info.getUserFieldValue(number) try:
if field_value_str: if property_value.typeName == 'com.sun.star.util.DateTime':
fieldname = document_info.getUserFieldName(number) # It is a local time and we have no timezone information.
metadata[fieldname] = field_value_str datetime = "%02d/%02d/%04d %02d:%02d:%02d" % (property_value.Day, property_value.Month,
property_value.Year, property_value.Hours, property_value.Minutes, property_value.Seconds)
metadata[property_name] = datetime
except AttributeError:
pass
service_manager = helper_util.getServiceManager(self.hostname, self.port, service_manager = helper_util.getServiceManager(self.hostname, self.port,
self.uno_path, self.uno_path,
self.office_binary_path) self.office_binary_path)
...@@ -221,35 +230,26 @@ class UnoConverter(object): ...@@ -221,35 +230,26 @@ class UnoConverter(object):
Keyword arguments: Keyword arguments:
metadata -- expected an dictionary with metadata. metadata -- expected an dictionary with metadata.
""" """
doc_info = self.document_loaded.getDocumentInfo() document_properties = self.document_loaded.getDocumentProperties()
prop_name_list = [prop.Name for prop in doc_info.getPropertyValues()] user_defined_properties = document_properties.getUserDefinedProperties()
user_info_counter = 0 for prop, value in metadata.items():
for prop, value in metadata.iteritems(): for container in [document_properties, user_defined_properties]:
if prop in prop_name_list: if getattr(container, prop, None) is not None:
doc_info.setPropertyValue(prop, value) setattr(container, prop, value)
else: container
max_index = doc_info.getUserFieldCount()
for index in range(max_index):
if doc_info.getUserFieldName(index).lower() == prop.lower():
doc_info.setUserFieldValue(index, value)
break
else:
doc_info.setUserFieldName(user_info_counter, prop)
doc_info.setUserFieldValue(user_info_counter, value)
user_info_counter += 1
self.document_loaded.store() self.document_loaded.store()
self.document_loaded.dispose() self.document_loaded.dispose()
def help(): def help():
print >> sys.stderr, __doc__ sys.stderr.write(__doc__)
sys.exit(1) sys.exit(1)
def main(): def main():
global mimemapper global mimemapper
help_msg = "\nUse --help or -h" help_msg = "\nUse --help or -h\n"
try: try:
opt_list, arg_list = getopt(sys.argv[1:], "h", ["help", "test", opt_list, arg_list = getopt(sys.argv[1:], "h", ["help", "test",
"convert", "getmetadata", "setmetadata", "convert", "getmetadata", "setmetadata",
...@@ -258,9 +258,9 @@ def main(): ...@@ -258,9 +258,9 @@ def main():
"document_url=", "destination_format=", "document_url=", "destination_format=",
"mimemapper=", "metadata=", "refresh=", "mimemapper=", "metadata=", "refresh=",
"unomimemapper_bin="]) "unomimemapper_bin="])
except GetoptError, msg: except GetoptError as msg:
msg = msg.msg + help_msg msg = msg.msg + help_msg
print >> sys.stderr, msg sys.stderr.write(msg)
sys.exit(2) sys.exit(2)
param_list = [tuple[0] for tuple in iter(opt_list)] param_list = [tuple[0] for tuple in iter(opt_list)]
...@@ -292,7 +292,7 @@ def main(): ...@@ -292,7 +292,7 @@ def main():
elif opt == '--refresh': elif opt == '--refresh':
refresh = json.loads(arg) refresh = json.loads(arg)
elif opt == '--metadata': elif opt == '--metadata':
arg = decodestring(arg) arg = decodestring(arg.encode('ascii')).decode('utf-8')
metadata = json.loads(arg) metadata = json.loads(arg)
elif opt == '--mimemapper': elif opt == '--mimemapper':
mimemapper = json.loads(arg) mimemapper = json.loads(arg)
...@@ -307,7 +307,7 @@ def main(): ...@@ -307,7 +307,7 @@ def main():
output = unoconverter.convert(destination_format) output = unoconverter.convert(destination_format)
elif '--getmetadata' in param_list and not '--convert' in param_list: elif '--getmetadata' in param_list and not '--convert' in param_list:
metadata_dict = unoconverter.getMetadata() metadata_dict = unoconverter.getMetadata()
output = encodestring(json.dumps(metadata_dict)) output = encodestring(json.dumps(metadata_dict).encode('utf-8')).decode('utf-8')
elif '--getmetadata' in param_list and '--convert' in param_list: elif '--getmetadata' in param_list and '--convert' in param_list:
document_url = unoconverter.convert() document_url = unoconverter.convert()
# Instanciate new UnoConverter instance with new url # Instanciate new UnoConverter instance with new url
...@@ -315,12 +315,12 @@ def main(): ...@@ -315,12 +315,12 @@ def main():
uno_path, office_binary_path, refresh) uno_path, office_binary_path, refresh)
metadata_dict = unoconverter.getMetadata() metadata_dict = unoconverter.getMetadata()
metadata_dict['document_url'] = document_url metadata_dict['document_url'] = document_url
output = encodestring(json.dumps(metadata_dict)) output = encodestring(json.dumps(metadata_dict).encode('utf-8')).decode('utf-8')
elif '--setmetadata' in param_list: elif '--setmetadata' in param_list:
unoconverter.setMetadata(metadata) unoconverter.setMetadata(metadata)
output = document_url output = document_url
print output sys.stdout.write(output)
if "__main__" == __name__: if "__main__" == __name__:
main() main()
...@@ -34,7 +34,6 @@ except ImportError: ...@@ -34,7 +34,6 @@ except ImportError:
import simplejson as json import simplejson as json
import helper_util import helper_util
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
from types import InstanceType
__doc__ = """ __doc__ = """
...@@ -72,7 +71,7 @@ class UnoMimemapper(object): ...@@ -72,7 +71,7 @@ class UnoMimemapper(object):
for obj in iter(element_list): for obj in iter(element_list):
if obj.Name in ignore_name_list: if obj.Name in ignore_name_list:
continue continue
elif type(obj.Value) == InstanceType: if not isinstance(obj.Value, (bool, int, str, tuple)):
continue continue
element_dict[obj.Name] = obj.Value element_dict[obj.Name] = obj.Value
service_dict[name] = element_dict service_dict[name] = element_dict
...@@ -93,7 +92,7 @@ class UnoMimemapper(object): ...@@ -93,7 +92,7 @@ class UnoMimemapper(object):
def help(): def help():
print >> sys.stderr, __doc__ sys.stderr.write(__doc__)
sys.exit(1) sys.exit(1)
...@@ -102,9 +101,9 @@ def main(): ...@@ -102,9 +101,9 @@ def main():
opt_list, arg_list = getopt(sys.argv[1:], "h", ["help", opt_list, arg_list = getopt(sys.argv[1:], "h", ["help",
"uno_path=", "office_binary_path=", "uno_path=", "office_binary_path=",
"hostname=", "port="]) "hostname=", "port="])
except GetoptError, msg: except GetoptError as msg:
msg = msg.msg + "\nUse --help or -h" msg = msg.msg + "\nUse --help or -h\n"
print >> sys.stderr, msg sys.stderr.write(msg)
sys.exit(2) sys.exit(2)
if not opt_list: if not opt_list:
...@@ -127,7 +126,7 @@ def main(): ...@@ -127,7 +126,7 @@ def main():
filter_dict = mimemapper.getFilterDict() filter_dict = mimemapper.getFilterDict()
type_dict = mimemapper.getTypeDict() type_dict = mimemapper.getTypeDict()
print json.dumps((filter_dict, type_dict)) sys.stdout.write(json.dumps((filter_dict, type_dict)))
if "__main__" == __name__: if "__main__" == __name__:
main() main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment