mirror of
https://github.com/iluvcapra/ptulsconv.git
synced 2026-10-02 01:37:17 +00:00
Ruff fixes in progress
This commit is contained in:
@@ -3,87 +3,16 @@ 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 = [
|
||||||
def make_entities(
|
TagMapping(source="Title", target="title", alt=TagMapping.ContentSource.Session),
|
||||||
from_events: List[Event],
|
|
||||||
) -> Tuple[List["GenericEvent"], List["ADRLine"]]:
|
|
||||||
"""
|
|
||||||
Accepts a list of Events and converts them into either ADRLine events or
|
|
||||||
GenricEvents by calling :func:`make_entity` on each member.
|
|
||||||
|
|
||||||
:param from_events: A list of `Event` objects.
|
|
||||||
|
|
||||||
:returns: A tuple of two lists, the first containing :class:`GenericEvent`
|
|
||||||
and the second containing :class:`ADRLine`.
|
|
||||||
"""
|
|
||||||
generic_events = list()
|
|
||||||
adr_lines = list()
|
|
||||||
|
|
||||||
for event in from_events:
|
|
||||||
result = make_entity(event)
|
|
||||||
if type(result) is ADRLine:
|
|
||||||
adr_lines.append(result)
|
|
||||||
elif type(result) is GenericEvent:
|
|
||||||
generic_events.append(result)
|
|
||||||
|
|
||||||
return generic_events, adr_lines
|
|
||||||
|
|
||||||
|
|
||||||
def make_entity(from_event: Event) -> Optional[object]:
|
|
||||||
"""
|
|
||||||
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"
|
|
||||||
tag field.
|
|
||||||
|
|
||||||
:param from_event: An :class:`Event`.
|
|
||||||
|
|
||||||
"""
|
|
||||||
instance = GenericEvent
|
|
||||||
tag_map = GenericEvent.tag_mapping
|
|
||||||
if "QN" in from_event.tags.keys():
|
|
||||||
instance = ADRLine
|
|
||||||
tag_map += ADRLine.tag_mapping
|
|
||||||
|
|
||||||
new = instance()
|
|
||||||
TagMapping.apply_rules(
|
|
||||||
tag_map,
|
|
||||||
from_event.tags,
|
|
||||||
from_event.clip_name,
|
|
||||||
from_event.track_name,
|
|
||||||
from_event.session_name,
|
|
||||||
new,
|
|
||||||
)
|
|
||||||
|
|
||||||
new.start = from_event.start
|
|
||||||
new.finish = from_event.finish
|
|
||||||
return new
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class GenericEvent:
|
|
||||||
title: str = ""
|
|
||||||
supervisor: Optional[str] = None
|
|
||||||
client: Optional[str] = None
|
|
||||||
scene: Optional[str] = None
|
|
||||||
version: Optional[str] = None
|
|
||||||
reel: Optional[str] = None
|
|
||||||
start: Fraction = Fraction(0, 1)
|
|
||||||
finish: Fraction = Fraction(0, 1)
|
|
||||||
omitted: bool = False
|
|
||||||
note: Optional[str] = None
|
|
||||||
requested_by: Optional[str] = None
|
|
||||||
|
|
||||||
tag_mapping = [
|
|
||||||
TagMapping(
|
|
||||||
source="Title", target="title", alt=TagMapping.ContentSource.Session
|
|
||||||
),
|
|
||||||
TagMapping(source="Supv", target="supervisor"),
|
TagMapping(source="Supv", target="supervisor"),
|
||||||
TagMapping(source="Client", target="client"),
|
TagMapping(source="Client", target="client"),
|
||||||
TagMapping(source="Sc", target="scene"),
|
TagMapping(source="Sc", target="scene"),
|
||||||
@@ -94,26 +23,7 @@ class GenericEvent:
|
|||||||
TagMapping(source="OMIT", target="omitted", formatter=(lambda x: len(x) > 0)),
|
TagMapping(source="OMIT", target="omitted", formatter=(lambda x: len(x) > 0)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
ADR_TAG_MAPPING = [
|
||||||
@dataclass
|
|
||||||
class ADRLine(GenericEvent):
|
|
||||||
priority: Optional[int] = None
|
|
||||||
cue_number: Optional[str] = None
|
|
||||||
character_id: Optional[str] = None
|
|
||||||
character_name: Optional[str] = None
|
|
||||||
actor_name: Optional[str] = None
|
|
||||||
prompt: Optional[str] = None
|
|
||||||
reason: Optional[str] = None
|
|
||||||
time_budget_mins: Optional[float] = None
|
|
||||||
spot: Optional[str] = None
|
|
||||||
shot: Optional[str] = None
|
|
||||||
effort: bool = False
|
|
||||||
tv: bool = False
|
|
||||||
tbw: bool = False
|
|
||||||
adlib: bool = False
|
|
||||||
optional: bool = False
|
|
||||||
|
|
||||||
tag_mapping = [
|
|
||||||
TagMapping(source="P", target="priority"),
|
TagMapping(source="P", target="priority"),
|
||||||
TagMapping(source="QN", target="cue_number"),
|
TagMapping(source="QN", target="cue_number"),
|
||||||
TagMapping(source="CN", target="character_id"),
|
TagMapping(source="CN", target="character_id"),
|
||||||
@@ -134,3 +44,92 @@ class ADRLine(GenericEvent):
|
|||||||
TagMapping(source="ADLIB", target="adlib", 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)),
|
TagMapping(source="OPT", target="optional", formatter=(lambda x: len(x) > 0)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def make_entities(
|
||||||
|
from_events: list[Event],
|
||||||
|
) -> tuple[list[GenericEvent], list[ADRLine]]:
|
||||||
|
"""
|
||||||
|
Accepts a list of Events and converts them into either ADRLine events or
|
||||||
|
GenricEvents by calling :func:`make_entity` on each member.
|
||||||
|
|
||||||
|
:param from_events: A list of `Event` objects.
|
||||||
|
|
||||||
|
:returns: A tuple of two lists, the first containing :class:`GenericEvent`
|
||||||
|
and the second containing :class:`ADRLine`.
|
||||||
|
"""
|
||||||
|
generic_events = []
|
||||||
|
adr_lines = []
|
||||||
|
|
||||||
|
for event in from_events:
|
||||||
|
result = make_entity(event)
|
||||||
|
if type(result) is ADRLine:
|
||||||
|
adr_lines.append(result)
|
||||||
|
elif type(result) is GenericEvent:
|
||||||
|
generic_events.append(result)
|
||||||
|
|
||||||
|
return generic_events, adr_lines
|
||||||
|
|
||||||
|
|
||||||
|
def make_entity(from_event: Event) -> object | None:
|
||||||
|
"""
|
||||||
|
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"
|
||||||
|
tag field.
|
||||||
|
|
||||||
|
:param from_event: An :class:`Event`.
|
||||||
|
|
||||||
|
"""
|
||||||
|
instance = GenericEvent
|
||||||
|
tag_map = GENERIC_TAG_MAPPING
|
||||||
|
if "QN" in from_event.tags:
|
||||||
|
instance = ADRLine
|
||||||
|
tag_map += ADR_TAG_MAPPING
|
||||||
|
|
||||||
|
new = instance()
|
||||||
|
TagMapping.apply_rules(
|
||||||
|
rules=tag_map,
|
||||||
|
tags=from_event.tags,
|
||||||
|
clip_content=from_event.clip_name,
|
||||||
|
track_content=from_event.track_name,
|
||||||
|
session_content=from_event.session_name,
|
||||||
|
to=new,
|
||||||
|
)
|
||||||
|
|
||||||
|
new.start = from_event.start
|
||||||
|
new.finish = from_event.finish
|
||||||
|
return new
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class GenericEvent:
|
||||||
|
title: str = ""
|
||||||
|
supervisor: str | None = None
|
||||||
|
client: str | None = None
|
||||||
|
scene: str | None = None
|
||||||
|
version: str | None = None
|
||||||
|
reel: str | None = None
|
||||||
|
start: Fraction = Fraction(0, 1)
|
||||||
|
finish: Fraction = Fraction(0, 1)
|
||||||
|
omitted: bool = False
|
||||||
|
note: str | None = None
|
||||||
|
requested_by: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ADRLine(GenericEvent):
|
||||||
|
priority: int | None = None
|
||||||
|
cue_number: str | None = None
|
||||||
|
character_id: str | None = None
|
||||||
|
character_name: str | None = None
|
||||||
|
actor_name: str | None = None
|
||||||
|
prompt: str | None = None
|
||||||
|
reason: str | None = None
|
||||||
|
time_budget_mins: float | None = None
|
||||||
|
spot: str | None = None
|
||||||
|
shot: str | None = None
|
||||||
|
effort: bool = False
|
||||||
|
tv: bool = False
|
||||||
|
tbw: bool = False
|
||||||
|
adlib: bool = False
|
||||||
|
optional: bool = False
|
||||||
|
|||||||
@@ -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"]
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user