9
9
10
10
from red_utils .core .constants import JSON_DIR
11
11
12
+ from .constants import VALID_RETURN_TYPES
13
+
12
14
def file_ts (fmt : str = "%Y-%m-%d_%H:%M:%S" ) -> str :
13
15
"""Return a formatted timestamp, useful for prepending to dir/file names."""
14
16
now : str = datetime .now ().strftime (fmt )
@@ -65,6 +67,102 @@ def export_json(
65
67
66
68
67
69
def crawl_dir (
70
+ target : Union [str , Path ] = None ,
71
+ filetype_filter : str | None = None ,
72
+ return_type : str = "all" ,
73
+ ) -> Union [dict [str , list [Path ]], list [Path ]]:
74
+ def validate_target (target : Union [str , Path ] = target ) -> Path :
75
+ if target is None :
76
+ raise ValueError ("Missing a target directory to scan" )
77
+ if isinstance (target , str ):
78
+ target : Path = Path (target )
79
+ if not target .exists ():
80
+ msg = FileNotFoundError (f"Could not find directory: { target } " )
81
+
82
+ raise msg
83
+
84
+ return target
85
+
86
+ def validate_return_type (
87
+ return_type : str = return_type ,
88
+ VALID_RETURN_TYPES : list [str ] = VALID_RETURN_TYPES ,
89
+ ) -> str :
90
+ if return_type is None :
91
+ raise ValueError ("Missing return type" )
92
+ if not isinstance (return_type , str ):
93
+ raise TypeError (
94
+ f"Invalid type for return_type: ({ type (return_type )} ). Must be one of { VALID_RETURN_TYPES } "
95
+ )
96
+ if return_type not in VALID_RETURN_TYPES :
97
+ msg = ValueError (
98
+ f"Invalid return type: { return_type } . Must be one of: { VALID_RETURN_TYPES } "
99
+ )
100
+
101
+ raise msg
102
+
103
+ return return_type
104
+
105
+ def _crawl (
106
+ target = target , search_str : str = "**/*" , return_type = return_type
107
+ ) -> Union [dict [str , list [Path ]], list [Path ]]:
108
+ """Run Path crawl."""
109
+ return_obj : dict [str , list [Path ]] = {"files" : [], "dirs" : []}
110
+
111
+ for i in target .glob (search_str ):
112
+ if i .is_file ():
113
+ if return_type in ["all" , "files" ]:
114
+ return_obj ["files" ].append (i )
115
+ else :
116
+ pass
117
+ else :
118
+ if return_type in ["all" , "dirs" ]:
119
+ return_obj ["dirs" ].append (i )
120
+
121
+ match return_type :
122
+ case "all" :
123
+ return return_obj
124
+ case "files" :
125
+ return return_obj ["files" ]
126
+ case "dirs" :
127
+ return return_obj ["dirs" ]
128
+
129
+ if filetype_filter :
130
+ if not isinstance (filetype_filter , str ):
131
+ raise TypeError (
132
+ f"Invalid type for filetype_filter: ({ type (filetype_filter )} ). Must be of type str"
133
+ )
134
+ if not filetype_filter .startswith ("." ):
135
+ filetype_filter : str = f".{ filetype_filter } "
136
+
137
+ search_str : str = f"**/*{ filetype_filter } "
138
+ else :
139
+ search_str : str = "**/*"
140
+
141
+ target : Path = validate_target ()
142
+ return_type : str = validate_return_type ()
143
+
144
+ return_obj = _crawl (target = target , search_str = search_str , return_type = return_type )
145
+
146
+ return return_obj
147
+
148
+ # return_obj: dict[str, list[Path]] = {"files": [], "dirs": []}
149
+
150
+ # for i in target.glob(search_str):
151
+ # if i.is_file():
152
+ # return_obj["files"].append(i)
153
+ # else:
154
+ # return_obj["dirs"].append(i)
155
+
156
+ # match return_type:
157
+ # case "all":
158
+ # return return_obj
159
+ # case "files":
160
+ # return return_obj["files"]
161
+ # case "dirs":
162
+ # return return_obj["dirs"]
163
+
164
+
165
+ def crawl_dir_old (
68
166
in_dir : Union [str , Path ] = None ,
69
167
return_type : str = "all" ,
70
168
ext_filter : str | None = None ,
@@ -88,7 +186,7 @@ def crawl_dir(
88
186
found in path, including in subdirectories. return_obj['dirs'] will be a list of
89
187
dirs and subdirs found during crawl.
90
188
"""
91
- valid_return_types : list [str ] = ["all" , "files" , "dirs" ]
189
+ VALID_RETURN_TYPES : list [str ] = ["all" , "files" , "dirs" ]
92
190
if not return_type :
93
191
return_type = "all"
94
192
@@ -97,9 +195,9 @@ def crawl_dir(
97
195
98
196
return_type = return_type .lower ()
99
197
100
- if return_type not in valid_return_types :
198
+ if return_type not in VALID_RETURN_TYPES :
101
199
raise ValueError (
102
- f"Invalid return type: { return_type } . Must be one of { valid_return_types } "
200
+ f"Invalid return type: { return_type } . Must be one of { VALID_RETURN_TYPES } "
103
201
)
104
202
105
203
if ext_filter is not None :
0 commit comments