Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions namer/watchdog.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""
A file watching service to rename movie files and move them
to relevant locations after match the file against the porndb.
to relevant locations after match the file against the theporndb.
"""

from contextlib import suppress
import os
import shutil
import sys
import tempfile
import time
from contextlib import suppress
from pathlib import Path
from platform import system
from queue import Queue
from threading import Thread
from typing import Optional
Expand All @@ -19,15 +20,45 @@
from watchdog.events import EVENT_TYPE_MODIFIED, EVENT_TYPE_MOVED, FileSystemEvent, PatternMatchingEventHandler
from watchdog.observers.polling import PollingObserver

from namer.command import Command, gather_target_files_from_dir, is_interesting_movie, make_command_relative_to, move_command_files
from namer.configuration import NamerConfig
from namer.configuration_utils import verify_configuration
from namer.command import gather_target_files_from_dir, is_interesting_movie, make_command_relative_to, move_command_files, Command
from namer.metadataapi import get_user_info
from namer.name_formatter import PartialFormatter
from namer.namer import process_file
from namer.web.server import NamerWebServer


def __is_file_in_use_windows(file: Path):
try:
file.rename(file)
except PermissionError:
return True
else:
return False


def __is_file_in_use_unix(file: Path):
try:
# pylint: disable=consider-using-with
buffered_reader = open(file, mode='rb') # noqa: SIM115
buffered_reader.close()
except PermissionError:
return True
else:
return False


def is_file_in_use(file: Optional[Path]):
if not file or not file.exists():
return False

if system() == 'Windows':
return __is_file_in_use_windows(file)
else:
return __is_file_in_use_unix(file)


def done_copying(file: Optional[Path]) -> bool:
"""
Determines if a file is being copied by checking its size in 2 second
Expand All @@ -36,14 +67,8 @@ def done_copying(file: Optional[Path]) -> bool:
if not file or not file.exists():
return False

while True:
try:
# pylint: disable=consider-using-with
buffered_reader = open(file, mode='rb') # noqa: SIM115
buffered_reader.close()
break
except PermissionError:
time.sleep(0.2)
while is_file_in_use(file):
time.sleep(2)

return True

Expand Down Expand Up @@ -109,7 +134,7 @@ def on_any_event(self, event: FileSystemEvent):
return

relative_path = str(path.relative_to(self.__namer_config.watch_dir))
if not self.__namer_config.ignored_dir_regex.search(relative_path) and done_copying(path) and is_interesting_movie(path, self.__namer_config):
if not self.__namer_config.ignored_dir_regex.search(relative_path) and is_interesting_movie(path, self.__namer_config) and done_copying(path):
logger.info('watchdog process called for {}', relative_path)

# Extra wait time in case other files are copies in as well.
Expand Down Expand Up @@ -259,7 +284,7 @@ def start(self):
return

relative_path = str(file.relative_to(self.__namer_config.watch_dir))
if not self.__namer_config.ignored_dir_regex.search(relative_path) and done_copying(file) and is_interesting_movie(file, self.__namer_config):
if not self.__namer_config.ignored_dir_regex.search(relative_path) and is_interesting_movie(file, self.__namer_config) and done_copying(file):
self.__event_handler.prepare_file_for_processing(file)

def stop(self):
Expand Down Expand Up @@ -334,4 +359,3 @@ def main(config: NamerConfig):
logger.add(sys.stdout, format=config.console_format, level=level, diagnose=config.diagnose_errors)

create_watcher(config).run()

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
"@popperjs/core": "^2.11.8",
"bootstrap": "^5.3.6",
"bootstrap-icons": "^1.13.1",
"datatables.net": "^2.3.0",
"datatables.net-bs5": "^2.3.0",
"datatables.net": "^2.3.1",
"datatables.net-bs5": "^2.3.1",
"datatables.net-buttons": "^3.2.3",
"datatables.net-buttons-bs5": "^3.2.3",
"datatables.net-colreorder": "^2.1.0",
"datatables.net-colreorder-bs5": "^2.1.0",
"datatables.net-fixedheader": "^4.0.1",
"datatables.net-fixedheader-bs5": "^4.0.1",
"datatables.net-fixedheader": "^4.0.2",
"datatables.net-fixedheader-bs5": "^4.0.2",
"datatables.net-responsive": "^3.0.4",
"datatables.net-responsive-bs5": "^3.0.4",
"jquery": "^3.7.1",
Expand All @@ -32,26 +32,26 @@
"@babel/core": "^7.27.1",
"@babel/preset-env": "^7.27.2",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.26.0",
"@eslint/js": "^9.27.0",
"babel-loader": "^10.0.0",
"copy-webpack-plugin": "^13.0.0",
"css-loader": "^7.1.2",
"css-minimizer-webpack-plugin": "^7.0.2",
"eslint": "^9.26.0",
"eslint": "^9.27.0",
"eslint-config-standard": "^17.1.0",
"file-loader": "^6.2.0",
"globals": "^16.1.0",
"html-minimizer-webpack-plugin": "^5.0.2",
"husky": "^9.1.7",
"lint-staged": "^15.5.2",
"lint-staged": "^16.0.0",
"mini-css-extract-plugin": "^2.9.2",
"postcss": "^8.5.3",
"postcss-loader": "^8.1.1",
"postcss-preset-env": "^10.1.6",
"sass": "^1.87.0",
"sass": "^1.89.0",
"sass-loader": "^16.0.5",
"terser-webpack-plugin": "^5.3.14",
"webpack": "^5.99.8",
"webpack": "^5.99.9",
"webpack-cli": "^6.0.1"
},
"engines": {
Expand Down
Loading
Loading