Compare commits
2 Commits
7bab8a9436
...
352af7da14
Author | SHA1 | Date | |
---|---|---|---|
352af7da14 | |||
f4d9c37687 |
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@ -15,7 +15,9 @@
|
||||
"${workspaceFolder}/tests/resources/groups.csv",
|
||||
"target",
|
||||
"replaced",
|
||||
"-d"
|
||||
"-d",
|
||||
"-e",
|
||||
"abc"
|
||||
],
|
||||
"justMyCode": true
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ def rename(
|
||||
current: str,
|
||||
become: str,
|
||||
dry: bool,
|
||||
extension: str,
|
||||
keep_extension: bool,
|
||||
):
|
||||
replacement_dict = {}
|
||||
@ -54,7 +55,7 @@ def rename(
|
||||
logging.warning(
|
||||
'Group "%s" was not matched to any row in the provided CSV. '
|
||||
"Skipping...",
|
||||
match.group(1)
|
||||
match.group(1),
|
||||
)
|
||||
continue
|
||||
original = subitem_path
|
||||
@ -62,6 +63,8 @@ def rename(
|
||||
os.path.dirname(subitem_path),
|
||||
re.sub(match.re, replacement_dict[match.group(1)], subitem.strip()),
|
||||
)
|
||||
if extension:
|
||||
objective += ("." if not extension.startswith(".") else "") + extension
|
||||
if keep_extension:
|
||||
objective += os.path.splitext(subitem_path)[1]
|
||||
logging.info(f'Will rename "{original}" to "{os.path.basename(objective)}"')
|
||||
@ -80,14 +83,20 @@ def rename(
|
||||
def run(args):
|
||||
candidates = find_all_candidates(args.input_dir, args.regex, args.recursive)
|
||||
rename(
|
||||
args.csv, candidates, args.current, args.become, args.dry, args.keep_extension
|
||||
args.csv,
|
||||
candidates,
|
||||
args.current,
|
||||
args.become,
|
||||
args.dry,
|
||||
args.extension,
|
||||
args.keep_extension,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
program_name = "renamebycsv"
|
||||
argparser = argparse.ArgumentParser(
|
||||
program_name, "Rename all files by using a CSV as a dictionary."
|
||||
program_name, description="Rename all files by using a CSV as a dictionary."
|
||||
)
|
||||
argparser.add_argument(
|
||||
"input_dir",
|
||||
@ -116,13 +125,13 @@ def main():
|
||||
argparser.add_argument(
|
||||
"-r",
|
||||
"--recursive",
|
||||
help="Perform renaming action recursively",
|
||||
help="Perform renaming action recursively.",
|
||||
action="store_true",
|
||||
)
|
||||
argparser.add_argument(
|
||||
"-f",
|
||||
"--force",
|
||||
help="Overwrite files if file already exists",
|
||||
help="Overwrite files if file already exists.",
|
||||
action="store_true",
|
||||
)
|
||||
argparser.add_argument(
|
||||
@ -131,17 +140,28 @@ def main():
|
||||
argparser.add_argument(
|
||||
"-V",
|
||||
"--verbosity",
|
||||
help="Set the logging verbosity",
|
||||
help="Set the logging verbosity.",
|
||||
required=False,
|
||||
type=str,
|
||||
default="INFO",
|
||||
)
|
||||
argparser.add_argument(
|
||||
"-e",
|
||||
"--extension",
|
||||
help='Sets the new file extension after the renaming. Use empty string ("") '
|
||||
"to not add extension. Will use empty string by default.",
|
||||
type=str,
|
||||
default="",
|
||||
required=False,
|
||||
)
|
||||
argparser.add_argument(
|
||||
"-k",
|
||||
"--keep-extension",
|
||||
help="Keeps the original file's extension by appending it to the end of the "
|
||||
"name defined by the CSV.",
|
||||
help="Keeps the OS recognized extension from the original filename. Will append "
|
||||
'to end of argument given by "-e" or "--extension".',
|
||||
action="store_true",
|
||||
default=False,
|
||||
required=False,
|
||||
)
|
||||
|
||||
args = argparser.parse_args()
|
||||
|
Loading…
Reference in New Issue
Block a user