From 55dbe8193e16dc628e7010ce415ce6729609c091 Mon Sep 17 00:00:00 2001 From: Naglis Jonaitis Date: Sun, 22 Dec 2024 10:07:11 +0200 Subject: [PATCH] Revise SMIL parsing/serialization --- aeneas/globalfunctions.py | 7 ++ aeneas/syncmap/smfsmil.py | 136 ++++++++++++++++++++++---------------- 2 files changed, 87 insertions(+), 56 deletions(-) diff --git a/aeneas/globalfunctions.py b/aeneas/globalfunctions.py index 2be0a97..7f62298 100644 --- a/aeneas/globalfunctions.py +++ b/aeneas/globalfunctions.py @@ -1120,3 +1120,10 @@ def bundle_directory(): if FROZEN: return sys._MEIPASS return None + + +def with_ns(tag: str, ns: str) -> str: + """ + Return tag nam with namespace applied. + """ + return f"{{{ns}}}{tag}" diff --git a/aeneas/syncmap/smfsmil.py b/aeneas/syncmap/smfsmil.py index c9bc50c..adb5c7f 100644 --- a/aeneas/syncmap/smfsmil.py +++ b/aeneas/syncmap/smfsmil.py @@ -20,13 +20,22 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import functools + import lxml.etree as ET +from aeneas.exacttiming import TimeValue from aeneas.syncmap.missingparametererror import SyncMapMissingParameterError from aeneas.syncmap.smfgxml import SyncMapFormatGenericXML import aeneas.globalconstants as gc import aeneas.globalfunctions as gf +SMIL_NS = "http://www.w3.org/ns/SMIL" +EPUB_NS = "http://www.idpf.org/2007/ops" + +with_smil_ns = functools.partial(gf.with_ns, ns=SMIL_NS) +with_epub_ns = functools.partial(gf.with_ns, ns=EPUB_NS) + class SyncMapFormatSMIL(SyncMapFormatGenericXML): """ @@ -52,7 +61,14 @@ def __init__(self, variant=DEFAULT, parameters=None, rconf=None, logger=None): else: self.format_time_function = gf.time_to_hhmmssmmm - def parse(self, input_text, syncmap): + @staticmethod + def _autodetect_parse_duration(value: str) -> TimeValue: + if ":" in value: + return gf.time_from_hhmmssmmm(value) + else: + return gf.time_from_ssmmm(value) + + def parse(self, input_text: str, syncmap): """ Read from SMIL file. @@ -61,29 +77,20 @@ def parse(self, input_text, syncmap): 2. timings must have ``hh:mm:ss.mmm`` or ``ss.mmm`` format (autodetected) 3. both ``clipBegin`` and ``clipEnd`` attributes of ``