Skip to content

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Visser committed Jan 16, 2025
1 parent f00e9df commit 437e663
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
30 changes: 15 additions & 15 deletions nginx_config_reloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import sys
import threading
import time
from typing import Optional
from pathlib import Path
from typing import Optional

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from dasbus.loop import EventLoop
from dasbus.signal import Signal
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

from nginx_config_reloader.copy_files import safe_copy_files
from nginx_config_reloader.dbus.common import NGINX_CONFIG_RELOADER, SYSTEM_BUS
Expand Down Expand Up @@ -105,8 +105,10 @@ def on_modified(self, event):

def on_any_event(self, event):
"""Triggered by inotify when watched dir is moved or deleted"""
if event.is_directory and event.event_type in ['moved', 'deleted']:
self.logger.warning(f"Directory {event.src_path} has been {event.event_type}.")
if event.is_directory and event.event_type in ["moved", "deleted"]:
self.logger.warning(
f"Directory {event.src_path} has been {event.event_type}."
)
raise ListenTargetTerminated

def handle_event(self, event):
Expand All @@ -117,13 +119,15 @@ def handle_event(self, event):
or file_path.name.endswith("~")
):
return
if (event.is_directory):

if event.is_directory:
return

basename = os.path.basename(event.src_path)
if not any(fnmatch.fnmatch(basename, pat) for pat in WATCH_IGNORE_FILES):
self.logger.debug(f"{event.event_type.upper()} detected on {event.src_path}")
self.logger.debug(
f"{event.event_type.upper()} detected on {event.src_path}"
)
self.dirty = True
# Additional handling if necessary

Expand Down Expand Up @@ -329,10 +333,7 @@ def reload(self, send_signal=True):
def start_observer(self):
self.observer = Observer()
self.observer.schedule(
self,
self.dir_to_watch,
recursive=True,
follow_symlink=True
self, self.dir_to_watch, recursive=True, follow_symlink=True
)
self.observer.start()

Expand All @@ -341,6 +342,7 @@ def stop_observer(self):
self.observer.join()
sys.exit()


class ListenTargetTerminated(BaseException):
pass

Expand Down Expand Up @@ -411,7 +413,6 @@ def wait_loop(
)
time.sleep(5)


try:
logger.info(f"Listening for changes to {dir_to_watch}")
nginx_config_changed_handler.start_observer()
Expand All @@ -425,7 +426,6 @@ def wait_loop(
except KeyboardInterrupt:
logger.info("Shutting down observer.")
nginx_config_changed_handler.stop_observer()



def as_unprivileged_user():
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ black==23.1.0
pre-commit==2.21.0
pygobject
pygobject-stubs
dasbus==1.7
dasbus==1.7
17 changes: 4 additions & 13 deletions tests/test_watchdog_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@
from tempfile import NamedTemporaryFile, mkdtemp

import mock
from watchdog.events import (
DirCreatedEvent,
DirDeletedEvent,
DirMovedEvent,
FileCreatedEvent,
FileDeletedEvent,
FileModifiedEvent,
FileMovedEvent,
)
from watchdog.events import DirCreatedEvent, FileDeletedEvent, FileMovedEvent

import nginx_config_reloader

Expand Down Expand Up @@ -50,9 +42,7 @@ def test_that_handle_event_is_called_when_a_file_is_removed(self):

def test_that_handle_event_is_called_when_a_file_is_moved_in(self):
with NamedTemporaryFile(delete=False) as f:
event = FileMovedEvent(
f.name, os.path.join(self.dir, "newfile")
)
event = FileMovedEvent(f.name, os.path.join(self.dir, "newfile"))
self.handler.on_moved(event)

self.assertEqual(len(self.handle_event.mock_calls), 1)
Expand All @@ -78,6 +68,7 @@ def test_that_handle_event_is_called_when_a_file_is_renamed(self):

self.assertGreaterEqual(len(self.handle_event.mock_calls), 1)


class TestWatchdogRecursiveCallbacks(TestWatchdogCallbacks):
# Run all callback tests on a subdir
def setUp(self):
Expand All @@ -95,4 +86,4 @@ def setUp(self):
self.handler.observer = self.observer

def tearDown(self):
shutil.rmtree(self.rootdir, ignore_errors=True)
shutil.rmtree(self.rootdir, ignore_errors=True)

0 comments on commit 437e663

Please sign in to comment.