Skip to content

Commit

Permalink
cyclomatic complexity reduction decode(), show(), proxy() etc...
Browse files Browse the repository at this point in the history
  • Loading branch information
futzu authored Oct 14, 2024
1 parent 9e96ac9 commit 188435f
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions threefive/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ def iter_pkts(self, num_pkts=1):
"""
iter_pkts iterates a mpegts stream into packets
"""
return iter(partial(self._tsdata.read, self.PACKET_SIZE * num_pkts), b"")
if self._find_start():
return iter(partial(self._tsdata.read, self.PACKET_SIZE * num_pkts), b"")
return False

def _mk_pkts(self, chunk):
return [
Expand All @@ -253,12 +255,10 @@ def decode(self, func=show_cue):
func can be set to a custom function that accepts
a threefive.Cue instance as it's only argument.
"""
if not self._find_start():
return False
for pkt in self.iter_pkts():
cue = self._parse(pkt)
if cue:
func(cue)
func(cue)
return False

def decode_fu(self, func=show_cue):
Expand All @@ -267,20 +267,17 @@ def decode_fu(self, func=show_cue):
num_pkts packets at a time.
Super Fast with pypy3.
"""
if self._find_start():
num_pkts = 3000
for chunk in self.iter_pkts(num_pkts=num_pkts):
_ = [func(cue) for cue in self._mk_pkts(chunk) if cue]
del _
num_pkts = 3000
for chunk in self.iter_pkts(num_pkts=num_pkts):
_ = [func(cue) for cue in self._mk_pkts(chunk) if cue]
del _
return False

def decode_next(self):
"""
Stream.decode_next returns the next
SCTE35 cue as a threefive.Cue instance.
"""
if not self._find_start():
return False
for pkt in self.iter_pkts():
cue = self._parse(pkt)
if cue:
Expand Down Expand Up @@ -319,8 +316,6 @@ def proxy(self, func=show_cue_stderr):
for piping into another program like mplayer.
SCTE-35 cues are print2`ed to stderr.
"""
if not self._find_start():
return False
for pkt in self.iter_pkts():
cue = self._parse(pkt)
if cue:
Expand Down Expand Up @@ -352,13 +347,12 @@ def show_pts(self):
"""
show_pts displays current pts by pid.
"""
if self._find_start():
print2("\tPID\tPTS")
for pkt in self.iter_pkts():
pid = self._parse_info(pkt)
if self._pusi_flag(pkt) and pid != 0:
self._parse_pts(pkt, pid)
print(f"\t{pid}\t{self.pid2pts(pid)}", end="\r")
print2("\tPID\tPTS")
for pkt in self.iter_pkts():
pid = self._parse_info(pkt)
if self._pusi_flag(pkt) and pid != 0:
self._parse_pts(pkt, pid)
print(f"\t{pid}\t{self.pid2pts(pid)}", end="\r")

def pts(self):
"""
Expand Down

0 comments on commit 188435f

Please sign in to comment.