Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some type file preview and more #28

Merged
merged 52 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4b2dade
Corrects tests, adds missing informations in README
Sep 25, 2018
2655a5c
Merge branch 'master' of github.com:algoo/preview-generator
Sep 25, 2018
458afd8
PEP8
Sep 26, 2018
993a512
adds test for xcf
Sep 27, 2018
b19a98d
adds scribus .sla support + updates readme + adds test for sla
Sep 27, 2018
c865250
PEP8
Sep 27, 2018
f8cdbda
forgotten ipdb
basilelegal Sep 27, 2018
63f066c
refactor of scribus and openoffice in document_generic builder- Todo …
basilelegal Sep 28, 2018
8731c8d
fix exit tool
basilelegal Sep 28, 2018
407f6c1
Upadtes setup for travis
basilelegal Sep 28, 2018
8166893
add travis command to build exiftool
buxx Sep 28, 2018
236844b
Fix remarks from PR, adds xvfb for scribus test
basilelegal Sep 28, 2018
ee8d98b
Merge branch 'scribus' of github.com:algoo/preview-generator into scr…
basilelegal Sep 28, 2018
c595684
travis: exiftool before pip
buxx Sep 28, 2018
0110d01
use xvfb context manager instead of command xvfb-run because it kills…
basilelegal Sep 28, 2018
74c5e68
Merge branch 'scribus' of github.com:algoo/preview-generator into scr…
basilelegal Sep 28, 2018
2049432
travis: allow sudo
buxx Sep 28, 2018
2321acc
fix travis
buxx Sep 28, 2018
b33b933
add `pip install xvfbwrapper` to travis install
buxx Oct 4, 2018
170f0c5
fixes odt/txt input unit tests
Oct 4, 2018
1fe3ac0
make unit test work again
Oct 5, 2018
823b94f
fixes mypy
Oct 5, 2018
74bb884
remove useless imports
Oct 5, 2018
05dc8c8
update .travis.yml
Oct 5, 2018
adb3977
remove wrong check of file path
Oct 5, 2018
e6fdbd1
add missing dependency for mimetype binary
Oct 5, 2018
3047129
trying to restore support of python from 3.3 to 3.7
Oct 5, 2018
3014700
Fixes setup and PEP8
basilelegal Oct 8, 2018
cabac85
adds xvfb in travis and define new dist for python 3.7
basilelegal Oct 8, 2018
131e1b0
test travis cache
basilelegal Oct 8, 2018
40a48d1
adds pathlib to setup.py
basilelegal Oct 8, 2018
5a24234
adds xvfbwrapper in setup
basilelegal Oct 8, 2018
5dd362d
test setup for travis
basilelegal Oct 8, 2018
7e24437
test setup for travis
basilelegal Oct 8, 2018
ef5e859
Fixes xvfbwrapper
basilelegal Oct 8, 2018
03b7a55
Fix depencie for scribus
basilelegal Oct 8, 2018
bc4af75
adds dev depencies in travis
basilelegal Oct 9, 2018
b0e26e1
test with another ubuntu dist
basilelegal Oct 9, 2018
b9b29c6
test with another ubuntu dist
basilelegal Oct 9, 2018
92e3a18
removes pythonmagick
basilelegal Oct 9, 2018
3e0b3d1
error in dist travis
basilelegal Oct 9, 2018
7ab2ff0
fixes unraise error
basilelegal Oct 9, 2018
35ba9f9
replace wand for pdf2image
basilelegal Oct 9, 2018
930a13c
updates setup and remove useless imports
basilelegal Oct 9, 2018
8ad9fe5
missing depency
basilelegal Oct 9, 2018
bed1942
Compatibility debian/ubuntu, disable sla test for travis
basilelegal Oct 10, 2018
2cf33f4
Fixes test and pep8
basilelegal Oct 10, 2018
4c0f5db
Fixes test
basilelegal Oct 10, 2018
cb5519a
remove useless test
basilelegal Oct 10, 2018
4223abd
Fixes bug with svg - collision between inkscape and Imconvert, inksca…
basilelegal Oct 10, 2018
14391f0
Fixes #30 error with txt file without extension
basilelegal Oct 10, 2018
42b5c14
Adds date to comment
basilelegal Oct 11, 2018
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
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