1- import inotify .adapters
2- import os
1+ """
2+ Module for scanning directories
3+ """
4+
35import logging
4- from pathtraits .pathpair import *
5- from pathtraits .db import *
6+ import os
67import re
78
9+ import inotify .adapters
10+ from pathtraits .pathpair import PathPair
11+ from pathtraits .db import TraitsDB
12+
813logger = logging .getLogger (__name__ )
914
1015yaml_re = re .compile (r"(\.)?(meta)?\.(yaml|yml)$" )
1116
1217
18+ # pylint: disable=W0102
1319def scan_meta_yml (path , pathpairs = []):
20+ """
21+ Scan a directory recursively for meta yml files and return PathPairs.
22+
23+ :param path: Root path to scan
24+ :param pathpairs: list of pathpairs found so far (used for recursion)
25+ """
1426 try :
1527 # faster than os.walk
1628 with os .scandir (path ) as ents :
@@ -26,12 +38,19 @@ def scan_meta_yml(path, pathpairs=[]):
2638 pair = PathPair (object_path , e .path )
2739 pathpairs .append (pair )
2840 return pathpairs
29- except Exception as ex :
30- logger .error (f "skip { path } : { ex } " )
41+ except ( FileNotFoundError , PermissionError , OSError ) as ex :
42+ logger .error ("skip %s: %s" , path , ex )
3143 return pathpairs
3244
3345
3446def batch (path , db_path , verbose ):
47+ """
48+ Update database once, searches for all directories recursively.
49+
50+ :param path: path to scan in batch mode recursively
51+ :param db_path: path to the database
52+ :param verbose: enable verbose logging
53+ """
3554 if verbose :
3655 logging .basicConfig (level = logging .DEBUG )
3756
@@ -44,6 +63,13 @@ def batch(path, db_path, verbose):
4463
4564
4665def watch (path , db_path , verbose ):
66+ """
67+ Update database continiously, watches for new or changed files.
68+
69+ :param path: path to watch recursively
70+ :param db_path: path to the database
71+ :param verbose: enable verbose logging
72+ """
4773 if verbose :
4874 logging .basicConfig (level = logging .DEBUG )
4975
@@ -57,13 +83,13 @@ def watch(path, db_path, verbose):
5783 for event in i .event_gen (yield_nones = False ):
5884 (_ , type_names , dir_path , filename ) = event
5985
60- if not type_names .__contains__ ("IN_CLOSE_WRITE" ):
86+ if not type_names .contains ("IN_CLOSE_WRITE" ):
6187 continue
6288
6389 # watch afor both yml and object files
6490 # yml file might be created first and will be ignored
6591 path = os .path .join (dir_path , filename )
6692 pair = PathPair .find (path )
6793 if pair :
68- logger .debug (f "add pathpair: { pair } " )
94+ logger .debug ("add pathpair: %s" , pair )
6995 db .add_pathpair (pair )
0 commit comments