From 554c21041ae06ec89de3265d78a300514b42355c Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sat, 11 Jun 2005 03:01:03 +0000 Subject: [PATCH] goodnight (times not parsed yet) --- CommandParser.py | 60 ++++++++++++++++++++++++++++++++++++++++++ tests/test_itinerary_parser.py | 4 --- 2 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 CommandParser.py diff --git a/CommandParser.py b/CommandParser.py new file mode 100644 index 0000000..d024406 --- /dev/null +++ b/CommandParser.py @@ -0,0 +1,60 @@ +# +# vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab: + +"""Splits a command into source, destination, and time.""" + +import sre + +import PlanTime + +class Command: + def __init__(self): + self.src = None + self.dest = None + self.time = None + +class CommandParseException(Exception): + pass + +class CommandParser: + def __init__(self, str): + self.cmd = None + + self.parse(str) + + def parse(self, str): + # Look for src and dest at the start of the string. + match = _full_rx.match(str) + if not match: + raise CommandParseException("Failed to extract source and " + "destination from command '%s'" % str); + self.cmd = Command() + self.cmd.src = match.group("src") + self.cmd.dest = match.group("dest") + + # Now look for time constraints. + rule = match.group("rule") + timestr = match.group("time") + if rule and timestr: + ruleMap = { + "by": PlanTime.MUST_ARRIVE_BEFORE, + "at": PlanTime.MUST_LEAVE_AFTER, + } + self.cmd.time \ + = PlanTime.PlanTime(self.decodeTime(timestr), ruleMap[rule]) + + def decodeTime(self, str): + raise CommandParseException("Unable to decode timespec '%s'" % str) + +_full_re = ("^\s*(?P.*?)\s+to\s+(?P.*?)\s*" + # at this point the pattern either ends, or there's a timespec + "(?:$|(?Pby|at)\s+(?P