Commit 5603ec5f authored by Gabriel Monnerat's avatar Gabriel Monnerat

refactor code to use json instead of jsonpickle. With that, jsonpickle is not used more on cloudooo

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk/utils@41728 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent ba4585a0
1.0.10 (unreleased)
===================
- Refactor code to use json instead of jsonpickle.
- Add getTableItem and getTableItemList for OOGranulate
- Add getParagraphItemList and getParagraphItem for OOGranulate
- Add getImageItemList and getImage for OOGranulate
......
......@@ -26,7 +26,7 @@
#
##############################################################################
import jsonpickle
import json
import pkg_resources
from base64 import decodestring, encodestring
from os import environ, path
......@@ -69,8 +69,6 @@ class OOHandler:
def _getCommand(self, *args, **kw):
"""Transforms all parameters passed in a command"""
hostname, port = openoffice.getAddress()
jsonpickle_path = "/".join(pkg_resources.resource_filename("jsonpickle",
"").split("/")[:-1])
kw['hostname'] = hostname
kw['port'] = port
command_list = [path.join(self.office_binary_path, "python"),
......@@ -78,8 +76,7 @@ class OOHandler:
path.join("helper", "unoconverter.py")),
"--uno_path='%s'" % self.uno_path,
"--office_binary_path='%s'" % self.office_binary_path,
"--document_url='%s'" % self.document.getUrl(),
"--jsonpickle_path='%s'" % jsonpickle_path]
"--document_url='%s'" % self.document.getUrl()]
for arg in args:
command_list.insert(3, "--%s" % arg)
for k, v in kw.iteritems():
......@@ -150,9 +147,9 @@ class OOHandler:
mimemapper.getFilterName(extension,
service_type)))
return jsonpickle.encode(dict(doc_type_list_by_extension=mimemapper._doc_type_list_by_extension,
filter_list=filter_list,
mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))
return json.dumps(dict(doc_type_list_by_extension=mimemapper._doc_type_list_by_extension,
filter_list=filter_list,
mimetype_by_filter_type=mimemapper._mimetype_by_filter_type))
def convert(self, destination_format=None, **kw):
"""Convert a document to another format supported by the OpenOffice
......@@ -165,7 +162,7 @@ class OOHandler:
if destination_format:
kw['destination_format'] = destination_format
kw['mimemapper'] = self._serializeMimemapper()
kw['refresh'] = jsonpickle.encode(self.refresh)
kw['refresh'] = json.dumps(self.refresh)
try:
stdout, stderr = self._callUnoConverter(*['convert'], **kw)
finally:
......@@ -196,7 +193,7 @@ class OOHandler:
openoffice.release()
if self.monitor.is_alive():
self._stopTimeout()
metadata = jsonpickle.decode(decodestring(stdout))
metadata = json.loads(decodestring(stdout))
if metadata.get("Data"):
self.document.reload(metadata['Data'])
metadata['Data'] = self.document.getContent()
......@@ -211,7 +208,7 @@ class OOHandler:
metadata -- expected an dictionary with metadata.
"""
openoffice.acquire()
metadata_pickled = jsonpickle.encode(metadata)
metadata_pickled = json.dumps(metadata)
logger.debug("setMetadata")
kw = dict(metadata=encodestring(metadata_pickled))
try:
......
......@@ -60,7 +60,7 @@ Options:
extension to export the document
--mimemapper=OBJECT_SERIALIZED
Mimemapper serialized. The object is passed using
jsonpickle. IF this option is None, the object is
json. IF this option is None, the object is
created
--metadata=DICT_SERIALIZED
Dictionary with metadata
......@@ -146,7 +146,7 @@ class UnoConverter(object):
doc_type_list = mimemapper["doc_type_list_by_extension"].get(destination_format)
if self.document_type not in doc_type_list:
raise AttributeError, \
"This Document can not be converted for this format"
"This Document can not be converted for this format"
type = self.document_type
filter_name = self._getFilterName(destination_format, type)
property_list = []
......@@ -279,7 +279,7 @@ def main():
"hostname=", "port=", "source_format=",
"document_url=", "destination_format=",
"mimemapper=", "metadata=", "refresh=",
"unomimemapper_bin=", "jsonpickle_path="])
"unomimemapper_bin="])
except GetoptError, msg:
msg = msg.msg + help_msg
print >> sys.stderr, msg
......@@ -287,12 +287,7 @@ def main():
param_list = [tuple[0] for tuple in iter(opt_list)]
for opt, arg in iter(opt_list):
if opt == "--jsonpickle_path":
sys.path.append(arg)
break
import jsonpickle
import json
refresh = None
for opt, arg in iter(opt_list):
if opt in ('-h', '--help'):
......@@ -314,12 +309,12 @@ def main():
elif opt == '--source_format':
source_format = arg
elif opt == '--refresh':
refresh = jsonpickle.decode(arg)
refresh = json.loads(arg)
elif opt == '--metadata':
arg = decodestring(arg)
metadata = jsonpickle.decode(arg)
metadata = json.loads(arg)
elif opt == '--mimemapper':
mimemapper = jsonpickle.decode(arg)
mimemapper = json.loads(arg)
kw = {}
if "uno_path" in locals():
......@@ -341,11 +336,11 @@ def main():
output = unoconverter.convert(destination_format)
elif '--getmetadata' in param_list and not '--convert' in param_list:
metadata_dict = unoconverter.getMetadata()
output = encodestring(jsonpickle.encode(metadata_dict))
output = encodestring(json.dumps(metadata_dict))
elif '--getmetadata' in param_list and '--convert' in param_list:
metadata_dict = unoconverter.getMetadata()
metadata_dict['Data'] = unoconverter.convert()
output = encodestring(jsonpickle.encode(metadata_dict))
output = encodestring(json.dumps(metadata_dict))
elif '--setmetadata' in param_list:
unoconverter.setMetadata(metadata)
output = document_url
......
......@@ -242,6 +242,7 @@ class Manager(object):
zip = False
try:
response_dict = {}
response_dict["mime"] = ""
# XXX - use html format instead of xhtml
if orig_format in ("presentation", "graphics") and extension == "xhtml":
extension = 'html'
......@@ -266,7 +267,7 @@ class Manager(object):
return (200, response_dict, "")
except Exception, e:
logger.error(e)
return (402, {}, e.args[0])
return (402, response_dict, e.args[0])
def getAllowedTargetItemList(self, content_type):
"""Wrapper getAllowedExtensionList but returns a dict.
......
......@@ -27,7 +27,7 @@
##############################################################################
import unittest
import jsonpickle
import json
import pkg_resources
from subprocess import Popen, PIPE
from os.path import exists, join
......@@ -59,9 +59,7 @@ class TestUnoConverter(cloudoooTestCase):
'com.sun.star.text.TextDocument',
'MS Word 97')],
doc_type_list_by_extension=dict(doc=['com.sun.star.text.TextDocument']))
jsonpickle_path = "/".join(pkg_resources.resource_filename("jsonpickle",
"").split("/")[:-1])
mimemapper_pickled = jsonpickle.encode(mimemapper)
mimemapper_pickled = json.dumps(mimemapper)
command = [join(self.office_binary_path, "python"),
pkg_resources.resource_filename("cloudooo",
"helper/unoconverter.py"),
......@@ -73,8 +71,7 @@ class TestUnoConverter(cloudoooTestCase):
"--document_url='%s'" % self.document.getUrl(),
"--destination_format='%s'" % "doc",
"--source_format='%s'" % "odt",
"--mimemapper='%s'" % mimemapper_pickled,
"--jsonpickle_path='%s'" % jsonpickle_path]
"--mimemapper='%s'" % mimemapper_pickled]
stdout, stderr = Popen(' '.join(command), shell=True,
stdout=PIPE, stderr=PIPE).communicate()
self.assertEquals(stderr, '')
......
......@@ -31,7 +31,6 @@ setup(name='cloudooo',
'PasteDeploy',
'PasteScript',
'WSGIUtils',
'jsonpickle',
'psutil',
'lxml',
],
......
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