From 51ed92f5dfdde5d790b0d7d172ae66a4c1e81bc9 Mon Sep 17 00:00:00 2001 From: Jamie Hardt Date: Wed, 31 May 2023 18:10:44 -0700 Subject: [PATCH] More typing --- pycmx/channel_map.py | 28 ++++++++++++++-------------- pycmx/parse_cmx_events.py | 15 +++++++-------- pycmx/parse_cmx_statements.py | 10 ++++++---- pycmx/transition.py | 24 ++++++++++++++---------- 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/pycmx/channel_map.py b/pycmx/channel_map.py index a7071ef..f52cc4f 100644 --- a/pycmx/channel_map.py +++ b/pycmx/channel_map.py @@ -2,7 +2,7 @@ # (c) 2018 Jamie Hardt from re import (compile, match) -from typing import Dict, Tuple +from typing import Dict, Tuple, Generator class ChannelMap: """ @@ -24,62 +24,62 @@ class ChannelMap: self.v = v @property - def video(self): + def video(self) -> bool: 'True if video is included' return self.v @property - def audio(self): + def audio(self) -> bool: 'True if an audio channel is included' return len(self._audio_channel_set) > 0 @property - def channels(self): + def channels(self) -> Generator[int, None, None]: 'A generator for each audio channel' for c in self._audio_channel_set: yield c @property - def a1(self): + def a1(self) -> bool: """True if A1 is included""" return self.get_audio_channel(1) @a1.setter - def a1(self,val): + def a1(self, val: bool): self.set_audio_channel(1,val) @property - def a2(self): + def a2(self) -> bool: """True if A2 is included""" return self.get_audio_channel(2) @a2.setter - def a2(self,val): + def a2(self, val: bool): self.set_audio_channel(2,val) @property - def a3(self): + def a3(self) -> bool: """True if A3 is included""" return self.get_audio_channel(3) @a3.setter - def a3(self,val): + def a3(self, val: bool): self.set_audio_channel(3,val) @property - def a4(self): + def a4(self) -> bool: """True if A4 is included""" return self.get_audio_channel(4) @a4.setter - def a4(self,val): + def a4(self,val: bool): self.set_audio_channel(4,val) - def get_audio_channel(self,chan_num): + def get_audio_channel(self, chan_num) -> bool: """True if chan_num is included""" return (chan_num in self._audio_channel_set) - def set_audio_channel(self,chan_num,enabled): + def set_audio_channel(self,chan_num, enabled: bool): """If enabled is true, chan_num will be included""" if enabled: self._audio_channel_set.add(chan_num) diff --git a/pycmx/parse_cmx_events.py b/pycmx/parse_cmx_events.py index 211e2ad..176c0cc 100644 --- a/pycmx/parse_cmx_events.py +++ b/pycmx/parse_cmx_events.py @@ -1,20 +1,19 @@ # pycmx # (c) 2018 Jamie Hardt -from collections import namedtuple +# from collections import namedtuple -from .parse_cmx_statements import (parse_cmx3600_statements, StmtEvent,StmtFCM ) +from .parse_cmx_statements import (parse_cmx3600_statements) from .edit_list import EditList -def parse_cmx3600(f): +from typing import TextIO + +def parse_cmx3600(f: TextIO): """ Parse a CMX 3600 EDL. - Args: - f : a file-like object, anything that's readlines-able. - - Returns: - An :class:`pycmx.edit_list.EditList`. + :param TextIO f: a file-like object, anything that's readlines-able. + :returns: An :class:`pycmx.edit_list.EditList`. """ statements = parse_cmx3600_statements(f) return EditList(statements) diff --git a/pycmx/parse_cmx_statements.py b/pycmx/parse_cmx_statements.py index d391698..957c238 100644 --- a/pycmx/parse_cmx_statements.py +++ b/pycmx/parse_cmx_statements.py @@ -5,6 +5,8 @@ import re import sys from collections import namedtuple from itertools import count +from typing import TextIO, List + from .util import collimate @@ -18,12 +20,12 @@ StmtSourceFile = namedtuple("SourceFile",["filename","line_number"]) StmtRemark = namedtuple("Remark",["text","line_number"]) StmtEffectsName = namedtuple("EffectsName",["name","line_number"]) StmtSourceUMID = namedtuple("Source",["name","umid","line_number"]) -StmtSplitEdit = namedtuple("SplitEdit",["video","magnitue", "line_number"]) +StmtSplitEdit = namedtuple("SplitEdit",["video","magnitude", "line_number"]) StmtMotionMemory = namedtuple("MotionMemory",["source","fps"]) # FIXME needs more fields StmtUnrecognized = namedtuple("Unrecognized",["content","line_number"]) -def parse_cmx3600_statements(file): +def parse_cmx3600_statements(file: TextIO) -> List[object]: """ Return a list of every statement in the file argument. """ @@ -109,7 +111,7 @@ def _parse_extended_audio_channels(line, line_number): else: return StmtUnrecognized(content=line, line_number=line_number) -def _parse_remark(line, line_number): +def _parse_remark(line, line_number) -> object: if line.startswith("FROM CLIP NAME:"): return StmtClipName(name=line[15:].strip() , affect="from", line_number=line_number) elif line.startswith("TO CLIP NAME:"): @@ -119,7 +121,7 @@ def _parse_remark(line, line_number): else: return StmtRemark(text=line, line_number=line_number) -def _parse_effects_name(line, line_number): +def _parse_effects_name(line, line_number) -> StmtEffectsName: name = line[16:].strip() return StmtEffectsName(name=name, line_number=line_number) diff --git a/pycmx/transition.py b/pycmx/transition.py index 988b411..5d2ebe3 100644 --- a/pycmx/transition.py +++ b/pycmx/transition.py @@ -1,5 +1,7 @@ # pycmx -# (c) 2018 Jamie Hardt +# (c) 2023 Jamie Hardt + +from typing import Optional class Transition: """ @@ -19,7 +21,7 @@ class Transition: self.name = name @property - def kind(self): + def kind(self) -> Optional[str]: """ Return the kind of transition: Cut, Wipe, etc """ @@ -37,22 +39,22 @@ class Transition: return Transition.KeyOut @property - def cut(self): + def cut(self) -> bool: "`True` if this transition is a cut." return self.transition == 'C' @property - def dissolve(self): + def dissolve(self) -> bool: "`True` if this traansition is a dissolve." return self.transition == 'D' @property - def wipe(self): + def wipe(self) -> bool: "`True` if this transition is a wipe." return self.transition.startswith('W') @property - def effect_duration(self): + def effect_duration(self) -> int: """The duration of this transition, in frames of the record target. In the event of a key event, this is the duration of the fade in. @@ -60,7 +62,7 @@ class Transition: return int(self.operand) @property - def wipe_number(self): + def wipe_number(self) -> Optional[int]: "Wipes are identified by a particular number." if self.wipe: return int(self.transition[1:]) @@ -68,19 +70,21 @@ class Transition: return None @property - def key_background(self): + def key_background(self) -> bool: "`True` if this edit is a key background." return self.transition == Transition.KeyBackground @property - def key_foreground(self): + def key_foreground(self) -> bool: "`True` if this edit is a key foreground." return self.transition == Transition.Key @property - def key_out(self): + def key_out(self) -> bool: """ `True` if this edit is a key out. This material will removed from the key foreground and replaced with the key background. """ return self.transition == Transition.KeyOut + +