Skip to content

Commit

Permalink
Add a hook for tzdata
Browse files Browse the repository at this point in the history
Collects timezone data files as well as submodules that are required
for proper timezone data discovery via importlib.resources.

Add a test that simulates the data loading as performed by zoneinfo
and backports.zoneinfo.
  • Loading branch information
rokm committed Oct 26, 2021
1 parent abf3dca commit d4097be
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/339.new.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add hook for ``tzdata``.
4 changes: 4 additions & 0 deletions requirements-test-libraries.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Backport of importlib.resources for python 3.8 and earlier.
importlib_resources==5.3.0; python_version < '3.9'

# ------------------ LIBRARIES ------------------ #
# TODO: Add most of the libraries we have hooks for, and write tests
av==8.0.3
Expand Down Expand Up @@ -58,6 +61,7 @@ swagger-spec-validator==2.7.3
thinc==8.0.10
timezonefinder==5.2.0
tableauhyperapi==0.0.13617
tzdata==2021.5
Unidecode==1.3.2
web3==5.24.0
websockets==10.0; python_version >= '3.7'
Expand Down
22 changes: 22 additions & 0 deletions src/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-tzdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ------------------------------------------------------------------
# Copyright (c) 2021 PyInstaller Development Team.
#
# This file is distributed under the terms of the GNU General Public
# License (version 2.0 or later).
#
# The full license is available in LICENSE.GPL.txt, distributed with
# this software.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ------------------------------------------------------------------

from PyInstaller.utils.hooks import collect_data_files, collect_submodules

# Collect timezone data files
datas = collect_data_files("tzdata")

# Collect submodules; each data subdirectory is in fact a package
# (e.g., zoneinfo.Europe), so we need its __init__.py for data files
# (e.g., zoneinfo/Europe/Ljubljana) to be discoverable via
# importlib.resources
hiddenimports = collect_submodules("tzdata")
29 changes: 28 additions & 1 deletion src/_pyinstaller_hooks_contrib/tests/test_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pytest
from pathlib import Path
from PyInstaller.compat import is_darwin, is_linux, is_py39, is_win
from PyInstaller.utils.hooks import is_module_satisfies
from PyInstaller.utils.hooks import is_module_satisfies, can_import_module
from PyInstaller.utils.tests import importorskip, requires, xfail


Expand Down Expand Up @@ -886,3 +886,30 @@ def test_pypeteer(pyi_builder):
import pypeteer
print(pypeteer.version)
""")


@importorskip("tzdata")
@pytest.mark.skipif(not is_py39 and not can_import_module('importlib_resources'),
reason='importlib_resources is required on python < 3.9.')
def test_tzdata(pyi_builder):
pyi_builder.test_source("""
import tzdata.zoneinfo # hiddenimport
try:
import importlib.resources as importlib_resources
except ImportError:
import importlib_resources
# This emulates time-zone data retrieval from tzdata, as peformed by
# zoneinfo / backports.zoneinfo
zone_name = "Europe/Ljubljana"
components = zone_name.split("/")
package_name = ".".join(["tzdata.zoneinfo"] + components[:-1])
resource_name = components[-1]
with importlib_resources.open_binary(package_name, resource_name) as fp:
data = fp.read()
print(data)
""")

0 comments on commit d4097be

Please sign in to comment.