Skip to content

Commit b6aeedd

Browse files
tikuma-lsuhschokiedsp
authored andcommitted
moved escape(), unescape() to _utils.py
1 parent 4f39251 commit b6aeedd

3 files changed

Lines changed: 71 additions & 75 deletions

File tree

src/ffmpegio/_utils.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,73 @@ def is_fileobj(
168168
raise ValueError("Requested non-writable file object but it is writable.")
169169

170170
return True
171+
172+
def escape(txt: str) -> str:
173+
"""apply FFmpeg single quote escaping
174+
175+
:param txt: Unescaped string
176+
:return: Escaped string
177+
178+
See https://ffmpeg.org/ffmpeg-utils.html#Quoting-and-escaping
179+
"""
180+
181+
txt = str(txt)
182+
183+
if re.search(r"\s", txt, re.MULTILINE):
184+
# quote if txt has any white space
185+
txt = txt.replace("'", r"'\''")
186+
return f"'{txt}'"
187+
else:
188+
# if not quoted, escape quotes and backslashes
189+
return re.sub(r"(['\\])", r"\\\1", txt)
190+
191+
192+
def unescape(txt: str) -> str:
193+
"""undo FFmpeg single quote escaping
194+
195+
:param txt: Escaped string
196+
:return: Original string
197+
198+
See https://ffmpeg.org/ffmpeg-utils.html#Quoting-and-escaping
199+
"""
200+
201+
n = len(txt)
202+
if not n:
203+
return txt
204+
205+
re_start = re.compile(r"[^\\](?:\\\\)*'")
206+
re_sub = re.compile(r"\\([\\'])")
207+
208+
blks = []
209+
210+
# look for a first quoted text block
211+
m = re.search(r"(?:^|[^\\])(?:\\\\)*'", txt)
212+
if m:
213+
i0 = m.end()
214+
if i0 > 1:
215+
# unescape the initial unquoted block
216+
blks.append(re_sub.sub(r"\1", txt[0 : i0 - 1]))
217+
else:
218+
# no quoted text block, unescape the whole string
219+
return re_sub.sub(r"\1", txt)
220+
221+
# always starts with quoted block
222+
in_quote = True
223+
224+
while i0 < n:
225+
226+
if in_quote:
227+
# find the end quote
228+
i1 = txt.find("'", i0)
229+
if i1 < 0:
230+
raise ValueError("incorrectly escaped text: missing a closing quote.")
231+
blks.append(txt[i0:i1])
232+
else:
233+
# find the next starting quote
234+
m = re_start.search(txt, i0 - 1)
235+
i1 = m.end() - 1 if m else n
236+
blks.append(re_sub.sub(r"\1", txt[i0:i1]))
237+
i0 = i1 + 1
238+
in_quote = not in_quote
239+
240+
return "".join(blks)

src/ffmpegio/utils/__init__.py

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,80 +18,6 @@
1818
# import sys
1919
# sys.byteorder
2020

21-
22-
23-
24-
def escape(txt: str) -> str:
25-
"""apply FFmpeg single quote escaping
26-
27-
:param txt: Unescaped string
28-
:return: Escaped string
29-
30-
See https://ffmpeg.org/ffmpeg-utils.html#Quoting-and-escaping
31-
"""
32-
33-
txt = str(txt)
34-
35-
if re.search(r"\s", txt, re.MULTILINE):
36-
# quote if txt has any white space
37-
txt = txt.replace("'", r"'\''")
38-
return f"'{txt}'"
39-
else:
40-
# if not quoted, escape quotes and backslashes
41-
return re.sub(r"(['\\])", r"\\\1", txt)
42-
43-
44-
def unescape(txt: str) -> str:
45-
"""undo FFmpeg single quote escaping
46-
47-
:param txt: Escaped string
48-
:return: Original string
49-
50-
See https://ffmpeg.org/ffmpeg-utils.html#Quoting-and-escaping
51-
"""
52-
53-
n = len(txt)
54-
if not n:
55-
return txt
56-
57-
re_start = re.compile(r"[^\\](?:\\\\)*'")
58-
re_sub = re.compile(r"\\([\\'])")
59-
60-
blks = []
61-
62-
# look for a first quoted text block
63-
m = re.search(r"(?:^|[^\\])(?:\\\\)*'", txt)
64-
if m:
65-
i0 = m.end()
66-
if i0 > 1:
67-
# unescape the initial unquoted block
68-
blks.append(re_sub.sub(r"\1", txt[0 : i0 - 1]))
69-
else:
70-
# no quoted text block, unescape the whole string
71-
return re_sub.sub(r"\1", txt)
72-
73-
# always starts with quoted block
74-
in_quote = True
75-
76-
while i0 < n:
77-
78-
if in_quote:
79-
# find the end quote
80-
i1 = txt.find("'", i0)
81-
if i1 < 0:
82-
raise ValueError("incorrectly escaped text: missing a closing quote.")
83-
blks.append(txt[i0:i1])
84-
else:
85-
# find the next starting quote
86-
m = re_start.search(txt, i0 - 1)
87-
i1 = m.end() - 1 if m else n
88-
blks.append(re_sub.sub(r"\1", txt[i0:i1]))
89-
i0 = i1 + 1
90-
in_quote = not in_quote
91-
92-
return "".join(blks)
93-
94-
9521
def get_pixel_config(
9622
input_pix_fmt: str, pix_fmt: str | None = None
9723
) -> tuple[str, int, str, bool]:

src/ffmpegio/utils/concat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
logger = logging.getLogger("ffmpegio")
1212

13-
from . import escape, unescape
13+
from .._utils import escape, unescape
1414

1515
# https://trac.ffmpeg.org/wiki/Concatenate
1616
# https://ffmpeg.org/ffmpeg-formats.html#concat

0 commit comments

Comments
 (0)