Skip to content

Commit

Permalink
mypy clean
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed May 7, 2024
1 parent fa9853b commit 1d6a90b
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 69 deletions.
20 changes: 0 additions & 20 deletions .gitattributes

This file was deleted.

34 changes: 34 additions & 0 deletions .github/workflows/publish-python-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# https://docs.pypi.org/trusted-publishers/using-a-publisher/

name: publish

on:
release:
types: [published]

jobs:
release:

runs-on: ubuntu-latest

environment: release

permissions:
id-token: write

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Install builder
run: pip install build

- name: Build package
run: python -m build

- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "pylivestream"
Expand Down Expand Up @@ -34,7 +35,3 @@ line-length = 100
[tool.mypy]
files = ["src"]
ignore_missing_imports = true
strict_optional = false
allow_redefinition = true
show_error_context = false
show_column_numbers = true
12 changes: 6 additions & 6 deletions src/pylivestream/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def stream_file(
ini_file: Path,
websites: str | list[str],
video_file: Path,
loop: bool = None,
loop: bool | None = None,
assume_yes: bool = False,
timeout: float = None,
timeout: float | None = None,
):
S = FileIn(ini_file, websites, infn=video_file, loop=loop, yes=assume_yes, timeout=timeout)
sites: list[str] = list(S.streams.keys())
Expand All @@ -50,9 +50,9 @@ def stream_microphone(
ini_file: Path,
websites: list[str],
*,
still_image: Path = None,
assume_yes: bool = False,
timeout: float = None,
still_image: Path | None = None,
assume_yes: bool | None = False,
timeout: float | None = None,
):
"""
livestream audio, with still image background
Expand All @@ -70,7 +70,7 @@ def stream_microphone(


def capture_screen(
ini_file: Path, *, out_file: Path, assume_yes: bool = False, timeout: float = None
ini_file: Path, *, out_file: Path, assume_yes: bool = False, timeout: float | None = None
):

s = SaveDisk(ini_file, out_file, yes=assume_yes, timeout=timeout)
Expand Down
6 changes: 3 additions & 3 deletions src/pylivestream/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(self, inifn: Path, site: str, **kwargs) -> None:
+ ["-f", "null", "-"] # camera needs at output
)

def startlive(self, sinks: list[str] = None):
def startlive(self, sinks: list[str] | None = None):
"""
start the stream(s)
"""
Expand Down Expand Up @@ -141,7 +141,7 @@ def startlive(self, sinks: list[str] = None):
proc.terminate()
yield

def check_device(self, site: str = None) -> bool:
def check_device(self, site: str | None = None) -> bool:
"""
requires stream to have been configured first.
does a quick test stream to "null" to verify device is actually accessible
Expand Down Expand Up @@ -251,7 +251,7 @@ def golive(self) -> None:


