Added ability to define extension to append
All checks were successful
ydeng/renamebycsv/pipeline/head This commit looks good

This commit is contained in:
Harrison Deng 2023-04-25 09:48:07 -05:00
parent f4d9c37687
commit 352af7da14
2 changed files with 34 additions and 12 deletions

4
.vscode/launch.json vendored
View File

@ -15,7 +15,9 @@
"${workspaceFolder}/tests/resources/groups.csv", "${workspaceFolder}/tests/resources/groups.csv",
"target", "target",
"replaced", "replaced",
"-d" "-d",
"-e",
"abc"
], ],
"justMyCode": true "justMyCode": true
} }

View File

@ -31,6 +31,7 @@ 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 = {}
@ -62,6 +63,8 @@ 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)}"')
@ -80,7 +83,13 @@ 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, candidates, args.current, args.become, args.dry, args.keep_extension args.csv,
candidates,
args.current,
args.become,
args.dry,
args.extension,
args.keep_extension,
) )
@ -91,38 +100,38 @@ def main():
) )
argparser.add_argument( argparser.add_argument(
"input_dir", "input_dir",
help="The directory containing the items that is to be renamed", help="The directory containing the items that is to be renamed.",
metavar="I", metavar="I",
) )
argparser.add_argument( argparser.add_argument(
"regex", "regex",
help="The regex to apply to each file name. The first capture group is used to " help="The regex to apply to each file name. The first capture group is used to "
"perform the replacement", "perform the replacement.",
metavar="R", metavar="R",
) )
argparser.add_argument( argparser.add_argument(
"csv", "csv",
help="The CSV to use as the dictionary for the substitutions in file name", help="The CSV to use as the dictionary for the substitutions in file name.",
metavar="C", metavar="C",
) )
argparser.add_argument( argparser.add_argument(
"current", "current",
help="The column header to look for the text matched by the regex", help="The column header to look for the text matched by the regex.",
metavar="F", metavar="F",
) )
argparser.add_argument( argparser.add_argument(
"become", help="The column header to replace the regex match", metavar="T" "become", help="The column header to replace the regex match.", metavar="T"
) )
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(
@ -131,17 +140,28 @@ 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 original file's extension by appending it to the end of the " help="Keeps the OS recognized extension from the original filename. Will append "
"name defined by the CSV", 'to end of argument given by "-e" or "--extension".',
action="store_true", action="store_true",
default=False,
required=False,
) )
args = argparser.parse_args() args = argparser.parse_args()