Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Mime types #7

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions ringo_printtemplate/lib/mime_detection.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
# This file offers a mimetype detection based on python-magic.
# There are two packages with that name (pypi- oriented version and
# linux-distributed versions). Two syntax versions need to be covered here.
import magic
# This file offers a mimetype detection based on 'file'.

def check_mime_type_from_buffer(data):
try:
magic_ = magic.open(magic.MAGIC_MIME_TYPE)
magic_.load()
except AttributeError:
return magic.from_buffer(data, mime=True)
return magic_.buffer(data)
import subprocess

def check_mime_type_from_buffer(data):
if data:
p = subprocess.Popen(['file', '-b', '--mime-type', '-'],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE)
out, err = p.communicate(input=data)
return out.rstrip()
return None

def check_mime_type_from_file(data):
try:
magic_ = magic.open(magic.MAGIC_MIME_TYPE)
magic_.load()
except AttributeError:
return magic.from_file(data, mime=True)
return magic_.file(data)
if data:
p = subprocess.Popen(['file', '-b', '--mime-type', data],
stdout = subprocess.PIPE)
out, err = p.communicate()
return out.rstrip()
return None

# Validator for formbar
def odt_validator(field, data, form):
Expand All @@ -32,5 +31,4 @@ def odt_validator(field, data, form):
return False
if check_mime_type_from_buffer(v_data) == "application/vnd.oasis.opendocument.text":
return True

return False
1 change: 0 additions & 1 deletion ringo_printtemplate/odfconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from tempfile import NamedTemporaryFile
import logging
from ringo.lib.cache import CACHE_MISC
from ringo_printtemplate.lib.mime_detection import check_mime_type_from_buffer

log = logging.getLogger(__name__)

Expand Down
1 change: 1 addition & 0 deletions ringo_printtemplate/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def _render_template(request, template, item):
temp = Template(StringIO.StringIO(data), out)
temp.render({"item": item, "print_item": PrintValueGetter(item, request)})
return out
log.error('An invalid template was found on the database, template id '+ str(template.id));
return None


Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ requires =
python-py3o-renderers-pyuno
python-genshi
python-requests
python-magic

[compile_catalog]
directory = ringo_printtemplate/locale
Expand Down
11 changes: 5 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,
zip_safe=False,
install_requires= ['python_magic',
'ringo',
'py3o.template',
'py3o.renderers.pyuno',
'genshi',
'requests'],
install_requires= ['ringo',
'py3o.template',
'py3o.renderers.pyuno',
'genshi',
'requests'],
scripts=['ringo-odfconverter'],
entry_points="""
[paste.app_factory]
Expand Down