Commit 2f72c671 authored by Nicolas Chauvat's avatar Nicolas Chauvat
Browse files

introduce base class for exceptions

parent aba16e10ce55
......@@ -18,7 +18,7 @@
--help print this help message and exit
:version: $Revision: 1.26 $
:version: $Revision: 1.26 $
:author: Logilab
"""
......@@ -64,8 +64,8 @@
input = raw_input('>>> ')
try:
answer = brain.think(input)
except NoRewriteError :
answer = '...'
except AilError, exc:
answer = 'Error: %s' % exc
if answer.startswith('assert') :
print "I learned that", answer[6:]
elif answer.startswith('search') :
......@@ -128,6 +128,9 @@
"""handler function for rgx.sub to transform \n to %(n-1)s"""
return r'%%(%s)s' % (int(match.group(1)) - 1)
class NoRewriteError(Exception):
class AilError(Exception):
"""base ail exception"""
class NoRewriteError(AilError):
"""exception used when no more rewrite rules are available"""
......@@ -132,5 +135,5 @@
"""exception used when no more rewrite rules are available"""
class InfiniteCycleError(Exception):
class InfiniteCycleError(AilError):
"""exception used when the thinking cycles and doesn't seem to find an end"""
......@@ -135,5 +138,5 @@
"""exception used when the thinking cycles and doesn't seem to find an end"""
class BadAilRule(Exception):
class BadAilRule(AilError):
"""exception used when a malformatted rule is parsed"""
......@@ -138,6 +141,9 @@
"""exception used when a malformatted rule is parsed"""
class AILBrainHandler:
class BadAilCommand(AilError):
"""exception used when an unknown command was used"""
class AILBrainHandler(object):
"""a default command handler used to interactivly play with ail"""
def command(self, cmdname, *args):
return 'command : %s %s' % (cmdname, str(args))
......@@ -162,8 +168,8 @@
if encoding is not None:
string = unicode(string, encoding)
return string
class Brain:
class Brain(object):
"""the ail brain: process text according to loaded rules
"""
......@@ -172,7 +178,7 @@
self._cmds_hdlr = cmds_hdlr
self.encoding = encoding
self.source_files = []
def load(self, filenames, env_context = None):
"""load rules files"""
for name in filenames:
......@@ -187,7 +193,6 @@
self.source_files = []
self.load(filenames)
def load_stream(self, stream, base_dir='', env_context = None):
"""load rules from stream"""
self._add_patterns(read_from_stream(stream, base_dir, env_context))
......@@ -191,7 +196,7 @@
def load_stream(self, stream, base_dir='', env_context = None):
"""load rules from stream"""
self._add_patterns(read_from_stream(stream, base_dir, env_context))
def load_string(self, string, base_dir='', env_context = None):
"""load rules from string"""
self._add_patterns(read_from_string(string, base_dir, env_context))
......@@ -218,7 +223,7 @@
next = next % dict([(str(key), val) for key, val in enumerate(groups)])
return target, next
raise NoRewriteError()
def think(self, input, encoding=None):
"""process the given input, returning the value produced by thinking
(i.e. a direct answer or the result of a command)
......@@ -238,7 +243,7 @@
rule_type, next = self._rewrite(next)
## except NoRewriteError :
## # FIXME (syt+adim): should not catch this exception, imo rewrite without
## # ending command is a bug in the rule file
## # ending command is a bug in the rule file
## return next
## if rule_type == 'TERMINAL':
words = self.split(next)
......@@ -242,7 +247,10 @@
## return next
## if rule_type == 'TERMINAL':
words = self.split(next)
method = getattr(self._cmds_hdlr, words[0].lower())
try:
method = getattr(self._cmds_hdlr, words[0].lower())
except AttributeError:
raise AilError('Unknown action %s' % words[0].lower())
return method(*[type_cast(w, encoding) for w in words[1:]])
def split(self, string):
......@@ -259,5 +267,5 @@
continue
if self.split(next)[:len(searching)] == searching:
yield rgx.pattern
......@@ -263,4 +271,4 @@
if __name__ == '__main__' :
run(sys.argv[1:])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment