Skip to content

Commit

Permalink
Add isort and ruff linter (#6575)
Browse files Browse the repository at this point in the history
* Add isort and ruff linter

Both linters are pretty common among modern python code bases.

The isort tool provides stable sorting and grouping, as well as pruning
of unused imports.

Ruff is a modern linter, that is very fast due to being written in rust.
It can detect many common issues in a python codebase.

Removes the pylint dev requirement, since ruff replaces it.

* treewide: fix issues detected by ruff

* treewide: fix bare except clauses

* .devcontainer: Set up isort

* treewide: optimize imports

* treewide: apply black

* treewide: make regex patterns raw strings

This is necessary for escape sequences to be properly recognized.
  • Loading branch information
mweinelt committed May 29, 2023
1 parent 1e17dba commit ab50d0b
Show file tree
Hide file tree
Showing 71 changed files with 338 additions and 451 deletions.
6 changes: 5 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"mikestead.dotenv",
"csstools.postcss",
"blanu.vscode-styled-jsx",
"bradlc.vscode-tailwindcss"
"bradlc.vscode-tailwindcss",
"ms-python.isort"
],
"settings": {
"remote.autoForwardPorts": false,
Expand All @@ -68,6 +69,9 @@
"python.testing.unittestArgs": ["-v", "-s", "./frigate/test"],
"files.trimTrailingWhitespace": true,
"eslint.workingDirectories": ["./web"],
"isort.args": [
"--settings-path=./pyproject.toml"
],
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
Expand Down
14 changes: 10 additions & 4 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ jobs:
python-version: ${{ env.DEFAULT_PYTHON }}
- name: Install requirements
run: |
pip install pip
pip install -r requirements-dev.txt
- name: Lint
python3 -m pip install -U pip
python3 -m pip install -r requirements-dev.txt
- name: Check black
run: |
black --check --diff frigate migrations docker *.py
- name: Check isort
run: |
isort --check --diff frigate migrations docker *.py
- name: Check ruff
run: |
python3 -m black frigate --check
ruff check frigate migrations docker *.py
python_tests:
runs-on: ubuntu-latest
Expand Down
10 changes: 5 additions & 5 deletions benchmark.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
from statistics import mean
import datetime
import multiprocessing as mp
from statistics import mean

import numpy as np
import datetime

from frigate.config import DetectorTypeEnum
from frigate.object_detection import (
LocalObjectDetector,
ObjectDetectProcess,
RemoteObjectDetector,
load_labels,
Expand Down Expand Up @@ -53,7 +53,7 @@ def start(id, num_detections, detection_queue, event):
frame_times = []
for x in range(0, num_detections):
start_frame = datetime.datetime.now().timestamp()
detections = object_detector.detect(my_frame)
object_detector.detect(my_frame)
frame_times.append(datetime.datetime.now().timestamp() - start_frame)

duration = datetime.datetime.now().timestamp() - start
Expand Down
7 changes: 5 additions & 2 deletions docker/rootfs/usr/local/go2rtc/create_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import json
import os
import sys

import yaml

sys.path.insert(0, "/opt/frigate")
from frigate.const import BIRDSEYE_PIPE, BTBN_PATH
from frigate.ffmpeg_presets import parse_preset_hardware_acceleration_encode
from frigate.const import BIRDSEYE_PIPE, BTBN_PATH # noqa: E402
from frigate.ffmpeg_presets import ( # noqa: E402
parse_preset_hardware_acceleration_encode,
)

sys.path.remove("/opt/frigate")

Expand Down
7 changes: 4 additions & 3 deletions frigate/__main__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import faulthandler
import threading

from flask import cli

from frigate.app import FrigateApp

faulthandler.enable()
import threading

threading.current_thread().name = "frigate"

from frigate.app import FrigateApp

cli.show_server_banner = lambda *x: None

if __name__ == "__main__":
Expand Down
22 changes: 11 additions & 11 deletions frigate/app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import logging
import multiprocessing as mp
from multiprocessing.queues import Queue
from multiprocessing.synchronize import Event as MpEvent
import os
import shutil
import signal
import sys
from typing import Optional
import traceback
from multiprocessing.queues import Queue
from multiprocessing.synchronize import Event as MpEvent
from types import FrameType
import psutil
from typing import Optional

import traceback
import psutil
from peewee_migrate import Router
from playhouse.sqlite_ext import SqliteExtDatabase
from playhouse.sqliteq import SqliteQueueDatabase
Expand All @@ -27,13 +27,13 @@
MODEL_CACHE_DIR,
RECORD_DIR,
)
from frigate.object_detection import ObjectDetectProcess
from frigate.events.cleanup import EventCleanup
from frigate.events.external import ExternalEventProcessor
from frigate.events.maintainer import EventProcessor
from frigate.http import create_app
from frigate.log import log_process, root_configurer
from frigate.models import Event, Recordings, Timeline
from frigate.object_detection import ObjectDetectProcess
from frigate.object_processing import TrackedObjectProcessor
from frigate.output import output_frames
from frigate.plus import PlusApi
Expand All @@ -42,10 +42,10 @@
from frigate.stats import StatsEmitter, stats_init
from frigate.storage import StorageMaintainer
from frigate.timeline import TimelineProcessor
from frigate.types import CameraMetricsTypes, RecordMetricsTypes
from frigate.version import VERSION
from frigate.video import capture_camera, track_camera
from frigate.watchdog import FrigateWatchdog
from frigate.types import CameraMetricsTypes, RecordMetricsTypes

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -133,10 +133,10 @@ def set_log_levels(self) -> None:
for log, level in self.config.logger.logs.items():
logging.getLogger(log).setLevel(level.value.upper())

if not "werkzeug" in self.config.logger.logs:
if "werkzeug" not in self.config.logger.logs:
logging.getLogger("werkzeug").setLevel("ERROR")

if not "ws4py" in self.config.logger.logs:
if "ws4py" not in self.config.logger.logs:
logging.getLogger("ws4py").setLevel("ERROR")

def init_queues(self) -> None:
Expand Down Expand Up @@ -294,7 +294,7 @@ def start_detected_frames_processor(self) -> None:
def start_video_output_processor(self) -> None:
output_processor = mp.Process(
target=output_frames,
name=f"output_processor",
name="output_processor",
args=(
self.config,
self.video_output_queue,
Expand Down Expand Up @@ -467,7 +467,7 @@ def receiveSignal(signalNumber: int, frame: Optional[FrameType]) -> None:
self.stop()

def stop(self) -> None:
logger.info(f"Stopping...")
logger.info("Stopping...")
self.stop_event.set()

for detector in self.detectors.values():
Expand Down
15 changes: 6 additions & 9 deletions frigate/comms/dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
"""Handle communication between Frigate and other applications."""

import logging

from typing import Any, Callable

from abc import ABC, abstractmethod
from typing import Any, Callable

from frigate.config import FrigateConfig
from frigate.ptz import OnvifController, OnvifCommandEnum
from frigate.ptz import OnvifCommandEnum, OnvifController
from frigate.types import CameraMetricsTypes, RecordMetricsTypes
from frigate.util import restart_frigate


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -72,15 +69,15 @@ def _receive(self, topic: str, payload: str) -> None:
camera_name = topic.split("/")[-3]
command = topic.split("/")[-2]
self._camera_settings_handlers[command](camera_name, payload)
except IndexError as e:
except IndexError:
logger.error(f"Received invalid set command: {topic}")
return
elif topic.endswith("ptz"):
try:
# example /cam_name/ptz payload=MOVE_UP|MOVE_DOWN|STOP...
camera_name = topic.split("/")[-2]
self._on_ptz_command(camera_name, payload)
except IndexError as e:
except IndexError:
logger.error(f"Received invalid ptz command: {topic}")
return
elif topic == "restart":
Expand Down Expand Up @@ -128,7 +125,7 @@ def _on_motion_command(self, camera_name: str, payload: str) -> None:
elif payload == "OFF":
if self.camera_metrics[camera_name]["detection_enabled"].value:
logger.error(
f"Turning off motion is not allowed when detection is enabled."
"Turning off motion is not allowed when detection is enabled."
)
return

Expand Down Expand Up @@ -196,7 +193,7 @@ def _on_recordings_command(self, camera_name: str, payload: str) -> None:
if payload == "ON":
if not self.config.cameras[camera_name].record.enabled_in_config:
logger.error(
f"Recordings must be enabled in the config to be turned on via MQTT."
"Recordings must be enabled in the config to be turned on via MQTT."
)
return

Expand Down
12 changes: 5 additions & 7 deletions frigate/comms/mqtt.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import logging
import threading

from typing import Any, Callable

import paho.mqtt.client as mqtt

from frigate.comms.dispatcher import Communicator
from frigate.config import FrigateConfig


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -177,10 +175,10 @@ def _start(self) -> None:
f"{self.mqtt_config.topic_prefix}/restart", self.on_mqtt_command
)

if not self.mqtt_config.tls_ca_certs is None:
if self.mqtt_config.tls_ca_certs is not None:
if (
not self.mqtt_config.tls_client_cert is None
and not self.mqtt_config.tls_client_key is None
self.mqtt_config.tls_client_cert is not None
and self.mqtt_config.tls_client_key is not None
):
self.client.tls_set(
self.mqtt_config.tls_ca_certs,
Expand All @@ -189,9 +187,9 @@ def _start(self) -> None:
)
else:
self.client.tls_set(self.mqtt_config.tls_ca_certs)
if not self.mqtt_config.tls_insecure is None:
if self.mqtt_config.tls_insecure is not None:
self.client.tls_insecure_set(self.mqtt_config.tls_insecure)
if not self.mqtt_config.user is None:
if self.mqtt_config.user is not None:
self.client.username_pw_set(
self.mqtt_config.user, password=self.mqtt_config.password
)
Expand Down
8 changes: 3 additions & 5 deletions frigate/comms/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import json
import logging
import threading

from typing import Callable

from wsgiref.simple_server import make_server

from ws4py.server.wsgirefserver import (
WebSocketWSGIHandler,
WebSocketWSGIRequestHandler,
Expand All @@ -18,7 +17,6 @@
from frigate.comms.dispatcher import Communicator
from frigate.config import FrigateConfig


logger = logging.getLogger(__name__)


Expand All @@ -45,7 +43,7 @@ def received_message(self, message: WebSocket.received_message) -> None:
"topic": json_message.get("topic"),
"payload": json_message.get("payload"),
}
except Exception as e:
except Exception:
logger.warning(
f"Unable to parse websocket message as valid json: {message.data.decode('utf-8')}"
)
Expand Down Expand Up @@ -82,7 +80,7 @@ def publish(self, topic: str, payload: str, _: bool) -> None:
"payload": payload,
}
)
except Exception as e:
except Exception:
# if the payload can't be decoded don't relay to clients
logger.debug(f"payload for {topic} wasn't text. Skipping...")
return
Expand Down
Loading

0 comments on commit ab50d0b

Please sign in to comment.