Skip to content

Commit d6b66b9

Browse files
committed
Add support to run script recursively
1 parent 534aa9e commit d6b66b9

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

nama_nama.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
--suffix SUFFIX Adds a sufix string to every output filename
2424
--separator {-,_, } Choose how the words in the output filename should be separated by. Defaults to a space
2525
--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.
2627
-e regexp, --regexp regexp
2728
Filter results with regular expression, instead of selecting all files in directory
2829
--prune prune Remove any characters from the original filename before renaming it (does not apply on file extension).
@@ -41,19 +42,19 @@
4142
import re
4243

4344

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\']+')
4647

4748

4849
def main():
4950
args = parse_arguments()
50-
setup_logger(args.verbose)
51+
setup_logger(args)
5152
print_arguments(args)
5253

5354
entrypoint = Path(args.input_dir).resolve()
5455

55-
files = select_files(entrypoint, args.pattern)
56-
56+
files = select_files(entrypoint, args.recursive, args.pattern)
57+
5758
renamed_files = rename_files(files, args)
5859

5960
if args.dry:
@@ -89,21 +90,21 @@ def rename_files(files, args):
8990

9091
if args.title:
9192
filename = make_pascal_case(filename)
92-
93+
9394
if args.upper:
9495
filename = filename.upper()
95-
96+
9697
if args.lower:
9798
filename = filename.lower()
9899

99100
renamed = Path(args.prefix + filename + ext + args.suffix)
100101

101-
yield (file, Path.joinpath(args.input_dir, renamed))
102+
yield (file, Path.joinpath(file.parent, renamed))
102103

103104

104105
def make_pascal_case(filename):
105106
"""Converts a given string into Pascal Case"""
106-
107+
107108
# Produce "negative" lists of chars and separators, if any
108109
alpha = [x.title() for x in SEPARATOR_CHARS.split(filename)]
109110
non_alpha = SEPARATOR_CHARS.findall(filename)
@@ -124,16 +125,22 @@ def prune_filename(filename, prune_char):
124125
return filename.replace(prune_char, '')
125126

126127

127-
def select_files(dir_path, pattern = None):
128+
def select_files(dir_path, recursive = False, pattern = None):
128129
"""Matches all files in the provided directory and yields them"""
129130

130131
if pattern:
131132
regexp = re.compile(pattern)
132133

133134
for file in Path.iterdir(dir_path):
134135

136+
if file.name.startswith('.'):
137+
continue
138+
135139
# 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
137144
continue
138145

139146
if pattern and not regexp.search(file.name):
@@ -142,12 +149,18 @@ def select_files(dir_path, pattern = None):
142149
yield file
143150

144151

145-
def setup_logger(verbosity):
152+
153+
def setup_logger(args):
146154
"""Defines the handler and formatter for the module's logger instance."""
147155

156+
verbosity = args.verbose
157+
158+
if args.dry:
159+
verbosity = max(2, verbosity)
160+
148161
if verbosity == 1:
149162
logger.setLevel(logging.INFO)
150-
163+
151164
if verbosity > 1:
152165
logger.setLevel(logging.DEBUG)
153166

@@ -163,7 +176,7 @@ def setup_logger(verbosity):
163176

164177
def print_arguments(args):
165178
"""Prints the arugments the script uses to run."""
166-
179+
167180
logger.debug(f'Running as user {os.getenv("USER")}')
168181
logger.debug(f'Running with arguments: {args}')
169182
logger.info(f'reading from "{args.input_dir.resolve()}"')
@@ -253,6 +266,10 @@ def parse_arguments():
253266
to --separator (does not apply on extension)""",
254267
action='store_true'
255268
)
269+
parser.add_argument('-r', '--recursive',
270+
help="Recursively runs this script on all directories found.",
271+
action='store_true'
272+
)
256273
parser.add_argument('-e', '--regexp',
257274
help="""Filter results with regular expression, instead of selecting all
258275
files in directory""",

0 commit comments

Comments
 (0)