11"""
22A file watching service to rename movie files and move them
3- to relevant locations after match the file against the porndb .
3+ to relevant locations after match the file against the theporndb .
44"""
55
6- from contextlib import suppress
76import os
87import shutil
98import sys
109import tempfile
1110import time
11+ from contextlib import suppress
1212from pathlib import Path
13+ from platform import system
1314from queue import Queue
1415from threading import Thread
1516from typing import Optional
1920from watchdog .events import EVENT_TYPE_MODIFIED , EVENT_TYPE_MOVED , FileSystemEvent , PatternMatchingEventHandler
2021from watchdog .observers .polling import PollingObserver
2122
23+ from namer .command import Command , gather_target_files_from_dir , is_interesting_movie , make_command_relative_to , move_command_files
2224from namer .configuration import NamerConfig
2325from namer .configuration_utils import verify_configuration
24- from namer .command import gather_target_files_from_dir , is_interesting_movie , make_command_relative_to , move_command_files , Command
2526from namer .metadataapi import get_user_info
2627from namer .name_formatter import PartialFormatter
2728from namer .namer import process_file
2829from namer .web .server import NamerWebServer
2930
3031
32+ def __is_file_in_use_windows (file : Path ):
33+ try :
34+ file .rename (file )
35+ except PermissionError :
36+ return True
37+ else :
38+ return False
39+
40+
41+ def __is_file_in_use_unix (file : Path ):
42+ try :
43+ # pylint: disable=consider-using-with
44+ buffered_reader = open (file , mode = 'rb' ) # noqa: SIM115
45+ buffered_reader .close ()
46+ except PermissionError :
47+ return True
48+ else :
49+ return False
50+
51+
52+ def is_file_in_use (file : Optional [Path ]):
53+ if not file or not file .exists ():
54+ return False
55+
56+ if system () == 'Windows' :
57+ return __is_file_in_use_windows (file )
58+ else :
59+ return __is_file_in_use_unix (file )
60+
61+
3162def done_copying (file : Optional [Path ]) -> bool :
3263 """
3364 Determines if a file is being copied by checking its size in 2 second
@@ -36,14 +67,8 @@ def done_copying(file: Optional[Path]) -> bool:
3667 if not file or not file .exists ():
3768 return False
3869
39- while True :
40- try :
41- # pylint: disable=consider-using-with
42- buffered_reader = open (file , mode = 'rb' ) # noqa: SIM115
43- buffered_reader .close ()
44- break
45- except PermissionError :
46- time .sleep (0.2 )
70+ while is_file_in_use (file ):
71+ time .sleep (2 )
4772
4873 return True
4974
@@ -109,7 +134,7 @@ def on_any_event(self, event: FileSystemEvent):
109134 return
110135
111136 relative_path = str (path .relative_to (self .__namer_config .watch_dir ))
112- if not self .__namer_config .ignored_dir_regex .search (relative_path ) and done_copying ( path ) and is_interesting_movie (path , self .__namer_config ):
137+ if not self .__namer_config .ignored_dir_regex .search (relative_path ) and is_interesting_movie (path , self .__namer_config ) and done_copying ( path ):
113138 logger .info ('watchdog process called for {}' , relative_path )
114139
115140 # Extra wait time in case other files are copies in as well.
@@ -259,7 +284,7 @@ def start(self):
259284 return
260285
261286 relative_path = str (file .relative_to (self .__namer_config .watch_dir ))
262- if not self .__namer_config .ignored_dir_regex .search (relative_path ) and done_copying ( file ) and is_interesting_movie (file , self .__namer_config ):
287+ if not self .__namer_config .ignored_dir_regex .search (relative_path ) and is_interesting_movie (file , self .__namer_config ) and done_copying ( file ):
263288 self .__event_handler .prepare_file_for_processing (file )
264289
265290 def stop (self ):
@@ -334,4 +359,3 @@ def main(config: NamerConfig):
334359 logger .add (sys .stdout , format = config .console_format , level = level , diagnose = config .diagnose_errors )
335360
336361 create_watcher (config ).run ()
337-
0 commit comments