From 6a982d3a8cf43f5d34d857c1fa949e04f93c09b2 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sat, 1 Jan 2011 17:19:00 -0500 Subject: [PATCH] reworked command processing, "show" aliases to "status", updated tests to pass --- Command.py | 123 ++++++++++++++++++++++++++++++----------------------- test | 7 +-- 2 files changed, 74 insertions(+), 56 deletions(-) diff --git a/Command.py b/Command.py index 3a5bb8c..3a8927e 100644 --- a/Command.py +++ b/Command.py @@ -27,68 +27,85 @@ 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: - 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) - count = int(m.group(2)) - if hms == 'm': - seconds = count * 60 - elif hms == 's': - seconds = count - else: # hours - seconds = count * 60 * 60 - delta = timedelta(seconds = seconds) - if plus_minus == '-': - the_time -= delta - else: - the_time += delta - matched = True - if matched: - self.time = the_time - return matched + 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 = '' if m.group(3) is None else m.group(3) + count = int(m.group(2)) + if hms == 'm': + seconds = count * 60 + elif hms == 's': + seconds = count + else: # hours + seconds = count * 60 * 60 + delta = timedelta(seconds = seconds) + if plus_minus == '-': + self.time -= delta + else: + self.time += delta + return True + return False diff --git a/test b/test index 8d3d4ce..e43c0c0 100755 --- a/test +++ b/test @@ -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: