-
Notifications
You must be signed in to change notification settings - Fork 0
/
my.py
executable file
·341 lines (268 loc) · 9.86 KB
/
my.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python3
'''
Kevin's custom cli customizations:
1. Reduced the group list to only those groups I actually use.
2. Rearrange the order of the fields so that activity is prompted after the description is entered.
'''
import sys
import os
import cli
import log
class MultiYearCli(cli.ReportCli):
def displayHelp(self, *args):
'''
Called when "help" or "h" is typed. Prints a help message.
'''
#!print("displayHelp(", args, ")")
print("""
Commands:
help (h) displays this help
groups (lg) list all available groups
codes (pc) list all available pay codes
chd set a custom date to be used as the default
cld clear the custom date
lsd list the default date
sum [cat] displays the month summary w/o details (hours)
psum [cat] displays the month summary w/o details (percent)
rep [cat] displays the month summary w/ details (hours)
prep [cat] displays the month summary w/ details (percent)
print (p) [#] prints info from the log file (default=everything)
list [label ...] list the labels used in the log. Default labels are
activity, group and title.
""")
return True
# Override the definition function to customize interface defaults
def definitions(self):
# Only a subset of commands apply for the year
self.commands = {
"help": self.displayHelp,
"h": self.displayHelp,
"print": self.displayLog,
"p": self.displayLog,
"chd": self.changeDate,
"cld": self.clearDate,
"lsd": self.showDate,
"exit": self.quit,
"quit": self.quit,
"q": self.quit,
"sum": self.displaySummary,
"psum": self.displayPercentSummary,
"rep": self.displayReport,
"prep": self.displayPercentReport,
"list": self.listLabels,
"l": self.listLabels,
"groups": self.listGroups,
"lg": self.listGroups,
"codes": self.listPayCodes,
"pc": self.listPayCodes,
"yt": self.displayYearTable,
}
# You can specify a different default name for the log file
self.logFilename = "work_log.xml"
# You can change the terminal width that is used to wrap text when displaying summaries
self.terminalWidth = 132
# Restore pre-Jira wr/wrep output
#!self.wrDateSort = False
# Here is where you can specify only the groups you regularly use
self.possibleGroups = ['',
'BCDA', 'Jira', 'MEDM Replacement', 'motor', 'synApps',
#'EPICS base', 'EPICS clients', 'areaDetector',
#'Python', 'spec', 'VxWorks', 'Beamline Comp Env',
'09ID-B', '09ID-C', '15ID', '26ID', '32ID', '33BM', '33ID',
'34ID-C', '34ID-E', 'XSD', 'Leave', '33ID-C HFM',
'ATOMIC/3DMN Support', 'ATOMIC/3DMN Design',
'ATOMIC 34ID-F', '3DMN 34ID-E']
self.payCodeDict = {#'None':'None',
'VAC':'Vacation',
'SIC':'Sick Pay',
'SLF':'Sick Leave Family',
'FHL':'Floating Holiday',
#'BRV':'Bereavement',
#'CL1':'Operations Suspended',
#'JUR':'Jury Duty',
#'PAR':'Parental Leave*',
'RG':'Regular',
'TEL':'TELECOMMUTING*'
}
self.possiblePayCodes = sorted(list(self.payCodeDict.keys()))
# Improve correctEntry prompts
#!self.showCorrectDefaults = True
#!self.showCorrectDescLen = 60
### Improve the wt command for Dayforce
self.showWBSCodes = True
# The cost codes are now workday project plans, which are only differentiated by the WBS code. Save screen space and omit them.
self.showCostCodes = False
# Pay codes only appear on d, ds and wd output
self.showPayCodes = True
# Use the default pay code for everything
self.promptForPayCode = False
# Temporarily change default pay code
self.defaultPayCode = "TEL"
def _getLogFilenames(self):
'''
Internal routine that determines the user-specified log file names
'''
# Allow the user to specify an log file when running the script
if len(sys.argv) > 1:
args = sys.argv[1:]
else:
args = ["{}/{}".format(os.getcwd(), self.logFilename),]
filepaths = []
for arg in args:
# Check to see if the file exists
if os.path.isfile(arg):
filepaths.append(arg)
else:
print("{} DOES NOT EXIST!".format(arg))
return filepaths[:]
# Override the createReportLog function so that MultiYearLog is used instead of ReportLog
def createReportLog(self, filepaths):
return MultiYearLog(filepaths)
def _getYear(self, num):
'''
Internal function to return current or previous weeks
'''
# Current date is needed to determine past years
currentYear = datetime.date.today().year
if num > 0:
year = num
else:
year = currentYear + num
return year
def _handleYearArgs(self, args):
'''
Internal routine to handle week args for displayYear* methods
'''
if len(args) == 0:
yArgs = "all"
else:
yArgs = []
for arg in args:
try:
if '-' in arg:
yArgs += self._getYear(int(arg))
if '+' in arg:
# ignore future years
continue
elif int(arg) == 0:
yArgs += self._getYear(0)
else:
yArgs.append("{:4d}".format(int(arg)))
except ValueError:
return -1
# This would be a good place to sort the list, if the wArgs contained date object rather than strings
return yArgs[:]
def displayYearTable(self, *args):
'''
Called when "yt" is typed. Prints a table with data.
args is a tuple of day strings. If no days are specified, the current year is displayed. Zero will return the current year. Negative numbers will return previous years.
'''
#!print("displayDaySummary(", args, ")")
yArgs = self._handleYearArgs(args)
if yArgs == -1:
print("Error: Days must be integers")
return True
#!print(yArgs)
years, groups, totals = self.logObj.calcYearTotals()
if yArgs == "all":
yearsToDisplay = years
else:
yearsToDisplay = yArgs
# Calculate group totals across years while making a list of groups to be displayed
groupsToDisplay = []
groupTotals = {}
for index, year in enumerate(yearsToDisplay):
for group in totals[year].keys():
if group not in groupsToDisplay:
groupsToDisplay.append(group)
groupTotals[group] = totals[year][group]
else:
groupTotals[group] += totals[year][group]
yearsToDisplay = sorted(yearsToDisplay)
groupsToDisplay.remove("Total")
groupsToDisplay = sorted(groupsToDisplay)
groupsToDisplay.append("Total")
###
### Build hours table
###
print("Group" + "".join([",{}".format(y) for y in yearsToDisplay]) + ",Total")
for group in groupsToDisplay:
print(group, end='')
for year in yearsToDisplay:
try:
#
print(",{}".format(totals[year][group]), end='')
except KeyError:
#
print(",0", end='')
print(",{}".format(groupTotals[group]))
### Group
# Find longest group name
#!maxGroupLen = 1
#!for group in groups:
#! if len(group) > maxGroupLen:
#! maxGroupLen = len(group)
#!print("maxGroupLen", maxGroupLen)
#!headString = "Group" + '\t' * self._calcTabs(maxGroupLen, "Group")
#!separator = "-" * maxGroupLen + '\t'
#!totalStr = "Total" + '\t' * self._calcTabs(maxGroupLen, "Total")
return True
# Override runMainLoop to allow handling multiple filenames
def runMainLoop(self):
# Get log filenames
filepaths = self._getLogFilenames()
if len(filepaths) > 0:
self.run = True
self.logObj = self.createReportLog(filepaths)
self.logEntryDef = self.logObj.getLogEntryDef()
# Run the main loop
self.main()
class MultiYearLog(log.ReportLog):
def __init__(self, logFiles):
self.definitions()
# Note: the ReportLog class also defines a logFile member, but that isn't needed here;
# all of the commands that use it have been removed.
self.entryArray = []
for logFile in logFiles:
print("Reading {}".format(logFile))
# Append the entries from each log file to the entry array
self.entryArray += (self.createLogFileObj(logFile, self.logEntryDef[:])).convertLogToObjs()
# Override the definition function to change the order of items in logEntryDef
# NOTE: group must always come before title
def definitions(self):
self.logEntryDef = [
"date",
"duration",
"group",
"title",
"description",
"activity",
"payCode"
]
def calcYearTotals(self):
#
years = []
groups = []
totals = {}
totalGroup = 'Total'
for entryObj in self.entryArray:
year = entryObj.getYear()
group = entryObj.getGroup()
duration = entryObj.getDurationFloat()
if group == None:
group = entryObj.category
if year not in years:
years.append(year)
if group not in groups:
groups.append(group)
if year not in totals.keys():
totals[year] = {group:duration, totalGroup:duration}
elif group not in totals[year].keys():
totals[year][group] = duration
totals[year][totalGroup] += duration
else:
totals[year][group] += duration
totals[year][totalGroup] += duration
return (years[:], groups[:], totals)
if __name__ == '__main__':
multiYearCli = MultiYearCli()