def connection_made(self, transport): """Prepare connection. """ peername = transport.get_extra_info('peername') print('Connection from {}'.format(peername)) self.connection = transport self.parser = CmdParser()
def test_command_add_update_generate_segments_correctly(self): parser = CmdParser() i11 = r'a "Weber Street" (2,-1) (2,2) (5,5) (5,6) (3,8)' i11points = [ Point2d(2, -1), Point2d(2, 2), Point2d(5, 5), Point2d(5, 6), Point2d(3, 8) ] i11segments = [ Segment2d(Point2d(2, -1), Point2d(2, 2)), Segment2d(Point2d(2, 2), Point2d(5, 5)), Segment2d(Point2d(5, 5), Point2d(5, 6)), Segment2d(Point2d(5, 6), Point2d(3, 8)) ] db = {} command = parser.parse(i11) command.execute(db) self.assertEqual(command.name, 'add street') self.assertEqual(db[command.street].name, 'Weber Street') self.assertItemsEqual(db[command.street].segments, i11segments)
def test_status_command_does_anything(self): game = Game() p = CmdParser(game) msg = FakeIRCmsg('status') p.parse(msg) game.command(p) self.assertNotEqual([], cahirc.Cahirc.say.mock_calls)
def test_status_wait_answers(self): bob = Player('Bob', '~bobbo') jim = Player('Jim', '~jimbo') joe = Player('Joe', '~joebo') game = Game() game.start() #status should how be wait_answers game.add_player(bob) game.add_player(jim) game.add_player(joe) p = CmdParser(game) msg = FakeIRCmsg('play 1', user=jim) p.parse(msg) game.command(p) question = game.question.formattedvalue players = gameclass.playerlist_format([joe.nick]) text = self.config['text']['en']['status']['wait_answers'] text = text.format(players=players, question=question) expected = call(text) game.state(bob, []) self.assertEqual( str(expected), str(cahirc.Cahirc.say.mock_calls[-1]), msg= 'This test fails regularly since the order of the player list is not deterministic' )
def test_commander_runs_start_command(self): game = Game() msg = FakeIRCmsg('start') # bob is the default player p = CmdParser(game) p.parse(msg) game.command(p) self.assertEqual('wait_players', game.status)
def test_player_starts_and_is_registered(self): game = Game() bob = Player('Bob', '~bobbo') msg = FakeIRCmsg('start') p = CmdParser(game) p.parse(msg) game.command(p) self.assertEqual(str(bob), str(game.players[0]))
def test_game_start_joining_works(self): game = Game() bob = Player('Bob', '~bobbo') p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) self.assertEqual(str(bob), str(game.players[0]))
def test_game_start_says_game_start(self): config = Config() game = Game() bob = Player('Bob', '~bobbo') p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) self.assertEqual(2, len(cahirc.Cahirc.say.mock_calls))
def test_game_start_text_says_game_start(self): config = Config() text = config.data['text']['en']['round_start'] game = Game() bob = Player('Bob', '~bobbo') p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) self.assertEqual('call("{}")'.format(text), str(cahirc.Cahirc.say.mock_calls[0]))
def test_cards_replays_question(self): config = Config().data game = start_game() player = game.players[1] p = CmdParser(game) msg = FakeIRCmsg('cards', user=player) p.parse(msg) game.command(p) question = game.question.formattedvalue annc = config['text']['en']['question_announcement'] annc = annc.format(card=question) expected = call(annc) self.assertEqual(str(expected), str(cahirc.Cahirc.say.mock_calls[-2]))
def test_irc_game(self): config = Config().data game = Game() bob = Player('Bob', '~bobbo') jim = Player('Jim', '~jimbo') joe = Player('Joe', '~joebo') cahirc.Cahirc.say.reset_mock() self.assertEqual([], cahirc.Cahirc.say.mock_calls) p = CmdParser(game) msg = FakeIRCmsg('start', user=bob) p.parse(msg) game.command(p) self.assertEqual(2, len(cahirc.Cahirc.say.mock_calls)) msg = FakeIRCmsg('join', user=jim) p.parse(msg) game.command(p) self.assertEqual(3, len(cahirc.Cahirc.say.mock_calls)) msg = FakeIRCmsg('join', user=joe) p.parse(msg) game.command(p) self.assertTrue( re.search(config['text']['en']['round_start'], str(cahirc.Cahirc.say.mock_calls[0]))) round_annc = config['text']['en']['round_announcement'].format( round_num=1, czar='Bob') round_call = call(round_annc) self.assertEqual(str(round_call), str(cahirc.Cahirc.say.mock_calls[4]))
def test_list_command_works(self): config = Config().data game = start_game() player = game.players[1] msg = FakeIRCmsg('list', user=player) p = CmdParser(game) p.parse(msg) game.command(p) playerlist = [player.nick for player in game.players] players = gameclass.playerlist_format(playerlist) annc = config['text']['en']['player_list'] annc = annc.format(players=players) expected = call(annc) self.assertEqual(str(expected), str(cahirc.Cahirc.say.mock_calls[-1]))
def test_commander_runs_play_command(self): hand_size = Config().data['hand_size'] game = Game() p = CmdParser(game) bob = Player('Bob', '~bobbo') jim = Player('Jim', '~jimbo') joe = Player('Joe', '~joemg') msg = FakeIRCmsg('play 1', user=jim) game.start() game.add_player(bob) game.add_player(jim) game.add_player(joe) p.parse(msg) game.command(p) self.assertEqual(hand_size - 1, len(jim.show_hand()))
def test_correct_input_generate_command_parses_successfully(self): parser = CmdParser() i1 = r'g' command = parser.parse(i1) self.assertEqual(command.name, 'generate graph') i2 = r' g' command = parser.parse(i2) self.assertEqual(command.name, 'generate graph') i3 = r' g ' command = parser.parse(i3) self.assertEqual(command.name, 'generate graph')
def test_correct_input_remove_street_command_parses_sucessfully(self): parser = CmdParser() i1 = r'r "Davenport Road"' command = parser.parse(i1) self.assertEqual(command.name, 'remove street') self.assertEqual(command.street, 'Davenport Road') i2 = r' r "Davenport Road"' command = parser.parse(i2) self.assertEqual(command.name, 'remove street') self.assertEqual(command.street, 'Davenport Road') i3 = r' r "Davenport Road" ' command = parser.parse(i3) self.assertEqual(command.name, 'remove street') self.assertEqual(command.street, 'Davenport Road')
def test_cards_command_works(self): config = Config().data game = start_game() player = game.players[1] p = CmdParser(game) msg = FakeIRCmsg('cards', user=player) p.parse(msg) game.command(p) hand = player.show_hand() annc = config['text']['en']['player_hand'] handstring = '' i = 0 for card in hand: handstring += '[{}] {} '.format(i, card) i += 1 expected = call(annc.format(cards=handstring)) self.assertIn(str(expected), str(cahirc.Cahirc.say.mock_calls[-1])) self.assertEqual(player.nick, game.irc.destination)
def test_scenario2(self): i1 = r'a "up and across st" (0,0) (10,10)' i2 = r'a "StreetB" (0,10)(10,0)' i3 = 'g' parser = CmdParser() db = {} c1 = parser.parse(i1) c2 = parser.parse(i2) c1.execute(db) c2.execute(db) c3 = parser.parse(i3) has_out, out = c3.execute(db) if has_out: print out self.assertTrue(True)
def test_aliases_work_at_all(self): bob = Player('Bob', '~bobbo') joe = Player('Joe', '~bobbo') jim = Player('Jim', '~bobbo') self.game.start() self.game.add_player(bob) self.game.add_player(joe) self.game.add_player(jim) config = Config().data player = self.game.players[1] msg = FakeIRCmsg('players', user=player) p = CmdParser(self.game) p.parse(msg) self.game.command(p) playerlist = [player.nick for player in self.game.players] players = gameclass.playerlist_format(playerlist) annc = config['text']['en']['player_list'] annc = annc.format(players=players) expected = call(annc) self.assertEqual(str(expected), str(cahirc.Cahirc.say.mock_calls[-1]))
def test_game_three_join_starts(self): game = Game() bob = Player('Bob', '~bobbo') p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) jim = Player('Jim', '~bobbo') msg = FakeIRCmsg('join', user=jim) p.parse(msg) p.player = jim game.command(p) joe = Player('Joe', '~bobbo') msg = FakeIRCmsg('join', user=joe) p.parse(msg) game.command(p) self.assertEqual('wait_answers', game.status)
def test_scenario1(self): i1 = r'a "Weber Street" (2,-1)(2,2) (5,5) (5,6) (3,8)' i2 = r'a "King Street S" (4,2) (4,8)' i3 = r'a "Davenport Road" (1,4) (5,8)' i4 = r'c "Weber Street" (2,1) (2,2)' i5 = r'r "King Street S"' i6 = r'g' parser = CmdParser() db = {} c1 = parser.parse(i1) c2 = parser.parse(i2) c3 = parser.parse(i3) c4 = parser.parse(i4) c5 = parser.parse(i5) c1.execute(db) c2.execute(db) c3.execute(db) c4.execute(db) c5.execute(db) c6 = parser.parse(i6) has_out, out = c6.execute(db) if has_out: print out self.assertTrue(True)
def test_scenario4(self): i1 = r'a "Amphitheatre Prkw" (2,-1)(2,2) (5,5) (5,6) (3,8)' i2 = r'c "amphitheatre PRKW" (0,0)(4,0)' i3 = r'a "One Infinite Loop" (3,0)(5,0)' i4 = r'g' parser = CmdParser() db = {} c1 = parser.parse(i1) c2 = parser.parse(i2) c3 = parser.parse(i3) c4 = parser.parse(i4) c1.execute(db) c2.execute(db) c3.execute(db) has_out, out = c4.execute(db) if has_out: print out self.assertTrue(True)
def test_scenario3(self): i1 = r'a "Weber Street" (2,-1) (2,2) (5,5) (5,6) (3,8)' i2 = r'a "King Street S" (3,2) (4,8)' i3 = r'a "Davenport Road" (0,0) (5,8)' i4 = r'g' parser = CmdParser() db = {} c1 = parser.parse(i1) c2 = parser.parse(i2) c3 = parser.parse(i3) c4 = parser.parse(i4) c1.execute(db) c2.execute(db) c3.execute(db) has_out, out = c4.execute(db) if has_out: print out self.assertTrue(True)
class DataStorage: """Combines the methods of storage. """ data = None def __init__(self): self.data = {} self.parser = CmdParser() def write(self, data: bytes) -> str: """Main write method. """ self.parser.clear() self.parser.fill(data) code, data = self.parser.get_data_fast() if code not in self.data: self.data[code] = CellStorage() self.data[code].extend(data) return code def read(self, data: bytes) -> (str, float): """Main read method. """ self.parser.clear() self.parser.fill(data) result = 0.0 code, data = self.parser.get_data_fast() stor = self.data.get(code) if stor: result = stor.search_variant(data) return code, result def sum(self, code: str) -> float: """Get sum of values by code. """ stor = self.data.get(code) return stor.sum() if stor else 0.0
def test_correct_input_addupdate_street_command_parses_successfully(self): """ Tests that valid input for the 'Add street' command is parsed correctly """ parser = CmdParser() i11 = r'a "Weber Street" (2,-1) (2,2) (5,5) (5,6) (3,8)' command = parser.parse(i11) i11segments = [ Segment2d(Point2d(2, -1), Point2d(2, 2)), Segment2d(Point2d(2, 2), Point2d(5, 5)), Segment2d(Point2d(5, 5), Point2d(5, 6)), Segment2d(Point2d(5, 6), Point2d(3, 8)) ] self.assertEqual(command.name, 'add street') self.assertEqual(command.street, 'Weber Street') self.assertItemsEqual(self.db[command.street], command.points) i12 = r' a "Weber Street" (2,-1) (2,2) (5,5) (5,6) (3,8)' command = parser.parse(i12) self.assertEqual(command.name, 'add street') self.assertEqual(command.street, 'Weber Street') self.assertItemsEqual(self.db[command.street], command.points) i13 = r' a "Weber Street" (2,-1) (2,2) (5,5) (5,6) (3,8) ' command = parser.parse(i13) self.assertEqual(command.name, 'add street') self.assertEqual(command.street, 'Weber Street') self.assertItemsEqual(self.db[command.street], command.points) i21 = r'c "Weber Street" (4,2) (4,8)' command = parser.parse(i21) self.assertEqual(command.name, 'update street') self.assertEqual(command.street, 'Weber Street') self.assertItemsEqual([Point2d(4, 2), Point2d(4, 8)], command.points) i22 = r' c "Weber Street" (4,2) (4,8)' command = parser.parse(i22) self.assertEqual(command.name, 'update street') self.assertEqual(command.street, 'Weber Street') self.assertItemsEqual([Point2d(4, 2), Point2d(4, 8)], command.points) i23 = r' c "Weber Street" (4,2) (4,8) ' command = parser.parse(i23) self.assertEqual(command.name, 'update street') self.assertEqual(command.street, 'Weber Street') self.assertItemsEqual([Point2d(4, 2), Point2d(4, 8)], command.points)
def test_game_three_join_starts(self): game = Game() bob = Player('Bob', '~bobbo') p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) jim = Player('Jim', '~jimbo') msg = FakeIRCmsg('join', user=jim) p.parse(msg) game.command(p) joe = Player('Joe', '~joebo') msg = FakeIRCmsg('join', user=joe) p.parse(msg) game.command(p) self.assertEqual(10, len(game.players[0].show_hand())) self.assertEqual(10, len(game.players[1].show_hand())) self.assertEqual(10, len(game.players[2].show_hand()))
def test_player_start_cant_join(self): game = Game() p = CmdParser(game) bob = Player('Bob', '~bobbo') msg = FakeIRCmsg('start') p.parse(msg) game.command(p) msg = FakeIRCmsg('join') p.parse(msg) game.command(p) self.assertEqual(1, len(game.players))
def test_game_join_joining_works(self): game = Game() bob = Player('Bob', '~bobbo') p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) jim = Player('Jim', '~bobbo') msg = FakeIRCmsg('join', user=jim) p.parse(msg) game.command(p) self.assertEqual([str(bob), str(jim)], [str(p) for p in game.players])
def test_cards_command_works_with_no_cards(self): game = Game() bob = Player('Bob', '~bobbo') p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) msg = FakeIRCmsg('cards') p.parse(msg) game.command(p) self.assertEqual(call("Game hasn't started yet"), cahirc.Cahirc.say.mock_calls[-1])
def test_status_wait_players(self): text = self.config['text']['en']['status']['wait_players'] text = text.format(num=2) expected = call(text) game = Game() p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) msg = FakeIRCmsg('status') p.parse(msg) game.command(p) self.assertEqual(expected, cahirc.Cahirc.say.mock_calls[-1])
def test_double_join_complains(self): cahirc.Cahirc.say.reset_mock() game = Game() p = CmdParser(game) msg = FakeIRCmsg('start') p.parse(msg) game.command(p) msg = FakeIRCmsg('join') p.parse(msg) game.command(p) config = Config() self.assertTrue( re.search(config.data['text']['en']['double_join'], str(cahirc.Cahirc.say.mock_calls[-1])))
def __init__(self, parent, serialThermal): self.parent = parent self.serialThermal = serialThermal self.connect_thermal_camera() self.shift_ammount = 0 self.thermal_data = ThermalData(64) self.thermal_data_updated = False self.thermal_data.attach(self) # attaching application as data observer (on notification, update_notification() will be called) incoming = queue.Queue() outgoing = queue.Queue() # Start serial monitor serial_monitor = serialHelpers.SerialMonitorThread(self.serialThermal, incoming, outgoing) serial_monitor.setDaemon(True) serial_monitor.start() # Thread that parses incoming messages and edits thermal_data accordingly cmd_parser = CmdParser(incoming, self.thermal_data) cmd_parser.setDaemon(True) cmd_parser.start() self.thermal_camera = ThermalCamera(outgoing) # Setup window size w = self.parent.winfo_screenwidth() - 20 h = self.parent.winfo_screenheight() / 3 * 2 # sw = self.parent.winfo_screenwidth() sh = self.parent.winfo_screenheight() # x = (sw - w) / 2 y = (sh - h) / 2 x = 0 self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y)) # self.parent.geometry('%dx%d+%d+%d' % (sw - 10, sh - 100, 0, 0)) self.parent.title("Thermal camera control") # Frame self.frame = tk.Frame(self.parent) self.frame.style = ttk.Style() self.frame.style.theme_use("default") self.frame.pack(fill = tk.BOTH, expand = 1) # Quit button self.quitButton = ttk.Button(self.frame, text = "QUIT", command = self.quit) self.quitButton.pack(side = tk.LEFT) # Connect button # self.connect_button = ttk.Button(self.frame, text = "Reconnect", command = self.thermal_cam.serial_connection.reconnect) # self.connect_button.pack(side = tk.LEFT) # Start scan button self.connect_button = ttk.Button(self.frame, text = "Scan", command = self.thermal_camera.start_scan) self.connect_button.pack(side = tk.LEFT) # self.serial_text_widget = scrolledtext.ScrolledText(self.frame, width = 40, height = 10, state = 'disabled', wrap = tk.WORD, font = 'helvetica 9') # self.serial_text_widget.pack(side = tk.LEFT) # self.serial_text_widget.tag_configure('incoming', background = '#8AB8E6') # self.serial_text_widget.tag_configure('outgoing', background = "#A3D1FF") # self.serial_text_widget.tag_configure('error', foreground = "red") # self.writeToLog("Tere! -siina olen mina!", ("incoming")) # self.writeToLog("Tere! -siina olen mina!", ("outgoing")) # self.writeToLog("Tere! -siina olen mina!", ("incoming", "error")) # Todo: send the variable to serial port self.create_servo_sliders() self.create_temp_limit_sliders() self.create_shift_slider() # Canvas # matplotlib setup self.fig = plt.figure(figsize = (14, 5), dpi = 100) self.ren_canvas = FigureCanvasTkAgg(self.fig, master = self.frame) self.ren_canvas.show() self.ren_canvas.get_tk_widget().pack() self.create_thermal_image() self.create_histogram() self.cycle();