reworked command processing, "show" aliases to "status", updated tests to pass

This commit is contained in:
Josh Holtrop 2011-01-01 17:19:00 -05:00
parent 2bce1b33e1
commit 6a982d3a8c
2 changed files with 74 additions and 56 deletions

View File

@ -27,55 +27,74 @@ class Command:
'adj' : 'adjust',
'end' : 'out',
'st' : 'status',
'show' : 'status',
'f' : 'fill'
}
parts = cmdline.split(None, 1)
if len(parts) > 1:
timespec = parts[0].strip()
if self.parseTimeSpec(timespec):
parts = parts[1].split(None, 1)
if len(parts) == 1:
command = parts[0].strip()
rest = ''
else:
command, rest = parts
if command in COMMANDS:
self.command = command
self.argstr = rest.strip()
else:
self.argstr = ' '.join([command, rest]).strip()
args = []
foundArgs = False
def parseTimeSpec(self, timespec):
the_time = self.time
matched = False
m = re.match('^(?:(\d{4})[-/])?(\d{1,2})[-/](\d{1,2}),(.+)$', timespec)
while True:
cmdline = cmdline.strip()
if len(cmdline) < 1:
break
parts = cmdline.split(None, 1)
token = parts[0]
if not foundArgs:
if self.parseDate(token):
pass
elif self.parseTime(token):
pass
elif token in COMMANDS:
self.command = token
foundArgs = True
elif token in ALIASES:
self.command = ALIASES[token]
foundArgs = True
else:
foundArgs = True
args.append(token)
else:
args.append(token)
if len(parts) < 2:
break
cmdline = parts[1]
self.argstr = ' '.join(args)
def parseDate(self, dt):
if dt.lower() == "yesterday":
today = datetime.today()
y = today - timedelta(days = 1)
self.time.replace(year = y.year, month = y.month, day = y.day)
return True
m = re.match('^(?:(\d{4})[-/])?(\d{1,2})[-/](\d{1,2})$', dt)
if m is not None:
# a date was given
# dt was a date string
if m.group(1) is not None:
the_time = the_time.replace(year = int(m.group(1)))
the_time = the_time.replace(month = int(m.group(2)),
day = int(m.group(3)))
timespec = m.group(4)
self.time = self.time.replace(year = int(m.group(1)))
month, day = int(m.group(2)), int(m.group(3))
self.time = self.time.replace(month = month, day = day)
return True
return False
def parseTime(self, timespec):
m = re.match('^(\d{1,2}):?(\d{2})?(am?|pm?)?$', timespec, re.I)
if m is not None:
# an absolute time was given
h = int(m.group(1))
mins = 0 if m.group(2) is None else int(m.group(2))
am_pm = '' if m.group(3) is None else m.group(3).lower()
if am_pm == '' and h < 7:
am_pm = '' if m.group(3) is None else m.group(3)[0].lower()
if am_pm == 'p' and h < 12:
h += 12
elif len(am_pm) >= 1 and am_pm[0] == 'p' and h < 12:
h += 12
elif len(am_pm) >= 1 and am_pm[0] == 'a' and h == 12:
elif am_pm == 'a' and h == 12:
h = 0
the_time = the_time.replace(hour = h, minute = mins, second = 0)
matched = True
else:
self.time = self.time.replace(hour = h, minute = mins, second = 0)
return True
m = re.match('^([-+])(\d+(?:\.\d+)?)([hms])?$', timespec, re.I)
if m is not None:
# a relative time was given
plus_minus = m.group(1)
hms = m.group(3)
hms = '' if m.group(3) is None else m.group(3)
count = int(m.group(2))
if hms == 'm':
seconds = count * 60
@ -85,10 +104,8 @@ class Command:
seconds = count * 60 * 60
delta = timedelta(seconds = seconds)
if plus_minus == '-':
the_time -= delta
self.time -= delta
else:
the_time += delta
matched = True
if matched:
self.time = the_time
return matched
self.time += delta
return True
return False

7
test
View File

@ -29,15 +29,16 @@ def main():
testcmd('fill', 'fill', '')
testcmd('adjust', 'adjust', '')
testcmd('2 report', 'report', '')
testcmd('show', 'show', '')
testcmd('show', 'status', '')
testcmd('5pm out', 'out', '')
testcmd('show 45h', 'show', '45h')
testcmd('st 45h', 'status', '45h')
testcmd('fill 40h', 'fill', '40h')
testcmd('-10m arlx', 'start', 'arlx')
testcmd(' adjust -10m ', 'adjust', '-10m')
testcmd(' 9:45 wr: nog: pbit ram test ',
'start', 'wr: nog: pbit ram test')
testcmd('12/14,3pm start arlx', 'start', 'arlx')
testcmd('12/14 3pm start arlx', 'start', 'arlx')
testcmd('yesterday 7P out', 'out', '')
if n_tests == n_pass:
print " >= SUCCESS <="
else: