fixed that CachePackage.open works also for filenames as strings

This commit is contained in:
Jenny Danzmayr 2023-12-07 17:56:19 +01:00
parent 6e12553c5f
commit fc7e3a1ecf

View file

@ -2,8 +2,9 @@ import os
import struct import struct
from collections import namedtuple from collections import namedtuple
from io import BytesIO from io import BytesIO
from pathlib import Path
from tarfile import TarFile, TarInfo from tarfile import TarFile, TarInfo
from typing import BinaryIO, Self from typing import BinaryIO, Self, Optional
from pyzstd import CParameter, ZstdError, ZstdFile from pyzstd import CParameter, ZstdError, ZstdFile
@ -107,10 +108,12 @@ class CachePackage:
return cls(bounds, levels) return cls(bounds, levels)
@classmethod @classmethod
def open(cls, package=None): def open(cls, package:Optional[str | os.PathLike] = None):
if package is None: if package is None:
from django.conf import settings from django.conf import settings
package = settings.CACHE_ROOT / 'package.tar' package = settings.CACHE_ROOT / 'package.tar'
elif not hasattr(package, 'open'):
package = Path(package)
return cls.read(package.open('rb')) return cls.read(package.open('rb'))
cached = LocalContext() cached = LocalContext()