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,68 +27,85 @@ class Command:
'adj' : 'adjust', 'adj' : 'adjust',
'end' : 'out', 'end' : 'out',
'st' : 'status', 'st' : 'status',
'show' : 'status',
'f' : 'fill' 'f' : 'fill'
} }
parts = cmdline.split(None, 1) args = []
if len(parts) > 1: foundArgs = False
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()
def parseTimeSpec(self, timespec): while True:
the_time = self.time cmdline = cmdline.strip()
matched = False if len(cmdline) < 1:
m = re.match('^(?:(\d{4})[-/])?(\d{1,2})[-/](\d{1,2}),(.+)$', timespec) 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: if m is not None:
# a date was given # dt was a date string
if m.group(1) is not None: if m.group(1) is not None:
the_time = the_time.replace(year = int(m.group(1))) self.time = self.time.replace(year = int(m.group(1)))
the_time = the_time.replace(month = int(m.group(2)), month, day = int(m.group(2)), int(m.group(3))
day = int(m.group(3))) self.time = self.time.replace(month = month, day = day)
timespec = m.group(4) return True
return False
def parseTime(self, timespec):
m = re.match('^(\d{1,2}):?(\d{2})?(am?|pm?)?$', timespec, re.I) m = re.match('^(\d{1,2}):?(\d{2})?(am?|pm?)?$', timespec, re.I)
if m is not None: if m is not None:
# an absolute time was given # an absolute time was given
h = int(m.group(1)) h = int(m.group(1))
mins = 0 if m.group(2) is None else int(m.group(2)) 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() am_pm = '' if m.group(3) is None else m.group(3)[0].lower()
if am_pm == '' and h < 7: if am_pm == 'p' and h < 12:
h += 12 h += 12
elif len(am_pm) >= 1 and am_pm[0] == 'p' and h < 12: elif am_pm == 'a' and h == 12:
h += 12
elif len(am_pm) >= 1 and am_pm[0] == 'a' and h == 12:
h = 0 h = 0
the_time = the_time.replace(hour = h, minute = mins, second = 0) self.time = self.time.replace(hour = h, minute = mins, second = 0)
matched = True return True
else: m = re.match('^([-+])(\d+(?:\.\d+)?)([hms])?$', timespec, re.I)
m = re.match('^([-+])(\d+(?:\.\d+)?)([hms])?$', timespec, re.I) if m is not None:
if m is not None: # a relative time was given
# a relative time was given plus_minus = m.group(1)
plus_minus = m.group(1) hms = '' if m.group(3) is None else m.group(3)
hms = m.group(3) count = int(m.group(2))
count = int(m.group(2)) if hms == 'm':
if hms == 'm': seconds = count * 60
seconds = count * 60 elif hms == 's':
elif hms == 's': seconds = count
seconds = count else: # hours
else: # hours seconds = count * 60 * 60
seconds = count * 60 * 60 delta = timedelta(seconds = seconds)
delta = timedelta(seconds = seconds) if plus_minus == '-':
if plus_minus == '-': self.time -= delta
the_time -= delta else:
else: self.time += delta
the_time += delta return True
matched = True return False
if matched:
self.time = the_time
return matched

7
test
View File

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