Skip to content

Commit

Permalink
Avoid using bare except and add optional static_ffmpeg import
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindln committed Nov 18, 2023
1 parent b5af050 commit 21f45e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
39 changes: 25 additions & 14 deletions lddecode/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,9 @@ def doread(self, blocknums, MTF, redo=False, prefetch=False):
if b not in self.blocks:
LRUupdate(self.lru, b)

rawdata = self.loader(self.infile, b * self.blocksize, self.rf.blocklen)
rawdata = self.loader(
self.infile, b * self.blocksize, self.rf.blocklen
)

if rawdata is None or len(rawdata) < self.rf.blocklen:
self.blocks[b] = None
Expand All @@ -1183,29 +1185,38 @@ def doread(self, blocknums, MTF, redo=False, prefetch=False):
reached_end = True
break

try:
waiting = self.block_status[b]["waiting"]
except:
waiting = False
waiting = (
self.block_status[b].get("waiting", False)
if b in self.block_status
else False
)

try:
# Until the block is actually ready, this comparison will hit an unknown key
if not redo and not waiting and self.blocks[b]["request"] == self.block_status[b]['request']:
continue
except:
pass
# Until the block is actually ready, this comparison will hit an unknown key
if (
not redo
and not waiting
and "request" in self.blocks[b]
and "request" in self.block_status[b]
and self.blocks[b]["request"] == self.block_status[b]["request"]
):
continue

if redo or not waiting:
queuelist.append(b)
need_blocks.append(b)
elif waiting:
need_blocks.append(b)

if not prefetch:
self.waiting.add(b)

for b in queuelist:
self.block_status[b] = {'MTF': MTF, 'waiting': True, 'request': self.request, 'prefetch': prefetch}
self.block_status[b] = {
"MTF": MTF,
"waiting": True,
"request": self.request,
"prefetch": prefetch,
}
self.q_in.put(("DEMOD", b, self.blocks[b], MTF, self.request))

self.q_out_event.clear()
Expand Down Expand Up @@ -3035,7 +3046,7 @@ def determine_field_number(self):
# and on a bad disk, this value could be None...
if self.prevfield.phase_adjust[l] is not None:
prev_phaseadjust = self.prevfield.phase_adjust[l]
except:
except AttributeError:
pass

rising, self.phase_adjust[l] = self.compute_line_bursts(
Expand Down
10 changes: 8 additions & 2 deletions lddecode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
import numpy as np
import scipy.signal as sps

# Try to make sure ffmpeg is available
try:
import static_ffmpeg
except ImportError:
pass

# This runs a cubic scaler on a line.
# originally from https://www.paulinternet.nl/?page=bicubic
@njit(nogil=True, cache=True)
Expand Down Expand Up @@ -563,15 +569,15 @@ def ac3_pipe(outname: str):

def get_version():
""" get version info stashed in this directory's version file """

try:
# get the directory this file was pulled from, then add /version
scriptdir = os.path.dirname(os.path.realpath(__file__))
fd = open(os.path.join(scriptdir, 'version'), 'r')

fdata = fd.read()
return fdata.strip() # remove trailing \n etc
except: # usually FileNotFoundError
except (FileNotFoundError, OSError):
# Just return 'unknown' if we fail to find anything.
return "unknown"


Expand Down
3 changes: 2 additions & 1 deletion lddecode/utils_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import sys
import os


def init_logging(outfile_name, columns=80):
Expand Down Expand Up @@ -34,7 +35,7 @@ def emit(self, record):
# Delete old logfile if it exists
try:
os.unlink(outfile_name)
except Exception:
except (OSError, FileNotFoundError):
pass

logger_file = logging.FileHandler(outfile_name)
Expand Down

0 comments on commit 21f45e1

Please sign in to comment.