def createAlerts(): alert1 = alert.Alert() alert2 = alert.Alert() alert3 = alert.Alert() alert1.setGps(0, 32.250327, -110.935402, 0) alert2.setGps(1, 32.232162, -110.951885, 45) alert3.setGps(2, 32.226696, -110.960383, 90) return [alert1, alert2, alert3]
def test_alert(): alert1 = alert.Alert("SPY", alert.AlertType.MOVES_BELOW, 300) assert alert1.__repr__() == "(SPY, AlertType.MOVES_BELOW, 300)" alert2 = alert.Alert("SPY", alert.AlertType.MOVES_ABOVE, 300) assert alert2.__repr__() == "(SPY, AlertType.MOVES_ABOVE, 300)" alert3 = alert.Alert("SPY", alert.AlertType.MOVES_BELOW, 300) assert not alert1 == alert2 assert alert1 == alert3
def test_alert_manager(): am = alert.AlertManager() # Init state assert am.prices == {} assert am.alerts == {} # Add alert1 alert1 = alert.Alert("SPY", alert.AlertType.MOVES_BELOW, 300) am.add_alert(alert1) assert am.prices == {} assert am.alerts == {"SPY": [alert1]} assert am.get_stock_list() == ["SPY"] # Add alert2 alert2 = alert.Alert("SPY", alert.AlertType.MOVES_ABOVE, 300) am.add_alert(alert2) assert am.prices == {} assert am.alerts == {"SPY": [alert1, alert2]} # No price update yet assert not am.check_alert(alert1) assert am.check_all_alerts() == [] # Update price, trigger alert2 am.update_price("SPY", 301) assert am.prices == {"SPY": 301} assert not am.check_alert(alert1) assert am.check_alert(alert2) assert am.check_all_alerts() == [alert2] assert am.alert_msg(alert2) == "SPY moved above $300, price: $301" # Remove alert2 am.remove_alert(alert2) assert am.prices == {"SPY": 301} assert am.alerts == {"SPY": [alert1]} assert am.get_stock_list() == ["SPY"] # Update price, trigger alert1 am.update_price("SPY", 299) assert am.prices == {"SPY": 299} assert am.check_alert(alert1) assert am.check_all_alerts() == [alert1] assert am.alert_msg(alert1) == "SPY moved below $300, price: $299" # Remove alert1 am.remove_alert(alert1) assert am.prices == {} assert am.alerts == {} assert am.get_stock_list() == []
def main(): ''' main function to run the application''' avg_hits = config.initial_anticipated_avg #static value of avg hits traffic_queue = [] # keeps a tuple of (time of packet, url) url_counter = Counter() # keeps a count of url section # instances of classes - packet sniffer, alert and display packet_sniffer = sniffer.Sniffer(config.dev, url_counter, traffic_queue) alerts = alert.Alert(packet_sniffer, avg_hits) app_display = display.Display(packet_sniffer, alerts) try: #log configuration path = os.path.dirname(os.getcwd()) logging.basicConfig(filename=path + config.log_file, level=logging.INFO) except IOError as e: print str(e) try: #start all threads packet_sniffer.start() app_display.start() alerts.start() signal.pause() except KeyboardInterrupt: os._exit(1)
def alert(self, status, subject, check=None, description=None, cluster=None, timestamp=None, primary_attr='host', secondary_attr='check', **kwargs): """ Return a new Alert object associated with this client. Refer to bigpanda.Alert for more help. """ return alert.Alert(status, subject, check, description, cluster, timestamp, primary_attr, secondary_attr, client=self, **kwargs)
def check_special_chars(self, string): if not re.match(r'^\w+$', string): alert = al.Alert( self.parent.parent, "BOTTLE_NAME_ERROR: " + self._('Bottle name can not contain special characters'), 600, 90) response = alert.run() if response == Gtk.ResponseType.OK: alert.destroy() return False else: return True
def switch_to_alert(self): """Return an Alert object to interact with a modal dialog.""" alert_ = alert.Alert(self) alert_.get_text() return alert_
def remove_alert(sid, data): print('remove_alert', data) alert_manager.remove_alert( alert.Alert(data['ticker'], alert.AlertType(data['alert_type']), float(data['target'])))
def game_logic(self, keys, newkeys, mouse_pos, newclicks): # collect actions for alerts alerts_actions = [] for a in self.alerts: alert_actions = a.game_logic(keys, newkeys, mouse_pos, newclicks) for action in alert_actions: if action is not None: alerts_actions.append(action) for action in alerts_actions: # if the alert expired, remove it if action[0] == ALERT_EXP_MESSAGE: self.alerts.remove(action[1]) # only collect actions from the menu if needed if self.state == TD_MENU: # collect actions for main menu actions = [] mm_actions = self.mm.game_logic(keys, newkeys, mouse_pos, newclicks) for action in mm_actions: if action is not None: actions.append(action) # handle actions # for action in actions: if action[0] == BUTTON_PLAY_MSG: self.state = TD_LEVEL_SELECT elif action[0] == BUTTON_HELP_MSG: self.load_level(TUTORIAL_PATH) self.state = TD_TUTORIAL self.tut_progress = -1 # create and place some creeps x, y = self.world.get_waypoints()[0] c = self.creep_types[0]((0, 0)) for j in range(5): y += c.get_height() + CREEP_GAP c.set_position((x, y)) self.creeps.add(c) c = self.creep_types[0]((0, 0)) self.display_item(c) return elif action[0] == BUTTON_QUIT_MSG: self.quit = True return # only collect actions from the level select # if needed elif self.state == TD_LEVEL_SELECT: # collect actions for main menu actions = [] mm_actions = self.mm.game_logic(keys, newkeys, mouse_pos, newclicks) for action in mm_actions: if action is not None: actions.append(action) # collect actions for level select ls_actions = self.ls.game_logic(keys, newkeys, mouse_pos, newclicks) for action in ls_actions: if action is not None: actions.append(action) # handle actions # for action in actions: # load the level according to which button was pressed if action[0] == BUTTON_PLAY_MSG: if len(self.ls_buttons) > 0: self.load_level(self.ls_buttons[0][0]) else: return elif action[0] == BUTTON_QUIT_MSG: self.state = TD_MENU return elif action[0] == BUTTON_LEVEL_MSG: self.load_level(action[1]) elif self.state == TD_TUTORIAL: temp = self.tut_progress # first time if self.tut_progress == -1: self.tut_progress = 0 if MOUSE_LEFT in newclicks: self.tut_progress += 1 elif MOUSE_RIGHT in newclicks: self.tut_progress -= 1 if self.tut_progress < 0: self.tut_progress = 0 if self.tut_progress != temp: self.alerts.clear() if self.tut_progress >= len(TUTORIAL_ALERTS): self.state = TD_MENU return a = TUTORIAL_ALERTS[self.tut_progress] self.alerts.add(alert.Alert(a[0], a[1], a[2], a[3])) # end the game upon failure or success elif self.state == TD_FAILURE or self.state == TD_SUCCESS: if MOUSE_LEFT in newclicks: self.empty_data() # report success if all waves were cleared elif self.state == TD_CLEAR and not self.can_start_wave(): self.state = TD_SUCCESS self.alerts.add( alert.Alert(ALERT_SUCCESS_POS, ALERT_SUCCESS_WIDTH, ALERT_SUCCESS_HEIGHT, ALERT_SUCCESS_MESSAGE)) # continue the game else: # report failure when all lives are lost if self.lives <= 0: self.state = TD_FAILURE self.alerts.add( alert.Alert(ALERT_FAILURE_POS, ALERT_FAILURE_WIDTH, ALERT_FAILURE_HEIGHT, ALERT_FAILURE_MESSAGE)) # if we finished the wave # change the state if self.state == TD_PLAYING and len(self.creeps) == 0: self.waves_comp += 1 message = ALERT_WAVE_CLEARED_MESSAGE % (self.waves_comp) self.alerts.add( alert.Alert(ALERT_WAVE_CLEARED_POS, ALERT_WAVE_CLEARED_WIDTH, ALERT_WAVE_CLEARED_HEIGHT, message, True, ALERT_WAVE_CLEARED_DURATION)) self.state = TD_CLEAR # if we are displaying an object # update the display if self.selected is not None: self.display_item(self.selected) if MOUSE_LEFT in newclicks: # left mouse click # if we clicked on an empty cell # stop showing the previously # selected tower's range cell_num = self.world.get_cell_at(mouse_pos) if self.sub_state == TD_SHOW: if self.world.has_cell( cell_num) and not self.world.is_occupied(cell_num): if self.selected is not None and self.selected.get_kind( ) == KIND_TOWER: self.selected.deactivate() self.selected = None self.sub_state = TD_IDLE self.display.deactivate() # stops a tower placement elif MOUSE_RIGHT in newclicks: if self.sub_state == TD_FOLLOW: self.purchaser_object.deactivate() self.purchaser_object = None self.selected = None self.sub_state = TD_IDLE pygame.mouse.set_visible(True) self.display.deactivate() # handles life losing for creep in self.creeps: if not creep.has_destination(): self.lives -= 1 if self.lives <= 0: self.lives = 0 creep.health = 0 creep.set_mod(0.0) message = ALERT_LIFE_LOST_MESSAGE % (self.lives) self.alerts.add( alert.Alert(self.alert_life_lost_pos, ALERT_LIFE_LOST_WIDTH, ALERT_LIFE_LOST_HEIGHT, message, True, ALERT_LIFE_LOST_DURATION)) # if we are placing a tower # snap its location to the # cells of the world if self.sub_state == TD_FOLLOW: self.purchaser_object.set_position( self.calc_snap_loc(mouse_pos)) # collect actions for tower menu actions = [] # separate list for placing items actions_placing = [] menu_actions = self.menu.game_logic(keys, newkeys, mouse_pos, newclicks) for action in menu_actions: if action is not None: if action[0] == P_PLACE: actions_placing.append(action) else: actions.append(action) # collect actions for trap menu trap_menu_actions = self.trap_menu.game_logic( keys, newkeys, mouse_pos, newclicks) for action in trap_menu_actions: if action is not None: if action[0] == P_PLACE: actions_placing.append(action) else: actions.append(action) b_menu_actions = self.b_menu.game_logic(keys, newkeys, mouse_pos, newclicks) for action in b_menu_actions: if action is not None: actions.append(action) # collect actions for towers for tower in self.towers: tower_actions = tower.game_logic(keys, newkeys, mouse_pos, newclicks, self.creeps) for action in tower_actions: if action is not None: actions.append(action) # collect actions for creeps for creep in self.creeps: creep_actions = creep.game_logic(keys, newkeys, mouse_pos, newclicks) for action in creep_actions: if action is not None: actions.append(action) # collect actions for traps for trap in self.traps: trap_actions = trap.game_logic(keys, newkeys, mouse_pos, newclicks, self.creeps) for action in trap_actions: if action is not None: actions.append(action) # when not placing a tower, the cursor is visible if self.sub_state != TD_FOLLOW: pygame.mouse.set_visible(True) # handle actions for action in actions_placing: if self.purchaser_object is None: break placed = False # placing a tower if self.purchaser_object.kind == KIND_TOWER: # verify ability to place tower f_pos = self.calc_snap_loc(mouse_pos) f_dims = self.purchaser_object.get_dims() cell_num = self.world.get_cell_at(f_pos) if self.world.has_cell(cell_num): if self.purchaser_object.get_cost() <= self.money: if self.world.can_build_tower(f_pos, f_dims): self.world.occupy_area(f_pos, f_dims) self.purchaser_object.activate() self.towers.append(self.purchaser_object) self.money -= self.purchaser_object.get_cost() self.selected = self.purchaser_object self.sub_state = TD_SHOW # show new tower self.display_item(self.selected) placed = True # placing a trap elif self.purchaser_object.kind == KIND_TRAP: # verify ability to place tower f_pos = self.calc_snap_loc(mouse_pos) f_dims = self.purchaser_object.get_dims() cell_num = self.world.get_cell_at(f_pos) if self.world.has_cell(cell_num): if self.purchaser_object.get_cost() <= self.money: if self.world.can_build_trap(f_pos, f_dims): self.world.occupy_area(f_pos, f_dims) self.purchaser_object.activate() self.traps.append(self.purchaser_object) self.money -= self.purchaser_object.get_cost() self.selected = self.purchaser_object self.sub_state = TD_SHOW # show new trap self.display_item(self.selected) placed = True if not placed: self.sub_state = TD_IDLE self.display.deactivate() # handles placing the same type of tower # multiple times when pressing certain # buttons dupe = False for btn in DUPLICATE_BUTTONS: if btn in keys: dupe = True break if not dupe: self.purchaser_object = None pygame.mouse.set_visible(True) else: if self.selected is not None and self.selected.get_kind( ) == KIND_TOWER: self.selected.deactivate() # creates another, identical purchaser # of the last placed tower self.display.deactivate() self.selected = None self.sub_state = TD_FOLLOW self.purchaser_object = action[1][1] self.purchaser_object.activate() self.purchaser.toggle_status() pygame.mouse.set_visible(False) self.display_item(self.purchaser_object) for action in actions: if action[0] == P_FOLLOW: # if we clicked on a menu item # to start placing a tower # keep track of that tower if self.sub_state == TD_SHOW: if self.selected is not None and self.selected.get_kind( ) == KIND_TOWER: self.selected.deactivate() self.display.deactivate() self.selected = None self.sub_state = TD_FOLLOW self.purchaser = action[1][0] self.purchaser_object = action[1][1] self.purchaser_object.activate() pygame.mouse.set_visible(False) self.display_item(self.purchaser_object) # shows prices of each object in the # purchaser menu elif action[0] == P_HOVER: alert_w, alert_h = ALERT_P_HOVER_WIDTH, ALERT_P_HOVER_HEIGHT # grab the purchaser's position relative # to the menu x, y = action[1].get_position() # grab the menu's position if action[1].kind == KIND_TRAP: mx, my = self.trap_menu.get_position() else: mx, my = self.menu.get_position() # use the menu and purchaser positions # to properly place the alert w, h = action[1].get_dims() x += mx - .5 * ALERT_P_HOVER_WIDTH + .5 * w y = my - ALERT_P_HOVER_HEIGHT # if the alert is off of the screen # move it to the edge if x < 0: x = 0 if x + alert_w > self.width: x = self.width - alert_w if y < 0: y = 0 if y + alert_h > self.height: y = self.height - alert_h # substitute the dollar amount of the tower # into the alert's message message = action[1].get_hover_message() #ALERT_P_HOVER_MESSAGE %(action[1][3]) a = alert.Alert((x, y), alert_w, alert_h, message, True, ALERT_P_HOVER_DURATION) self.alerts.add(a) # if we clicked on a tower stop showing the range # of the previously selected tower and show this # tower's range elif action[0] == T_SELECTED: if self.sub_state == TD_FOLLOW: self.purchaser_object = None if self.selected is not None and self.selected.get_kind( ) == KIND_TOWER: self.selected.deactivate() self.selected = None self.selected = action[1] self.selected.activate() self.display_item(self.selected) self.sub_state = TD_SHOW # show new tower self.display_item(self.selected) # if a creep was clicked, display it elif action[0] == C_SELECTED: self.selected = action[1] self.sub_state = TD_SHOW self.display_item(self.selected) # if a creep died, remove it from the game elif action[0] == C_DEAD: if action[1] == self.selected: self.display.deactivate() self.selected = None self.sub_state = TD_IDLE self.money += action[1].get_value() self.creeps.remove(action[1]) # start a new wave when the new wave button is clicked elif action[0] == BUTTON_NEW_WAVE_MSG: self.begin_wave() # upgrade the selected tower, if any, when # the upgrade button is clicked elif action[0] == BUTTON_UPGRADE_MSG: if self.sub_state == TD_SHOW: if self.selected is not None and self.selected.kind == KIND_TOWER: if self.selected.can_be_upgraded(): cost = self.selected.get_upgrade_cost() if self.money >= cost: self.selected.upgrade() self.money -= cost self.display_item(self.selected) # sell the selected tower, if any, when # the sell button is clicked elif action[0] == BUTTON_SELL_MSG: if self.sub_state == TD_SHOW: if self.selected is not None and self.selected.kind != KIND_CREEP: # return the money self.money += self.selected.get_sell_amount() # free the space self.world.free_area(self.selected.get_position(), self.selected.get_dims()) if self.selected.kind == KIND_TOWER: # delete the tower for i in range(len(self.towers)): if self.towers[i].get_position( ) == self.selected.get_position(): self.towers.pop(i) break elif self.selected.kind == KIND_TRAP: # delete the trap for i in range(len(self.traps)): if self.traps[i].get_position( ) == self.selected.get_position(): self.traps.pop(i) break self.selected = None # change the game mode self.sub_state = TD_IDLE # update display self.display.deactivate() # reset the data when the quit button # is clicked elif action[0] == BUTTON_QUIT_MSG: self.empty_data() # update the tower-to-be-placed's range based # on whether or not it can be placed currently if self.purchaser_object is not None: if self.purchaser_object.kind == KIND_TOWER: if not self.world.can_build_tower( self.purchaser_object.get_position(), self.purchaser_object.get_dims()): self.purchaser_object.bad_pos() else: self.purchaser_object.good_pos() elif self.purchaser_object.kind == KIND_TRAP: if not self.world.can_build_trap( self.purchaser_object.get_position(), self.purchaser_object.get_dims()): self.purchaser_object.bad_pos() else: self.purchaser_object.good_pos()