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.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from fractions import Fraction
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from ptulsconv.docparser.tag_compiler import Event
|
||||
from ptulsconv.docparser.tag_mapping import TagMapping
|
||||
|
||||
|
||||
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 = 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
|
||||
),
|
||||
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"),
|
||||
@@ -94,26 +23,7 @@ class GenericEvent:
|
||||
TagMapping(source="OMIT", target="omitted", formatter=(lambda x: len(x) > 0)),
|
||||
]
|
||||
|
||||
|
||||
@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 = [
|
||||
ADR_TAG_MAPPING = [
|
||||
TagMapping(source="P", target="priority"),
|
||||
TagMapping(source="QN", target="cue_number"),
|
||||
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="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 fractions import Fraction
|
||||
from typing import List, Tuple
|
||||
|
||||
from ptulsconv.broadcast_timecode import TimecodeFormat
|
||||
|
||||
|
||||
class SessionDescriptor:
|
||||
header: "HeaderDescriptor"
|
||||
files: List["FileDescriptor"]
|
||||
clips: List["ClipDescriptor"]
|
||||
plugins: List["PluginDescriptor"]
|
||||
tracks: List["TrackDescriptor"]
|
||||
markers: List["MarkerDescriptor"]
|
||||
header: HeaderDescriptor
|
||||
files: list[FileDescriptor]
|
||||
clips: list[ClipDescriptor]
|
||||
plugins: list[PluginDescriptor]
|
||||
tracks: list[TrackDescriptor]
|
||||
markers: list[MarkerDescriptor]
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.header = kwargs["header"]
|
||||
@@ -23,7 +24,7 @@ class SessionDescriptor:
|
||||
|
||||
def markers_timed(
|
||||
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.
|
||||
"""
|
||||
@@ -35,7 +36,7 @@ class SessionDescriptor:
|
||||
# marker_time = self.header.convert_timecode(marker.location)
|
||||
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.
|
||||
"""
|
||||
@@ -46,7 +47,7 @@ class SessionDescriptor:
|
||||
def track_clips_timed(
|
||||
self,
|
||||
) -> 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
|
||||
@@ -97,11 +98,11 @@ class HeaderDescriptor:
|
||||
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)
|
||||
|
||||
@property
|
||||
def start_time(self) -> Fraction:
|
||||
def start_time(self) -> Fraction | None:
|
||||
"""
|
||||
The start time of this session.
|
||||
:return: Start time in seconds
|
||||
@@ -117,7 +118,7 @@ class HeaderDescriptor:
|
||||
return self._get_tc_format_params[1]
|
||||
|
||||
@property
|
||||
def _get_tc_format_params(self) -> Tuple[int, Fraction]:
|
||||
def _get_tc_format_params(self) -> tuple[int, Fraction]:
|
||||
frame_rates = {
|
||||
"23.976": (24, Fraction(1001, 24_000)),
|
||||
"24": (24, Fraction(1, 24)),
|
||||
@@ -131,7 +132,7 @@ class HeaderDescriptor:
|
||||
if self.timecode_fps in frame_rates:
|
||||
return frame_rates[self.timecode_fps]
|
||||
else:
|
||||
raise ValueError("Unrecognized TC rate (%s)" % self.timecode_format)
|
||||
raise ValueError(f"Unrecognized TC rate ({self.timecode_format})")
|
||||
|
||||
|
||||
class TrackDescriptor:
|
||||
@@ -139,9 +140,9 @@ class TrackDescriptor:
|
||||
name: str
|
||||
comments: str
|
||||
user_delay_samples: int
|
||||
state: List[str]
|
||||
plugins: List[str]
|
||||
clips: List["TrackClipDescriptor"]
|
||||
state: list[str]
|
||||
plugins: list[str]
|
||||
clips: list[TrackClipDescriptor]
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.index = kwargs["index"]
|
||||
|
||||
@@ -173,41 +173,33 @@ class DocParserVisitor(NodeVisitor):
|
||||
|
||||
@staticmethod
|
||||
def visit_files_section(_, visited_children):
|
||||
return list(
|
||||
map(
|
||||
lambda child: FileDescriptor(filename=child[0], path=child[2]),
|
||||
visited_children[2],
|
||||
)
|
||||
)
|
||||
return [
|
||||
FileDescriptor(filemname=child[0], path=child[2])
|
||||
for child in visited_children[2]
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def visit_clips_section(_, visited_children):
|
||||
channel = next(iter(visited_children[2][3]), 1)
|
||||
|
||||
return list(
|
||||
map(
|
||||
lambda child: ClipDescriptor(
|
||||
clip_name=child[0], file=child[2], channel=channel
|
||||
),
|
||||
visited_children[2],
|
||||
)
|
||||
)
|
||||
return [
|
||||
ClipDescriptor(clip_name=child[0], file=child[2], channel=channel)
|
||||
for child in visited_children[2]
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def visit_plugin_listing(_, visited_children):
|
||||
return list(
|
||||
map(
|
||||
lambda child: PluginDescriptor(
|
||||
|
||||
return [
|
||||
PluginDescriptor(
|
||||
manufacturer=child[0],
|
||||
plugin_name=child[2],
|
||||
version=child[4],
|
||||
format=child[6],
|
||||
stems=child[8],
|
||||
count_instances=child[10],
|
||||
),
|
||||
visited_children[2],
|
||||
)
|
||||
)
|
||||
for child in visited_children[2]
|
||||
]
|
||||
|
||||
# @staticmethod
|
||||
def visit_track_block(self, _, visited_children):
|
||||
@@ -276,6 +268,7 @@ class DocParserVisitor(NodeVisitor):
|
||||
markers = []
|
||||
|
||||
for marker in visited_children[1][0][1]:
|
||||
marker: MarkerDescriptor
|
||||
markers.append(marker)
|
||||
|
||||
return markers
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from enum import Enum
|
||||
from typing import Any, Callable, List, Optional
|
||||
from typing import Any, Callable
|
||||
|
||||
|
||||
class TagMapping:
|
||||
@@ -10,14 +12,14 @@ class TagMapping:
|
||||
Clip = (3,)
|
||||
|
||||
source: str
|
||||
alternate_source: Optional[ContentSource]
|
||||
alternate_source: ContentSource | None
|
||||
formatter: Callable[[str], Any]
|
||||
|
||||
@staticmethod
|
||||
def print_rules(for_type: object, output=sys.stdout):
|
||||
format_str = "%-20s | %-20s | %-25s"
|
||||
hr = "%s+%s+%s" % ("-" * 21, "-" * 23, "-" * 26)
|
||||
print("Tag mapping for %s" % for_type.__name__)
|
||||
hr = f"{'-' * 21}+{'-' * 23}+{'-' * 26}"
|
||||
print(f"Tag mapping for {for_type.__class__}")
|
||||
print(hr)
|
||||
print(format_str % ("Tag Source", "Target", "Type"), file=output)
|
||||
print(hr)
|
||||
@@ -33,7 +35,7 @@ class TagMapping:
|
||||
|
||||
@staticmethod
|
||||
def apply_rules(
|
||||
rules: List["TagMapping"],
|
||||
rules: list[TagMapping],
|
||||
tags: dict,
|
||||
clip_content: str,
|
||||
track_content: str,
|
||||
@@ -52,7 +54,7 @@ class TagMapping:
|
||||
self,
|
||||
source: str,
|
||||
target: str,
|
||||
alt: Optional[ContentSource] = None,
|
||||
alt: ContentSource | None = None,
|
||||
formatter=None,
|
||||
):
|
||||
self.source = source
|
||||
|
||||
Reference in New Issue
Block a user