Last active
May 4, 2016 08:14
-
-
Save rhaidiz/65a64ee3c809c84031a16904e238702b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.5 | |
# -*- coding: utf-8 -*- | |
import os | |
import re | |
import logging | |
import argparse | |
# create logger, set to from from INFO up to CRITICAL | |
logger = logging.getLogger("rename") | |
logger.setLevel(logging.INFO) | |
# create StreamHandler | |
ch = logging.StreamHandler() | |
# create formatter and add it to the handlers | |
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') | |
ch.setFormatter(formatter) | |
# add handler to the logger | |
logger.addHandler(ch) | |
def main(): | |
# cmd-line parameters | |
cmd = argparse.ArgumentParser() | |
cmd.add_argument("--folder",help="The directory where to check, if not specified the current directory is used") | |
cmd.add_argument("--search", help="The regexp to check inside dir") | |
cmd.add_argument("--replace-with", help="") | |
cmd.add_argument("--simulate",help="Before running the substitution show the result first", action='store_true') | |
cmd.add_argument("--prefix",help="Prefix the given string") | |
cmd.add_argument("--extension",help="change the extension") | |
#TODO: add recursive option | |
# get parameters | |
args = cmd.parse_args() | |
simulation = args.simulate | |
folder = args.folder | |
# set current folder if no folder is specified | |
if folder == None: | |
folder = "." | |
folder = os.path.expanduser(folder) | |
if not os.path.isdir(folder): | |
criticalMsg = "path {} does not exists".format(folder) | |
logger.critical(criticalMsg) | |
exit() | |
search = args.search | |
if args.extension: | |
# re for extension | |
search = "("+args.extension+")$" | |
if args.prefix: | |
search = "^(" + args.prefix + ")" | |
replace_with = args.replace_with | |
if search == None or replace_with == None: | |
criticalMsg = "search value is not valid" | |
logger.critical(criticalMsg) | |
exit() | |
if simulation: | |
__engine(folder,search,replace_with,simulation) | |
logger.warning("The operation IS NOT reversible!!!") | |
c = __ask_y_n("Continue ") | |
if not c: | |
exit() | |
__engine(folder,search,replace_with,False) | |
def __engine(folder,search,replace_with,simulation): | |
one = False | |
for f in os.listdir(folder): | |
if re.search(search,f): | |
one = True | |
new_name = os.path.join(folder,re.sub(search,replace_with,f)) | |
infoMsg = "{} ==> {}".format(os.path.join(folder,f), new_name) | |
logger.info(infoMsg) | |
if not simulation: | |
os.rename(os.path.join(folder,f),new_name) | |
if not one: | |
infoMsg = "No file matchin {} found in {}".format(search,folder) | |
logger.info(infoMsg) | |
exit() | |
def __ask_y_n(msg): | |
inputMsg = "{}[Y/n]".format(msg) | |
read = input(inputMsg) | |
if read == "" or read.lower() == "y": | |
return True | |
elif read.lower() == "n": | |
return False | |
else: | |
logger.warning("Invalid input.") | |
return __ask_y_n(msg) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment