Skip to content

Commit

Permalink
[project] Use ruff for linting project
Browse files Browse the repository at this point in the history
Now passes `ruff check` with some fixes suppressed.
  • Loading branch information
Breakthrough committed Jul 7, 2024
1 parent 5c329ed commit e4a56b7
Show file tree
Hide file tree
Showing 44 changed files with 180 additions and 182 deletions.
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
Expand Down
15 changes: 7 additions & 8 deletions docs/generate_cli_docs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Generate formatted CLI documentation for PySceneDetect.
#
# Inspired by sphinx-click: https://github.com/click-contrib/sphinx-click
Expand All @@ -10,22 +9,22 @@
Run from main repo folder as working directory."""

import inspect
import os
import re
import sys
import inspect
import typing as ty
import re
from dataclasses import dataclass

# Add parent folder to path so we can resolve `scenedetect` imports.
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir)
from scenedetect._cli import scenedetect

# Third-party imports
import click

from scenedetect._cli import scenedetect

StrGenerator = ty.Generator[str, None, None]

INDENT = " " * 4
Expand Down Expand Up @@ -79,7 +78,7 @@ def patch_help(s: str, commands: ty.List[str]) -> str:
assert pos > 0
s = s[: pos + 1] + "scenedetect " + s[pos + 1 :]

for command in [command for command in commands if not command in INFO_COMMANDS]:
for command in [command for command in commands if command not in INFO_COMMANDS]:

def add_link(_match: re.Match) -> str:
return ":ref:`%s <command-%s>`" % (command, command)
Expand Down Expand Up @@ -136,7 +135,7 @@ def extract_default_value(s: str) -> ty.Tuple[str, ty.Optional[str]]:
assert span[1] == len(s)
s, default = s[: span[0]].strip(), s[span[0] : span[1]][len("[default: ") : -1]
# Double-quote any default values that contain spaces.
if " " in default and not '"' in default and not "," in default:
if " " in default and '"' not in default and "," not in default:
default = '"%s"' % default
return (s, default)

Expand Down Expand Up @@ -240,7 +239,7 @@ def generate_subcommands(ctx: click.Context, commands: ty.List[str]) -> StrGener
output_commands = [
command
for command in commands
if (not command.startswith("detect-") and not command in INFO_COMMANDS)
if (not command.startswith("detect-") and command not in INFO_COMMANDS)
]
for command in output_commands:
yield from generate_command_help(ctx, ctx.command.get_command(ctx, command), ctx.info_name)
Expand Down
30 changes: 30 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.ruff]
exclude = [
"docs"
]
line-length = 100
indent-width = 4

Expand All @@ -21,3 +24,30 @@ quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
docstring-code-format = true

[tool.ruff.lint]
select = [
# pycodestyle
"E",
# Pyflakes
"F",
# TODO - Rule sets to enable:
# pyupgrade
#"UP",
# flake8-bugbear
#"B",
# flake8-simplify
#"SIM",
# isort
#"I",
]
ignore = [
# TODO: Determine if we should use __all__, a reudndant alias, or keep this suppressed.
"F401",
# Line too long
"E501",
# Do not assign a `lambda` expression, use a `def`
"E731",
]
fixable = ["ALL"]
unfixable = []
4 changes: 2 additions & 2 deletions scenedetect/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# PySceneDetect: Python-Based Video Scene Detector
# -------------------------------------------------------------------
Expand All @@ -15,6 +14,7 @@
can be used to open a video for a
:class:`SceneManager <scenedetect.scene_manager.SceneManager>`.
"""
# ruff: noqa: I001

from logging import getLogger
from typing import List, Optional, Tuple, Union
Expand Down Expand Up @@ -169,6 +169,6 @@ def detect(
show_progress=show_progress,
end_time=end_time,
)
if not scene_manager.stats_manager is None:
if scene_manager.stats_manager is not None:
scene_manager.stats_manager.save_to_csv(csv_file=stats_file_path)
return scene_manager.get_scene_list(start_in_scene=start_in_scene)
6 changes: 2 additions & 4 deletions scenedetect/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# PySceneDetect: Python-Based Video Scene Detector
# -------------------------------------------------------------------
Expand All @@ -12,14 +11,13 @@
#
"""Entry point for PySceneDetect's command-line interface."""

from logging import getLogger
import sys
from logging import getLogger

