-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_renamer.py
120 lines (108 loc) · 2.77 KB
/
2_renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python
# pseudocode definition
# imports
import sys
import os
import getopt
# define constants
INPUT_REGEX_SPLIT_TOKEN = '||'
PARSE_STRING_VARIABLE_TOKEN = '%'
# main program
def main(argv):
# define command line option strings for getopts
cmdLineShortOpts = 'hvd:s:m:r:f:'
cmdLineLongOpts = ['help', 'debug', 'dir=', 'search=', 'mask=', 'replace=', 'files=']
global _DEBUG
_DEBUG = False
# process command line options
try:
process_args(argv)
# TODO: change this to reflect argparse
except getopt.GetoptError:
usage()
if _DEBUG:
print "exiting due to GetoptError"
sys.exit(2)
# parse arguments
# help
# debug
# directory
# string_identifiers
# replace_chars
# rename_mask
# file_list
def process_args(args):
opts, args = getopt.getopt(argv, cmdLineShortOpts, cmdLineLongOpts)
print 'printing opts'
for o in opts: print o
# process command-line options
for opt, arg in opts:
if opt in ('v', '--debug'):
# turn on debugging output
_DEBUG = True
elif opt in('-h', '--help'):
# show usage help
usage()
sys.exit()
elif opt in ('d', '--dir'):
# set directory to first command line option
dirPath = arg
if _DEBUG:
print 'just set dirPath'
elif opt in ('s', '--search'):
# split the input regex string
parsestrings = split_input_regex_string(arg)
searchdict = split_parse_string_variables(parsestrings)
if _DEBUG:
print 'just split input parse string'
elif opt in ('m', '--mask'):
renamemask = arg
if _DEBUG:
print 'just set renamemask'
elif opt in ('r', '--replace'):
replacestring = arg
if _DEBUG:
print 'just set replacestring'
elif opt in ('f', '--files'):
# identify list of files to rename
fileList = arg
if _DEBUG:
print 'printing files'
#for file in fileList:
# print file
# get directory listing, if appropriate
# return a list of files in given directory
def getfiles(dir):
if _DEBUG:
print "in getfiles"
return [f for f in os.listdir(dir) if os.path.isfile(os.listdir(dir))]
# parse string_identifiers
def split_input_regex_string(instring):
if _DEBUG:
print 'splitting input regex string'
print instring.split(INPUT_REGEX_SPLIT_TOKEN)
return instring.split(INPUT_REGEX_SPLIT_TOKEN)
def split_parse_string_variables(inlist):
vardict = {}
for instring in inlist:
left, right = instring.split('=')
left = left.strip()
right = right.strip()
vardict[left] = [right]
if _DEBUG:
print 'creating parse string variables'
for k in vardict.keys():
print vardict[k]
return vardict
# identify file list
# present sample renamed file
# prompt for confirmation
# rename files
# log renaming actions to text file
# print command line usage
def usage():
print "usage()"
pass
#print sys.argv[0] + "USAGE HERE"
if __name__ == '__main__':
main(sys.argv[1:])