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