-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha03_rn.py
106 lines (89 loc) · 2.62 KB
/
a03_rn.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
#!/usr/bin/python
# concepts for the app:
# naming mask = user-defined variables that identify substrings of interest
# within the file name. These variables are of the format
# "%varname% = <regex matching string> || %varname2% = <regex>
# renaming mask = user-defined string containing naming mask variables and
# a filename mask for use in renaming
# imports
import sys
import os
import getopt
import Renamer
# define constants
# main program
def main(argv):
# define command line option strings for getopts
cmdLineShortOpts = 'hvd:n:m:r:f:'
cmdLineLongOpts = ['help', 'debug', 'dir=', 'name_mask=', 'renaming_mask=', 'replace=', 'files=']
# set debugging on
global _DEBUG
_DEBUG = True
# create Renamer object
fileRenamer = Renamer.Renamer()
# process command line options
try:
process_args(argv, cmdLineShortOpts, cmdLineLongOpts, fileRenamer)
# TODO: change this to use argparse
except getopt.GetoptError:
usage()
if _DEBUG:
print "exiting due to GetoptError"
sys.exit(2)
# print the resulting naming mask variables of the Renamer object
fileRenamer.printNamingMask()
# parse arguments
# help
# debug
# directory
# naming_mask
# replace_chars
# rename_mask
# file_list
def process_args(argv, cmdLineShortOpts, cmdLineLongOpts, fileRenamer):
opts, args = getopt.getopt(argv, cmdLineShortOpts, cmdLineLongOpts)
if _DEBUG:
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
# TODO: this should set the Renamer object's debug mode on
pass
elif opt in('-h', '--help'):
# show usage help
usage()
sys.exit()
elif opt in ('d', '--dir'):
# set directory to first command line option
fileRenamer.setDirPath(arg)
elif opt in ('n', '--name_mask'):
fileRenamer.setNameMaskString(arg)
elif opt in ('m', '--renaming_mask'):
fileRenamer.setRenamingMask(arg)
elif opt in ('r', '--replace'):
# replacestring = arg
# TODO: implement string replacement in Renamer?
pass
elif opt in ('f', '--files'):
# identify list of files to rename
fileRenamer.setFileList(arg)
# 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))]
# 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:])