fonttools 4.58.5__cp39-cp39-win32.whl → 4.59.1__cp39-cp39-win32.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/cffLib/CFF2ToCFF.py +40 -10
- fontTools/cffLib/transforms.py +11 -6
- fontTools/cu2qu/cu2qu.cp39-win32.pyd +0 -0
- fontTools/feaLib/builder.py +6 -1
- fontTools/feaLib/lexer.cp39-win32.pyd +0 -0
- fontTools/merge/__init__.py +1 -1
- fontTools/misc/bezierTools.cp39-win32.pyd +0 -0
- 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/psCharStrings.py +17 -2
- fontTools/misc/sstruct.py +2 -6
- fontTools/misc/xmlWriter.py +28 -1
- fontTools/pens/momentsPen.cp39-win32.pyd +0 -0
- fontTools/pens/roundingPen.py +2 -2
- fontTools/qu2cu/qu2cu.cp39-win32.pyd +0 -0
- 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/_g_v_a_r.py +6 -3
- fontTools/ttLib/tables/_h_d_m_x.py +4 -4
- fontTools/ttLib/tables/_h_m_t_x.py +7 -3
- 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/featureVars.py +8 -0
- fontTools/varLib/hvar.py +1 -1
- fontTools/varLib/instancer/__init__.py +65 -5
- fontTools/varLib/iup.cp39-win32.pyd +0 -0
- fontTools/varLib/mutator.py +11 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.1.dist-info}/METADATA +42 -12
- {fonttools-4.58.5.dist-info → fonttools-4.59.1.dist-info}/RECORD +61 -49
- {fonttools-4.58.5.dist-info → fonttools-4.59.1.dist-info}/licenses/LICENSE.external +29 -0
- {fonttools-4.58.5.data → fonttools-4.59.1.data}/data/share/man/man1/ttx.1 +0 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.1.dist-info}/WHEEL +0 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.1.dist-info}/entry_points.txt +0 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.1.dist-info}/licenses/LICENSE +0 -0
- {fonttools-4.58.5.dist-info → fonttools-4.59.1.dist-info}/top_level.txt +0 -0
fontTools/__init__.py
CHANGED
fontTools/cffLib/CFF2ToCFF.py
CHANGED
|
@@ -2,13 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
from fontTools.ttLib import TTFont, newTable
|
|
4
4
|
from fontTools.misc.cliTools import makeOutputFileName
|
|
5
|
+
from fontTools.misc.psCharStrings import T2StackUseExtractor
|
|
5
6
|
from fontTools.cffLib import (
|
|
6
7
|
TopDictIndex,
|
|
7
8
|
buildOrder,
|
|
8
9
|
buildDefaults,
|
|
9
10
|
topDictOperators,
|
|
10
11
|
privateDictOperators,
|
|
12
|
+
FDSelect,
|
|
11
13
|
)
|
|
14
|
+
from .transforms import desubroutinizeCharString
|
|
15
|
+
from .specializer import specializeProgram
|
|
12
16
|
from .width import optimizeWidths
|
|
13
17
|
from collections import defaultdict
|
|
14
18
|
import logging
|
|
@@ -27,7 +31,7 @@ def _convertCFF2ToCFF(cff, otFont):
|
|
|
27
31
|
The CFF2 font cannot be variable. (TODO Accept those and convert to the
|
|
28
32
|
default instance?)
|
|
29
33
|
|
|
30
|
-
This assumes a decompiled
|
|
34
|
+
This assumes a decompiled CFF2 table. (i.e. that the object has been
|
|
31
35
|
filled via :meth:`decompile` and e.g. not loaded from XML.)"""
|
|
32
36
|
|
|
33
37
|
cff.major = 1
|
|
@@ -51,9 +55,14 @@ def _convertCFF2ToCFF(cff, otFont):
|
|
|
51
55
|
if hasattr(topDict, key):
|
|
52
56
|
delattr(topDict, key)
|
|
53
57
|
|
|
54
|
-
fdArray = topDict.FDArray
|
|
55
58
|
charStrings = topDict.CharStrings
|
|
56
59
|
|
|
60
|
+
fdArray = topDict.FDArray
|
|
61
|
+
if not hasattr(topDict, "FDSelect"):
|
|
62
|
+
# FDSelect is optional in CFF2, but required in CFF.
|
|
63
|
+
fdSelect = topDict.FDSelect = FDSelect()
|
|
64
|
+
fdSelect.gidArray = [0] * len(charStrings.charStrings)
|
|
65
|
+
|
|
57
66
|
defaults = buildDefaults(privateDictOperators)
|
|
58
67
|
order = buildOrder(privateDictOperators)
|
|
59
68
|
for fd in fdArray:
|
|
@@ -69,6 +78,7 @@ def _convertCFF2ToCFF(cff, otFont):
|
|
|
69
78
|
if hasattr(privateDict, key):
|
|
70
79
|
delattr(privateDict, key)
|
|
71
80
|
|
|
81
|
+
# Add ending operators
|
|
72
82
|
for cs in charStrings.values():
|
|
73
83
|
cs.decompile()
|
|
74
84
|
cs.program.append("endchar")
|
|
@@ -100,23 +110,43 @@ def _convertCFF2ToCFF(cff, otFont):
|
|
|
100
110
|
if width != private.defaultWidthX:
|
|
101
111
|
cs.program.insert(0, width - private.nominalWidthX)
|
|
102
112
|
|
|
113
|
+
# Handle stack use since stack-depth is lower in CFF than in CFF2.
|
|
114
|
+
for glyphName in charStrings.keys():
|
|
115
|
+
cs, fdIndex = charStrings.getItemAndSelector(glyphName)
|
|
116
|
+
if fdIndex is None:
|
|
117
|
+
fdIndex = 0
|
|
118
|
+
private = fdArray[fdIndex].Private
|
|
119
|
+
extractor = T2StackUseExtractor(
|
|
120
|
+
getattr(private, "Subrs", []), cff.GlobalSubrs, private=private
|
|
121
|
+
)
|
|
122
|
+
stackUse = extractor.execute(cs)
|
|
123
|
+
if stackUse > 48: # CFF stack depth is 48
|
|
124
|
+
desubroutinizeCharString(cs)
|
|
125
|
+
cs.program = specializeProgram(cs.program)
|
|
126
|
+
|
|
127
|
+
# Unused subroutines are still in CFF2 (ie. lacking 'return' operator)
|
|
128
|
+
# because they were not decompiled when we added the 'return'.
|
|
129
|
+
# Moreover, some used subroutines may have become unused after the
|
|
130
|
+
# stack-use fixup. So we remove all unused subroutines now.
|
|
131
|
+
cff.remove_unused_subroutines()
|
|
132
|
+
|
|
103
133
|
mapping = {
|
|
104
|
-
name: ("cid" + str(n) if n else ".notdef")
|
|
134
|
+
name: ("cid" + str(n).zfill(5) if n else ".notdef")
|
|
105
135
|
for n, name in enumerate(topDict.charset)
|
|
106
136
|
}
|
|
107
137
|
topDict.charset = [
|
|
108
|
-
"cid" + str(n) if n else ".notdef" for n in range(len(topDict.charset))
|
|
138
|
+
"cid" + str(n).zfill(5) if n else ".notdef" for n in range(len(topDict.charset))
|
|
109
139
|
]
|
|
110
140
|
charStrings.charStrings = {
|
|
111
141
|
mapping[name]: v for name, v in charStrings.charStrings.items()
|
|
112
142
|
}
|
|
113
143
|
|
|
114
|
-
|
|
115
|
-
# the output if I add it.
|
|
116
|
-
# topDict.ROS = ("Adobe", "Identity", 0)
|
|
144
|
+
topDict.ROS = ("Adobe", "Identity", 0)
|
|
117
145
|
|
|
118
146
|
|
|
119
147
|
def convertCFF2ToCFF(font, *, updatePostTable=True):
|
|
148
|
+
if "CFF2" not in font:
|
|
149
|
+
raise ValueError("Input font does not contain a CFF2 table.")
|
|
120
150
|
cff = font["CFF2"].cff
|
|
121
151
|
_convertCFF2ToCFF(cff, font)
|
|
122
152
|
del font["CFF2"]
|
|
@@ -131,7 +161,7 @@ def convertCFF2ToCFF(font, *, updatePostTable=True):
|
|
|
131
161
|
|
|
132
162
|
|
|
133
163
|
def main(args=None):
|
|
134
|
-
"""Convert
|
|
164
|
+
"""Convert CFF2 OTF font to CFF OTF font"""
|
|
135
165
|
if args is None:
|
|
136
166
|
import sys
|
|
137
167
|
|
|
@@ -140,8 +170,8 @@ def main(args=None):
|
|
|
140
170
|
import argparse
|
|
141
171
|
|
|
142
172
|
parser = argparse.ArgumentParser(
|
|
143
|
-
"fonttools cffLib.
|
|
144
|
-
description="
|
|
173
|
+
"fonttools cffLib.CFF2ToCFF",
|
|
174
|
+
description="Convert a non-variable CFF2 font to CFF.",
|
|
145
175
|
)
|
|
146
176
|
parser.add_argument(
|
|
147
177
|
"input", metavar="INPUT.ttf", help="Input OTF file with CFF table."
|
fontTools/cffLib/transforms.py
CHANGED
|
@@ -94,17 +94,22 @@ class _DesubroutinizingT2Decompiler(SimpleT2Decompiler):
|
|
|
94
94
|
cs._patches.append((index, subr._desubroutinized))
|
|
95
95
|
|
|
96
96
|
|
|
97
|
+
def desubroutinizeCharString(cs):
|
|
98
|
+
"""Desubroutinize a charstring in-place."""
|
|
99
|
+
cs.decompile()
|
|
100
|
+
subrs = getattr(cs.private, "Subrs", [])
|
|
101
|
+
decompiler = _DesubroutinizingT2Decompiler(subrs, cs.globalSubrs, cs.private)
|
|
102
|
+
decompiler.execute(cs)
|
|
103
|
+
cs.program = cs._desubroutinized
|
|
104
|
+
del cs._desubroutinized
|
|
105
|
+
|
|
106
|
+
|
|
97
107
|
def desubroutinize(cff):
|
|
98
108
|
for fontName in cff.fontNames:
|
|
99
109
|
font = cff[fontName]
|
|
100
110
|
cs = font.CharStrings
|
|
101
111
|
for c in cs.values():
|
|
102
|
-
c
|
|
103
|
-
subrs = getattr(c.private, "Subrs", [])
|
|
104
|
-
decompiler = _DesubroutinizingT2Decompiler(subrs, c.globalSubrs, c.private)
|
|
105
|
-
decompiler.execute(c)
|
|
106
|
-
c.program = c._desubroutinized
|
|
107
|
-
del c._desubroutinized
|
|
112
|
+
desubroutinizeCharString(c)
|
|
108
113
|
# Delete all the local subrs
|
|
109
114
|
if hasattr(font, "FDArray"):
|
|
110
115
|
for fd in font.FDArray:
|
|
Binary file
|
fontTools/feaLib/builder.py
CHANGED
|
@@ -926,6 +926,11 @@ class Builder(object):
|
|
|
926
926
|
l.lookup_index for l in lookups if l.lookup_index is not None
|
|
927
927
|
)
|
|
928
928
|
)
|
|
929
|
+
# order doesn't matter, but lookup_indices preserves it.
|
|
930
|
+
# We want to combine identical sets of lookups (order doesn't matter)
|
|
931
|
+
# but also respect the order provided by the user (although there's
|
|
932
|
+
# a reasonable argument to just sort and dedupe, which fontc does)
|
|
933
|
+
lookup_key = frozenset(lookup_indices)
|
|
929
934
|
|
|
930
935
|
size_feature = tag == "GPOS" and feature_tag == "size"
|
|
931
936
|
force_feature = self.any_feature_variations(feature_tag, tag)
|
|
@@ -943,7 +948,7 @@ class Builder(object):
|
|
|
943
948
|
"stash debug information. See fonttools#2065."
|
|
944
949
|
)
|
|
945
950
|
|
|
946
|
-
feature_key = (feature_tag,
|
|
951
|
+
feature_key = (feature_tag, lookup_key)
|
|
947
952
|
feature_index = feature_indices.get(feature_key)
|
|
948
953
|
if feature_index is None:
|
|
949
954
|
feature_index = len(table.FeatureList.FeatureRecord)
|
|
Binary file
|
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(
|
|
Binary file
|
|
@@ -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__
|