-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
92 lines (76 loc) · 2.7 KB
/
helpers.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
#!python3
from os.path import isfile
import glob, time
def getFiles(arg):
''' Checks that the file(s) passed as a string actually exist, and
returns it/them as a list. '''
if isfile(arg):
return [arg]
else:
files = [file for file in glob.glob(arg) if isfile(file)]
if len(files) >= 0:
return files
else:
print(file, 'was not found')
return []
def parseDate(string):
''' Converts a string in the format DDMMYYYY to a format used by the
`time` library. '''
try:
return time.strptime(string, '%d%m%Y')
except ValueError as e:
print(e)
def parseTime(string):
''' Converts a string in the format HHMMSS to a format used by the
`time` library. '''
try:
return time.strptime(string, '%H%M%S')
except ValueError as e:
print(e)
def setPlatform(val):
''' Checks whether the machine is running Linux or Windows. If it's not
running Linux or Windows, it returns 'E' for "Error". '''
if val == 'Linux':
return 'L'
elif val == 'Windows':
return 'W'
else:
return 'E'
def makeList(argv, args):
''' Converts the arguments given to this program into a formatted list
for easier execution. '''
masterExecutionList = []
# set some important variables...
trimCount = 0
replaceCount = 0
numberCount = 0
dateCount = 0
timeCount = 0
''' loop through sys.argv, building a list of execution commands, tupled
with their parameters'''
for cmd in argv:
if cmd in ('-l', '--lower'):
masterExecutionList.append(('lower', None))
elif cmd in ('-u', '--upper'):
masterExecutionList.append(('upper', None))
elif cmd in ('-d', '--delete'):
masterExecutionList.append(('delete', None))
elif cmd in ('-dt', '--touch'):
masterExecutionList.append(('touch', None))
elif cmd in ('-t', '--trim'):
masterExecutionList.append(('trim', args.trim[trimCount][0]))
trimCount += 1
elif cmd in ('-r', '--replace'):
masterExecutionList.append(('replace', args.replace[replaceCount]))
replaceCount += 1 # keep track of what repeat we've found for lookup
elif cmd in ('-n', '--number'):
masterExecutionList.append(('countstring', \
args.number[numberCount][0]))
numberCount += 1
elif cmd in ('-D', '--date'):
masterExecutionList.append(('date', args.date[dateCount][0]))
dateCount += 1
elif cmd in ('-T', '--time'):
masterExecutionList.append(('time', args.time[timeCount][0]))
timeCount += 1
return masterExecutionList