Skip to content

Commit

Permalink
Add some type file preview and more (#28)
Browse files Browse the repository at this point in the history
Adds preview for scribus file .sla. fix #25
Adds test for gimp file .xfc. fix #26
Refactor openoffice and scribus preview builder.
Complete README.
Fix bug with exif tool. fix #21
Adds some PEP8.
  • Loading branch information
basilelegal authored and buxx committed Oct 11, 2018
1 parent b979040 commit 2426a0d
Show file tree
Hide file tree
Showing 43 changed files with 1,829 additions and 794 deletions.
19 changes: 10 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
sudo: false
sudo: true
language: python
python:
- "3.3"
- "3.4"
- "3.5"
- "3.6"

addons:
apt:
packages:
- libreoffice

install:
# Install specific version of exiftool
- wget https://sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-11.11.tar.gz && gzip -dc Image-ExifTool-11.11.tar.gz | tar -xf - && cd Image-ExifTool-11.11 && perl Makefile.PL && sudo make install
- cd ..
- sudo apt-get install xvfb libreoffice libfile-mimeinfo-perl inkscape poppler-utils -y
- pip install xvfbwrapper
- python setup.py install
- pip install preview-generator
- pip install pytest
- python3 -m pip install -U mypy

# command to run tests
script:
- "cd ${TRAVIS_BUILD_DIR} && py.test tests"
- "cd ${TRAVIS_BUILD_DIR} && mypy preview_generator --ignore-missing-imports --disallow-untyped-defs"

# - "cd ${TRAVIS_BUILD_DIR} && mypy preview_generator --ignore-missing-imports --disallow-untyped-defs"




37 changes: 35 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -777,14 +777,47 @@ From scratch on a terminal :
* build your virtual env (I can say that it work with python 3.4 but did not try with other versions)(env will be called "myenv", you can name it the way you want): `python3.4 -m venv myenv`
* if it's not already, activate it : `source myenv/bin/activate`. (`deactivate` to deactivate)
- install dependencies :

* Exiftool - Follow instruction on the main website: https://sno.phy.queensu.ca/~phil/exiftool/
* `apt-get install zlib1g-dev`
* `apt-get install libjpeg-dev`
* `apt-get install python3-pythonmagick`
* `apt-get install inkscape`
* `apt-get install xvfb`
* `pip install wand`
* `pip install python-magick`
* `pip install pillow`
* `pip install Pillow`
* `pip install PyPDF2`
* `pip install python-magic`
* `pip install pyexifinfo`
* `pip install packaging`
* `pip install xvfbwrapper`
* if you use python 3.5 or less `pip install typing`


.. code:: console
# general dependencies
apt-get install libjpeg-dev libjpeg-dev python3-pythonmagick inkscape xvfb
pip install wand Pillow PyPDF2 python-magic pyexifinfo packaging xvfbwrapper
# Exiftool
wget https://sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-11.11.tar.gz
gzip -dc Image-ExifTool-11.11.tar.gz | tar -xf -
cd Image-ExifTool-11.11
perl Makefile.PL
sudo make install
If you need to preview scribus `.sla` files you will need scribus >= 1.5.
If it's not available in your distribution you can use an AppImage.

Download the last AppImage from the official website https://www.scribus.net/downloads/unstable-branch/

.. code:: console
mv /path/to/image/scribus-x.y.appimage /usr/local/bin/scribus
chmod +x /usr/local/bin/scribus
Running Pytest :
----------------
Pytest is a motor for unit testing
Expand Down
5 changes: 1 addition & 4 deletions preview_generator/file_converter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-

from io import BytesIO
import json
import logging
from PIL import Image
from PyPDF2 import PdfFileReader
import typing
from wand.image import Image as WImage
from preview_generator.utils import PreviewGeneratorJsonEncoder


def txt_to_txt(text: typing.IO[typing.Any]) -> typing.IO[typing.Any]:
logging.info('Converting text to text')
Expand Down
53 changes: 26 additions & 27 deletions preview_generator/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import typing

from preview_generator.preview.builder.office__libreoffice import OfficePreviewBuilderLibreoffice # nopep8
from preview_generator.preview.builder.document__scribus import DocumentPreviewBuilderScribus # nopep8
from preview_generator.preview.builder_factory import PreviewBuilderFactory
from preview_generator.utils import ImgDims

Expand Down Expand Up @@ -109,7 +110,7 @@ def get_jpeg_preview(
builder = self._factory.get_preview_builder(mimetype)
extension = '.jpeg'

if isinstance(builder, OfficePreviewBuilderLibreoffice):
if type(builder) in [OfficePreviewBuilderLibreoffice, DocumentPreviewBuilderScribus]:
file_path = self.get_pdf_preview(
file_path=file_path,
force=force,
Expand All @@ -134,11 +135,11 @@ def get_jpeg_preview(
return preview_file_path

def get_pdf_preview(
self,
file_path: str,
page: int = -1,
force: bool = False,
file_ext: str = ''
self,
file_path: str,
page: int = -1,
force: bool = False,
file_ext: str = ''
) -> str:
"""
Return a PDF preview of given file, according to parameters
Expand Down Expand Up @@ -174,10 +175,10 @@ def get_pdf_preview(
raise Exception('Error while getting the file the file preview')

def get_text_preview(
self,
file_path: str,
force: bool = False,
file_ext: str = ''
self,
file_path: str,
force: bool = False,
file_ext: str = ''
) -> str:
"""
Return a TXT preview of given file, according to parameters
Expand Down Expand Up @@ -208,10 +209,10 @@ def get_text_preview(
raise Exception('Error while getting the file the file preview')

def get_html_preview(
self,
file_path: str,
force: bool = False,
file_ext: str = ''
self,
file_path: str,
force: bool = False,
file_ext: str = ''
) -> str:
"""
Return a HTML preview of given file, according to parameters
Expand Down Expand Up @@ -242,10 +243,10 @@ def get_html_preview(
raise Exception('Error while getting the file the file preview')

def get_json_preview(
self,
file_path: str,
force: bool = False,
file_ext: str = ''
self,
file_path: str,
force: bool = False,
file_ext: str = ''
) -> str:
"""
Return a HTML preview of given file, according to parameters
Expand All @@ -255,7 +256,7 @@ def get_json_preview(
it's usefull if the extension can't be found in file_path
:return: path to the generated preview file
"""
mimetype = self._factory.get_file_mimetype(file_path,file_ext)
mimetype = self._factory.get_file_mimetype(file_path, file_ext)
logging.info('Mimetype of the document is :' + mimetype)
builder = self._factory.get_preview_builder(mimetype)
extension = '.json'
Expand All @@ -275,10 +276,10 @@ def get_json_preview(
raise Exception('Error while getting the file preview')

def _get_file_hash(
self,
file_path: str,
size: ImgDims=None,
page: int = None,
self,
file_path: str,
size: ImgDims=None,
page: int = None,
) -> str:
"""
Build a hash based on the given parameters.
Expand Down Expand Up @@ -314,10 +315,8 @@ def _get_file_hash(
def get_supported_mimetypes(self) -> typing.List[str]:
return self._factory.get_supported_mimetypes()

def get_file_extensions(self, mime: str) -> typing.List[str]:
def get_file_extension(self, mime: str) -> str:
return mimetypes.guess_extension(mime)

def get_supported_file_extensions(self) -> typing.List[str]:
return [
ext for ext in mimetypes.guess_extension(self.get_supported_mimetypes())
]
return [mimetypes.guess_extension(mime) for mime in self.get_supported_mimetypes()]
20 changes: 11 additions & 9 deletions preview_generator/preview/builder/archive__zip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

from datetime import datetime
from io import BytesIO
import json
import logging
Expand All @@ -9,19 +10,20 @@
from preview_generator.utils import PreviewGeneratorJsonEncoder
from preview_generator.preview.generic_preview import OnePagePreviewBuilder


class FileInfo(object):
FILE = 'file'
DIR = 'dir'
UNDEFINED = 'undefined'

def __init__(self):
self.last_modification = None
def __init__(self) -> None:
self.last_modification = None # type: datetime
self.name = ''
self.type = FileInfo.UNDEFINED
self.size = 0
self.size__compressed = 0

def to_dict(self):
def to_dict(self) -> dict:
return {
'lastModification': self.last_modification,
'name': self.name,
Expand All @@ -31,21 +33,21 @@ def to_dict(self):


class ArchiveInfo(object):
def __init__(self):
self.files = [] # typing.List[FileInfo]
def __init__(self) -> None:
self.files = [] # type: typing.List[FileInfo]
self.size = 0
self.size__compressed = 0
self.last_modification = None
self.last_modification = None # type: datetime

@property
def compression_rate(self) -> float:
return self.size / self.size__compressed

@property
def file_nb(self):
def file_nb(self) -> int:
return len(self.files)

def to_dict(self):
def to_dict(self) -> dict:
return {
'fileNb': self.file_nb,
'files': [file.to_dict() for file in self.files],
Expand All @@ -55,6 +57,7 @@ def to_dict(self):
'compressionRate': self.compression_rate
}


def archive_info_to_text(archive_info: ArchiveInfo) -> str:
text = ''
text__files = ''
Expand Down Expand Up @@ -181,7 +184,6 @@ def zipfile_to_infos(self, zipfile: zipfile.ZipFile) -> ArchiveInfo:
archive_info = ArchiveInfo()
for ziplineinfo in zipfile.infolist():
fileinfo = FileInfo()
from datetime import datetime
fileinfo.last_modification = datetime(
year=ziplineinfo.date_time[0],
month=ziplineinfo.date_time[1],
Expand Down
Loading

0 comments on commit 2426a0d

Please sign in to comment.