23
23
--suffix SUFFIX Adds a sufix string to every output filename
24
24
--separator {-,_, } Choose how the words in the output filename should be separated by. Defaults to a space
25
25
--split Replace non-alphanumeric characters with the value provided to --separator (does not apply on extension)
26
+ -r, --recursive Recursively runs this script on all directories found.
26
27
-e regexp, --regexp regexp
27
28
Filter results with regular expression, instead of selecting all files in directory
28
29
--prune prune Remove any characters from the original filename before renaming it (does not apply on file extension).
41
42
import re
42
43
43
44
44
- CURRENT_VERSION = '0.1.0 '
45
- SEPARATOR_CHARS = re .compile (r'[\+=\-_\.,;:<>()\[\]\s] ' )
45
+ CURRENT_VERSION = '0.1.1 '
46
+ SEPARATOR_CHARS = re .compile (r'[\+=\-_\.,;:<>\s\']+ ' )
46
47
47
48
48
49
def main ():
49
50
args = parse_arguments ()
50
- setup_logger (args . verbose )
51
+ setup_logger (args )
51
52
print_arguments (args )
52
53
53
54
entrypoint = Path (args .input_dir ).resolve ()
54
55
55
- files = select_files (entrypoint , args .pattern )
56
-
56
+ files = select_files (entrypoint , args .recursive , args . pattern )
57
+
57
58
renamed_files = rename_files (files , args )
58
59
59
60
if args .dry :
@@ -89,21 +90,21 @@ def rename_files(files, args):
89
90
90
91
if args .title :
91
92
filename = make_pascal_case (filename )
92
-
93
+
93
94
if args .upper :
94
95
filename = filename .upper ()
95
-
96
+
96
97
if args .lower :
97
98
filename = filename .lower ()
98
99
99
100
renamed = Path (args .prefix + filename + ext + args .suffix )
100
101
101
- yield (file , Path .joinpath (args . input_dir , renamed ))
102
+ yield (file , Path .joinpath (file . parent , renamed ))
102
103
103
104
104
105
def make_pascal_case (filename ):
105
106
"""Converts a given string into Pascal Case"""
106
-
107
+
107
108
# Produce "negative" lists of chars and separators, if any
108
109
alpha = [x .title () for x in SEPARATOR_CHARS .split (filename )]
109
110
non_alpha = SEPARATOR_CHARS .findall (filename )
@@ -124,16 +125,22 @@ def prune_filename(filename, prune_char):
124
125
return filename .replace (prune_char , '' )
125
126
126
127
127
- def select_files (dir_path , pattern = None ):
128
+ def select_files (dir_path , recursive = False , pattern = None ):
128
129
"""Matches all files in the provided directory and yields them"""
129
130
130
131
if pattern :
131
132
regexp = re .compile (pattern )
132
133
133
134
for file in Path .iterdir (dir_path ):
134
135
136
+ if file .name .startswith ('.' ):
137
+ continue
138
+
135
139
# Ignore directories and hidden files
136
- if file .is_dir () or file .name .startswith ('.' ):
140
+ if file .is_dir ():
141
+ if recursive :
142
+ for _file in select_files (file .resolve (), recursive , pattern ):
143
+ yield _file
137
144
continue
138
145
139
146
if pattern and not regexp .search (file .name ):
@@ -142,12 +149,18 @@ def select_files(dir_path, pattern = None):
142
149
yield file
143
150
144
151
145
- def setup_logger (verbosity ):
152
+
153
+ def setup_logger (args ):
146
154
"""Defines the handler and formatter for the module's logger instance."""
147
155
156
+ verbosity = args .verbose
157
+
158
+ if args .dry :
159
+ verbosity = max (2 , verbosity )
160
+
148
161
if verbosity == 1 :
149
162
logger .setLevel (logging .INFO )
150
-
163
+
151
164
if verbosity > 1 :
152
165
logger .setLevel (logging .DEBUG )
153
166
@@ -163,7 +176,7 @@ def setup_logger(verbosity):
163
176
164
177
def print_arguments (args ):
165
178
"""Prints the arugments the script uses to run."""
166
-
179
+
167
180
logger .debug (f'Running as user { os .getenv ("USER" )} ' )
168
181
logger .debug (f'Running with arguments: { args } ' )
169
182
logger .info (f'reading from "{ args .input_dir .resolve ()} "' )
@@ -253,6 +266,10 @@ def parse_arguments():
253
266
to --separator (does not apply on extension)""" ,
254
267
action = 'store_true'
255
268
)
269
+ parser .add_argument ('-r' , '--recursive' ,
270
+ help = "Recursively runs this script on all directories found." ,
271
+ action = 'store_true'
272
+ )
256
273
parser .add_argument ('-e' , '--regexp' ,
257
274
help = """Filter results with regular expression, instead of selecting all
258
275
files in directory""" ,
0 commit comments