Ruff fixes in progress

This commit is contained in:
2026-09-17 20:06:46 -07:00
parent f7808e2236
commit 9e9ad75f77
4 changed files with 113 additions and 118 deletions
+67 -68
View File
@@ -3,17 +3,52 @@ This module defines classes and methods for converting :class:`Event` objects
into :class:`ADRLine` objects. into :class:`ADRLine` objects.
""" """
from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from fractions import Fraction from fractions import Fraction
from typing import List, Optional, Tuple
from ptulsconv.docparser.tag_compiler import Event from ptulsconv.docparser.tag_compiler import Event
from ptulsconv.docparser.tag_mapping import TagMapping from ptulsconv.docparser.tag_mapping import TagMapping
GENERIC_TAG_MAPPING = [
TagMapping(source="Title", target="title", alt=TagMapping.ContentSource.Session),
TagMapping(source="Supv", target="supervisor"),
TagMapping(source="Client", target="client"),
TagMapping(source="Sc", target="scene"),
TagMapping(source="Ver", target="version"),
TagMapping(source="Reel", target="reel"),
TagMapping(source="Note", target="note"),
TagMapping(source="Rq", target="requested_by"),
TagMapping(source="OMIT", target="omitted", formatter=(lambda x: len(x) > 0)),
]
ADR_TAG_MAPPING = [
TagMapping(source="P", target="priority"),
TagMapping(source="QN", target="cue_number"),
TagMapping(source="CN", target="character_id"),
TagMapping(
source="Char", target="character_name", alt=TagMapping.ContentSource.Track
),
TagMapping(source="Actor", target="actor_name"),
TagMapping(source="Line", target="prompt", alt=TagMapping.ContentSource.Clip),
TagMapping(source="R", target="reason"),
TagMapping(
source="Mins", target="time_budget_mins", formatter=(lambda n: float(n))
),
TagMapping(source="Spot", target="spot"),
TagMapping(source="Shot", target="shot"),
TagMapping(source="EFF", target="effort", formatter=(lambda x: len(x) > 0)),
TagMapping(source="TV", target="tv", formatter=(lambda x: len(x) > 0)),
TagMapping(source="TBW", target="tbw", formatter=(lambda x: len(x) > 0)),
TagMapping(source="ADLIB", target="adlib", formatter=(lambda x: len(x) > 0)),
TagMapping(source="OPT", target="optional", formatter=(lambda x: len(x) > 0)),
]
def make_entities( def make_entities(
from_events: List[Event], from_events: list[Event],
) -> Tuple[List["GenericEvent"], List["ADRLine"]]: ) -> tuple[list[GenericEvent], list[ADRLine]]:
""" """
Accepts a list of Events and converts them into either ADRLine events or Accepts a list of Events and converts them into either ADRLine events or
GenricEvents by calling :func:`make_entity` on each member. GenricEvents by calling :func:`make_entity` on each member.
@@ -23,8 +58,8 @@ def make_entities(
:returns: A tuple of two lists, the first containing :class:`GenericEvent` :returns: A tuple of two lists, the first containing :class:`GenericEvent`
and the second containing :class:`ADRLine`. and the second containing :class:`ADRLine`.
""" """
generic_events = list() generic_events = []
adr_lines = list() adr_lines = []
for event in from_events: for event in from_events:
result = make_entity(event) result = make_entity(event)
@@ -36,7 +71,7 @@ def make_entities(
return generic_events, adr_lines return generic_events, adr_lines
def make_entity(from_event: Event) -> Optional[object]: def make_entity(from_event: Event) -> object | None:
""" """
Accepts an event and creates either an :class:`ADRLine` or a Accepts an event and creates either an :class:`ADRLine` or a
:class:`GenericEvent`. An event is an "ADRLine" if it has a cue number/"QN" :class:`GenericEvent`. An event is an "ADRLine" if it has a cue number/"QN"
@@ -46,19 +81,19 @@ def make_entity(from_event: Event) -> Optional[object]:
""" """
instance = GenericEvent instance = GenericEvent
tag_map = GenericEvent.tag_mapping tag_map = GENERIC_TAG_MAPPING
if "QN" in from_event.tags.keys(): if "QN" in from_event.tags:
instance = ADRLine instance = ADRLine
tag_map += ADRLine.tag_mapping tag_map += ADR_TAG_MAPPING
new = instance() new = instance()
TagMapping.apply_rules( TagMapping.apply_rules(
tag_map, rules=tag_map,
from_event.tags, tags=from_event.tags,
from_event.clip_name, clip_content=from_event.clip_name,
from_event.track_name, track_content=from_event.track_name,
from_event.session_name, session_content=from_event.session_name,
new, to=new,
) )
new.start = from_event.start new.start = from_event.start
@@ -69,68 +104,32 @@ def make_entity(from_event: Event) -> Optional[object]:
@dataclass @dataclass
class GenericEvent: class GenericEvent:
title: str = "" title: str = ""
supervisor: Optional[str] = None supervisor: str | None = None
client: Optional[str] = None client: str | None = None
scene: Optional[str] = None scene: str | None = None
version: Optional[str] = None version: str | None = None
reel: Optional[str] = None reel: str | None = None
start: Fraction = Fraction(0, 1) start: Fraction = Fraction(0, 1)
finish: Fraction = Fraction(0, 1) finish: Fraction = Fraction(0, 1)
omitted: bool = False omitted: bool = False
note: Optional[str] = None note: str | None = None
requested_by: Optional[str] = None requested_by: str | None = None
tag_mapping = [
TagMapping(
source="Title", target="title", alt=TagMapping.ContentSource.Session
),
TagMapping(source="Supv", target="supervisor"),
TagMapping(source="Client", target="client"),
TagMapping(source="Sc", target="scene"),
TagMapping(source="Ver", target="version"),
TagMapping(source="Reel", target="reel"),
TagMapping(source="Note", target="note"),
TagMapping(source="Rq", target="requested_by"),
TagMapping(source="OMIT", target="omitted", formatter=(lambda x: len(x) > 0)),
]
@dataclass @dataclass
class ADRLine(GenericEvent): class ADRLine(GenericEvent):
priority: Optional[int] = None priority: int | None = None
cue_number: Optional[str] = None cue_number: str | None = None
character_id: Optional[str] = None character_id: str | None = None
character_name: Optional[str] = None character_name: str | None = None
actor_name: Optional[str] = None actor_name: str | None = None
prompt: Optional[str] = None prompt: str | None = None
reason: Optional[str] = None reason: str | None = None
time_budget_mins: Optional[float] = None time_budget_mins: float | None = None
spot: Optional[str] = None spot: str | None = None
shot: Optional[str] = None shot: str | None = None
effort: bool = False effort: bool = False
tv: bool = False tv: bool = False
tbw: bool = False tbw: bool = False
adlib: bool = False adlib: bool = False
optional: bool = False optional: bool = False
tag_mapping = [
TagMapping(source="P", target="priority"),
TagMapping(source="QN", target="cue_number"),
TagMapping(source="CN", target="character_id"),
TagMapping(
source="Char", target="character_name", alt=TagMapping.ContentSource.Track
),
TagMapping(source="Actor", target="actor_name"),
TagMapping(source="Line", target="prompt", alt=TagMapping.ContentSource.Clip),
TagMapping(source="R", target="reason"),
TagMapping(
source="Mins", target="time_budget_mins", formatter=(lambda n: float(n))
),
TagMapping(source="Spot", target="spot"),
TagMapping(source="Shot", target="shot"),
TagMapping(source="EFF", target="effort", formatter=(lambda x: len(x) > 0)),
TagMapping(source="TV", target="tv", formatter=(lambda x: len(x) > 0)),
TagMapping(source="TBW", target="tbw", formatter=(lambda x: len(x) > 0)),
TagMapping(source="ADLIB", target="adlib", formatter=(lambda x: len(x) > 0)),
TagMapping(source="OPT", target="optional", formatter=(lambda x: len(x) > 0)),
]
+18 -17
View File
@@ -1,17 +1,18 @@
from __future__ import annotations
from collections.abc import Iterator from collections.abc import Iterator
from fractions import Fraction from fractions import Fraction
from typing import List, Tuple
from ptulsconv.broadcast_timecode import TimecodeFormat from ptulsconv.broadcast_timecode import TimecodeFormat
class SessionDescriptor: class SessionDescriptor:
header: "HeaderDescriptor" header: HeaderDescriptor
files: List["FileDescriptor"] files: list[FileDescriptor]
clips: List["ClipDescriptor"] clips: list[ClipDescriptor]
plugins: List["PluginDescriptor"] plugins: list[PluginDescriptor]
tracks: List["TrackDescriptor"] tracks: list[TrackDescriptor]
markers: List["MarkerDescriptor"] markers: list[MarkerDescriptor]
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.header = kwargs["header"] self.header = kwargs["header"]
@@ -23,7 +24,7 @@ class SessionDescriptor:
def markers_timed( def markers_timed(
self, only_ruler_markers: bool = True self, only_ruler_markers: bool = True
) -> Iterator[Tuple["MarkerDescriptor", Fraction]]: ) -> Iterator[tuple[MarkerDescriptor, Fraction]]:
""" """
Iterate each marker in the session with its respective time reference. Iterate each marker in the session with its respective time reference.
""" """
@@ -35,7 +36,7 @@ class SessionDescriptor:
# marker_time = self.header.convert_timecode(marker.location) # marker_time = self.header.convert_timecode(marker.location)
yield marker, marker_time yield marker, marker_time
def tracks_clips(self) -> Iterator[Tuple["TrackDescriptor", "TrackClipDescriptor"]]: def tracks_clips(self) -> Iterator[tuple[TrackDescriptor, TrackClipDescriptor]]:
""" """
Iterate each track clip with its respective owning clip. Iterate each track clip with its respective owning clip.
""" """
@@ -46,7 +47,7 @@ class SessionDescriptor:
def track_clips_timed( def track_clips_timed(
self, self,
) -> Iterator[ ) -> Iterator[
Tuple["TrackDescriptor", "TrackClipDescriptor", Fraction, Fraction, Fraction] tuple[TrackDescriptor, TrackClipDescriptor, Fraction, Fraction, Fraction | None]
]: ]:
""" """
Iterate each track clip with its respective owning clip and timing Iterate each track clip with its respective owning clip and timing
@@ -97,11 +98,11 @@ class HeaderDescriptor:
drop_frame=self.timecode_drop_frame, drop_frame=self.timecode_drop_frame,
) )
def convert_timecode(self, tc_string: str) -> Fraction: def convert_timecode(self, tc_string: str) -> Fraction | None:
return self.timecode_format.smpte_to_seconds(tc_string) return self.timecode_format.smpte_to_seconds(tc_string)
@property @property
def start_time(self) -> Fraction: def start_time(self) -> Fraction | None:
""" """
The start time of this session. The start time of this session.
:return: Start time in seconds :return: Start time in seconds
@@ -117,7 +118,7 @@ class HeaderDescriptor:
return self._get_tc_format_params[1] return self._get_tc_format_params[1]
@property @property
def _get_tc_format_params(self) -> Tuple[int, Fraction]: def _get_tc_format_params(self) -> tuple[int, Fraction]:
frame_rates = { frame_rates = {
"23.976": (24, Fraction(1001, 24_000)), "23.976": (24, Fraction(1001, 24_000)),
"24": (24, Fraction(1, 24)), "24": (24, Fraction(1, 24)),
@@ -131,7 +132,7 @@ class HeaderDescriptor:
if self.timecode_fps in frame_rates: if self.timecode_fps in frame_rates:
return frame_rates[self.timecode_fps] return frame_rates[self.timecode_fps]
else: else:
raise ValueError("Unrecognized TC rate (%s)" % self.timecode_format) raise ValueError(f"Unrecognized TC rate ({self.timecode_format})")
class TrackDescriptor: class TrackDescriptor:
@@ -139,9 +140,9 @@ class TrackDescriptor:
name: str name: str
comments: str comments: str
user_delay_samples: int user_delay_samples: int
state: List[str] state: list[str]
plugins: List[str] plugins: list[str]
clips: List["TrackClipDescriptor"] clips: list[TrackClipDescriptor]
def __init__(self, **kwargs): def __init__(self, **kwargs):
self.index = kwargs["index"] self.index = kwargs["index"]
+20 -27
View File
@@ -173,41 +173,33 @@ class DocParserVisitor(NodeVisitor):
@staticmethod @staticmethod
def visit_files_section(_, visited_children): def visit_files_section(_, visited_children):
return list( return [
map( FileDescriptor(filemname=child[0], path=child[2])
lambda child: FileDescriptor(filename=child[0], path=child[2]), for child in visited_children[2]
visited_children[2], ]
)
)
@staticmethod @staticmethod
def visit_clips_section(_, visited_children): def visit_clips_section(_, visited_children):
channel = next(iter(visited_children[2][3]), 1) channel = next(iter(visited_children[2][3]), 1)
return [
return list( ClipDescriptor(clip_name=child[0], file=child[2], channel=channel)
map( for child in visited_children[2]
lambda child: ClipDescriptor( ]
clip_name=child[0], file=child[2], channel=channel
),
visited_children[2],
)
)
@staticmethod @staticmethod
def visit_plugin_listing(_, visited_children): def visit_plugin_listing(_, visited_children):
return list(
map( return [
lambda child: PluginDescriptor( PluginDescriptor(
manufacturer=child[0], manufacturer=child[0],
plugin_name=child[2], plugin_name=child[2],
version=child[4], version=child[4],
format=child[6], format=child[6],
stems=child[8], stems=child[8],
count_instances=child[10], count_instances=child[10],
),
visited_children[2],
) )
) for child in visited_children[2]
]
# @staticmethod # @staticmethod
def visit_track_block(self, _, visited_children): def visit_track_block(self, _, visited_children):
@@ -276,6 +268,7 @@ class DocParserVisitor(NodeVisitor):
markers = [] markers = []
for marker in visited_children[1][0][1]: for marker in visited_children[1][0][1]:
marker: MarkerDescriptor
markers.append(marker) markers.append(marker)
return markers return markers
+8 -6
View File
@@ -1,6 +1,8 @@
from __future__ import annotations
import sys import sys
from enum import Enum from enum import Enum
from typing import Any, Callable, List, Optional from typing import Any, Callable
class TagMapping: class TagMapping:
@@ -10,14 +12,14 @@ class TagMapping:
Clip = (3,) Clip = (3,)
source: str source: str
alternate_source: Optional[ContentSource] alternate_source: ContentSource | None
formatter: Callable[[str], Any] formatter: Callable[[str], Any]
@staticmethod @staticmethod
def print_rules(for_type: object, output=sys.stdout): def print_rules(for_type: object, output=sys.stdout):
format_str = "%-20s | %-20s | %-25s" format_str = "%-20s | %-20s | %-25s"
hr = "%s+%s+%s" % ("-" * 21, "-" * 23, "-" * 26) hr = f"{'-' * 21}+{'-' * 23}+{'-' * 26}"
print("Tag mapping for %s" % for_type.__name__) print(f"Tag mapping for {for_type.__class__}")
print(hr) print(hr)
print(format_str % ("Tag Source", "Target", "Type"), file=output) print(format_str % ("Tag Source", "Target", "Type"), file=output)
print(hr) print(hr)
@@ -33,7 +35,7 @@ class TagMapping:
@staticmethod @staticmethod
def apply_rules( def apply_rules(
rules: List["TagMapping"], rules: list[TagMapping],
tags: dict, tags: dict,
clip_content: str, clip_content: str,
track_content: str, track_content: str,
@@ -52,7 +54,7 @@ class TagMapping:
self, self,
source: str, source: str,
target: str, target: str,
alt: Optional[ContentSource] = None, alt: ContentSource | None = None,
formatter=None, formatter=None,
): ):
self.source = source self.source = source