Skip to content

Commit

Permalink
feat: make pillow optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jun 18, 2021
1 parent 01736ba commit 9778883
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install
run: poetry install --extras image
- name: Build and deploy documentation
run: |
git config --global user.name "github-actions"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install
run: poetry install --extras image
- name: Run tests
run: poetry run pytest -v
- name: Run type-checking
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ The package can be installed with `pip`.
$ pip install beet
```

To create and edit images programmatically you should install `beet` with the `image` extra or install `Pillow` separately.

```bash
$ pip install beet[image]
$ pip install beet Pillow
```

You can make sure that `beet` was successfully installed by trying to use the toolchain from the command-line.

```bash
Expand Down Expand Up @@ -96,7 +103,7 @@ Commands:
Contributions are welcome. Make sure to first open an issue discussing the problem or the new feature before creating a pull request. The project uses [`poetry`](https://python-poetry.org).

```bash
$ poetry install
$ poetry install --extras image
```

You can run the tests with `poetry run pytest`. We use [`pytest-minecraft`](https://github.com/vberlier/pytest-minecraft) to run tests against actual Minecraft releases.
Expand Down
28 changes: 20 additions & 8 deletions beet/core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@
from typing import Any, Callable, Generic, Optional, Type, TypeVar, Union
from zipfile import ZipFile

from PIL import Image as img
try:
from PIL.Image import Image
from PIL.Image import new as new_image
from PIL.Image import open as open_image
except ImportError:
Image = Any

def new_image(*args: Any, **kwargs: Any) -> Any:
raise RuntimeError("Please install Pillow to create images programmatically")

def open_image(*args: Any, **kwargs: Any) -> Any:
raise RuntimeError("Please install Pillow to edit images programmatically")


from .utils import FileSystemPath, JsonDict, dump_json, extra_field

Expand Down Expand Up @@ -314,21 +326,21 @@ def default(cls) -> JsonDict:
return {}


class PngFile(BinaryFileBase[img.Image]):
class PngFile(BinaryFileBase[Image]):
"""Class representing a png file."""

image: FileDeserialize[img.Image] = FileDeserialize()
image: FileDeserialize[Image] = FileDeserialize()

@classmethod
def to_bytes(cls, content: img.Image) -> bytes:
def to_bytes(cls, content: Image) -> bytes:
dst = io.BytesIO()
content.save(dst, format="png")
return dst.getvalue()

@classmethod
def from_bytes(cls, content: bytes) -> img.Image:
return img.open(io.BytesIO(content))
def from_bytes(cls, content: bytes) -> Image:
return open_image(io.BytesIO(content))

@classmethod
def default(cls) -> img.Image:
return img.new("RGB", (16, 16), "black")
def default(cls) -> Image:
return new_image("RGB", (16, 16), "black")
9 changes: 6 additions & 3 deletions beet/library/resource_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@

from copy import deepcopy
from dataclasses import dataclass
from typing import Dict, Optional, Type
from typing import Any, Dict, Optional, Type

from PIL import Image as img
try:
from PIL.Image import Image
except ImportError:
Image = Any

from beet.core.file import BinaryFile, BinaryFileContent, JsonFile, PngFile, TextFile
from beet.core.utils import JsonDict, extra_field
Expand Down Expand Up @@ -148,7 +151,7 @@ class TextureMcmeta(JsonFile, NamespaceFile):
class Texture(PngFile, NamespaceFile):
"""Class representing a texture."""

content: BinaryFileContent[img.Image] = None
content: BinaryFileContent[Image] = None
mcmeta: Optional[JsonDict] = extra_field(default=None)

scope = ("textures",)
Expand Down
24 changes: 5 additions & 19 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ include = ["beet/py.typed"]
python = "^3.8"
nbtlib = "^1.12.0"
pathspec = "^0.8.1"
Pillow = "^8.2.0"
pydantic = "^1.8.1"
click = "^7.1.2"
click-help-colors = "^0.9"
Jinja2 = "^3.0.1"
colorama = {version = "*", markers = 'sys_platform == "win32"'}
fnvhash = "^0.1.0"
base58 = "^2.1.0"
toml = "^0.10.2"
PyYAML = "^5.4.1"
Pillow = {version = "*", optional = true}
colorama = {version = "*", markers = 'sys_platform == "win32"'}

[tool.poetry.extras]
image = ["Pillow"]

[tool.poetry.dev-dependencies]
black = "^21.6b0"
Expand Down

0 comments on commit 9778883

Please sign in to comment.