diff --git a/django_dump_die/templatetags/dump_die.py b/django_dump_die/templatetags/dump_die.py index 3be9af19624422b7c452384cb9be57fd4c6c78c9..e5fc4f5a352244514ec7547e18b8eb1befe7319a 100644 --- a/django_dump_die/templatetags/dump_die.py +++ b/django_dump_die/templatetags/dump_die.py @@ -1,7 +1,6 @@ """Template Tags for DumpDie""" import inspect -import pytz import re import types @@ -25,6 +24,7 @@ from django_dump_die.constants import ( INCLUDE_MAGIC_METHODS, INCLUDE_ATTRIBUTES, INCLUDE_FUNCTIONS, + PYTZ_PRESENT, ) from django_dump_die.utils import ( @@ -46,6 +46,11 @@ from django_dump_die.utils import ( is_magic, ) +# Imports that may not be accessible, depending on local python environment setup. +if PYTZ_PRESENT: + import pytz + + register = template.Library() @@ -135,7 +140,7 @@ def dump_object( # OR if direct parent is a intermediate (excluding pytz timezone objects). elif ( _is_simple_type(obj) - or (parent_is_intermediate and not isinstance(obj, pytz.BaseTzInfo)) + or (PYTZ_PRESENT and parent_is_intermediate and not isinstance(obj, pytz.BaseTzInfo)) ): return _handle_simple_type(obj) @@ -341,7 +346,7 @@ def _is_intermediate_type(obj): """Return if the obj is an intermediate type.""" # Special handling for pytz timezone objects. - if isinstance(obj, pytz.BaseTzInfo): + if PYTZ_PRESENT and isinstance(obj, pytz.BaseTzInfo): return True # Handling for all other objects. diff --git a/django_dump_die/utils.py b/django_dump_die/utils.py index 473b5f5be2ff291f58f176c092da6194a3e7e033..07d83347f69ae90236e6fa90af5e206e8f48864d 100644 --- a/django_dump_die/utils.py +++ b/django_dump_die/utils.py @@ -5,7 +5,6 @@ import inspect import linecache import tokenize -import pytz import io import re from tokenize import ( @@ -24,7 +23,12 @@ from enum import EnumMeta from django.core.exceptions import ObjectDoesNotExist -from django_dump_die.constants import INCLUDE_FILENAME_LINENUMBER, COLORIZE_DUMPED_OBJECT_NAME +from django_dump_die.constants import COLORIZE_DUMPED_OBJECT_NAME, INCLUDE_FILENAME_LINENUMBER, PYTZ_PRESENT + +# Imports that may not be accessible, depending on local python environment setup. +if PYTZ_PRESENT: + import pytz + class Enum: """Enum faker class so an entire Enum can be dumped correctly.""" @@ -394,7 +398,7 @@ def get_obj_type(obj): # Special handling for certain types. if obj_type == 'NoneType': obj_type = 'null' - elif isinstance(obj, pytz.BaseTzInfo): + elif PYTZ_PRESENT and isinstance(obj, pytz.BaseTzInfo): obj_type = 'pytz_timezone' return obj_type diff --git a/django_dump_die/views/example_helpers.py b/django_dump_die/views/example_helpers.py index 13bf84dc54f5076963eff17309484fad95004f70..743aac38c972d9b07205cccbc991122d86c09600 100644 --- a/django_dump_die/views/example_helpers.py +++ b/django_dump_die/views/example_helpers.py @@ -6,7 +6,6 @@ to general package usage. """ from enum import Enum -import pytz import os from datetime import datetime, timedelta from decimal import Decimal @@ -17,8 +16,10 @@ from django.utils import timezone from pathlib import Path, PosixPath, PurePath, WindowsPath from types import ModuleType -from django_dump_die.constants import ZONEINFO_PRESENT +from django_dump_die.constants import PYTZ_PRESENT, ZONEINFO_PRESENT +if PYTZ_PRESENT: + import pytz if ZONEINFO_PRESENT: from zoneinfo import ZoneInfo @@ -603,7 +604,8 @@ def dump_datetime_types(): sample_tz_time = timezone.now().time() sample_dt_timedelta = timedelta(days=1) sample_tz_timedelta = timezone.timedelta(days=2) - sample_pytz_timezone = pytz.timezone('UTC') + if PYTZ_PRESENT: + sample_pytz_timezone = pytz.timezone('UTC') if ZONEINFO_PRESENT: sample_zoneinfo_timezone = ZoneInfo('UTC') @@ -618,7 +620,8 @@ def dump_datetime_types(): dump(sample_tz_time) dump(sample_dt_timedelta) dump(sample_tz_timedelta) - dump(sample_pytz_timezone) + if PYTZ_PRESENT: + dump(sample_pytz_timezone) if ZONEINFO_PRESENT: dump(sample_zoneinfo_timezone)