from scenedetect._cli import scenedetect
from scenedetect._cli.context import CliContext
from scenedetect._cli.controller import run_scenedetect

from scenedetect.platform import logging_redirect_tqdm, FakeTqdmLoggingRedirect
from scenedetect.platform import FakeTqdmLoggingRedirect, logging_redirect_tqdm


def main():
Expand Down
9 changes: 4 additions & 5 deletions scenedetect/_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# PySceneDetect: Python-Based Video Scene Detector
# -------------------------------------------------------------------
Expand Down Expand Up @@ -27,17 +26,17 @@
import click

import scenedetect
from scenedetect._cli.config import CHOICE_MAP, CONFIG_FILE_PATH, CONFIG_MAP
from scenedetect._cli.context import USER_CONFIG, CliContext
from scenedetect.backends import AVAILABLE_BACKENDS
from scenedetect.detectors import (
AdaptiveDetector,
ContentDetector,
HashDetector,
HistogramDetector,
ThresholdDetector,
)
from scenedetect.backends import AVAILABLE_BACKENDS
from scenedetect.platform import get_system_version_info
from scenedetect._cli.config import CHOICE_MAP, CONFIG_FILE_PATH, CONFIG_MAP
from scenedetect._cli.context import CliContext, USER_CONFIG

_PROGRAM_VERSION = scenedetect.__version__
"""Used to avoid name conflict with named `scenedetect` command below."""
Expand Down Expand Up @@ -345,7 +344,7 @@ def help_command(ctx: click.Context, command_name: str):
parent_command = ctx.parent.command
all_commands = set(parent_command.list_commands(ctx))
if command_name is not None:
if not command_name in all_commands:
if command_name not in all_commands:
error_strs = [
"unknown command. List of valid commands:",
" %s" % ", ".join(sorted(all_commands)),
Expand Down
13 changes: 6 additions & 7 deletions scenedetect/_cli/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# PySceneDetect: Python-Based Video Scene Detector
# -------------------------------------------------------------------
Expand All @@ -15,12 +14,12 @@
possible and re-used by the CLI so that there is one source of truth.
"""

from abc import ABC, abstractmethod
from configparser import ConfigParser, ParsingError
from enum import Enum
import logging
import os
import os.path
from abc import ABC, abstractmethod
from configparser import ConfigParser, ParsingError
from enum import Enum
from typing import Any, AnyStr, Dict, List, Optional, Tuple, Union

from platformdirs import user_config_dir
Expand Down Expand Up @@ -408,11 +407,11 @@ def _validate_structure(config: ConfigParser) -> List[str]:
"""
errors: List[str] = []
for section in config.sections():
if not section in CONFIG_MAP.keys():
if section not in CONFIG_MAP.keys():
errors.append("Unsupported config section: [%s]" % (section))
continue
for option_name, _ in config.items(section):
if not option_name in CONFIG_MAP[section].keys():
if option_name not in CONFIG_MAP[section].keys():
errors.append("Unsupported config option in [%s]: %s" % (section, option_name))
return errors

Expand Down Expand Up @@ -555,7 +554,7 @@ def _load_from_disk(self, path=None):
# Try to load and parse the config file at `path`.
config = ConfigParser()
try:
with open(path, "r") as config_file:
with open(path) as config_file:
config_file_contents = config_file.read()
config.read_string(config_file_contents, source=path)
except ParsingError as ex:
Expand Down
37 changes: 18 additions & 19 deletions scenedetect/_cli/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# PySceneDetect: Python-Based Video Scene Detector
# -------------------------------------------------------------------
Expand All @@ -19,29 +18,29 @@
import click

import scenedetect # Required to access __version__
from scenedetect import open_video, AVAILABLE_BACKENDS
from scenedetect import AVAILABLE_BACKENDS, open_video
from scenedetect._cli.config import (
CHOICE_MAP,
DEFAULT_JPG_QUALITY,
DEFAULT_WEBP_QUALITY,
ConfigLoadFailure,
ConfigRegistry,
TimecodeFormat,
)
from scenedetect.detectors import (
AdaptiveDetector,
ContentDetector,
ThresholdDetector,
HashDetector,
HistogramDetector,
ThresholdDetector,
)
from scenedetect.frame_timecode import FrameTimecode, MAX_FPS_DELTA
from scenedetect.platform import get_and_create_path, get_cv2_imwrite_params, init_logger
from scenedetect.scene_detector import SceneDetector, FlashFilter
from scenedetect.scene_manager import SceneManager, Interpolation
from scenedetect.frame_timecode import MAX_FPS_DELTA, FrameTimecode
from scenedetect.platform import get_cv2_imwrite_params, init_logger
from scenedetect.scene_detector import FlashFilter, SceneDetector
from scenedetect.scene_manager import Interpolation, SceneManager
from scenedetect.stats_manager import StatsManager
from scenedetect.video_splitter import is_mkvmerge_available, is_ffmpeg_available
from scenedetect.video_stream import VideoStream, VideoOpenFailure, FrameRateUnavailable
from scenedetect._cli.config import (
ConfigRegistry,
ConfigLoadFailure,
TimecodeFormat,
CHOICE_MAP,
DEFAULT_JPG_QUALITY,
DEFAULT_WEBP_QUALITY,
)
from scenedetect.video_splitter import is_ffmpeg_available, is_mkvmerge_available
from scenedetect.video_stream import FrameRateUnavailable, VideoOpenFailure, VideoStream

logger = logging.getLogger("pyscenedetect")

Expand Down Expand Up @@ -742,7 +741,7 @@ def handle_save_images(

self.image_extension = "jpg" if jpeg else "png" if png else "webp"
valid_params = get_cv2_imwrite_params()
if not self.image_extension in valid_params or valid_params[self.image_extension] is None:
if self.image_extension not in valid_params or valid_params[self.image_extension] is None:
error_strs = [
"Image encoder type `%s` not supported." % self.image_extension.upper(),
"The specified encoder type could not be found in the current OpenCV module.",
Expand Down Expand Up @@ -860,7 +859,7 @@ def _open_video_stream(
if backend is None:
backend = self.config.get_value("global", "backend")
else:
if not backend in AVAILABLE_BACKENDS:
if backend not in AVAILABLE_BACKENDS:
raise click.BadParameter(
"Specified backend %s is not available on this system!" % backend,
param_hint="-b/--backend",
Expand Down
13 changes: 6 additions & 7 deletions scenedetect/_cli/controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# PySceneDetect: Python-Based Video Scene Detector
# -------------------------------------------------------------------
Expand All @@ -15,10 +14,11 @@
import csv
import logging
import os
from string import Template
import time
import typing as ty
from string import Template

from scenedetect._cli.context import CliContext, check_split_video_requirements
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.platform import get_and_create_path
from scenedetect.scene_manager import (
Expand All @@ -27,9 +27,8 @@
write_scene_list,
write_scene_list_html,
)
from scenedetect.video_splitter import split_video_mkvmerge, split_video_ffmpeg
from scenedetect.video_splitter import split_video_ffmpeg, split_video_mkvmerge
from scenedetect.video_stream import SeekError
from scenedetect._cli.context import CliContext, check_split_video_requirements

logger = logging.getLogger("pyscenedetect")

Expand Down Expand Up @@ -176,7 +175,7 @@ def _list_scenes(context: CliContext, scene_list: SceneList, cut_list: CutList)
context.scene_list_dir if context.scene_list_dir is not None else context.output_dir,
)
logger.info("Writing scene list to CSV file:\n %s", scene_list_path)
with open(scene_list_path, "wt") as scene_list_file:
with open(scene_list_path, "w") as scene_list_file:
write_scene_list(
output_csv_file=scene_list_file,
scene_list=scene_list,
Expand Down Expand Up @@ -316,10 +315,10 @@ def _load_scenes(context: CliContext) -> ty.Tuple[SceneList, CutList]:
assert context.load_scenes_input
assert os.path.exists(context.load_scenes_input)

with open(context.load_scenes_input, "r") as input_file:
with open(context.load_scenes_input) as input_file:
file_reader = csv.reader(input_file)
csv_headers = next(file_reader)
if not context.load_scenes_column_name in csv_headers:
if context.load_scenes_column_name not in csv_headers:
csv_headers = next(file_reader)
# Check to make sure column headers are present
if context.load_scenes_column_name not in csv_headers:
Expand Down
1 change: 0 additions & 1 deletion scenedetect/_thirdparty/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# PySceneDetect: Python-Based Video Scene Detector
# -------------------------------------------------------------------
Expand Down
Loading

0 comments on commit e4a56b7

Please sign in to comment.