Skip to content

Commit

Permalink
Add support for Python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Oct 20, 2024
1 parent 0c2d20f commit 554dc4a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
matrix:
os: ["ubuntu-latest"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
fail-fast: false

env:
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Skeem changelog

in progress
===========
- Added support for Python 3.12


2023-03-09 0.1.0
Expand Down
3 changes: 3 additions & 0 deletions skeem/frictionless/monkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .loader_stream import read_byte_stream_create
from .pandas_plugin import create_parser
from .parser_jsonl import read_cell_stream_create
from .parser_xlsx import read_loader
from .resource import ResourcePlus


Expand All @@ -28,6 +29,7 @@ def patch_modules():
- Don't croak when reading streams without `name` attribute.
"""

import frictionless.formats.excel.parsers
import frictionless.formats.json.parsers
import frictionless.formats.pandas.plugin
import frictionless.schemes.aws.loaders.s3
Expand All @@ -37,3 +39,4 @@ def patch_modules():
frictionless.formats.pandas.plugin.PandasPlugin.create_parser = create_parser
frictionless.schemes.aws.loaders.s3.S3Loader.read_byte_stream_create = s3_read_byte_stream_create
frictionless.schemes.stream.loader.StreamLoader.read_byte_stream_create = read_byte_stream_create
frictionless.formats.excel.parsers.XlsxParser.read_loader = read_loader
52 changes: 52 additions & 0 deletions skeem/frictionless/parser_xlsx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import atexit
import os
import shutil
import tempfile

from frictionless.formats.excel.control import ExcelControl
from frictionless.resource.resource import Resource
from frictionless.system import system


def read_loader(self):
"""
Patched for Python 3.12.
https://github.com/frictionlessdata/frictionless-py/issues/1642
https://github.com/frictionlessdata/frictionless-py/pull/1684
"""
control = ExcelControl.from_dialect(self.resource.dialect)
loader = system.create_loader(self.resource)
if not loader.remote:
return loader.open()

# Remote
# Create copy for remote source
# For remote stream we need local copy (will be deleted on close by Python)
# https://docs.python.org/3.5/library/tempfile.html#tempfile.TemporaryFile
if loader.remote:
path = self.resource.normpath

# Cached
if control.workbook_cache is not None and path in control.workbook_cache:
# TODO: rebase on using resource without system?
resource = Resource(path, scheme="file", format="xlsx")
resource.infer(sample=False)
loader = system.create_loader(resource)
return loader.open()

with loader as loader:
delete = control.workbook_cache is None
target = tempfile.NamedTemporaryFile(delete=delete)
shutil.copyfileobj(loader.byte_stream, target)
target.seek(0)
if not delete:
control.workbook_cache[path] = target.name # type: ignore
atexit.register(os.remove, target.name)
# TODO: rebase on using resource without system?
resource = Resource(target, scheme="stream", format="xlsx")
resource.infer(sample=False)
loader = system.create_loader(resource)
return loader.open()

return None

0 comments on commit 554dc4a

Please sign in to comment.