core: implement more methods for ZipPath and better support for get_files
This commit is contained in:
parent
0c5b2b4a09
commit
d464b1e607
2 changed files with 21 additions and 2 deletions
|
@ -161,6 +161,11 @@ from .logging import setup_logger, LazyLogger
|
||||||
Paths = Union[Sequence[PathIsh], PathIsh]
|
Paths = Union[Sequence[PathIsh], PathIsh]
|
||||||
|
|
||||||
|
|
||||||
|
def _is_zippath(p: Path) -> bool:
|
||||||
|
# weak type check here, don't want to depend on .kompress module in get_files
|
||||||
|
return type(p).__name__ == 'ZipPath'
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_GLOB = '*'
|
DEFAULT_GLOB = '*'
|
||||||
def get_files(
|
def get_files(
|
||||||
pp: Paths,
|
pp: Paths,
|
||||||
|
@ -183,7 +188,7 @@ def get_files(
|
||||||
return () # early return to prevent warnings etc
|
return () # early return to prevent warnings etc
|
||||||
sources = [Path(pp)]
|
sources = [Path(pp)]
|
||||||
else:
|
else:
|
||||||
sources = [Path(p) for p in pp]
|
sources = [p if isinstance(p, Path) else Path(p) for p in pp]
|
||||||
|
|
||||||
def caller() -> str:
|
def caller() -> str:
|
||||||
import traceback
|
import traceback
|
||||||
|
@ -192,6 +197,10 @@ def get_files(
|
||||||
|
|
||||||
paths: List[Path] = []
|
paths: List[Path] = []
|
||||||
for src in sources:
|
for src in sources:
|
||||||
|
if _is_zippath(src):
|
||||||
|
paths.append(src)
|
||||||
|
continue
|
||||||
|
|
||||||
if src.parts[0] == '~':
|
if src.parts[0] == '~':
|
||||||
src = src.expanduser()
|
src = src.expanduser()
|
||||||
# note: glob handled first, because e.g. on Windows asterisk makes is_dir unhappy
|
# note: glob handled first, because e.g. on Windows asterisk makes is_dir unhappy
|
||||||
|
@ -226,7 +235,7 @@ def get_files(
|
||||||
|
|
||||||
if guess_compression:
|
if guess_compression:
|
||||||
from .kompress import CPath, is_compressed
|
from .kompress import CPath, is_compressed
|
||||||
paths = [CPath(p) if is_compressed(p) else p for p in paths]
|
paths = [CPath(p) if is_compressed(p) and not _is_zippath(p) else p for p in paths]
|
||||||
return tuple(paths)
|
return tuple(paths)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ Various helpers for compression
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from functools import total_ordering
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import pathlib
|
import pathlib
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
@ -155,6 +156,7 @@ else:
|
||||||
zipfile_Path = object
|
zipfile_Path = object
|
||||||
|
|
||||||
|
|
||||||
|
@total_ordering
|
||||||
class ZipPath(zipfile_Path):
|
class ZipPath(zipfile_Path):
|
||||||
# NOTE: is_dir/is_file might not behave as expected, the base class checks it only based on the slash in path
|
# NOTE: is_dir/is_file might not behave as expected, the base class checks it only based on the slash in path
|
||||||
|
|
||||||
|
@ -175,6 +177,9 @@ class ZipPath(zipfile_Path):
|
||||||
def absolute(self) -> ZipPath:
|
def absolute(self) -> ZipPath:
|
||||||
return ZipPath(self.filepath.absolute(), self.at)
|
return ZipPath(self.filepath.absolute(), self.at)
|
||||||
|
|
||||||
|
def expanduser(self) -> ZipPath:
|
||||||
|
return ZipPath(self.filepath.expanduser(), self.at)
|
||||||
|
|
||||||
def exists(self) -> bool:
|
def exists(self) -> bool:
|
||||||
if self.at == '':
|
if self.at == '':
|
||||||
# special case, the base class returns False in this case for some reason
|
# special case, the base class returns False in this case for some reason
|
||||||
|
@ -224,6 +229,11 @@ class ZipPath(zipfile_Path):
|
||||||
return False
|
return False
|
||||||
return (self.filepath, self.subpath) == (other.filepath, other.subpath)
|
return (self.filepath, self.subpath) == (other.filepath, other.subpath)
|
||||||
|
|
||||||
|
def __lt__(self, other) -> bool:
|
||||||
|
if not isinstance(other, ZipPath):
|
||||||
|
return False
|
||||||
|
return (self.filepath, self.subpath) < (other.filepath, other.subpath)
|
||||||
|
|
||||||
def __hash__(self) -> int:
|
def __hash__(self) -> int:
|
||||||
return hash((self.filepath, self.subpath))
|
return hash((self.filepath, self.subpath))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue