seqparse 2.0.0rc1__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.
- seqparse/__init__.py +210 -0
- seqparse/cli/__init__.py +1 -0
- seqparse/cli/seqls.py +380 -0
- seqparse/containers.py +470 -0
- seqparse/files.py +168 -0
- seqparse/py.typed +0 -0
- seqparse/regex.py +191 -0
- seqparse/seqparse.py +532 -0
- seqparse/sequences.py +1288 -0
- seqparse-2.0.0rc1.dist-info/METADATA +335 -0
- seqparse-2.0.0rc1.dist-info/RECORD +15 -0
- seqparse-2.0.0rc1.dist-info/WHEEL +5 -0
- seqparse-2.0.0rc1.dist-info/entry_points.txt +2 -0
- seqparse-2.0.0rc1.dist-info/licenses/LICENSE.txt +21 -0
- seqparse-2.0.0rc1.dist-info/top_level.txt +1 -0
seqparse/containers.py
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
"""Classes utilized by the Seqparse class."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterable, Iterator, MutableMapping, MutableSet
|
|
6
|
+
import functools
|
|
7
|
+
import itertools
|
|
8
|
+
import operator
|
|
9
|
+
import os
|
|
10
|
+
from typing import Any, Final, overload, TypeVar
|
|
11
|
+
|
|
12
|
+
import seqparse
|
|
13
|
+
from seqparse import files, sequences
|
|
14
|
+
|
|
15
|
+
__all__ = ("FileExtension", "FileSequenceContainer", "SingletonContainer")
|
|
16
|
+
|
|
17
|
+
# Sentinel for "no default given" (None is a valid default for pop()).
|
|
18
|
+
_MISSING: Final = object()
|
|
19
|
+
|
|
20
|
+
_K = TypeVar("_K")
|
|
21
|
+
_V = TypeVar("_V")
|
|
22
|
+
_T = TypeVar("_T")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
###############################################################################
|
|
26
|
+
# Class: _AutoMapping
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _AutoMapping(MutableMapping[_K, _V]):
|
|
30
|
+
"""
|
|
31
|
+
Mapping that creates missing entries when indexed, like defaultdict.
|
|
32
|
+
|
|
33
|
+
Unlike MutableMapping's defaults, `in`, get(), pop() and setdefault() look
|
|
34
|
+
at the stored entries directly and never create one as a side effect.
|
|
35
|
+
Subclasses implement _create() and __setitem__().
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self) -> None:
|
|
39
|
+
"""Initialise the instance."""
|
|
40
|
+
self._data: dict[_K, _V] = {}
|
|
41
|
+
|
|
42
|
+
def _create(self, key: _K) -> _V:
|
|
43
|
+
"""Create the entry for a missing key."""
|
|
44
|
+
raise NotImplementedError
|
|
45
|
+
|
|
46
|
+
def __getitem__(self, key: _K) -> _V:
|
|
47
|
+
"""Define key getter logic (per collections.defaultdict)."""
|
|
48
|
+
if key not in self._data:
|
|
49
|
+
self._data[key] = self._create(key)
|
|
50
|
+
return self._data[key]
|
|
51
|
+
|
|
52
|
+
def __delitem__(self, key: _K) -> None:
|
|
53
|
+
"""Define key deletion logic (per standard dictionary)."""
|
|
54
|
+
del self._data[key]
|
|
55
|
+
|
|
56
|
+
def __contains__(self, key: object) -> bool:
|
|
57
|
+
"""Membership test that, like defaultdict, never creates entries."""
|
|
58
|
+
return key in self._data
|
|
59
|
+
|
|
60
|
+
def __iter__(self) -> Iterator[_K]:
|
|
61
|
+
"""Define key iteration logic (per standard dictionary)."""
|
|
62
|
+
return iter(self._data)
|
|
63
|
+
|
|
64
|
+
def __len__(self) -> int:
|
|
65
|
+
"""Define item length logic (per standard dictionary)."""
|
|
66
|
+
return len(self._data)
|
|
67
|
+
|
|
68
|
+
@overload
|
|
69
|
+
def get(self, key: _K) -> _V | None: ...
|
|
70
|
+
@overload
|
|
71
|
+
def get(self, key: _K, default: _V | _T) -> _V | _T: ...
|
|
72
|
+
def get(self, key: _K, default: object = None) -> object:
|
|
73
|
+
"""Look up a key without creating it (per collections.defaultdict)."""
|
|
74
|
+
return self._data.get(key, default)
|
|
75
|
+
|
|
76
|
+
@overload
|
|
77
|
+
def pop(self, key: _K) -> _V: ...
|
|
78
|
+
@overload
|
|
79
|
+
def pop(self, key: _K, default: _V) -> _V: ...
|
|
80
|
+
@overload
|
|
81
|
+
def pop(self, key: _K, default: _T) -> _V | _T: ...
|
|
82
|
+
def pop(self, key: _K, default: object = _MISSING) -> object:
|
|
83
|
+
"""Remove and return a key's value without creating missing keys."""
|
|
84
|
+
if default is _MISSING:
|
|
85
|
+
return self._data.pop(key)
|
|
86
|
+
return self._data.pop(key, default)
|
|
87
|
+
|
|
88
|
+
def setdefault(self, key: _K, default: Any = None) -> _V:
|
|
89
|
+
"""
|
|
90
|
+
Return a key's value, inserting `default` if the key is missing.
|
|
91
|
+
|
|
92
|
+
With no default, a missing key gets a new empty child (like indexing,
|
|
93
|
+
per collections.defaultdict).
|
|
94
|
+
"""
|
|
95
|
+
if key not in self._data:
|
|
96
|
+
if default is None:
|
|
97
|
+
return self[key]
|
|
98
|
+
self[key] = default
|
|
99
|
+
return self._data[key]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
###############################################################################
|
|
103
|
+
# Class: FileExtension
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class FileExtension(_AutoMapping[int, sequences.FileSequence]):
|
|
107
|
+
"""
|
|
108
|
+
Container for frame sequences, indexed by zero-padding.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
name (str, optional): The file extension used by the contents of the
|
|
112
|
+
container (ie, "exr", "tif").
|
|
113
|
+
parent (FileSequenceContainer, optional): The container from which this
|
|
114
|
+
instance was spawned.
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
_CHILD_CLASS = sequences.FileSequence
|
|
118
|
+
|
|
119
|
+
def __init__(
|
|
120
|
+
self,
|
|
121
|
+
name: str | None = None,
|
|
122
|
+
parent: FileSequenceContainer | None = None,
|
|
123
|
+
) -> None:
|
|
124
|
+
"""Initialise the instance."""
|
|
125
|
+
super().__init__()
|
|
126
|
+
self._name: str | None = None
|
|
127
|
+
self._parent: FileSequenceContainer | None = None
|
|
128
|
+
|
|
129
|
+
self.name = name
|
|
130
|
+
self.parent = parent
|
|
131
|
+
|
|
132
|
+
def _create(self, key: int) -> sequences.FileSequence:
|
|
133
|
+
"""Create an empty sequence for a missing pad."""
|
|
134
|
+
return self._adopt(self._CHILD_CLASS(ext=self.name, pad=key))
|
|
135
|
+
|
|
136
|
+
def __repr__(self) -> str: # pragma: no cover
|
|
137
|
+
"""Pretty representation of the instance."""
|
|
138
|
+
return f"{type(self).__name__}(name={self.name!r}, pads={sorted(self)})"
|
|
139
|
+
|
|
140
|
+
def __setitem__(self, key: int, value: object) -> None:
|
|
141
|
+
"""Define item setting logic (per standard dictionary)."""
|
|
142
|
+
if isinstance(value, (list, tuple, set)):
|
|
143
|
+
value = self._adopt(
|
|
144
|
+
self._CHILD_CLASS(ext=self.name, frames=value, pad=key)
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
if not isinstance(value, self._CHILD_CLASS):
|
|
148
|
+
raise TypeError(
|
|
149
|
+
f'Container may only hold "{self._CHILD_CLASS.__name__}" '
|
|
150
|
+
f'instances ("{type(value).__name__}" provided)'
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
self._data[key] = value
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def name(self) -> str | None:
|
|
157
|
+
"""str: name of the file extension."""
|
|
158
|
+
return self._name
|
|
159
|
+
|
|
160
|
+
@name.setter
|
|
161
|
+
def name(self, val: object) -> None:
|
|
162
|
+
self._name = str(val) if val else None
|
|
163
|
+
|
|
164
|
+
@property
|
|
165
|
+
def parent(self) -> FileSequenceContainer | None:
|
|
166
|
+
"""FileSequenceContainer: parent of the instance."""
|
|
167
|
+
return self._parent
|
|
168
|
+
|
|
169
|
+
@parent.setter
|
|
170
|
+
def parent(self, val: object) -> None:
|
|
171
|
+
self._parent = None
|
|
172
|
+
if isinstance(val, FileSequenceContainer):
|
|
173
|
+
self._parent = val
|
|
174
|
+
# Sequences added before the parent was set adopt its names too.
|
|
175
|
+
for child in self._data.values():
|
|
176
|
+
self._adopt(child)
|
|
177
|
+
|
|
178
|
+
def _adopt(self, child: sequences.FileSequence) -> sequences.FileSequence:
|
|
179
|
+
"""Give a child sequence the parent container's path and name."""
|
|
180
|
+
# An empty container is falsy (it's a mapping): test identity.
|
|
181
|
+
if self.parent is not None:
|
|
182
|
+
child.path = self.parent.path
|
|
183
|
+
child.name = self.parent.name
|
|
184
|
+
return child
|
|
185
|
+
|
|
186
|
+
def output(self) -> Iterator[sequences.FileSequence]:
|
|
187
|
+
"""
|
|
188
|
+
Calculate a sorted list of all contained file extentions.
|
|
189
|
+
|
|
190
|
+
Yields:
|
|
191
|
+
FrameSequence, sorted by zero-pad length.
|
|
192
|
+
"""
|
|
193
|
+
# First, check to see if we need to consolidate our file sequences.
|
|
194
|
+
# Walk the pads from largest to smallest: an unpadded sequence merges
|
|
195
|
+
# into the sequence with the next smaller pad.
|
|
196
|
+
data = sorted(
|
|
197
|
+
self._data.items(), key=operator.itemgetter(0), reverse=True
|
|
198
|
+
)
|
|
199
|
+
for (pad, fseq), (_, next_fseq) in itertools.pairwise(data):
|
|
200
|
+
# NOTE: the is_padded() method will force recalculation if the
|
|
201
|
+
# object is dirty.
|
|
202
|
+
if not fseq.is_padded:
|
|
203
|
+
next_fseq.update(fseq)
|
|
204
|
+
del self[pad]
|
|
205
|
+
|
|
206
|
+
for pad in sorted(self):
|
|
207
|
+
yield self[pad]
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
###############################################################################
|
|
211
|
+
# Class: FileSequenceContainer
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@functools.total_ordering
|
|
215
|
+
class FileSequenceContainer(_AutoMapping[str, FileExtension]):
|
|
216
|
+
"""
|
|
217
|
+
Container for file sequences, indexed by file extension.
|
|
218
|
+
|
|
219
|
+
Args:
|
|
220
|
+
name (str, optional): Base name of the contained files.
|
|
221
|
+
file_path (str, optional): Directory in which the contained files
|
|
222
|
+
reside.
|
|
223
|
+
"""
|
|
224
|
+
|
|
225
|
+
_CHILD_CLASS = FileExtension
|
|
226
|
+
|
|
227
|
+
# Mutable, and equality depends on mutable state: deliberately
|
|
228
|
+
# unhashable (like set and dict).
|
|
229
|
+
__hash__ = None # type: ignore[assignment]
|
|
230
|
+
|
|
231
|
+
def __init__(
|
|
232
|
+
self, name: str | None = None, file_path: str | None = None
|
|
233
|
+
) -> None:
|
|
234
|
+
"""Initialise the instance."""
|
|
235
|
+
super().__init__()
|
|
236
|
+
self._full = ""
|
|
237
|
+
self._name: str | None = None
|
|
238
|
+
self._path: str | None = None
|
|
239
|
+
|
|
240
|
+
self.name = name
|
|
241
|
+
self.path = file_path
|
|
242
|
+
|
|
243
|
+
def _create(self, key: str) -> FileExtension:
|
|
244
|
+
"""Create an empty extension container for a missing extension."""
|
|
245
|
+
return self._CHILD_CLASS(name=key, parent=self)
|
|
246
|
+
|
|
247
|
+
def __eq__(self, other: object) -> bool:
|
|
248
|
+
"""
|
|
249
|
+
Define equality between instances.
|
|
250
|
+
|
|
251
|
+
NOTE: Equality is solely based upon comparison of the "full_name"
|
|
252
|
+
property and is only used for output sorting.
|
|
253
|
+
"""
|
|
254
|
+
if isinstance(other, FileSequenceContainer) and type(other) is type(
|
|
255
|
+
self
|
|
256
|
+
):
|
|
257
|
+
return self.full_name == other.full_name
|
|
258
|
+
return False
|
|
259
|
+
|
|
260
|
+
def __lt__(self, other: object) -> bool:
|
|
261
|
+
"""
|
|
262
|
+
Define whether one instance may be sorted below another.
|
|
263
|
+
|
|
264
|
+
NOTE: Equality is solely based upon comparison of the "full_name"
|
|
265
|
+
property and is only used for output sorting.
|
|
266
|
+
"""
|
|
267
|
+
if isinstance(other, FileSequenceContainer) and type(other) is type(
|
|
268
|
+
self
|
|
269
|
+
):
|
|
270
|
+
return self.full_name < other.full_name
|
|
271
|
+
return True
|
|
272
|
+
|
|
273
|
+
def __repr__(self) -> str: # pragma: no cover
|
|
274
|
+
"""Pretty representation of the instance."""
|
|
275
|
+
return (
|
|
276
|
+
f"{type(self).__name__}(full_name={self.full_name!r}, "
|
|
277
|
+
f"exts={sorted(self)})"
|
|
278
|
+
)
|
|
279
|
+
|
|
280
|
+
def __setitem__(self, key: str, value: object) -> None:
|
|
281
|
+
"""Define item setting logic (per standard dictionary)."""
|
|
282
|
+
if not isinstance(value, self._CHILD_CLASS):
|
|
283
|
+
raise TypeError(
|
|
284
|
+
f'Container may only hold "{self._CHILD_CLASS.__name__}" '
|
|
285
|
+
f'instances ("{type(value).__name__}" provided)'
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
if key != value.name:
|
|
289
|
+
raise ValueError(
|
|
290
|
+
"Key value must match extension name of provided value "
|
|
291
|
+
f"({key!r} != {value.name!r})"
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
self._data[key] = value
|
|
295
|
+
value.parent = self
|
|
296
|
+
|
|
297
|
+
@property
|
|
298
|
+
def full_name(self) -> str:
|
|
299
|
+
"""str: Full (base) name of the file sequence."""
|
|
300
|
+
return self._full
|
|
301
|
+
|
|
302
|
+
@property
|
|
303
|
+
def name(self) -> str | None:
|
|
304
|
+
"""str: Base name of the file sequence (no containing directory)."""
|
|
305
|
+
return self._name
|
|
306
|
+
|
|
307
|
+
@name.setter
|
|
308
|
+
def name(self, val: object) -> None:
|
|
309
|
+
self._name = str(val) if val else None
|
|
310
|
+
self._full = os.path.join(self._path or "", self._name or "")
|
|
311
|
+
|
|
312
|
+
@property
|
|
313
|
+
def path(self) -> str | None:
|
|
314
|
+
"""str: directory in which the contained files reside."""
|
|
315
|
+
return self._path
|
|
316
|
+
|
|
317
|
+
@path.setter
|
|
318
|
+
def path(self, val: object) -> None:
|
|
319
|
+
self._path = str(val) if val else None
|
|
320
|
+
self._full = os.path.join(self._path or "", self._name or "")
|
|
321
|
+
|
|
322
|
+
def output(self) -> Iterator[sequences.FileSequence]:
|
|
323
|
+
"""
|
|
324
|
+
Calculate a sorted list of all contained file sequences.
|
|
325
|
+
|
|
326
|
+
Yields:
|
|
327
|
+
FileSequence, sorted (in order) by file path, extension, and zero-
|
|
328
|
+
padding length.
|
|
329
|
+
"""
|
|
330
|
+
for ext in sorted(self):
|
|
331
|
+
yield from self[ext].output()
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
###############################################################################
|
|
335
|
+
# class: SingletonContainer
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
class SingletonContainer(MutableSet[str]):
|
|
339
|
+
"""
|
|
340
|
+
Container for singleton files, indexed alphabetically by file path.
|
|
341
|
+
|
|
342
|
+
Args:
|
|
343
|
+
file_names (list-like of str, optional): List of base file names to
|
|
344
|
+
store in the container.
|
|
345
|
+
file_path (str, optional): Directory in which the contained files
|
|
346
|
+
reside.
|
|
347
|
+
"""
|
|
348
|
+
|
|
349
|
+
def __init__(
|
|
350
|
+
self,
|
|
351
|
+
file_names: Iterable[object] | None = None,
|
|
352
|
+
file_path: str | None = None,
|
|
353
|
+
) -> None:
|
|
354
|
+
"""Initialise the instance."""
|
|
355
|
+
self._data: set[str] = set()
|
|
356
|
+
self._path = ""
|
|
357
|
+
self._stat: dict[str, os.stat_result] = {}
|
|
358
|
+
|
|
359
|
+
for item in file_names or []:
|
|
360
|
+
self.add(item)
|
|
361
|
+
|
|
362
|
+
self.path = file_path
|
|
363
|
+
|
|
364
|
+
def __contains__(self, item: object) -> bool:
|
|
365
|
+
"""Defining containment logic (per standard set)."""
|
|
366
|
+
return str(item) in self._data
|
|
367
|
+
|
|
368
|
+
def __iter__(self) -> Iterator[str]:
|
|
369
|
+
"""Defining item iteration logic (per standard set)."""
|
|
370
|
+
return iter(self._data)
|
|
371
|
+
|
|
372
|
+
def __len__(self) -> int:
|
|
373
|
+
"""Defining item length logic (per standard set)."""
|
|
374
|
+
return len(self._data)
|
|
375
|
+
|
|
376
|
+
def __repr__(self) -> str: # pragma: no cover
|
|
377
|
+
"""Pretty representation of the instance."""
|
|
378
|
+
return (
|
|
379
|
+
f"{type(self).__name__}(path='{self.path}', "
|
|
380
|
+
f"files=set({sorted(self._data)}))"
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
def __str__(self) -> str:
|
|
384
|
+
"""String reprentation of the singleton files."""
|
|
385
|
+
return "\n".join(map(str, self.output()))
|
|
386
|
+
|
|
387
|
+
@property
|
|
388
|
+
def path(self) -> str:
|
|
389
|
+
"""Directory in which the contained files are located."""
|
|
390
|
+
return self._path
|
|
391
|
+
|
|
392
|
+
@path.setter
|
|
393
|
+
def path(self, val: object) -> None:
|
|
394
|
+
self._path = str(val or "")
|
|
395
|
+
|
|
396
|
+
def add(self, value: object) -> None:
|
|
397
|
+
"""Defining item addition logic (per standard set)."""
|
|
398
|
+
self._data.add(str(value))
|
|
399
|
+
|
|
400
|
+
def discard(self, value: object) -> None:
|
|
401
|
+
"""Defining item discard logic (per standard set)."""
|
|
402
|
+
value = str(value)
|
|
403
|
+
self._data.discard(value)
|
|
404
|
+
self._stat.pop(value, None)
|
|
405
|
+
|
|
406
|
+
def update(self, iterable: Iterable[object]) -> None:
|
|
407
|
+
"""Defining update logic (per standard set)."""
|
|
408
|
+
for item in iterable:
|
|
409
|
+
self.add(item)
|
|
410
|
+
|
|
411
|
+
def cache_stat(
|
|
412
|
+
self, base_name: str, input_stat: seqparse.StatLike
|
|
413
|
+
) -> os.stat_result:
|
|
414
|
+
"""
|
|
415
|
+
Cache file system stat data for the specified file base name.
|
|
416
|
+
|
|
417
|
+
Input disk stat value will be stored in a new stat_result
|
|
418
|
+
instance.
|
|
419
|
+
|
|
420
|
+
Args:
|
|
421
|
+
base_name (str): Base name of the file for which the supplied disk
|
|
422
|
+
stats are being cached.
|
|
423
|
+
input_stat (stat_result): Value that you'd like to cache.
|
|
424
|
+
|
|
425
|
+
Returns:
|
|
426
|
+
stat_result that was successfully cached.
|
|
427
|
+
"""
|
|
428
|
+
self._stat[base_name] = seqparse.get_stat_result(input_stat)
|
|
429
|
+
return self._stat[base_name]
|
|
430
|
+
|
|
431
|
+
def output(self) -> Iterator[files.File]:
|
|
432
|
+
"""
|
|
433
|
+
Calculate formatted list of all contained file sequences.
|
|
434
|
+
|
|
435
|
+
Yields:
|
|
436
|
+
File, sorted alphabetically.
|
|
437
|
+
"""
|
|
438
|
+
for file_name in sorted(self):
|
|
439
|
+
yield files.File(
|
|
440
|
+
os.path.join(self.path, file_name), self.stat(file_name)
|
|
441
|
+
)
|
|
442
|
+
|
|
443
|
+
@overload
|
|
444
|
+
def stat(self) -> dict[str, os.stat_result]: ...
|
|
445
|
+
@overload
|
|
446
|
+
def stat(self, base_name: str) -> os.stat_result | None: ...
|
|
447
|
+
def stat(
|
|
448
|
+
self, base_name: str | None = None
|
|
449
|
+
) -> dict[str, os.stat_result] | os.stat_result | None:
|
|
450
|
+
"""
|
|
451
|
+
Individual file system status, indexed by base name.
|
|
452
|
+
|
|
453
|
+
This method only returns cached disk stats (if any exist). Use the
|
|
454
|
+
`cache_stat` method if you'd like to set new values.
|
|
455
|
+
|
|
456
|
+
Args:
|
|
457
|
+
base_name (str, optional): Base name of the file for which you'd
|
|
458
|
+
like to return the disk stats.
|
|
459
|
+
|
|
460
|
+
Returns:
|
|
461
|
+
None if a file has been specified but disk stats have not been
|
|
462
|
+
cached.
|
|
463
|
+
stat_result if a file has been specified and disk stats have
|
|
464
|
+
been previously cached.
|
|
465
|
+
dict of disk stats, indexed by str base name if no name has been
|
|
466
|
+
specified.
|
|
467
|
+
"""
|
|
468
|
+
if base_name is None:
|
|
469
|
+
return self._stat
|
|
470
|
+
return self._stat.get(base_name, None)
|
seqparse/files.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""Singleton file-related data structures utilized by the Seqparse module."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import functools
|
|
6
|
+
import os
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import seqparse
|
|
10
|
+
|
|
11
|
+
__all__ = ("File",)
|
|
12
|
+
|
|
13
|
+
###############################################################################
|
|
14
|
+
# Class: File
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@functools.total_ordering
|
|
18
|
+
class File:
|
|
19
|
+
"""
|
|
20
|
+
Simple representation of files on disk.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
file_name (str): Full path to the input file.
|
|
24
|
+
stat (stat_result, optional): Disk stats you'd like to cache for the
|
|
25
|
+
specified file.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
# __dict__ and __weakref__ keep custom attributes and weak references
|
|
29
|
+
# working (the dict is only created if used).
|
|
30
|
+
__slots__ = ("__dict__", "__weakref__", "_full", "_name", "_path", "_stat")
|
|
31
|
+
|
|
32
|
+
_full: str
|
|
33
|
+
_name: str
|
|
34
|
+
_path: str
|
|
35
|
+
_stat: os.stat_result | None
|
|
36
|
+
|
|
37
|
+
def __init__(
|
|
38
|
+
self, file_name: str, stat: seqparse.StatLike | None = None
|
|
39
|
+
) -> None:
|
|
40
|
+
"""Initialise the instance."""
|
|
41
|
+
self._stat = None
|
|
42
|
+
self._cache_stat(stat)
|
|
43
|
+
self._set_name(file_name)
|
|
44
|
+
|
|
45
|
+
def __setstate__(self, state: Any) -> None:
|
|
46
|
+
"""Unpickle, including pickles made by seqparse 1.0.x."""
|
|
47
|
+
if isinstance(state, tuple):
|
|
48
|
+
seqparse.restore_slots(self, state)
|
|
49
|
+
return
|
|
50
|
+
info = state["_info"] # 1.0.x kept its state in plain dicts
|
|
51
|
+
self._full, self._name, self._path = (
|
|
52
|
+
info["full"],
|
|
53
|
+
info["name"],
|
|
54
|
+
info["path"],
|
|
55
|
+
)
|
|
56
|
+
self._stat = state["_stat"]
|
|
57
|
+
|
|
58
|
+
def __eq__(self, other: object) -> bool:
|
|
59
|
+
"""Define equality between instances."""
|
|
60
|
+
if type(other) is type(self):
|
|
61
|
+
assert isinstance(other, File)
|
|
62
|
+
return self.full_name == other.full_name
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
def __hash__(self) -> int:
|
|
66
|
+
"""Hash consistently with __eq__ (the name is fixed at creation)."""
|
|
67
|
+
return hash((type(self), self.full_name))
|
|
68
|
+
|
|
69
|
+
def __lt__(self, other: object) -> bool:
|
|
70
|
+
"""Define equality between instances."""
|
|
71
|
+
if type(other) is type(self):
|
|
72
|
+
assert isinstance(other, File)
|
|
73
|
+
return self.full_name < other.full_name
|
|
74
|
+
return True
|
|
75
|
+
|
|
76
|
+
def __repr__(self) -> str: # pragma: no cover
|
|
77
|
+
"""Pretty representation of the instance."""
|
|
78
|
+
return f"{type(self).__name__}({self._full!r})"
|
|
79
|
+
|
|
80
|
+
def __str__(self) -> str:
|
|
81
|
+
"""String representation of a File instance."""
|
|
82
|
+
return str(self.full_name)
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def full_name(self) -> str:
|
|
86
|
+
"""str: Full name of the sequence, including containing directory."""
|
|
87
|
+
return self._full
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def mtime(self) -> float | None:
|
|
91
|
+
"""
|
|
92
|
+
int: Modification time of the file.
|
|
93
|
+
|
|
94
|
+
Returns None if the files have not been stat'd on disk.
|
|
95
|
+
"""
|
|
96
|
+
if not self._stat:
|
|
97
|
+
return None
|
|
98
|
+
return self._stat.st_mtime
|
|
99
|
+
|
|
100
|
+
@property
|
|
101
|
+
def name(self) -> str:
|
|
102
|
+
"""str: Base name of the file sequence (no containing directory)."""
|
|
103
|
+
return self._name
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def path(self) -> str:
|
|
107
|
+
"""str: Directory in which the contained files are located."""
|
|
108
|
+
return self._path
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def size(self) -> int | None:
|
|
112
|
+
"""
|
|
113
|
+
int: Size of the file in bytes.
|
|
114
|
+
|
|
115
|
+
Returns None if the files have not been stat'd on disk.
|
|
116
|
+
"""
|
|
117
|
+
if not self._stat:
|
|
118
|
+
return None
|
|
119
|
+
return self._stat.st_size
|
|
120
|
+
|
|
121
|
+
def _cache_stat(
|
|
122
|
+
self, input_stat: seqparse.StatLike | None
|
|
123
|
+
) -> os.stat_result | None:
|
|
124
|
+
"""
|
|
125
|
+
Cache file system stat data.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
input_stat (stat_result): Value that you'd like to cache.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
stat_result that was successfully cached.
|
|
132
|
+
"""
|
|
133
|
+
self._stat = None
|
|
134
|
+
if input_stat:
|
|
135
|
+
self._stat = seqparse.get_stat_result(input_stat)
|
|
136
|
+
return self._stat
|
|
137
|
+
|
|
138
|
+
def _set_name(self, full_name: str) -> None:
|
|
139
|
+
"""
|
|
140
|
+
Set all name-related fields on the instance.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
full_name (str): Full path to the contained file.
|
|
144
|
+
"""
|
|
145
|
+
self._path, self._name = os.path.split(full_name)
|
|
146
|
+
self._full = full_name
|
|
147
|
+
|
|
148
|
+
def stat(
|
|
149
|
+
self, force: bool = False, lazy: bool = False
|
|
150
|
+
) -> os.stat_result | None:
|
|
151
|
+
"""
|
|
152
|
+
File system status.
|
|
153
|
+
|
|
154
|
+
Args:
|
|
155
|
+
force (bool, optional): Whether to force disk stat query,
|
|
156
|
+
regardless of caching status.
|
|
157
|
+
lazy (bool, optional): Whether to query disk stats should no cached
|
|
158
|
+
value exist.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
None if a frame has been specified but disk stats have not been
|
|
162
|
+
cached.
|
|
163
|
+
stat_result if a frame has been specified and disk stats have
|
|
164
|
+
been previously cached.
|
|
165
|
+
"""
|
|
166
|
+
if force or (lazy and self._stat is None):
|
|
167
|
+
self._cache_stat(os.stat(self.full_name))
|
|
168
|
+
return self._stat
|
seqparse/py.typed
ADDED
|
File without changes
|