Skip to content

Commit

Permalink
feat: make some builders dependencies optionals
Browse files Browse the repository at this point in the history
- make ffmpeg, cairosvg, scribus, vtk, as optionals dependencies.
- adapt doc to explain how to install support for each optionals builders.
- remove isort-seed-config from pre-commit : not needed anymore with isort>5.

Refs: #233 
Co-authored-by: raphj <[email protected]>
  • Loading branch information
inkhey and raphj authored Jun 23, 2021
1 parent ac976fd commit 38a525f
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 98 deletions.
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ repos:
- id: check-yaml
- id: debug-statements
- id: check-merge-conflict
- repo: https://github.com/asottile/seed-isort-config
rev: v1.8.0
hooks:
- id: seed-isort-config
- repo: https://github.com/pre-commit/mirrors-isort
rev: 'v5.0.9'
hooks:
Expand Down
22 changes: 14 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ sudo: true
language: python
dist: xenial

install:
before_install:
- sudo rm -f /etc/ImageMagick-6/policy.xml
- sudo apt-get install xvfb libreoffice libfile-mimeinfo-perl inkscape poppler-utils qpdf imagemagick libimage-exiftool-perl ufraw-batch ffmpeg ghostscript libsecret-1-0 -y
# main requirements
- sudo apt-get install -y poppler-utils qpdf libfile-mimeinfo-perl libimage-exiftool-perl ghostscript libsecret-1-0
# optionals
- sudo apt-get install libreoffice inkscape ufraw-batch ffmpeg xvfb
- DRAWIO_VERSION="12.6.5" && curl -LO https://github.com/jgraph/drawio-desktop/releases/download/v${DRAWIO_VERSION}/draw.io-amd64-${DRAWIO_VERSION}.deb && sudo dpkg -i draw.io-amd64-${DRAWIO_VERSION}.deb
- pip install xvfbwrapper
- python setup.py install
- pip install pytest
- python3 -m pip install -U mypy==0.770

install:
- pip install ".[all, testing]"
before_script:
- export DISPLAY=:99.0
- which Xvfb
Expand All @@ -28,6 +28,8 @@ jobs:
- stage: static-tests
name: fmt
python: "3.7"
before_install: skip
install: skip
before_script:
- pip install black isort
script:
Expand All @@ -38,6 +40,8 @@ jobs:
- stage: static-tests
name: flake8
python: "3.7"
before_install: skip
install: skip
before_script:
- pip install flake8
script:
Expand All @@ -46,8 +50,10 @@ jobs:
- stage: static-tests
name: mypy
python: "3.7"
before_install: skip
install: skip
before_script:
- pip install mypy
- pip install mypy==0.770
script:
- mypy --version
- mypy --ignore-missing-imports --disallow-untyped-defs .
Expand Down
139 changes: 117 additions & 22 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Those file formats are generated using libreoffice.
The preview generation has a default timeout of 60 seconds.
It is possible to change this timeout by setting the `LIBREOFFICE_PROCESS_TIMEOUT` environment variable to a number of seconds.
Setting a zero or negative value for this variable will disable the timeout.

Archive file
~~~~~~~~~~~~

Expand All @@ -69,25 +69,133 @@ Video format
Installation
------------

Dependencies:

``apt-get install zlib1g-dev libjpeg-dev python3-pythonmagick inkscape xvfb poppler-utils libfile-mimeinfo-perl qpdf libimage-exiftool-perl ufraw-batch ffmpeg``

After installing dependencies, you can install preview-generator using ``pip``::
Mandatory Dependencies:
~~~~~~~~~~~~~~~~~~~~~~~

pip install preview-generator
On debian :

Optional dependencies:
```bash
apt-get install poppler-utils qpdf libfile-mimeinfo-perl libimage-exiftool-perl ghostscript libsecret-1-0 zlib1g-dev libjpeg-dev
```

To handle previews for office documents you will need ``LibreOffice``, if you don't have it already::
install preview_generator without external addons:

apt-get install libreoffice
```bash
pip install preview-generator
```

