def redraw(self): # clear the window, data might have changed self.window.erase() # formatting for the main view format = '%3s %-15s %-15s %-19s %-13s %-13s' # fields, as specified by model fields = self.interface.model.get_fields()[0:5] headers = [PNAModel.stringify(f) for f in fields] headers.insert(0, '#') my_str = format % tuple(headers) self.window.addstr(0, 0, my_str, curses.A_REVERSE) # make sure we have the most up-to-date data self.update_data() for (i, item) in enumerate(self.data): data = [i + 1] for f in fields: data.append(item[f]) my_str = format % tuple(data) if self.selection == i: self.window.addstr(i + 1, 0, my_str, curses.A_REVERSE) else: self.window.addstr(i + 1, 0, my_str) # move all the changes we've made to the actual screen self.window.refresh()
def redraw(self) : # clear the window, data might have changed self.window.erase() # formatting for the main view format = '%3s %-15s %-15s %-19s %-13s %-13s' # fields, as specified by model fields = self.interface.model.get_fields()[0:5] headers = [PNAModel.stringify(f) for f in fields] headers.insert(0, '#') my_str = format % tuple(headers) self.window.addstr(0, 0, my_str, curses.A_REVERSE) # make sure we have the most up-to-date data self.update_data() for (i, item) in enumerate(self.data) : data = [ i+1 ] for f in fields : data.append(item[f]) my_str = format % tuple(data) if self.selection == i : self.window.addstr(i+1, 0, my_str, curses.A_REVERSE) else : self.window.addstr(i+1, 0, my_str) # move all the changes we've made to the actual screen self.window.refresh()
def redraw(self) : settings = self.interface.model.settings # Stringify the title bar my_str = '' program = '(intop)' for action in self.actions : my_str += action[0][0]+':'+action[1]+' ' my_str += ' '*(self.width-len(my_str)-len(program)) my_str += program self.window.addstr(0, 0, my_str, curses.A_REVERSE) # Stringify the sort key my_str = ' Sort Key: ' my_str += PNAModel.stringify(settings['sort-key']) self.window.addstr(1, 0, my_str) # Stringify the threshold my_str = ' Threshold: ' my_str += str(settings['threshold']) self.window.addstr(2, 0, my_str) # Stringify the filters self.window.addstr(self.height-1, 0, ' '*(self.width-1)) filter_str = ' Filters: ' if settings['filters'] == None or settings['filters'] == {} : my_str = filter_str my_str += 'None' self.window.addstr(3, 0, my_str) else: for (i, filter) in enumerate(settings['filters']) : if i == 0 : my_str = filter_str elif (i+3) >= self.height : my_str = ' [press "f" for more filters]' pos_y = self.width-len(my_str)-1 self.window.addstr(i+2, pos_y, my_str) break else : my_str = ' '*len(filter_str) my_str += PNAModel.stringify(filter) + ' of ' my_str += str(settings['filters'][filter]) my_str += ' '*(self.width-len(my_str)-1) self.window.addstr(i+3, 0, my_str) # move all the changes we've made to the actual screen self.window.refresh()
def redraw(self): settings = self.interface.model.settings # Stringify the title bar my_str = '' program = '(intop)' for action in self.actions: my_str += action[0][0] + ':' + action[1] + ' ' my_str += ' ' * (self.width - len(my_str) - len(program)) my_str += program self.window.addstr(0, 0, my_str, curses.A_REVERSE) # Stringify the sort key my_str = ' Sort Key: ' my_str += PNAModel.stringify(settings['sort-key']) self.window.addstr(1, 0, my_str) # Stringify the threshold my_str = ' Threshold: ' my_str += str(settings['threshold']) self.window.addstr(2, 0, my_str) # Stringify the filters self.window.addstr(self.height - 1, 0, ' ' * (self.width - 1)) filter_str = ' Filters: ' if settings['filters'] == None or settings['filters'] == {}: my_str = filter_str my_str += 'None' self.window.addstr(3, 0, my_str) else: for (i, filter) in enumerate(settings['filters']): if i == 0: my_str = filter_str elif (i + 3) >= self.height: my_str = ' [press "f" for more filters]' pos_y = self.width - len(my_str) - 1 self.window.addstr(i + 2, pos_y, my_str) break else: my_str = ' ' * len(filter_str) my_str += PNAModel.stringify(filter) + ' of ' my_str += str(settings['filters'][filter]) my_str += ' ' * (self.width - len(my_str) - 1) self.window.addstr(i + 3, 0, my_str) # move all the changes we've made to the actual screen self.window.refresh()
def view_details(self): # Make sure there is something available if 0 > self.selection or self.selection > len(self.data): curses.beep() return # Get and format the filter list name_len = 15 value_len = 17 format = ' %' + str(name_len) + 's: %-' + str(value_len) + 's' items = [] max_width = 0 for item in self.interface.model.get_fields(headers=False): name = PNAModel.stringify(item) if item[0:3] in ('tcp', 'udp') and item[3:] == '-tuples': ports = [] for tuple in self.data[self.selection][item]: local_port = str(tuple['local-port']) remote_port = str(tuple['remote-port']) ports.append(local_port + ':' + remote_port) value = ', '.join(ports) else: value = str(self.data[self.selection][item]) my_str = format % (name, value) if len(my_str) > self.width - 2: my_str = my_str[0:self.width - 5] my_str += '...' if len(my_str) > max_width: max_width = len(my_str) items.append(my_str) # Build selection box x = (self.width - (max_width + 2)) / 2 detail_box = curses.newwin(len(items) + 2, max_width + 2, 6, x) detail_box.box() detail_pane = curses.panel.new_panel(detail_box) # Establish interface for selection box while True: # Display view for (i, item) in enumerate(items): detail_box.addstr(i + 1, 1, item) detail_box.move(0, 0) detail_box.refresh() # Interaction try: key = self.interface.getkey(detail_box) except curses.error: continue if key in ( 'q', 'Q', ascii.ESC, '\r', '\n', ): break detail_pane.hide() detail_box.erase()
def set_sort_key(self) : settings = self.interface.model.settings current_key = settings['sort-key'] # Get and format the available keys format = '%-15s' sort_keys = [] for key in self.interface.model.sort_keys : sort_keys.append(format % PNAModel.stringify(key)) # Build selection box key_box = curses.newwin(len(sort_keys)+2, len(sort_keys[0])+2, 0, 11) key_box.box() key_pane = curses.panel.new_panel(key_box) # Establish interface for selection box selection = list(self.interface.model.sort_keys).index(current_key) while True : # Display view for (i, key) in enumerate(sort_keys) : if i == selection : key_box.addstr(i+1, 1, key, curses.A_REVERSE) else : key_box.addstr(i+1, 1, key) key_box.move(selection+1, 1) key_box.refresh() # Interaction try : key = self.interface.getkey(key_box) except curses.error : continue if key in ('q','Q',ascii.ESC,) : break elif key in ('\r', '\n',) : settings['sort-key'] = self.interface.model.sort_keys[selection] break elif key in ('k', curses.KEY_UP,) : selection -= 1 if selection < 0 : selection = 0 curses.beep() elif key in ('j', curses.KEY_DOWN,) : selection += 1 if selection > len(sort_keys) - 1 : selection = len(sort_keys) - 1 curses.beep() elif key in ('h', curses.KEY_LEFT,) : if selection == 0 : curses.beep() selection = 0 elif key in ('l', curses.KEY_RIGHT,) : if selection == len(sort_keys) - 1 : curses.beep() selection = len(sort_keys) - 1 key_pane.hide() key_box.erase() self.interface.windows['information'].clear()
def view_details(self) : # Make sure there is something available if 0 > self.selection or self.selection > len(self.data): curses.beep() return # Get and format the filter list name_len = 15 value_len = 17 format = ' %'+str(name_len)+'s: %-'+str(value_len)+'s' items = [] max_width = 0 for item in self.interface.model.get_fields(headers=False) : name = PNAModel.stringify(item) if item[0:3] in ('tcp', 'udp') and item[3:] == '-tuples' : ports = [] for tuple in self.data[self.selection][item] : local_port = str(tuple['local-port']) remote_port = str(tuple['remote-port']) ports.append(local_port+':'+remote_port) value = ', '.join(ports) else : value = str(self.data[self.selection][item]) my_str = format % (name, value) if len(my_str) > self.width - 2: my_str = my_str[0:self.width-5] my_str += '...' if len(my_str) > max_width : max_width = len(my_str) items.append(my_str) # Build selection box x = (self.width - (max_width + 2)) / 2 detail_box = curses.newwin(len(items)+2, max_width+2, 6, x) detail_box.box() detail_pane = curses.panel.new_panel(detail_box) # Establish interface for selection box while True : # Display view for (i, item) in enumerate(items) : detail_box.addstr(i+1, 1, item) detail_box.move(0,0) detail_box.refresh() # Interaction try : key = self.interface.getkey(detail_box) except curses.error : continue if key in ('q','Q',ascii.ESC,'\r','\n',) : break detail_pane.hide() detail_box.erase()
def cui_main(screen, directory, static, begin, end): # parse through the command line arguments (check if -d option) model = PNAModel() if static != None and directory != None: sys.exit(2) elif directory != None: add_data(model, directory, begin, end) elif static != None: add_data(model, static, begin, end) if directory: #print 'Directory to watch for updates needs to be specified' dirwatch = DirectoryWatcher(model, directory) interface = CursesInterface(screen, model, dirwatch) interface.interact() else: for file in args: model.add_file(file) interface = CursesInterface(screen, model, None) interface.interact() """
def cui_main(screen, directory, static, begin, end) : # parse through the command line arguments (check if -d option) model = PNAModel() if static != None and directory != None : sys.exit(2) elif directory != None : add_data(model, directory, begin, end) elif static != None : add_data(model, static, begin, end) if directory : #print 'Directory to watch for updates needs to be specified' dirwatch = DirectoryWatcher(model, directory) interface = CursesInterface(screen, model, dirwatch) interface.interact() else : for file in args : model.add_file(file) interface = CursesInterface(screen, model, None) interface.interact() """
def view_filters(self) : # Get and format the filter list name_len = 17 value_len = 21 format = ' %'+str(name_len)+'s: %-'+str(value_len)+'s' settings = self.interface.model.settings filters = [] for filter in self.interface.model.filters : name_str = PNAModel.stringify(filter) value_str = settings['filters'].get(filter, '') my_str = format % (name_str, value_str) filters.append(my_str) # Build selection box filter_box = curses.newwin(len(filters)+2, len(filters[0])+2, 2, 11) filter_box.box() filter_pane = curses.panel.new_panel(filter_box) # Establish interface for selection box selection = 0 while True : # Display view for (i, filter) in enumerate(filters) : if i == selection : filter_box.addstr(i+1, 1, filter, curses.A_REVERSE) else : filter_box.addstr(i+1, 1, filter) filter_box.move(selection+1, 1) filter_box.refresh() # Interaction try : key = self.interface.getkey(filter_box) except curses.error : continue if key in ('q','Q',ascii.ESC,) : break elif key in ('\r', '\n',) : # Get selected filter name filter = self.interface.model.filters[selection] regex = self.interface.model.filter_res[selection] filter_box.addstr(selection+1, 1, filters[selection]) filter_box.refresh() # Get some settings value = settings['filters'].get(filter, '') chars = string.letters+string.digits+'.-:/ ' y = selection+3 x = 12+name_len+3 # Get the new value (break on match) edit_box = EditField(self.interface, value_len, y, x) while True : # find current value and get new value value = edit_box.get_value(initial=value, chars=chars) # see if value matches acceptable format if value == '' or None != regex.match(value) : break curses.beep() if value == '' : if filter in settings['filters'] : del settings['filters'][filter] else : settings['filters'][filter] = value name_str = PNAModel.stringify(filter) filters[selection] = format % (name_str, value) elif key in (ascii.BS, ascii.DEL,) : # Clear the selected filter filter = self.interface.model.filters[selection] if filter in settings['filters'] : del settings['filters'][filter] name_str = PNAModel.stringify(filter) filters[selection] = format % (name_str, '') elif key in ('k', curses.KEY_UP,) : # Move selection up selection -= 1 if selection < 0 : selection = 0 curses.beep() elif key in ('j', curses.KEY_DOWN,) : # Move selection down selection += 1 if selection > len(filters) - 1 : selection = len(filters) - 1 curses.beep() elif key in ('h', curses.KEY_LEFT,) : # Move selection left if selection == 0 : curses.beep() selection = 0 elif key in ('l', curses.KEY_RIGHT,) : # Move selection right if selection == len(filters) - 1 : curses.beep() selection = len(filters) - 1 filter_pane.hide() filter_box.erase() self.interface.windows['information'].clear()
def view_filters(self): # Get and format the filter list name_len = 17 value_len = 21 format = ' %' + str(name_len) + 's: %-' + str(value_len) + 's' settings = self.interface.model.settings filters = [] for filter in self.interface.model.filters: name_str = PNAModel.stringify(filter) value_str = settings['filters'].get(filter, '') my_str = format % (name_str, value_str) filters.append(my_str) # Build selection box filter_box = curses.newwin( len(filters) + 2, len(filters[0]) + 2, 2, 11) filter_box.box() filter_pane = curses.panel.new_panel(filter_box) # Establish interface for selection box selection = 0 while True: # Display view for (i, filter) in enumerate(filters): if i == selection: filter_box.addstr(i + 1, 1, filter, curses.A_REVERSE) else: filter_box.addstr(i + 1, 1, filter) filter_box.move(selection + 1, 1) filter_box.refresh() # Interaction try: key = self.interface.getkey(filter_box) except curses.error: continue if key in ( 'q', 'Q', ascii.ESC, ): break elif key in ( '\r', '\n', ): # Get selected filter name filter = self.interface.model.filters[selection] regex = self.interface.model.filter_res[selection] filter_box.addstr(selection + 1, 1, filters[selection]) filter_box.refresh() # Get some settings value = settings['filters'].get(filter, '') chars = string.letters + string.digits + '.-:/ ' y = selection + 3 x = 12 + name_len + 3 # Get the new value (break on match) edit_box = EditField(self.interface, value_len, y, x) while True: # find current value and get new value value = edit_box.get_value(initial=value, chars=chars) # see if value matches acceptable format if value == '' or None != regex.match(value): break curses.beep() if value == '': if filter in settings['filters']: del settings['filters'][filter] else: settings['filters'][filter] = value name_str = PNAModel.stringify(filter) filters[selection] = format % (name_str, value) elif key in ( ascii.BS, ascii.DEL, ): # Clear the selected filter filter = self.interface.model.filters[selection] if filter in settings['filters']: del settings['filters'][filter] name_str = PNAModel.stringify(filter) filters[selection] = format % (name_str, '') elif key in ( 'k', curses.KEY_UP, ): # Move selection up selection -= 1 if selection < 0: selection = 0 curses.beep() elif key in ( 'j', curses.KEY_DOWN, ): # Move selection down selection += 1 if selection > len(filters) - 1: selection = len(filters) - 1 curses.beep() elif key in ( 'h', curses.KEY_LEFT, ): # Move selection left if selection == 0: curses.beep() selection = 0 elif key in ( 'l', curses.KEY_RIGHT, ): # Move selection right if selection == len(filters) - 1: curses.beep() selection = len(filters) - 1 filter_pane.hide() filter_box.erase() self.interface.windows['information'].clear()
def set_sort_key(self): settings = self.interface.model.settings current_key = settings['sort-key'] # Get and format the available keys format = '%-15s' sort_keys = [] for key in self.interface.model.sort_keys: sort_keys.append(format % PNAModel.stringify(key)) # Build selection box key_box = curses.newwin( len(sort_keys) + 2, len(sort_keys[0]) + 2, 0, 11) key_box.box() key_pane = curses.panel.new_panel(key_box) # Establish interface for selection box selection = list(self.interface.model.sort_keys).index(current_key) while True: # Display view for (i, key) in enumerate(sort_keys): if i == selection: key_box.addstr(i + 1, 1, key, curses.A_REVERSE) else: key_box.addstr(i + 1, 1, key) key_box.move(selection + 1, 1) key_box.refresh() # Interaction try: key = self.interface.getkey(key_box) except curses.error: continue if key in ( 'q', 'Q', ascii.ESC, ): break elif key in ( '\r', '\n', ): settings['sort-key'] = self.interface.model.sort_keys[ selection] break elif key in ( 'k', curses.KEY_UP, ): selection -= 1 if selection < 0: selection = 0 curses.beep() elif key in ( 'j', curses.KEY_DOWN, ): selection += 1 if selection > len(sort_keys) - 1: selection = len(sort_keys) - 1 curses.beep() elif key in ( 'h', curses.KEY_LEFT, ): if selection == 0: curses.beep() selection = 0 elif key in ( 'l', curses.KEY_RIGHT, ): if selection == len(sort_keys) - 1: curses.beep() selection = len(sort_keys) - 1 key_pane.hide() key_box.erase() self.interface.windows['information'].clear()
def main(arguments): model = PNAModel() interface = CommandLineInterface(arguments, model) interface.interact()