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/regex.py ADDED
@@ -0,0 +1,191 @@
1
+ """Container for all regular expressions used by the seqparse module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from typing import ClassVar, Literal, NamedTuple, overload
7
+
8
+ __all__ = (
9
+ "BITS_EXPR",
10
+ "FILE_NAME_EXPR",
11
+ "FILE_SEQ_EXPR",
12
+ "FRAME_EXPR",
13
+ "ChunkBits",
14
+ "FileNameBits",
15
+ "GroupDict",
16
+ "SeqparseRegexMixin",
17
+ "SequenceBits",
18
+ )
19
+
20
+ # BITS_EXPR is used to split a frame "chunk" into three sections: first
21
+ # (frame), last (frame). and step.
22
+ BITS_EXPR = r"(?P<first>\d+)(?:-(?P<last>\d+)(?:x(?P<step>\d+)?)?)?$"
23
+
24
+ # FILE_NAME_EXPR is used to split a file name into three sections: base (name),
25
+ # frame (chunk), and file ext(ension).
26
+ FILE_NAME_EXPR = r"(?:^(?P<name>.+)\.|^)(?P<frame>\d+)\.(?P<ext>[^\.]+)$"
27
+
28
+ # FRAME_EXPR is used to validate a "legal" sequence of frames.
29
+ FRAME_EXPR = r"(?:\d+(?:-\d+(?:x\d+)?)?(?:,+\d+(?:-\d+(?:x\d+)?)?)*)"
30
+
31
+ # FILE_SEQ_EXPR is used to validate and split a "legal" sequence of files into
32
+ # three sections: base (name), frame (sequence), and file ext(ension).
33
+ FILE_SEQ_EXPR = (
34
+ rf"(?:^(?P<name>.+)\.|^)(?P<frames>{FRAME_EXPR})\.(?P<ext>[^\.]+)$"
35
+ )
36
+
37
+ # The same grammar with possessive quantifiers, for matching: it never needs
38
+ # to backtrack (digits are always followed by "-", "x", "," or what follows
39
+ # the frames), and without them the regex engine keeps a backtracking state
40
+ # per chunk (~23 MB for a 50k-chunk sequence).
41
+ _FRAME_POSSESSIVE = (
42
+ r"\d++(?:-\d++(?:x\d++)?+)?+(?:,++\d++(?:-\d++(?:x\d++)?+)?+)*+"
43
+ )
44
+
45
+ GroupDict = dict[str, str | None]
46
+
47
+
48
+ class ChunkBits(NamedTuple):
49
+ """The parts of a frame chunk: "first[-last[xstep]]"."""
50
+
51
+ first: str
52
+ last: str | None
53
+ step: str | None
54
+
55
+
56
+ class FileNameBits(NamedTuple):
57
+ """The parts of a single-frame file name: "[name.]frame.ext"."""
58
+
59
+ name: str | None
60
+ frame: str
61
+ ext: str
62
+
63
+
64
+ class SequenceBits(NamedTuple):
65
+ """The parts of a file sequence string: "[name.]frames.ext"."""
66
+
67
+ name: str | None
68
+ frames: str
69
+ ext: str
70
+
71
+
72
+ ###############################################################################
73
+ # Class: SeqparseRegexMixin
74
+ class SeqparseRegexMixin:
75
+ """Base for classes that need to perform regular expression matches."""
76
+
77
+ __slots__ = ()
78
+
79
+ _bits_expr: ClassVar[re.Pattern[str]] = re.compile(BITS_EXPR)
80
+ _file_expr: ClassVar[re.Pattern[str]] = re.compile(FILE_NAME_EXPR)
81
+ _frame_expr: ClassVar[re.Pattern[str]] = re.compile(
82
+ rf",*+{_FRAME_POSSESSIVE},*+$"
83
+ )
84
+ _fseq_expr: ClassVar[re.Pattern[str]] = re.compile(
85
+ FILE_SEQ_EXPR.replace(FRAME_EXPR, _FRAME_POSSESSIVE)
86
+ )
87
+
88
+ @overload
89
+ def bits_match(
90
+ self, val: str, as_dict: Literal[False] = False
91
+ ) -> ChunkBits | None: ...
92
+ @overload
93
+ def bits_match(
94
+ self, val: str, as_dict: Literal[True]
95
+ ) -> GroupDict | None: ...
96
+ def bits_match(
97
+ self, val: str, as_dict: bool = False
98
+ ) -> ChunkBits | GroupDict | None:
99
+ """
100
+ Calculate first, last, step for valid string frame chunks.
101
+
102
+ Args:
103
+ val (str): Input chunk of a frame range.
104
+ as_dict (bool, optional): Whether to output return values as a
105
+ dict of regex groups. Defaults to False.
106
+
107
+ Returns:
108
+ None if input is an invalid chunk,
109
+ tuple consisting of (first frame, last frame, frame step) with
110
+ as_dict = False, or
111
+ dict of regex groups with as_dict = True.
112
+ """
113
+ bmatch = self._bits_expr.match(val)
114
+ if not bmatch:
115
+ return None
116
+ groups = bmatch.groupdict()
117
+ return groups if as_dict else ChunkBits(**groups)
118
+
119
+ @overload
120
+ def file_name_match(
121
+ self, val: str, as_dict: Literal[False] = False
122
+ ) -> FileNameBits | None: ...
123
+ @overload
124
+ def file_name_match(
125
+ self, val: str, as_dict: Literal[True]
126
+ ) -> GroupDict | None: ...
127
+ def file_name_match(
128
+ self, val: str, as_dict: bool = False
129
+ ) -> FileNameBits | GroupDict | None:
130
+ """
131
+ Calculate base name, frame, extension for valid string file name.
132
+
133
+ Args:
134
+ val (str): Input file name.
135
+ as_dict (bool, optional): Whether to output return values as a
136
+ dict of regex groups. Defaults to False.
137
+
138
+ Returns:
139
+ None if input is an invalid sequence file name,
140
+ tuple consisting of (base name, frame, extension) with
141
+ as_dict = False, or
142
+ dict of regex groups with as_dict = True.
143
+ """
144
+ fmatch = self._file_expr.match(val)
145
+ if not fmatch:
146
+ return None
147
+ groups = fmatch.groupdict()
148
+ return groups if as_dict else FileNameBits(**groups)
149
+
150
+ @overload
151
+ def file_seq_match(
152
+ self, val: str, as_dict: Literal[False] = False
153
+ ) -> SequenceBits | None: ...
154
+ @overload
155
+ def file_seq_match(
156
+ self, val: str, as_dict: Literal[True]
157
+ ) -> GroupDict | None: ...
158
+ def file_seq_match(
159
+ self, val: str, as_dict: bool = False
160
+ ) -> SequenceBits | GroupDict | None:
161
+ """
162
+ Calculate base name, sequence, extension for valid file sequence.
163
+
164
+ Args:
165
+ val (str): Input file sequence.
166
+ as_dict (bool, optional): Whether to output return values as a
167
+ dict of regex groups. Defaults to False.
168
+
169
+ Returns:
170
+ None if input is an invalid sequence file sequence,
171
+ tuple consisting of (base name, frame sequence, extension) with
172
+ as_dict = False, or
173
+ dict of regex groups with as_dict = True.
174
+ """
175
+ fmatch = self._fseq_expr.match(val)
176
+ if not fmatch:
177
+ return None
178
+ groups = fmatch.groupdict()
179
+ return groups if as_dict else SequenceBits(**groups)
180
+
181
+ def is_frame_sequence(self, val: str) -> bool:
182
+ """
183
+ Whether a string frame sequence is valid.
184
+
185
+ Args:
186
+ val (str): Input frame sequence.
187
+
188
+ Returns:
189
+ True if frame sequence is valid, False if it is not.
190
+ """
191
+ return bool(self._frame_expr.match(val))