From 6df9b4af61df97da3ee20c377a8c08bd6dfd2352 Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Sun, 21 Feb 2021 15:40:44 -0500 Subject: [PATCH] Make zstd optional --- pytiled_parser/layer.py | 14 +++++++++++++- setup.cfg | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pytiled_parser/layer.py b/pytiled_parser/layer.py index 1fdefc0..c155cdf 100644 --- a/pytiled_parser/layer.py +++ b/pytiled_parser/layer.py @@ -10,8 +10,9 @@ See: import base64 import gzip +import importlib.util +import sys import zlib -import zstd from pathlib import Path from typing import Any, Callable, List, Optional, Union from typing import cast as type_cast @@ -24,6 +25,12 @@ from . import tiled_object from .common_types import Color, OrderedPair, Size from .util import parse_color +zstd_spec = importlib.util.find_spec("zstd") +if zstd_spec: + import zstd # pylint: disable=import-outside-toplevel +else: + zstd = None # pylint: disable=invalid-name + @attr.s(auto_attribs=True, kw_only=True) class Layer: @@ -244,6 +251,11 @@ def _decode_tile_layer_data( unzipped_data = zlib.decompress(unencoded_data) elif compression == "gzip": unzipped_data = gzip.decompress(unencoded_data) + elif compression == "zstd" and zstd is None: + raise ValueError( + "zstd compression support is not installed." + "To install use 'pip install pytiled-parser[zstd]'" + ) elif compression == "zstd": unzipped_data = zstd.decompress(unencoded_data) else: diff --git a/setup.cfg b/setup.cfg index 2f55dcd..eafc65f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,7 +28,6 @@ setup_requires = setuptools >= 40.6 pip >= 10 install_requires = - zstd == 1.4.8.1 attrs typing-extensions @@ -38,6 +37,9 @@ include = pytiled_parser.* [options.extras_require] +zstd = + zstd == 1.4.8.1 + dev = pytest pytest-cov