def parser(self, s): def parseParam(v): if type(v) == str: return eval(v) # А-а-а-а!! else: n = v.getName() if n == 'object': obj = self.__botObjects[int(v[1])] if type(obj).__name__ != v[0]: raise UnaccessibleProperty(v) return obj elif n == 'list': return parseList(v) def parseList(v): return [ parseParam(x) for x in v ] obj = None course = False for x in parse(str(s)): n = x.getName() if obj == 0: if x[0] == 'self': obj = self else: obj = globals()[x[0]] elif n == 'ident': obj = obj.__getattribute__(x[0]) elif n == 'get': obj = obj.__getitem__(*parseList(x)) elif n == 'exec': obj = obj(*parseList(x)) if not (type(obj) == str or type(obj) == int or ('accessible' in dir(obj) and obj.accessible)): raise UnaccessibleProperty(x) if 'transition' in dir(obj) and obj.transition: if course: raise TwoTransitionProperty() course = True if course: raise CourseTransition() if type(obj) == str: return '"%s"' % obj elif type(obj) == int: return str(obj) else: self.__botObjects.append(obj) return '<%s %d>' % (type(obj).__name__, len(self.__botObjects)-1)
def _interactive(word=None): dicts = availabledictionaries() dic = 'auto' words = None if word: words = _lookup_and_print(word, dic) _prompt() # Main loop. while True: cmd, arg = parse(raw_input()) if cmd == Command.EXIT: sys.exit(0) elif cmd == Command.LIST_DICTS: _printavailabledictionaries() elif cmd == Command.SET_DICT: dic = arg puts("Dictionary set to {d}!".format(d=dicts[dic])) elif cmd == Command.DETAILS: _printdetailed(arg, words) elif cmd == Command.LOOKUP: words = _lookup_and_print(arg, dic) _prompt()