Permalink
Browse files

add word transform

  • Loading branch information...
1 parent 736c78a commit e2b5b1fa79608036c17054bad5b57afbe5014535 @kiorky kiorky committed Nov 16, 2012
Showing with 68 additions and 28 deletions.
  1. +3 −0 .gitignore
  2. +16 −0 README.rst
  3. +34 −24 src/convertit/converters/unoconv.py
  4. +15 −3 src/convertit/tests/test_functional.py
  5. +0 −1 src/convertit/views.py
View
@@ -34,3 +34,6 @@ etc/logrotate.conf
local.cfg
src/convertit/tests/data/tmp/
+src/convertit/tests/data/converted_dir/
+src/convertit/tests/data/download_dir/
+
View
@@ -3,6 +3,22 @@ Convertit
A conversion webservice.
+Idea is to make a GET request to the webservice and you ll get a downloadable transformed file:
+Documents are pulled by the webserver.
+
+GET parameters are:
+
+- **url**: a url to a document to be grabbed from transformation
+- **output_mt**: (optionnal, default to ``application/pdf`` if not provided)
+- **input_mt**: (optionnal, guessed from input url if not provided)
+
+Older transformed documents are cleaned after a defined time, tested at each http request on the application.
+
+base transforms:
+- odt -> pdf
+- odt -> doc
+- svg -> pdf
+
.. contents::
Feedback
@@ -2,35 +2,45 @@
import subprocess
import convertit as c
-ext = 'pdf'
-def to_pdf(source, target):
- """
- Some old version of unoconv do not accept the full target path.
- """
- target_dirname = os.path.dirname(target)
- command = ['unoconv', '-o', target_dirname, '--format', 'pdf', source]
- env = os.environ.copy()
- # do not mess pyuno pythonpath !
- if 'PYTHONPATH' in env:
- del env['PYTHONPATH']
- p = subprocess.Popen(command, stdout=subprocess.PIPE, env = env, stderr=subprocess.PIPE)
- p.wait()
- output = '\n'.join([p.stdout.read(), p.stderr.read()])
- source_basename = os.path.basename(source)
- source_filename, ext = os.path.splitext(
- source_basename)
- converted_path = os.path.join(
- target_dirname,
- source_filename + '.pdf')
- if not os.path.exists(converted_path):
- raise c.TransformError(output)
- os.rename(converted_path, target)
+def uno_transform(output_fmt='pdf', output_ext=None):
+ if not output_ext:
+ output_ext = output_fmt
+ def to_format(source, target):
+ """
+ Some old version of unoconv do not accept the full target path.
+ """
+ target_dirname = os.path.dirname(target)
+ command = ['unoconv', '-o', target_dirname, '--format', output_fmt, source]
+ env = os.environ.copy()
+ # do not mess pyuno pythonpath !
+ if 'PYTHONPATH' in env:
+ del env['PYTHONPATH']
+ p = subprocess.Popen(command, stdout=subprocess.PIPE, env = env, stderr=subprocess.PIPE)
+ p.wait()
+ output = '\n'.join([p.stdout.read(), p.stderr.read()])
+ source_basename = os.path.basename(source)
+ source_filename, ext = os.path.splitext(
+ source_basename)
+ converted_path = os.path.join(
+ target_dirname,
+ source_filename + '.'+output_ext)
+ if not os.path.exists(converted_path):
+ raise c.TransformError(output)
+ os.rename(converted_path, target)
+ return to_format
+
+to_pdf = uno_transform('pdf')
+to_doc = uno_transform('doc')
def register(converters=None):
c.register(('application/vnd.oasis.opendocument.text', 'application/pdf'),
- ext, to_pdf, c.test_program('unoconv'),
+ 'pdf', to_pdf, c.test_program('unoconv'),
+ converters=converters)
+ c.register(('application/vnd.oasis.opendocument.text', 'application/msword'),
+ 'doc', to_doc, c.test_program('unoconv'),
converters=converters)
+
register()
@@ -51,13 +51,27 @@ def test_with_valid_url(self):
filepath = os.path.join(self.settings['converted_dir'], filename)
self.assertTrue(os.path.exists(filepath))
+ def test_with_valid_url_toword(self):
+ "Get homepage with valid `url` param"
+
+ url = 'http://example.com/test_document.odt'
+ odt_data = open(os.path.join(self.data_dir, 'test_document.odt')).read()
+ with patch.object(urllib2, 'urlopen') as mock_urlopen:
+ mock_req = Mock()
+ mock_req.read.return_value = odt_data
+ mock_urlopen.return_value = mock_req
+ resp = self.testapp.get('/', params={'url': url, 'output_mt': 'application/msword'}, status=302)
+ mock_urlopen.assert_called_once_with(url)
+ filename = os.path.basename(resp.location)
+ filepath = os.path.join(self.settings['converted_dir'], filename)
+ self.assertTrue(os.path.exists(filepath))
+
def test_invalid_url_type(self):
"Get homepage with `url` missing a protocol identifier"
url = 'www.example.com/test_document.odt'
resp = self.testapp.get('/', params={'url': url}, status=400)
self.assertIn("unknown url type", resp.body)
-
def test_no_such_transform(self):
"Get homepage with `url` that triggers a DNS resolution error"
url = 'http://example.com/test_document.odt'
@@ -74,8 +88,6 @@ def test_no_such_transform(self):
'url': url,
'output_mt': 'application/pdfnocontent'}, status=400).body)
-
-
def test_invalid_hostname(self):
"Get homepage with `url` that triggers a DNS resolution error"
with patch.object(urllib2, 'urlopen') as mock_urlopen:
View
@@ -42,7 +42,6 @@ def home_view(request):
url = request.GET.get('url')
output_mt = request.GET.get('output_mt', 'application/pdf')
-
if url is None:
return HTTPBadRequest('Missing parameter: url')

0 comments on commit e2b5b1f

Please sign in to comment.