class TestCommandParser(UnitTest): def setUp(self): self.parser = CommandParser() def test_command_parser_returns_a_command_create_class(self): class_name = "class_name" arguments = ["create", "class", "--name", class_name] self.assert_equals( class_name, str(self.parser.parse(arguments).class_creator.class_name)) def test_command_parser_returns_a_command_create_interface(self): class_name = "class_name" arguments = ["create", "interface", "--name", class_name] self.assert_equals( class_name, str(self.parser.parse(arguments).interface_creator.class_name)) def test_command_parser_returns_a_command_create_project(self): project_name = "arbitrary_project_name" author_name = "author_name" arguments = [ "create", "project", "--name", project_name, "--author", author_name ] self.assert_equals(project_name, str(self.parser.parse(arguments).project.name))
def main(): parser = CommandParser() try: command_list = parser.parse() except CommandParser.ParserException as e: sys.exit(e) router = Router(command_list) runner_class = router.get_runner() timeline = Timeline() runner_class(timeline).run(command_list)
class Simulation(object): def __init__(self): self.command_parser = CommandParser() self.reset() def reset(self): self.board = Board(5, 5) self.robot = Robot(self.board) def run(self, line): command = self.command_parser.parse(line) try: command.invoke(target=self.robot) except MoveOutOfBoundsError as e: print 'Skip %s:' % command.identifier, e except MissingPlaceError as e: print 'Skip %s:' % command.identifier, e
def __init__(self, args): self.no_colors = False def parse_args_and_opts(args): optionParser = OptionParser() for opt in Todo.opts: optionParser.add_option(*opt[4:], help=opt[0], action=opt[1], dest=opt[2], default=opt[3]) return optionParser.parse_args() if not os.path.exists(os.path.dirname(_todo_file)): os.mkdir(os.path.dirname(_todo_file)) if not os.path.exists(_todo_file): with file(_todo_file, 'w') as f: pass try: opts, args = parse_args_and_opts(args) setattr(Todo, 'no_colors', opts.no_colors) parserGenerator = TaskParserGenerator(_todo_file) tasks, last_index = parserGenerator.parse() commandParser = CommandParser([cmd[1] for cmd in Todo.cmds]) command, args_left = commandParser.parse(args) changes = getattr(Todo.TodoCommands(tasks, last_index), command)(args_left) if changes: parserGenerator.generate(tasks) except (TodoError, IOError, ValueError) as err: print('error: {0}'.format(err))
def __init__(self, args): self.no_colors = False def parse_args_and_opts(args): optionParser = OptionParser() for opt in Todo.opts: optionParser.add_option(*opt[4:], help=opt[0], action=opt[1], dest=opt[2], default=opt[3]) return optionParser.parse_args() if not os.path.exists(os.path.dirname(_todo_file)): os.mkdir(os.path.dirname(_todo_file)) if not os.path.exists(_todo_file): with file(_todo_file, "w") as f: pass try: opts, args = parse_args_and_opts(args) setattr(Todo, "no_colors", opts.no_colors) parserGenerator = TaskParserGenerator(_todo_file) tasks, last_index = parserGenerator.parse() commandParser = CommandParser([cmd[1] for cmd in Todo.cmds]) command, args_left = commandParser.parse(args) changes = getattr(Todo.TodoCommands(tasks, last_index), command)(args_left) if changes: parserGenerator.generate(tasks) except (TodoError, IOError, ValueError) as err: print ("error: {0}".format(err))
def test_known_command(): parser = CommandParser(cmds) result_cmd, result_args = parser.parse([cmds[0]] + args) assert result_cmd == cmds[0] assert result_args == args
def test_unknown_command(): parser = CommandParser(cmds) cmd, _ = parser.parse(['greet'] + args)
class ClientQObject(QObject): # Flag of login state is_logged = False def __init__(self, queue_read: Queue, queue_sent: Queue): super().__init__() self.queue_read = queue_read self.queue_sent = queue_sent # queue_read Timer initialization self.queue_timer = QTimer(self) self.queue_timer.timeout.connect(self.queueChecker) # Better to use gui/qt for timer functionality to operate with qt inside qt self.queue_timer.setInterval( 10) # each 10ms call func to check queue messages from websocket self.queue_timer.start() # Init command parse self.cmd_parser = CommandParser() # Create communication protocol self.protocol = ClientProtocol() # Signals login_received = pyqtSignal(bool) # when received login result companies_list_received = pyqtSignal(list) # received list of companies cliend_data_received = pyqtSignal( dict) # recived data with client information # Check reading queue def queueChecker(self): if self.queue_read.qsize() > 0: # get message from websocket and parse it msg = self.queue_read.get() self.parse_server_message(msg) # Parse everything, that is not related to communication def not_connection_messages(self, msg): if self.is_logged is False: return res = self.cmd_parser.parse(msg) return res def parse_server_message(self, msg): # message should be json msg = self.protocol.verify_msg(msg) if msg is None: return switcher = { MessageType.LOGIN.value: self.on_login_request_answer, MessageType.KEEP_ALIVE.value: self.on_keep_alive_request } func = switcher.get(int(msg["type"]), self.not_connection_messages) res = func(msg) # After parsing need to make some stuff with received and parsed information # TODO : make it more clearly and optimized if res is not None: if int(msg["type"]) == MessageType.COMPANIES_OPEN_LIST.value: self.companies_list_received.emit(res) elif int(msg["type"]) == MessageType.CLIENT_DATA.value: self.cliend_data_received.emit(res) def on_keep_alive_request(self, msg): # server wants keep alive. Just send him to inform, that client is alive msg_json = self.protocol.create_keep_alive_msg() # put to sent queue self.queue_sent.put(msg_json) def on_login_request_answer(self, msg): if self.is_logged: # This functionality can be used only player is not logged into system return body = msg["body"] self.is_logged = body["result"] self.protocol.uuid = body[ "uuid"] # Store uuid. Client will use it in communication with server for better indentification self.login_received.emit(self.is_logged) def transfer_login_auth(self, login, password): # prepare message msg = self.protocol.create_login_msg(login, password) # sent message to websocket queue self.queue_sent.put(msg) def send_companies_list_request(self): msg_json = self.protocol.request_companies_list() self.queue_sent.put(msg_json) def send_client_data_request(self): msg_json = self.protocol.request_client_data() self.queue_sent.put(msg_json)
def test_unknown_command(): parser = CommandParser(cmds) cmd, _ = parser.parse(['greet'] + args)
def test_known_command(): parser = CommandParser(cmds) result_cmd, result_args = parser.parse([cmds[0]] + args) assert result_cmd == cmds[0] assert result_args == args