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