mirror of
https://github.com/iluvcapra/ptulsconv.git
synced 2025-12-31 08:50:48 +00:00
Created basic test case
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
from fractions import Fraction
|
from fractions import Fraction
|
||||||
from ptulsconv.broadcast_timecode import smpte_to_frame_count
|
from ptulsconv.broadcast_timecode import smpte_to_frame_count
|
||||||
from typing import Tuple, List, Generator
|
from typing import Tuple, List, Iterator
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from . import apply_appends
|
from . import apply_appends
|
||||||
from .tagged_string_parser_visitor import parse_tags
|
from .tagged_string_parser_visitor import parse_tags
|
||||||
@@ -22,17 +22,17 @@ class SessionDescriptor:
|
|||||||
self.tracks = kwargs['tracks']
|
self.tracks = kwargs['tracks']
|
||||||
self.markers = kwargs['markers']
|
self.markers = kwargs['markers']
|
||||||
|
|
||||||
def markers_timed(self) -> Generator[tuple['MarkerDescriptor', Fraction]]:
|
def markers_timed(self) -> Iterator[Tuple['MarkerDescriptor', Fraction]]:
|
||||||
for marker in self.markers:
|
for marker in self.markers:
|
||||||
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):
|
def tracks_clips(self) -> Iterator[Tuple['TrackDescriptor', 'TrackClipDescriptor']]:
|
||||||
for track_idx, track in enumerate(self.tracks):
|
for track in self.tracks:
|
||||||
for clip in track.clips:
|
for clip in track.clips:
|
||||||
yield track_idx, track, clip
|
yield track, clip
|
||||||
|
|
||||||
def track_clips_timed(self) -> Generator[Tuple["TrackDescriptor", "TrackClipDescriptor",
|
def track_clips_timed(self) -> Iterator[Tuple["TrackDescriptor", "TrackClipDescriptor",
|
||||||
Fraction, Fraction, Fraction]]:
|
Fraction, Fraction, Fraction]]:
|
||||||
"""
|
"""
|
||||||
:return: A Generator that yields track, clip, start time, finish time, and timestamp
|
:return: A Generator that yields track, clip, start time, finish time, and timestamp
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional, Callable, Any, List, Generator, Tuple
|
from typing import Optional, Callable, Any, List, Generator, Tuple, Iterator
|
||||||
from .doc_entity import SessionDescriptor
|
from .doc_entity import SessionDescriptor
|
||||||
from .tagged_string_parser_visitor import parse_tags, TagPreModes
|
from .tagged_string_parser_visitor import parse_tags, TagPreModes
|
||||||
from . import apply_appends
|
from . import apply_appends
|
||||||
@@ -10,7 +10,7 @@ from collections import namedtuple
|
|||||||
class TagCompiler:
|
class TagCompiler:
|
||||||
session: SessionDescriptor
|
session: SessionDescriptor
|
||||||
|
|
||||||
def compile_events(self) -> Generator[Tuple[str, str, str, dict, Fraction, Fraction]]:
|
def compile_events(self) -> [Tuple[str, str, str, dict, Fraction, Fraction]]:
|
||||||
yield from self.apply_tags(
|
yield from self.apply_tags(
|
||||||
self.collect_time_spans(
|
self.collect_time_spans(
|
||||||
self.apply_appends(
|
self.apply_appends(
|
||||||
@@ -45,9 +45,9 @@ class TagCompiler:
|
|||||||
Intermediate = namedtuple('Intermediate', 'track_content track_tags track_comment_tags '
|
Intermediate = namedtuple('Intermediate', 'track_content track_tags track_comment_tags '
|
||||||
'clip_content clip_tags clip_tag_mode start finish')
|
'clip_content clip_tags clip_tag_mode start finish')
|
||||||
|
|
||||||
def parse_data(self) -> Generator[Intermediate]:
|
def parse_data(self) -> Iterator[Intermediate]:
|
||||||
|
|
||||||
for track, clip, start, finish in self.session.track_clips_timed():
|
for track, clip, start, finish, _ in self.session.track_clips_timed():
|
||||||
if clip.state == 'Muted':
|
if clip.state == 'Muted':
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -63,13 +63,13 @@ class TagCompiler:
|
|||||||
start=start, finish=finish)
|
start=start, finish=finish)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def apply_appends(parsed: Generator[Intermediate]) -> Generator[Intermediate]:
|
def apply_appends(parsed: Iterator[Intermediate]) -> Iterator[Intermediate]:
|
||||||
|
|
||||||
def should_append(a, b):
|
def should_append(a, b):
|
||||||
return b.clip_tag_mode == TagPreModes.APPEND and b.start >= a.finish
|
return b.clip_tag_mode == TagPreModes.APPEND and b.start >= a.finish
|
||||||
|
|
||||||
def do_append(a, b):
|
def do_append(a, b):
|
||||||
merged_tags = a.clip_tags
|
merged_tags = dict(a.clip_tags)
|
||||||
merged_tags.update(b.clip_tags)
|
merged_tags.update(b.clip_tags)
|
||||||
return TagCompiler.Intermediate(track_content=a.track_content,
|
return TagCompiler.Intermediate(track_content=a.track_content,
|
||||||
track_tags=a.track_tags,
|
track_tags=a.track_tags,
|
||||||
@@ -81,8 +81,8 @@ class TagCompiler:
|
|||||||
yield from apply_appends(parsed, should_append, do_append)
|
yield from apply_appends(parsed, should_append, do_append)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def collect_time_spans(parsed: Generator[Intermediate]) -> \
|
def collect_time_spans(parsed: Iterator[Intermediate]) -> \
|
||||||
Generator[Tuple[Intermediate, List[dict, Fraction, Fraction]]]:
|
Iterator[Tuple[Intermediate, Tuple[dict, Fraction, Fraction]]]:
|
||||||
|
|
||||||
time_spans = list()
|
time_spans = list()
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ class TagCompiler:
|
|||||||
|
|
||||||
return retval
|
return retval
|
||||||
|
|
||||||
def apply_tags(self, parsed_with_time_spans) -> Generator[Tuple[str, str, str, dict, Fraction, Fraction]]:
|
def apply_tags(self, parsed_with_time_spans) -> Iterator[Tuple[str, str, str, dict, Fraction, Fraction]]:
|
||||||
|
|
||||||
session_parsed = parse_tags(self.session.header.session_name)
|
session_parsed = parse_tags(self.session.header.session_name)
|
||||||
|
|
||||||
|
|||||||
90
tests/test_tagcompiler.py
Normal file
90
tests/test_tagcompiler.py
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
import unittest
|
||||||
|
from ptulsconv.docparser import tag_mapping, doc_entity
|
||||||
|
from fractions import Fraction
|
||||||
|
import pprint
|
||||||
|
|
||||||
|
|
||||||
|
class MyTestCase(unittest.TestCase):
|
||||||
|
def test_something(self):
|
||||||
|
c = tag_mapping.TagCompiler()
|
||||||
|
|
||||||
|
test_header = doc_entity.HeaderDescriptor(session_name="Test Session $Ver=1.1",
|
||||||
|
sample_rate=48000,
|
||||||
|
timecode_format="24",
|
||||||
|
timecode_drop_frame=False,
|
||||||
|
bit_depth=24,
|
||||||
|
start_timecode='00:59:00:00',
|
||||||
|
count_audio_tracks=1,
|
||||||
|
count_clips=3,
|
||||||
|
count_files=0
|
||||||
|
)
|
||||||
|
|
||||||
|
test_clips = [
|
||||||
|
doc_entity.TrackClipDescriptor(channel=1, event=1,
|
||||||
|
clip_name='This is clip 1 {Color=Blue}',
|
||||||
|
start_time='01:00:00:00',
|
||||||
|
finish_time='01:00:01:03',
|
||||||
|
duration='00:00:01:03',
|
||||||
|
state='Unmuted',
|
||||||
|
timestamp=None),
|
||||||
|
doc_entity.TrackClipDescriptor(channel=1, event=2,
|
||||||
|
clip_name='This is the second clip {R=Noise} [B]',
|
||||||
|
start_time='01:00:01:10',
|
||||||
|
finish_time='01:00:02:00',
|
||||||
|
duration='00:00:00:14',
|
||||||
|
state='Unmuted',
|
||||||
|
timestamp=None),
|
||||||
|
doc_entity.TrackClipDescriptor(channel=1, event=3,
|
||||||
|
clip_name='& ...and this is the last clip $N=1',
|
||||||
|
start_time='01:00:02:00',
|
||||||
|
finish_time='01:00:03:00',
|
||||||
|
duration='00:00:01:00',
|
||||||
|
state='Unmuted',
|
||||||
|
timestamp=None),
|
||||||
|
]
|
||||||
|
|
||||||
|
test_track = doc_entity.TrackDescriptor(name="Track 1 [A] {Color=Red}",
|
||||||
|
comments="{Comment=This is some text in the comments}",
|
||||||
|
user_delay_samples=0,
|
||||||
|
plugins=[],
|
||||||
|
state=[],
|
||||||
|
|
||||||
|
clips=test_clips)
|
||||||
|
|
||||||
|
c.session = doc_entity.SessionDescriptor(header=test_header,
|
||||||
|
tracks=[test_track],
|
||||||
|
clips=[],
|
||||||
|
files=[],
|
||||||
|
markers=[],
|
||||||
|
plugins=[])
|
||||||
|
|
||||||
|
events = c.compile_events()
|
||||||
|
event1 = next(events)
|
||||||
|
self.assertEqual('This is clip 1', event1[0])
|
||||||
|
self.assertEqual('Track 1', event1[1])
|
||||||
|
self.assertEqual('Test Session', event1[2])
|
||||||
|
self.assertEqual(dict(A='A',
|
||||||
|
Color='Blue',
|
||||||
|
Ver='1.1',
|
||||||
|
Comment='This is some text in the comments'), event1[3])
|
||||||
|
self.assertEqual(Fraction(3600, 1), event1[4])
|
||||||
|
|
||||||
|
event2 = next(events)
|
||||||
|
self.assertEqual("This is the second clip ...and this is the last clip", event2[0])
|
||||||
|
self.assertEqual('Track 1', event2[1])
|
||||||
|
self.assertEqual('Test Session', event2[2])
|
||||||
|
self.assertEqual(dict(R='Noise', A='A', B='B',
|
||||||
|
Color='Red',
|
||||||
|
Comment='This is some text in the comments',
|
||||||
|
N='1',
|
||||||
|
Ver='1.1'), event2[3])
|
||||||
|
|
||||||
|
self.assertEqual(c.session.header.convert_timecode('01:00:01:10'), event2[4])
|
||||||
|
self.assertEqual(c.session.header.convert_timecode('01:00:03:00'), event2[5])
|
||||||
|
|
||||||
|
self.assertIsNone(next(events, None))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user