fonttools 4.58.5__py3-none-any.whl → 4.59.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of fonttools might be problematic. Click here for more details.
- fontTools/__init__.py +1 -1
- fontTools/merge/__init__.py +1 -1
- fontTools/misc/filesystem/__init__.py +68 -0
- fontTools/misc/filesystem/_base.py +134 -0
- fontTools/misc/filesystem/_copy.py +45 -0
- fontTools/misc/filesystem/_errors.py +54 -0
- fontTools/misc/filesystem/_info.py +75 -0
- fontTools/misc/filesystem/_osfs.py +164 -0
- fontTools/misc/filesystem/_path.py +67 -0
- fontTools/misc/filesystem/_subfs.py +92 -0
- fontTools/misc/filesystem/_tempfs.py +34 -0
- fontTools/misc/filesystem/_tools.py +34 -0
- fontTools/misc/filesystem/_walk.py +55 -0
- fontTools/misc/filesystem/_zipfs.py +204 -0
- fontTools/misc/sstruct.py +2 -6
- fontTools/misc/xmlWriter.py +28 -1
- fontTools/pens/roundingPen.py +2 -2
- fontTools/subset/__init__.py +11 -11
- fontTools/ttLib/sfnt.py +2 -3
- fontTools/ttLib/tables/S__i_l_f.py +2 -2
- fontTools/ttLib/tables/T_S_I__1.py +2 -5
- fontTools/ttLib/tables/T_S_I__5.py +2 -2
- fontTools/ttLib/tables/_c_m_a_p.py +1 -1
- fontTools/ttLib/tables/_c_v_t.py +1 -2
- fontTools/ttLib/tables/_g_l_y_f.py +9 -10
- fontTools/ttLib/tables/_h_d_m_x.py +4 -4
- fontTools/ttLib/tables/_l_o_c_a.py +2 -2
- fontTools/ttLib/tables/_p_o_s_t.py +3 -4
- fontTools/ttLib/tables/otBase.py +2 -4
- fontTools/ttLib/tables/otTables.py +7 -12
- fontTools/ttLib/tables/sbixStrike.py +3 -3
- fontTools/ttLib/ttFont.py +7 -16
- fontTools/ttLib/woff2.py +10 -13
- fontTools/ufoLib/__init__.py +20 -25
- fontTools/ufoLib/glifLib.py +12 -17
- fontTools/ufoLib/validators.py +2 -4
- fontTools/unicodedata/__init__.py +2 -0
- fontTools/varLib/hvar.py +1 -1
- {fonttools-4.58.5.dist-info → fonttools-4.59.0.dist-info}/METADATA +19 -3
- {fonttools-4.58.5.dist-info → fonttools-4.59.0.dist-info}/RECORD +46 -34
- {fonttools-4.58.5.dist-info → fonttools-4.59.0.dist-info}/licenses/LICENSE.external +29 -0
- {fonttools-4.58.5.data → fonttools-4.59.0.data}/data/share/man/man1/ttx.1 +0 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.0.dist-info}/WHEEL +0 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.0.dist-info}/entry_points.txt +0 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.0.dist-info}/licenses/LICENSE +0 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.0.dist-info}/top_level.txt +0 -0
fontTools/__init__.py
CHANGED
fontTools/merge/__init__.py
CHANGED
|
@@ -196,7 +196,7 @@ def main(args=None):
|
|
|
196
196
|
|
|
197
197
|
if len(fontfiles) < 1:
|
|
198
198
|
print(
|
|
199
|
-
"usage:
|
|
199
|
+
"usage: fonttools merge [font1 ... fontN] [--input-file=filelist.txt] [--output-file=merged.ttf] [--import-file=tables.ttx]",
|
|
200
200
|
file=sys.stderr,
|
|
201
201
|
)
|
|
202
202
|
print(
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Minimal, stdlib-only replacement for [`pyfilesystem2`][1] API for use by `fontTools.ufoLib`.
|
|
2
|
+
|
|
3
|
+
This package is a partial reimplementation of the `fs` package by Will McGugan, used under the
|
|
4
|
+
MIT license. See LICENSE.external for details.
|
|
5
|
+
|
|
6
|
+
Note this only exports a **subset** of the `pyfilesystem2` API, in particular the modules,
|
|
7
|
+
classes and functions that are currently used directly by `fontTools.ufoLib`.
|
|
8
|
+
|
|
9
|
+
It opportunistically tries to import the relevant modules from the upstream `fs` package
|
|
10
|
+
when this is available. Otherwise it falls back to the replacement modules within this package.
|
|
11
|
+
|
|
12
|
+
As of version 4.59.0, the `fonttools[ufo]` extra no longer requires the `fs` package, thus
|
|
13
|
+
this `fontTools.misc.filesystem` package is used by default.
|
|
14
|
+
|
|
15
|
+
Client code can either replace `import fs` with `from fontTools.misc import filesystem as fs`
|
|
16
|
+
if that happens to work (no guarantee), or they can continue to use `fs` but they will have
|
|
17
|
+
to specify it as an explicit dependency of their project.
|
|
18
|
+
|
|
19
|
+
[1]: https://github.com/PyFilesystem/pyfilesystem2
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
try:
|
|
25
|
+
__import__("fs")
|
|
26
|
+
except ImportError:
|
|
27
|
+
from . import _base as base
|
|
28
|
+
from . import _copy as copy
|
|
29
|
+
from . import _errors as errors
|
|
30
|
+
from . import _info as info
|
|
31
|
+
from . import _osfs as osfs
|
|
32
|
+
from . import _path as path
|
|
33
|
+
from . import _subfs as subfs
|
|
34
|
+
from . import _tempfs as tempfs
|
|
35
|
+
from . import _tools as tools
|
|
36
|
+
from . import _walk as walk
|
|
37
|
+
from . import _zipfs as zipfs
|
|
38
|
+
|
|
39
|
+
_haveFS = False
|
|
40
|
+
else:
|
|
41
|
+
import fs.base as base
|
|
42
|
+
import fs.copy as copy
|
|
43
|
+
import fs.errors as errors
|
|
44
|
+
import fs.info as info
|
|
45
|
+
import fs.osfs as osfs
|
|
46
|
+
import fs.path as path
|
|
47
|
+
import fs.subfs as subfs
|
|
48
|
+
import fs.tempfs as tempfs
|
|
49
|
+
import fs.tools as tools
|
|
50
|
+
import fs.walk as walk
|
|
51
|
+
import fs.zipfs as zipfs
|
|
52
|
+
|
|
53
|
+
_haveFS = True
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
__all__ = [
|
|
57
|
+
"base",
|
|
58
|
+
"copy",
|
|
59
|
+
"errors",
|
|
60
|
+
"info",
|
|
61
|
+
"osfs",
|
|
62
|
+
"path",
|
|
63
|
+
"subfs",
|
|
64
|
+
"tempfs",
|
|
65
|
+
"tools",
|
|
66
|
+
"walk",
|
|
67
|
+
"zipfs",
|
|
68
|
+
]
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import typing
|
|
4
|
+
from abc import ABC, abstractmethod
|
|
5
|
+
|
|
6
|
+
from ._copy import copy_dir, copy_file
|
|
7
|
+
from ._errors import (
|
|
8
|
+
DestinationExists,
|
|
9
|
+
DirectoryExpected,
|
|
10
|
+
FileExpected,
|
|
11
|
+
FilesystemClosed,
|
|
12
|
+
NoSysPath,
|
|
13
|
+
ResourceNotFound,
|
|
14
|
+
)
|
|
15
|
+
from ._path import dirname
|
|
16
|
+
from ._walk import BoundWalker
|
|
17
|
+
|
|
18
|
+
if typing.TYPE_CHECKING:
|
|
19
|
+
from typing import IO, Any, Collection, Iterator, Self, Type
|
|
20
|
+
|
|
21
|
+
from ._info import Info
|
|
22
|
+
from ._subfs import SubFS
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class FS(ABC):
|
|
26
|
+
"""Abstract base class for custom filesystems."""
|
|
27
|
+
|
|
28
|
+
_closed: bool = False
|
|
29
|
+
|
|
30
|
+
@abstractmethod
|
|
31
|
+
def open(self, path: str, mode: str = "rb", **kwargs) -> IO[Any]: ...
|
|
32
|
+
|
|
33
|
+
@abstractmethod
|
|
34
|
+
def exists(self, path: str) -> bool: ...
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def isdir(self, path: str) -> bool: ...
|
|
38
|
+
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def isfile(self, path: str) -> bool: ...
|
|
41
|
+
|
|
42
|
+
@abstractmethod
|
|
43
|
+
def listdir(self, path: str) -> list[str]: ...
|
|
44
|
+
|
|
45
|
+
@abstractmethod
|
|
46
|
+
def makedir(self, path: str, recreate: bool = False) -> SubFS: ...
|
|
47
|
+
|
|
48
|
+
@abstractmethod
|
|
49
|
+
def makedirs(self, path: str, recreate: bool = False) -> SubFS: ...
|
|
50
|
+
|
|
51
|
+
@abstractmethod
|
|
52
|
+
def getinfo(self, path: str, namespaces: Collection[str] | None = None) -> Info: ...
|
|
53
|
+
|
|
54
|
+
@abstractmethod
|
|
55
|
+
def remove(self, path: str) -> None: ...
|
|
56
|
+
|
|
57
|
+
@abstractmethod
|
|
58
|
+
def removedir(self, path: str) -> None: ...
|
|
59
|
+
|
|
60
|
+
@abstractmethod
|
|
61
|
+
def removetree(self, path: str) -> None: ...
|
|
62
|
+
|
|
63
|
+
@abstractmethod
|
|
64
|
+
def movedir(self, src: str, dst: str, create: bool = False) -> None: ...
|
|
65
|
+
|
|
66
|
+
def getsyspath(self, path: str) -> str:
|
|
67
|
+
raise NoSysPath(f"the filesystem {self!r} has no system path")
|
|
68
|
+
|
|
69
|
+
def close(self):
|
|
70
|
+
self._closed = True
|
|
71
|
+
|
|
72
|
+
def isclosed(self) -> bool:
|
|
73
|
+
return self._closed
|
|
74
|
+
|
|
75
|
+
def __enter__(self) -> Self:
|
|
76
|
+
return self
|
|
77
|
+
|
|
78
|
+
def __exit__(self, exc_type, exc, tb):
|
|
79
|
+
self.close()
|
|
80
|
+
return False # never swallow exceptions
|
|
81
|
+
|
|
82
|
+
def check(self):
|
|
83
|
+
if self._closed:
|
|
84
|
+
raise FilesystemClosed(f"the filesystem {self!r} is closed")
|
|
85
|
+
|
|
86
|
+
def opendir(self, path: str, *, factory: Type[SubFS] | None = None) -> SubFS:
|
|
87
|
+
"""Return a sub‑filesystem rooted at `path`."""
|
|
88
|
+
if factory is None:
|
|
89
|
+
from ._subfs import SubFS
|
|
90
|
+
|
|
91
|
+
factory = SubFS
|
|
92
|
+
return factory(self, path)
|
|
93
|
+
|
|
94
|
+
def scandir(
|
|
95
|
+
self, path: str, namespaces: Collection[str] | None = None
|
|
96
|
+
) -> Iterator[Info]:
|
|
97
|
+
return (self.getinfo(f"{path}/{p}", namespaces) for p in self.listdir(path))
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def walk(self) -> BoundWalker:
|
|
101
|
+
return BoundWalker(self)
|
|
102
|
+
|
|
103
|
+
def readbytes(self, path: str) -> bytes:
|
|
104
|
+
with self.open(path, "rb") as f:
|
|
105
|
+
return f.read()
|
|
106
|
+
|
|
107
|
+
def writebytes(self, path: str, data: bytes):
|
|
108
|
+
with self.open(path, "wb") as f:
|
|
109
|
+
f.write(data)
|
|
110
|
+
|
|
111
|
+
def create(self, path: str, wipe: bool = False):
|
|
112
|
+
if not wipe and self.exists(path):
|
|
113
|
+
return False
|
|
114
|
+
with self.open(path, "wb"):
|
|
115
|
+
pass # 'touch' empty file
|
|
116
|
+
return True
|
|
117
|
+
|
|
118
|
+
def copy(self, src_path: str, dst_path: str, overwrite=False):
|
|
119
|
+
if not self.exists(src_path):
|
|
120
|
+
raise ResourceNotFound(f"{src_path!r} does not exist")
|
|
121
|
+
elif not self.isfile(src_path):
|
|
122
|
+
raise FileExpected(f"path {src_path!r} should be a file")
|
|
123
|
+
if not overwrite and self.exists(dst_path):
|
|
124
|
+
raise DestinationExists(f"destination {dst_path!r} already exists")
|
|
125
|
+
if not self.isdir(dirname(dst_path)):
|
|
126
|
+
raise DirectoryExpected(f"path {dirname(dst_path)!r} should be a directory")
|
|
127
|
+
copy_file(self, src_path, self, dst_path)
|
|
128
|
+
|
|
129
|
+
def copydir(self, src_path: str, dst_path: str, create=False):
|
|
130
|
+
if not create and not self.exists(dst_path):
|
|
131
|
+
raise ResourceNotFound(f"{dst_path!r} does not exist")
|
|
132
|
+
if not self.isdir(src_path):
|
|
133
|
+
raise DirectoryExpected(f"path {src_path!r} should be a directory")
|
|
134
|
+
copy_dir(self, src_path, self, dst_path)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
from ._errors import IllegalDestination
|
|
6
|
+
from ._path import combine, frombase, isbase
|
|
7
|
+
from ._tools import copy_file_data
|
|
8
|
+
|
|
9
|
+
if typing.TYPE_CHECKING:
|
|
10
|
+
from ._base import FS
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def copy_file(src_fs: FS, src_path: str, dst_fs: FS, dst_path: str):
|
|
14
|
+
if src_fs is dst_fs and src_path == dst_path:
|
|
15
|
+
raise IllegalDestination(f"cannot copy {src_path!r} to itself")
|
|
16
|
+
|
|
17
|
+
with src_fs.open(src_path, "rb") as src_file:
|
|
18
|
+
with dst_fs.open(dst_path, "wb") as dst_file:
|
|
19
|
+
copy_file_data(src_file, dst_file)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def copy_structure(
|
|
23
|
+
src_fs: FS,
|
|
24
|
+
dst_fs: FS,
|
|
25
|
+
src_root: str = "/",
|
|
26
|
+
dst_root: str = "/",
|
|
27
|
+
):
|
|
28
|
+
if src_fs is dst_fs and isbase(src_root, dst_root):
|
|
29
|
+
raise IllegalDestination(f"cannot copy {src_fs!r} to itself")
|
|
30
|
+
|
|
31
|
+
dst_fs.makedirs(dst_root, recreate=True)
|
|
32
|
+
for dir_path in src_fs.walk.dirs(src_root):
|
|
33
|
+
dst_fs.makedir(combine(dst_root, frombase(src_root, dir_path)), recreate=True)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def copy_dir(src_fs: FS, src_path: str, dst_fs: FS, dst_path: str):
|
|
37
|
+
copy_structure(src_fs, dst_fs, src_path, dst_path)
|
|
38
|
+
|
|
39
|
+
for file_path in src_fs.walk.files(src_path):
|
|
40
|
+
copy_path = combine(dst_path, frombase(src_path, file_path))
|
|
41
|
+
copy_file(src_fs, file_path, dst_fs, copy_path)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def copy_fs(src_fs: FS, dst_fs: FS):
|
|
45
|
+
copy_dir(src_fs, "/", dst_fs, "/")
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
class FSError(Exception):
|
|
2
|
+
pass
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class CreateFailed(FSError):
|
|
6
|
+
pass
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class FilesystemClosed(FSError):
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class MissingInfoNamespace(FSError):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class NoSysPath(FSError):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class OperationFailed(FSError):
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class IllegalDestination(OperationFailed):
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class ResourceError(FSError):
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ResourceNotFound(ResourceError):
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class DirectoryExpected(ResourceError):
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class DirectoryNotEmpty(ResourceError):
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class FileExpected(ResourceError):
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class DestinationExists(ResourceError):
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class ResourceReadOnly(ResourceError):
|
|
54
|
+
pass
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import typing
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
|
|
6
|
+
from ._errors import MissingInfoNamespace
|
|
7
|
+
|
|
8
|
+
if typing.TYPE_CHECKING:
|
|
9
|
+
from collections.abc import Mapping
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def epoch_to_datetime(t: int | None) -> datetime | None:
|
|
14
|
+
"""Convert epoch time to a UTC datetime."""
|
|
15
|
+
if t is None:
|
|
16
|
+
return None
|
|
17
|
+
return datetime.fromtimestamp(t, tz=timezone.utc)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Info:
|
|
21
|
+
__slots__ = ["raw", "namespaces"]
|
|
22
|
+
|
|
23
|
+
def __init__(self, raw_info: Mapping[str, Any]):
|
|
24
|
+
self.raw = raw_info
|
|
25
|
+
self.namespaces = frozenset(raw_info.keys())
|
|
26
|
+
|
|
27
|
+
def get(self, namespace: str, key: str, default: Any | None = None) -> Any | None:
|
|
28
|
+
try:
|
|
29
|
+
return self.raw[namespace].get(key, default)
|
|
30
|
+
except KeyError:
|
|
31
|
+
raise MissingInfoNamespace(f"Namespace {namespace!r} does not exist")
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def name(self) -> str:
|
|
35
|
+
return self.get("basic", "name")
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def is_dir(self) -> bool:
|
|
39
|
+
return self.get("basic", "is_dir")
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def is_file(self) -> bool:
|
|
43
|
+
return not self.is_dir
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def accessed(self) -> datetime | None:
|
|
47
|
+
return epoch_to_datetime(self.get("details", "accessed"))
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def modified(self) -> datetime | None:
|
|
51
|
+
return epoch_to_datetime(self.get("details", "modified"))
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def size(self) -> int | None:
|
|
55
|
+
return self.get("details", "size")
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def type(self) -> int | None:
|
|
59
|
+
return self.get("details", "type")
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def created(self) -> datetime | None:
|
|
63
|
+
return epoch_to_datetime(self.get("details", "created"))
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def metadata_changed(self) -> datetime | None:
|
|
67
|
+
return epoch_to_datetime(self.get("details", "metadata_changed"))
|
|
68
|
+
|
|
69
|
+
def __str__(self) -> str:
|
|
70
|
+
if self.is_dir:
|
|
71
|
+
return "<dir '{}'>".format(self.name)
|
|
72
|
+
else:
|
|
73
|
+
return "<file '{}'>".format(self.name)
|
|
74
|
+
|
|
75
|
+
__repr__ = __str__
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import errno
|
|
4
|
+
import platform
|
|
5
|
+
import shutil
|
|
6
|
+
import stat
|
|
7
|
+
import typing
|
|
8
|
+
from os import PathLike
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from ._base import FS
|
|
12
|
+
from ._errors import (
|
|
13
|
+
CreateFailed,
|
|
14
|
+
DirectoryExpected,
|
|
15
|
+
DirectoryNotEmpty,
|
|
16
|
+
FileExpected,
|
|
17
|
+
IllegalDestination,
|
|
18
|
+
ResourceError,
|
|
19
|
+
ResourceNotFound,
|
|
20
|
+
)
|
|
21
|
+
from ._info import Info
|
|
22
|
+
from ._path import isbase
|
|
23
|
+
|
|
24
|
+
if typing.TYPE_CHECKING:
|
|
25
|
+
from collections.abc import Collection
|
|
26
|
+
from typing import IO, Any
|
|
27
|
+
|
|
28
|
+
from ._subfs import SubFS
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
_WINDOWS_PLATFORM = platform.system() == "Windows"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class OSFS(FS):
|
|
35
|
+
"""Filesystem for a directory on the local disk.
|
|
36
|
+
|
|
37
|
+
A thin layer on top of `pathlib.Path`.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(self, root: str | PathLike, create: bool = False):
|
|
41
|
+
super().__init__()
|
|
42
|
+
self._root = Path(root).resolve()
|
|
43
|
+
if create:
|
|
44
|
+
self._root.mkdir(parents=True, exist_ok=True)
|
|
45
|
+
else:
|
|
46
|
+
if not self._root.is_dir():
|
|
47
|
+
raise CreateFailed(
|
|
48
|
+
f"unable to create OSFS: {root!r} does not exist or is not a directory"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
def _abs(self, rel_path: str) -> Path:
|
|
52
|
+
self.check()
|
|
53
|
+
return (self._root / rel_path.strip("/")).resolve()
|
|
54
|
+
|
|
55
|
+
def open(self, path: str, mode: str = "rb", **kwargs) -> IO[Any]:
|
|
56
|
+
try:
|
|
57
|
+
return self._abs(path).open(mode, **kwargs)
|
|
58
|
+
except FileNotFoundError:
|
|
59
|
+
raise ResourceNotFound(f"No such file or directory: {path!r}")
|
|
60
|
+
|
|
61
|
+
def exists(self, path: str) -> bool:
|
|
62
|
+
return self._abs(path).exists()
|
|
63
|
+
|
|
64
|
+
def isdir(self, path: str) -> bool:
|
|
65
|
+
return self._abs(path).is_dir()
|
|
66
|
+
|
|
67
|
+
def isfile(self, path: str) -> bool:
|
|
68
|
+
return self._abs(path).is_file()
|
|
69
|
+
|
|
70
|
+
def listdir(self, path: str) -> list[str]:
|
|
71
|
+
return [p.name for p in self._abs(path).iterdir()]
|
|
72
|
+
|
|
73
|
+
def _mkdir(self, path: str, parents: bool = False, exist_ok: bool = False) -> SubFS:
|
|
74
|
+
self._abs(path).mkdir(parents=parents, exist_ok=exist_ok)
|
|
75
|
+
return self.opendir(path)
|
|
76
|
+
|
|
77
|
+
def makedir(self, path: str, recreate: bool = False) -> SubFS:
|
|
78
|
+
return self._mkdir(path, parents=False, exist_ok=recreate)
|
|
79
|
+
|
|
80
|
+
def makedirs(self, path: str, recreate: bool = False) -> SubFS:
|
|
81
|
+
return self._mkdir(path, parents=True, exist_ok=recreate)
|
|
82
|
+
|
|
83
|
+
def getinfo(self, path: str, namespaces: Collection[str] | None = None) -> Info:
|
|
84
|
+
path = self._abs(path)
|
|
85
|
+
if not path.exists():
|
|
86
|
+
raise ResourceNotFound(f"No such file or directory: {str(path)!r}")
|
|
87
|
+
info = {
|
|
88
|
+
"basic": {
|
|
89
|
+
"name": path.name,
|
|
90
|
+
"is_dir": path.is_dir(),
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
namespaces = namespaces or ()
|
|
94
|
+
if "details" in namespaces:
|
|
95
|
+
stat_result = path.stat()
|
|
96
|
+
details = info["details"] = {
|
|
97
|
+
"accessed": stat_result.st_atime,
|
|
98
|
+
"modified": stat_result.st_mtime,
|
|
99
|
+
"size": stat_result.st_size,
|
|
100
|
+
"type": stat.S_IFMT(stat_result.st_mode),
|
|
101
|
+
"created": getattr(stat_result, "st_birthtime", None),
|
|
102
|
+
}
|
|
103
|
+
ctime_key = "created" if _WINDOWS_PLATFORM else "metadata_changed"
|
|
104
|
+
details[ctime_key] = stat_result.st_ctime
|
|
105
|
+
return Info(info)
|
|
106
|
+
|
|
107
|
+
def remove(self, path: str):
|
|
108
|
+
path = self._abs(path)
|
|
109
|
+
try:
|
|
110
|
+
path.unlink()
|
|
111
|
+
except FileNotFoundError:
|
|
112
|
+
raise ResourceNotFound(f"No such file or directory: {str(path)!r}")
|
|
113
|
+
except OSError as e:
|
|
114
|
+
if path.is_dir():
|
|
115
|
+
raise FileExpected(f"path {str(path)!r} should be a file")
|
|
116
|
+
else:
|
|
117
|
+
raise ResourceError(f"unable to remove {str(path)!r}: {e}")
|
|
118
|
+
|
|
119
|
+
def removedir(self, path: str):
|
|
120
|
+
try:
|
|
121
|
+
self._abs(path).rmdir()
|
|
122
|
+
except NotADirectoryError:
|
|
123
|
+
raise DirectoryExpected(f"path {path!r} should be a directory")
|
|
124
|
+
except OSError as e:
|
|
125
|
+
if e.errno == errno.ENOTEMPTY:
|
|
126
|
+
raise DirectoryNotEmpty(f"Directory not empty: {path!r}")
|
|
127
|
+
else:
|
|
128
|
+
raise ResourceError(f"unable to remove {path!r}: {e}")
|
|
129
|
+
|
|
130
|
+
def removetree(self, path: str):
|
|
131
|
+
shutil.rmtree(self._abs(path))
|
|
132
|
+
|
|
133
|
+
def movedir(self, src_dir: str, dst_dir: str, create: bool = False):
|
|
134
|
+
if isbase(src_dir, dst_dir):
|
|
135
|
+
raise IllegalDestination(f"cannot move {src_dir!r} to {dst_dir!r}")
|
|
136
|
+
src_path = self._abs(src_dir)
|
|
137
|
+
if not src_path.exists():
|
|
138
|
+
raise ResourceNotFound(f"Source {src_dir!r} does not exist")
|
|
139
|
+
elif not src_path.is_dir():
|
|
140
|
+
raise DirectoryExpected(f"Source {src_dir!r} should be a directory")
|
|
141
|
+
dst_path = self._abs(dst_dir)
|
|
142
|
+
if not create and not dst_path.exists():
|
|
143
|
+
raise ResourceNotFound(f"Destination {dst_dir!r} does not exist")
|
|
144
|
+
if dst_path.is_file():
|
|
145
|
+
raise DirectoryExpected(f"Destination {dst_dir!r} should be a directory")
|
|
146
|
+
if create:
|
|
147
|
+
dst_path.parent.mkdir(parents=True, exist_ok=True)
|
|
148
|
+
if dst_path.exists():
|
|
149
|
+
if list(dst_path.iterdir()):
|
|
150
|
+
raise DirectoryNotEmpty(f"Destination {dst_dir!r} is not empty")
|
|
151
|
+
elif _WINDOWS_PLATFORM:
|
|
152
|
+
# on Unix os.rename silently replaces an empty dst_dir whereas on
|
|
153
|
+
# Windows it always raises FileExistsError, empty or not.
|
|
154
|
+
dst_path.rmdir()
|
|
155
|
+
src_path.rename(dst_path)
|
|
156
|
+
|
|
157
|
+
def getsyspath(self, path: str) -> str:
|
|
158
|
+
return str(self._abs(path))
|
|
159
|
+
|
|
160
|
+
def __repr__(self) -> str:
|
|
161
|
+
return f"{self.__class__.__name__}({str(self._root)!r})"
|
|
162
|
+
|
|
163
|
+
def __str__(self) -> str:
|
|
164
|
+
return f"<{self.__class__.__name__.lower()} '{self._root}'>"
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import platform
|
|
3
|
+
|
|
4
|
+
_WINDOWS_PLATFORM = platform.system() == "Windows"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def combine(path1: str, path2) -> str:
|
|
8
|
+
if not path1:
|
|
9
|
+
return path2
|
|
10
|
+
return "{}/{}".format(path1.rstrip("/"), path2.lstrip("/"))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def split(path: str) -> tuple[str, str]:
|
|
14
|
+
if "/" not in path:
|
|
15
|
+
return ("", path)
|
|
16
|
+
split = path.rsplit("/", 1)
|
|
17
|
+
return (split[0] or "/", split[1])
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def dirname(path: str) -> str:
|
|
21
|
+
return split(path)[0]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def basename(path: str) -> str:
|
|
25
|
+
return split(path)[1]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def forcedir(path: str) -> str:
|
|
29
|
+
# Ensure the path ends with a trailing forward slash.
|
|
30
|
+
if not path.endswith("/"):
|
|
31
|
+
return path + "/"
|
|
32
|
+
return path
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def abspath(path: str) -> str:
|
|
36
|
+
# FS objects have no concept of a *current directory*. This simply
|
|
37
|
+
# ensures the path starts with a forward slash.
|
|
38
|
+
if not path.startswith("/"):
|
|
39
|
+
return "/" + path
|
|
40
|
+
return path
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def isbase(path1: str, path2: str) -> bool:
|
|
44
|
+
# Check if `path1` is a base or prefix of `path2`.
|
|
45
|
+
_path1 = forcedir(abspath(path1))
|
|
46
|
+
_path2 = forcedir(abspath(path2))
|
|
47
|
+
return _path2.startswith(_path1)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def frombase(path1: str, path2: str) -> str:
|
|
51
|
+
# Get the final path of `path2` that isn't in `path1`.
|
|
52
|
+
if not isbase(path1, path2):
|
|
53
|
+
raise ValueError(f"path1 must be a prefix of path2: {path1!r} vs {path2!r}")
|
|
54
|
+
return path2[len(path1) :]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def relpath(path: str) -> str:
|
|
58
|
+
return path.lstrip("/")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def normpath(path: str) -> str:
|
|
62
|
+
normalized = os.path.normpath(path)
|
|
63
|
+
if _WINDOWS_PLATFORM:
|
|
64
|
+
# os.path.normpath converts backslashes to forward slashes on Windows
|
|
65
|
+
# but we want forward slashes, so we convert them back
|
|
66
|
+
normalized = normalized.replace("\\", "/")
|
|
67
|
+
return normalized
|