Skip to content

Commit

Permalink
Minor translateDoc refactoring (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
sushain97 committed May 17, 2018
1 parent bccf127 commit 6550a33
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions apertium_apy/handlers/translate_doc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import shutil
import subprocess
import tempfile
import zipfile
from subprocess import check_output, check_call, CalledProcessError, PIPE

import tornado
from tornado import gen
Expand All @@ -11,9 +12,9 @@
FILE_SIZE_LIMIT_BYTES = 32E6

MIMETYPE_COMMANDS = {
'mimetype': lambda x: check_output(['mimetype', '-b', x], universal_newlines=True).strip(),
'xdg-mime': lambda x: check_output(['xdg-mime', 'query', 'filetype', x], universal_newlines=True).strip(),
'file': lambda x: check_output(['file', '--mime-type', '-b', x], universal_newlines=True).strip(),
'mimetype': lambda x: subprocess.check_output(['mimetype', '-b', x], universal_newlines=True).strip(),
'xdg-mime': lambda x: subprocess.check_output(['xdg-mime', 'query', 'filetype', x], universal_newlines=True).strip(),
'file': lambda x: subprocess.check_output(['file', '--mime-type', '-b', x], universal_newlines=True).strip(),
}

ALLOWED_MIME_TYPES = {
Expand Down Expand Up @@ -62,12 +63,9 @@ class TranslateDocHandler(TranslateHandler):
def get_mime_type(cls, f):
if not cls.mime_type_command:
for command in MIMETYPE_COMMANDS.keys():
try:
check_call(['which', command], stdout=PIPE)
if shutil.which(command):
cls.mime_type_command = command
break
except CalledProcessError:
pass

mime_type = MIMETYPE_COMMANDS[cls.mime_type_command](f).split(';')[0]
if mime_type == 'application/zip':
Expand Down

0 comments on commit 6550a33

Please sign in to comment.