class SaveDisk(Stream):
def __init__(self, inifn: Path, outfn: Path = None, **kwargs):
def __init__(self, inifn: Path, outfn: Path | None = None, **kwargs):
"""
records to disk screen capture with audio
Expand Down
12 changes: 6 additions & 6 deletions src/pylivestream/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self):

self.THROTTLE = "-re"

def timelimit(self, t: str | int | float) -> list[str]:
def timelimit(self, t: str | int | float | None) -> list[str]:
if t is None:
return []

Expand All @@ -36,7 +36,7 @@ def timelimit(self, t: str | int | float) -> list[str]:
else:
return []

def drawtext(self, text: str = None) -> list[str]:
def drawtext(self, text: str) -> list[str]:
# fontfile=/path/to/font.ttf:
if not text: # None or '' or [] etc.
return []
Expand Down Expand Up @@ -85,7 +85,7 @@ def listener(self):

return proc

def movingBG(self, bgfn: Path = None) -> list[str]:
def movingBG(self, bgfn: Path | None = None) -> list[str]:
if not bgfn:
return []

Expand Down Expand Up @@ -125,9 +125,9 @@ def get_ffprobe() -> str:
return get_exe("ffprobe")


def get_meta(fn: Path, exein: str = None) -> dict[str, T.Any]:
def get_meta(fn: Path, exein: str | None = None) -> dict[str, T.Any]:
if not fn: # audio-only
return None
return {}

fn = Path(fn).expanduser()

Expand All @@ -147,6 +147,6 @@ def get_meta(fn: Path, exein: str = None) -> dict[str, T.Any]:
str(fn),
]

ret = subprocess.check_output(cmd, universal_newlines=True)
ret = subprocess.check_output(cmd, text=True)
# %% decode JSON from FFprobe
return json.loads(ret)
20 changes: 10 additions & 10 deletions src/pylivestream/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ def stream_files(
websites: list[str],
*,
video_path: Path,
glob: str = None,
glob: str | None = None,
assume_yes: bool = False,
loop: bool = None,
shuffle: bool = None,
still_image: Path = None,
no_meta: bool = None,
timeout: float = None,
loop: bool = False,
shuffle: bool = False,
still_image: Path | None = None,
no_meta: bool = False,
timeout: float | None = None,
):
# %% file / glob wranging
flist = fileglob(video_path, glob)
Expand All @@ -49,19 +49,19 @@ def stream_files(

def playonce(
flist: list[Path],
image: Path,
image: Path | None,
sites: list[str],
inifn: Path,
shuffle: bool,
usemeta: bool,
yes: bool,
timeout: float = None,
timeout: float | None = None,
):

if shuffle:
random.shuffle(flist)

caption: str
caption: str | None

for f in flist:
if usemeta and TinyTag:
Expand All @@ -80,7 +80,7 @@ def playonce(
s.golive()


def fileglob(path: Path, glob: str) -> list[Path]:
def fileglob(path: Path, glob: str | None) -> list[Path]:

path = Path(path).expanduser()

Expand Down
2 changes: 1 addition & 1 deletion src/pylivestream/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def stream_screen(
ini_file: Path, websites: list[str], *, assume_yes: bool = False, timeout: float = None
ini_file: Path, websites: list[str], *, assume_yes: bool = False, timeout: float | None = None
):

S = Screenshare(ini_file, websites, yes=assume_yes, timeout=timeout)
Expand Down
17 changes: 7 additions & 10 deletions src/pylivestream/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,16 @@ def __init__(self, inifn: Path, site: str, **kwargs):
self.site: str = site
self.vidsource = kwargs.get("vidsource")

if kwargs.get("image"):
self.image = Path(kwargs["image"]).expanduser()
else:
self.image = None
self.image = Path(kwargs["image"]).expanduser() if kwargs.get("image") else None

self.loop: bool = kwargs.get("loop")
self.loop: bool = kwargs.get("loop", False)

self.infn = Path(kwargs["infn"]).expanduser() if kwargs.get("infn") else None
self.yes: list[str] = self.F.YES if kwargs.get("yes") else []

self.queue: list[str] = [] # self.F.QUEUE

self.caption: str = kwargs.get("caption")
self.caption: str = kwargs.get("caption", "")

self.timelimit: list[str] = self.F.timelimit(kwargs.get("timeout"))

Expand Down Expand Up @@ -80,7 +77,7 @@ def osparam(self, fn: Path) -> None:

if self.vidsource == "camera":
self.res: list[str] = C.get("camera_size")
self.fps: float = C.get("camera_fps")
self.fps: float | None = C.get("camera_fps")
self.movingimage = self.staticimage = False
elif self.vidsource == "screen":
self.res = C.get("screencap_size")
Expand All @@ -94,10 +91,10 @@ def osparam(self, fn: Path) -> None:
self.res = utils.get_resolution(self.infn, self.probeexe)
self.fps = utils.get_framerate(self.infn, self.probeexe)
else: # audio-only
self.res = None
self.res = []
self.fps = None

if self.res is not None and len(self.res) != 2:
if self.res and len(self.res) != 2:
raise ValueError(f"need height, width of video resolution, I have: {self.res}")

self.audio_rate: str = C.get("audio_rate")
Expand Down Expand Up @@ -236,7 +233,7 @@ def video_bitrate(self) -> None:
if self.video_kbps: # per-site override
return

if self.res is not None:
if self.res:
x: int = int(self.res[1])
elif self.vidsource is None or self.vidsource == "file":
logging.info("assuming 480p input.")
Expand Down
2 changes: 1 addition & 1 deletion src/pylivestream/tests/test_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_exe(rex):

@pytest.mark.parametrize("inp", (None, ""))
def test_attrs(inp):
assert pls.utils.get_resolution(inp) is None
assert not pls.utils.get_resolution(inp)

with importlib.resources.as_file(
importlib.resources.files("pylivestream.data").joinpath("bunny.avi")
Expand Down
22 changes: 14 additions & 8 deletions src/pylivestream/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def run(cmd: list[str]):
I don't like putting enormous amounts in a PIPE, this can be unstable.
Better to let users know there's a problem.
ret = subprocess.run(cmd, stderr=subprocess.PIPE, universal_newlines=True)
ret = subprocess.run(cmd, stderr=subprocess.PIPE, text=True)
if ret.returncode != 0:
if "Connection reset by peer" in ret.stderr:
Expand All @@ -48,7 +48,7 @@ def check_device(cmd: list[str]) -> bool:
return ok


def check_display(fn: Path = None) -> bool:
def check_display(fn: Path | None = None) -> bool:
"""see if it's possible to display something with a test file"""

def _check_disp(fn: Path | contextlib.AbstractContextManager[Path]) -> int:
Expand Down Expand Up @@ -83,7 +83,7 @@ def meta_caption(meta) -> str:
return caption


def get_resolution(fn: Path, exe: str = None) -> list[str]:
def get_resolution(fn: Path | None, exe: str | None = None) -> list[str]:
"""
get resolution (widthxheight) of video file
http://trac.ffmpeg.org/wiki/FFprobeTips#WidthxHeight
Expand All @@ -101,11 +101,14 @@ def get_resolution(fn: Path, exe: str = None) -> list[str]:
if not a video file, None is returned.
"""

if fn is None:
return []

meta = get_meta(fn, exe)
if meta is None:
return None
if not meta:
return []

res = None
res = []

for s in meta["streams"]:
if s["codec_type"] != "video":
Expand All @@ -116,7 +119,7 @@ def get_resolution(fn: Path, exe: str = None) -> list[str]:
return res


def get_framerate(fn: Path, exe: str = None) -> float:
def get_framerate(fn: Path | None, exe: str | None = None) -> float | None:
"""
get framerate of video file
http://trac.ffmpeg.org/wiki/FFprobeTips#FrameRate
Expand All @@ -136,8 +139,11 @@ def get_framerate(fn: Path, exe: str = None) -> float:
video framerate (frames/second). If not a video file, None is returned.
"""

if fn is None:
return None

meta = get_meta(fn, exe)
if meta is None:
if not meta:
return None

fps = None
Expand Down

0 comments on commit 1d6a90b

Please sign in to comment.