To install all previews builders dependencies:
```bash
pip install preview-generator[all]
sudo apt-get install libreoffice inkscape ufraw-batch ffmpeg xvfb
DRAWIO_VERSION="12.6.5" && curl -LO https://github.com/jgraph/drawio-desktop/releases/download/v${DRAWIO_VERSION}/draw.io-amd64-${DRAWIO_VERSION}.deb && sudo dpkg -i draw.io-amd64-${DRAWIO_VERSION}.deb
```

To check dependencies, you can run::

preview --check-dependencies

Office Files (LibreOffice)
~~~~~~~~~~~~~~~~~~~~~~~~~~

```bash
apt-get install libreoffice
```

DTP(Scribus)
~~~~~~~~~~~~
If you need to preview scribus `.sla` files you will need scribus >= 1.5.

On debian :

```bash
apt-get install scribus xvfb
pip install preview-generator[scribus]
```


If scribus >=1.5 is 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
Vector Images (Inkscape)
~~~~~~~~~~~~~~~~~~~~~~~~

on debian:

```bash
apt-get install inkscape
```


Vector Images (cairosvg)
~~~~~~~~~~~~~~~~~~~~~~~~

```bash
pip install preview-generator[cairosvg]
```

Video(ffmpeg)
~~~~~~~~~~~~~

On debian :

```bash
apt-get install ffmpeg
pip install preview-generator[video]
```

RAW Images(ufraw-batch)
~~~~~~~~~~~~~

On debian :

```bash
apt-get install ufraw-batch
```

Diagram(DrawIO)
~~~~~~~~~~~~~~~

