Created
August 15, 2018 01:08
-
-
Save rwev/73346c1903a6e30e1aa85608f62a9709 to your computer and use it in GitHub Desktop.
Applies a naming standard to set of files specified by arguments.
This file contains hidden or 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
''' | |
VANILLIZE.PY | |
Applies a naming standard to set of files specified by arguments. | |
Author: rwev (https://github.com/rwev) | |
Built-in dependencies only. | |
Optional command-line arguments: | |
-s, --sub | Rename files in all subdirectories in addition to those in the current directory. | |
-d, --dir | Rename directories also, in addition to files. | |
-e, --execute | Run the renaming routine. Otherwise, renaming results are printed | |
See usage: | |
>python vanillize.py --help | |
Files are renamed according to the following rules: | |
1. all punctuation is removed, and replaced with dashes | |
2. all alphabetic characters are reduced to lowercase | |
3. dashes are inserted to separate numeric characters from alphabetic characters. | |
4. dashes are inserted to separate lowercase alphabetic chars from uppercase | |
To make a portable executable of the program to be used throughout your system, run: | |
>pip install pyinstaller | |
>pyinstaller vanillize.py -F | |
and place the generated /dist/vanillize.exe in your system path. | |
''' | |
from __future__ import print_function | |
import os, sys, shutil | |
from optparse import OptionParser | |
from string import punctuation, digits, letters | |
punctuation += ' ' | |
def getVanillizedName(name): | |
components = [] | |
ccomp = '' | |
ccomp_is_alphabetic = True | |
last_char_uppercase = False | |
for char in name: | |
if ccomp: | |
if char in punctuation: | |
components.append(ccomp) | |
ccomp = '' | |
continue | |
elif (char in digits) and (ccomp_is_alphabetic): | |
components.append(ccomp) | |
ccomp = '' | |
elif (char in letters) and (not ccomp_is_alphabetic): | |
components.append(ccomp) | |
ccomp = '' | |
elif (char in letters) and char.isupper() and (not last_char_uppercase): | |
components.append(ccomp) | |
ccomp = '' | |
elif (char in digits) or (char in letters): | |
ccomp += char.lower() | |
if not ccomp: | |
if char in digits: | |
ccomp_is_alphabetic = False | |
ccomp += char.lower() | |
elif char in letters: | |
ccomp_is_alphabetic = True | |
ccomp += char.lower() | |
else: | |
continue | |
if char.isupper(): | |
last_char_uppercase = True | |
else: | |
last_char_uppercase = False | |
if ccomp: | |
components.append(ccomp) | |
if not len(components): | |
return '' | |
return '-'.join(components) | |
#################################### PARSE COMMAND LINE ARGS ##################################### | |
parser = OptionParser() | |
parser.add_option("-s", "--sub", | |
action="store_true", dest="do_rename_in_subdirectories", default=False, | |
help="Rename files in all subdirectories in addition to those in the current directory") | |
parser.add_option("-d", "--dir", | |
action="store_true", dest="do_rename_directories", default=False, | |
help="Rename directories also, in addition to files.") | |
parser.add_option("-e", "--execute", | |
action="store_true", dest="do_execute", default=False, | |
help="Run the renaming routine. Otherwise, renaming results are printed.") | |
(options, args) = parser.parse_args(sys.argv) | |
#################################### DO RENAMING ################################################# | |
clean_name = '' | |
temp_file_ext = '' | |
err = '' | |
file_potential_rename_count = 0 | |
dir_potential_rename_count = 0 | |
file_success_rename_count = 0 | |
dir_success_rename_count = 0 | |
for name in os.listdir('.'): | |
if not os.path.isdir(name): | |
name, temp_file_ext = os.path.splitext(name) | |
clean_name = getVanillizedName(name) | |
if clean_name: | |
if temp_file_ext: | |
name += temp_file_ext | |
clean_name += temp_file_ext | |
if clean_name != name: | |
print ('File %s --> %s' % (name, clean_name), end='') | |
file_potential_rename_count += 1 | |
if options.do_execute: | |
try: | |
os.rename(name, clean_name) | |
print ('... success') | |
file_success_rename_count += 1 | |
except Exception as err: | |
print('... FAILURE: %s' % (err)) | |
else: | |
print (' ') | |
elif os.path.isdir(name) and options.do_rename_directories: | |
clean_name = getVanillizedName(name) | |
if clean_name: | |
if clean_name != name: | |
print ('Directory %s --> %s' % (name, clean_name), end='') | |
dir_potential_rename_count += 1 | |
if options.do_execute: | |
try: | |
os.rename(name, clean_name) | |
print ('... success') | |
dir_success_rename_count += 1 | |
except Exception as err: | |
print('... FAILURE: %s' % (err)) | |
else: | |
print (' ') | |
if options.do_rename_in_subdirectories: | |
for root_dir_str, dirs_list, files_list in os.walk('.', topdown=True): | |
for name in files_list: | |
name, temp_file_ext = os.path.splitext(name) | |
clean_name = getVanillizedName(name) | |
if clean_name: | |
if temp_file_ext: | |
name += temp_file_ext | |
clean_name += temp_file_ext | |
if clean_name != name: | |
print ('sub-File %s --> %s' % (os.path.join(root_dir_str, name), | |
os.path.join(root_dir_str, clean_name)), end='') | |
file_potential_rename_count += 1 | |
if options.do_execute: | |
try: | |
os.rename(os.path.join(root_dir_str, name), | |
os.path.join(root_dir_str, clean_name)) | |
print ('... success') | |
file_success_rename_count += 1 | |
except Exception as err: | |
print('... FAILURE: %s' % (err)) | |
else: | |
print (' ') | |
if options.do_rename_directories: | |
for name in dirs_list: | |
clean_name = getVanillizedName(name) | |
if clean_name: | |
if clean_name != name: | |
print ('sub-Directory %s --> %s' % (os.path.join(root_dir_str, name), | |
os.path.join(root_dir_str, clean_name)), end='') | |
dir_potential_rename_count += 1 | |
if options.do_execute: | |
try: | |
os.rename(os.path.join(root_dir_str, name), | |
os.path.join(root_dir_str, clean_name)) | |
print ('... success') | |
dir_success_rename_count += 1 | |
except Exception as err: | |
print('... FAILURE: %s' % (err)) | |
else: | |
print (' ') | |
##################################### PRINT RESULTS ############################################### | |
if not options.do_execute: | |
print ('%d directories to be renamed' % dir_potential_rename_count) | |
print ('%d files to be renamed' % file_potential_rename_count) | |
else: | |
print ('%d / %d directories successfully renamed' % (dir_success_rename_count, dir_potential_rename_count)) | |
print ('%d / %d files successfully renamed' % (file_success_rename_count, file_potential_rename_count)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment