class Main(object): def listen(self): self.commands = Commands() console = input('Enter command: ') action = self.parseInput(console) self.commands.execute(action) def parseInput(self, input): output = input.replace(')', '') output = output.split('(') command = output[0].lower().replace(' ', '') try: params = output[1].replace(' ', '') if (params.find(',') != -1): if (params.find(';') != -1): return 'Using , and ; delimiters together is forbidden! Use , or ; e.g. command(param1, param2) or command(param1; param2)' else: return [command, params.split(',')] if (params.find(';') != -1): if (params.find(',') != -1): return 'Using , and ; delimiters together is forbidden! Use , or ; e.g. command(param1, param2) or command(param1; param2)' else: return [command, params.split(';')] except Exception: print('No input params') return [command]
def test_commands_property(): p = user.CommandsProperty() p._validate(Commands()) with pytest.raises(TypeError): p._validate(None) s = Commands().toJSON() assert s == p._to_base_type(p._from_base_type(s))
def oauth_callback(service): assert_user_signed_out() s = oauth_services.get(service) if s == None: return redirect(SIGN_IN) token = None if s.name == 'twitter': oauth_token = request.args.get('oauth_token', '') oauth_verifier = request.args.get('oauth_verifier', '') token = s.getAccessToken(oauth_token, oauth_verifier) if token == None: return redirect(SIGN_IN) else: state = request.args.get('state', '') code = request.args.get('code', '') if not verify_oauth_state(state): return redirect(SIGN_IN) token = s.getAccessToken(state, code) if token == None: return redirect(SIGN_IN) user_id = s.getUserID(token) if user_id == None: return redirect(SIGN_IN) username = '******' % (service, user_id) user = User.fromUsername(username) if user == None: user = User( service=service, username=username, commands=Commands(), ) user.put() user_key = user.key.urlsafe() route = COMMANDS if user.commands.size() == 0: route = PICK_COMMANDS set_current_user_key(user_key) return redirect(route)
description='Translate to English with Bing Translator', url_pattern='https://www.bing.com/translator?to=en&text={0}', ), Command( name='mw', description='Search Merriam-Webster online', url_pattern='http://www.merriam-webster.com/dictionary/{0}', ), ), ] builtin_command_groups builtin_commands = Commands( [ command for command_group in builtin_command_groups for command in command_group.commands ], ) class SampleQuery(object): def __init__(self, command, comment, args=''): self.command = command self.args = args self.query = command + ' ' + args self.comment = comment sample_queries = [ SampleQuery(
def test_commands(): commands = Commands() assert len(commands.mapping) == 0 commands = Commands.fromJSON(builtin_commands.toJSON()) assert len(commands.mapping) == len(builtin_commands.mapping) for k, v in commands.mapping.items(): assert k == v.name other = builtin_commands.mapping[k] assert not (v < other) and not (other < v) assert isinstance(commands.toJSON(), str) json.loads(commands.toJSON()) l = commands.asSortedList() assert len(l) == commands.size() for c in l: other = commands.mapping[c.name] assert not (c < other) and not (other < c) assert commands.get('g') != None assert commands.get('name') == None commands.add(get_dummy_command()) assert commands.get('name') != None with pytest.raises(ParamException): commands.add(get_dummy_command()) with pytest.raises(ParamException): other_commands = Commands() for i in xrange(Commands.limit + 1): other_commands.add(get_dummy_command(name=str(i))) commands.remove('name') assert commands.get('name') == None with pytest.raises(MalformedQueryException): commands.getRedirectUrl('') with pytest.raises(MalformedQueryException): commands.getRedirectUrl(' ') with pytest.raises(UnknownCommandException): commands.getRedirectUrl('unknown_command') with pytest.raises(UnknownDefaultCommandException): commands.getRedirectUrl('unknown_command', 'unknown_default_command') with pytest.raises(NotEnoughArgumentsException): commands.getRedirectUrl('cron *') g_out = 'https://www.google.com/search?q=hello%20world' assert commands.getRedirectUrl('g hello world') == g_out assert commands.getRedirectUrl('g hello world', 'b') == g_out b_out = 'https://www.bing.com/search?q=hello%20world' assert commands.getRedirectUrl('hello world', 'b') == b_out
def newUser(self, username, commands=None): if commands == None: commands = Commands() return user.User(username=username, commands=commands).put().urlsafe()
def _from_base_type(self, json_str): return Commands.fromJSON(json_str)
def listen(self): self.commands = Commands() console = input('Enter command: ') action = self.parseInput(console) self.commands.execute(action)