1. install xvfb
2.install [draw-io package](https://github.com/jgraph/drawio-desktop/releases):
3.install python specific dependencies:

on debian:

```bash
apt install xvfb
DRAWIO_VERSION="12.6.5" && curl -LO https://github.com/jgraph/drawio-desktop/releases/download/v${DRAWIO_VERSION}/draw.io-amd64-${DRAWIO_VERSION}.deb && sudo dpkg -i draw.io-amd64-${DRAWIO_VERSION}.deb
pip install preview-generator[drawio]
```


3D files (VTK)
~~~~~~~~~~~~~~

:warning: VTK lib provided from pypi may not be builded for latest python version. You
can either decide to downgrade python version or build VTK yourself to make things work as expected.

On debian :

```bash
pip install preview-generator[3D]
```

HEIC support
~~~~~~~~~~~~

Building ImageMagick with heic support: `Building ImageMagick with heic support`_ .

.. _`Building ImageMagick with heic support`: doc/build_im_with_heic_support.rst


-----
Usage
Expand Down Expand Up @@ -328,20 +436,7 @@ will print

Preview created at path : the_zip-a733739af8006558720be26c4dc5569a.txt

------------
HEIC support
------------

Building ImageMagick with heic support: `Building ImageMagick with heic support`_ .

.. _`Building ImageMagick with heic support`: doc/build_im_with_heic_support.rst

---------------
Draw.io support
---------------

1. install package ``xvfb``.
2. install draw-io package

------------
Known Issues
Expand Down
41 changes: 3 additions & 38 deletions contribute.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,45 +44,10 @@ From scratch on a terminal :
* build your virtual env (env will be called "myenv", you can name it the way you want): `python3 -m venv myenv`
* if it's not already, activate it : `source myenv/bin/activate`. (`deactivate` to deactivate)
- install dependencies :
* `apt-get install poppler-utils qpdf libfile-mimeinfo-perl libimage-exiftool-perl ghostscript libsecret-1-0 zlib1g-dev libjpeg-dev`
* `pip install -e ".[dev, all]"`
* install external apt dependencies for specific builder (see README.md)

* `apt-get install libimage-exiftool-perl`
* `apt-get install zlib1g-dev`
* `apt-get install libjpeg-dev`
* `apt-get install python3-pythonmagick`
* `apt-get install inkscape`
* `apt-get install xvfb`
* `apt-get install poppler-utils`
* `apt-get install qpdf`
* `apt-get install libfile-mimeinfo-perl`
* `apt-get install ufraw-batch`
* `apt-get install ffmpeg`
* `pip install wand`
* `pip install Pillow`
* `pip install PyPDF2`
* `pip install python-magic`
* `pip install pyexifinfo`
* `pip install packaging`
* `pip install xvfbwrapper`
* `pip install pdf2image`
* `pip install pathlib`
* if you use python 3.5 or less `pip install typing`


.. code:: console
# general dependencies
apt-get install zlib1g-dev libjpeg-dev python3-pythonmagick inkscape xvfb poppler-utils qpdf libfile-mimeinfo-perl libimage-exiftool-perl
pip install wand Pillow PyPDF2 python-magic pyexifinfo packaging xvfbwrapper pdf2image pathlib
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

-----------------
Code Convention :
Expand Down
10 changes: 8 additions & 2 deletions preview_generator/preview/builder/document__drawio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import tempfile
import typing

from xvfbwrapper import Xvfb

from preview_generator.exception import BuilderDependencyNotFound
from preview_generator.exception import IntermediateFileBuildingFailed
from preview_generator.preview.builder.image__pillow import ImagePreviewBuilderPillow
Expand All @@ -15,12 +13,20 @@
from preview_generator.utils import MimetypeMapping
from preview_generator.utils import executable_is_available

xvfbwrapper_installed = True
try:
from xvfbwrapper import Xvfb
except ImportError:
xvfbwrapper_installed = False


class ImagePreviewBuilderDrawio(PreviewBuilder):
DRAWIO_MIMETYPES_MAPPING = [MimetypeMapping("application/drawio", ".drawio")]

@classmethod
def check_dependencies(cls) -> None:
if not xvfbwrapper_installed:
raise BuilderDependencyNotFound("this builder requires xvfbwrapper")
if not executable_is_available("xvfb-run"):
raise BuilderDependencyNotFound("this builder requires xvfb-run to be available")

Expand Down
10 changes: 8 additions & 2 deletions preview_generator/preview/builder/document__scribus.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from subprocess import check_output
import typing

from xvfbwrapper import Xvfb

from preview_generator.exception import BuilderDependencyNotFound
from preview_generator.extension import mimetypes_storage
from preview_generator.preview.builder.document_generic import DocumentPreviewBuilder
Expand All @@ -20,6 +18,12 @@
from preview_generator.utils import LOGGER_NAME
from preview_generator.utils import executable_is_available

xvfbwrapper_installed = True
try:
from xvfbwrapper import Xvfb
except ImportError:
xvfbwrapper_installed = False

SCRIPT_FOLDER_NAME = "scripts"
SCRIPT_NAME = "scribus_sla_to_pdf.py"
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -29,6 +33,8 @@
class DocumentPreviewBuilderScribus(DocumentPreviewBuilder):
@classmethod
def check_dependencies(cls) -> None:
if not xvfbwrapper_installed:
raise BuilderDependencyNotFound("this builder requires xvfbwrapper")
if not executable_is_available("scribus"):
raise BuilderDependencyNotFound("this builder requires scribus to be available")

Expand Down
15 changes: 13 additions & 2 deletions preview_generator/preview/builder/image__cairosvg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import tempfile
import typing

import cairosvg

# HACK - G.M - 2020-12-26 - Hack to allow loading modules without cairosvg installed
from preview_generator.exception import BuilderDependencyNotFound
from preview_generator.preview.builder.image__pillow import ImagePreviewBuilderPillow # nopep8
from preview_generator.preview.generic_preview import ImagePreviewBuilder
from preview_generator.utils import ImgDims

cairosvg_installed = True
try:
import cairosvg
except ImportError:
cairosvg_installed = False


class ImagePreviewBuilderCairoSVG(ImagePreviewBuilder):
"""
Expand All @@ -23,6 +29,11 @@ def get_label(cls) -> str:
def get_supported_mimetypes(cls) -> typing.List[str]:
return ["image/svg+xml", "image/svg"]

@classmethod
def check_dependencies(cls) -> None:
if not cairosvg_installed:
raise BuilderDependencyNotFound("this builder requires cairosvg to be available")

def build_jpeg_preview(
self,
file_path: str,
Expand Down
Loading

0 comments on commit 38a525f

Please sign in to comment.