1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Apply PEP 8 style guidance to zenmap

Using the pep8 tool (https://pypi.python.org/pypi/pep8), fixed the
following style issues:

Count   Issue
11      E201 whitespace after '['
8       E203 whitespace before ','
41      E211 whitespace before '('
11      E221 multiple spaces before operator
61      E225 missing whitespace around operator
237     E231 missing whitespace after ':'
91      E251 no spaces around keyword / parameter equals
19      E261 at least two spaces before inline comment
41      E301 expected 1 blank line, found 0
200     E302 expected 2 blank lines, found 1
356     E303 too many blank lines (2)
563     E501 line too long (106 characters)
39      E701 multiple statements on one line (colon)
13      E702 multiple statements on one line (semicolon)
4       W291 trailing whitespace
2       W293 blank line contains whitespace
8       W391 blank line at end of file
21      W601 .has_key() is deprecated, use 'in'
2       W602 deprecated form of raising exception

The remaining issues are long lines due to very deep data structures. I
chose not to alter them, as it would involve backslash-continuation
where whitespace is not permitted:

./zenmapGUI/ScanInterface.py:323:80: E501 line too long (90 characters)
./zenmapGUI/ScanInterface.py:456:80: E501 line too long (84 characters)
./zenmapGUI/ScanInterface.py:464:80: E501 line too long (84 characters)
./zenmapGUI/ScanInterface.py:472:80: E501 line too long (122 characters)
./zenmapGUI/ScanInterface.py:479:80: E501 line too long (122 characters)
./zenmapGUI/ScanInterface.py:920:80: E501 line too long (94 characters)
./zenmapGUI/ScanInterface.py:923:80: E501 line too long (93 characters)
./zenmapGUI/MainWindow.py:575:80: E501 line too long (99 characters)
./zenmapGUI/MainWindow.py:906:80: E501 line too long (99 characters)
This commit is contained in:
dmiller
2014-01-08 19:50:22 +00:00
parent 9210a7f1fa
commit 5c662fffdc
100 changed files with 2287 additions and 1814 deletions

View File

@@ -159,6 +159,7 @@ hildon = None
if is_maemo():
import hildon
class UmitScanWindow(hildon.Window):
def __init__(self):
hildon.Window.__init__(self)
@@ -174,9 +175,10 @@ else:
HIGMainWindow.__init__(self)
self.vbox = gtk.VBox()
def can_print():
"""Return true if we have printing operations (PyGTK 2.10 or later) or false
otherwise."""
"""Return true if we have printing operations (PyGTK 2.10 or later) or
false otherwise."""
try:
gtk.PrintOperation
except AttributeError:
@@ -184,6 +186,7 @@ def can_print():
else:
return True
class ScanWindow(UmitScanWindow):
def __init__(self):
UmitScanWindow.__init__(self)
@@ -199,7 +202,7 @@ class ScanWindow(UmitScanWindow):
# self.vbox is a container for the menubar and the scan interface
self.add(self.vbox)
self.connect ('delete-event', self._exit_cb)
self.connect('delete-event', self._exit_cb)
self._create_ui_manager()
self._create_menubar()
self._create_scan_interface()
@@ -433,25 +436,29 @@ class ScanWindow(UmitScanWindow):
def _search_scan_result(self, widget):
"""Displays a search window."""
search_window = SearchWindow(self._load_search_result, self._append_search_result)
search_window = SearchWindow(
self._load_search_result, self._append_search_result)
search_window.show_all()
def _filter_cb(self, widget):
self.scan_interface.toggle_filter_bar()
def _load_search_result(self, results):
"""This function is passed as an argument to the SearchWindow.__init__ method.
When the user selects scans in the search window and clicks on \"Open\", this
function is called to load each of the selected scans into a new window."""
"""This function is passed as an argument to the SearchWindow.__init__
method. When the user selects scans in the search window and clicks on
"Open", this function is called to load each of the selected scans into
a new window."""
for result in results:
self._load(self.get_empty_interface(), parsed_result = results[result][1])
self._load(self.get_empty_interface(),
parsed_result=results[result][1])
def _append_search_result(self, results):
"""This function is passed as an argument to the SearchWindow.__init__ method.
When the user selects scans in the search window and clicks on \"Append\", this
function is called to append the selected scans into the current window."""
"""This function is passed as an argument to the SearchWindow.__init__
method. When the user selects scans in the search window and clicks on
"Append", this function is called to append the selected scans into the
current window."""
for result in results:
self._load(self.scan_interface, parsed_result = results[result][1])
self._load(self.scan_interface, parsed_result=results[result][1])
def store_result(self, scan_interface):
"""Stores the network inventory into the database."""
@@ -459,23 +466,28 @@ class ScanWindow(UmitScanWindow):
try:
scan_interface.inventory.save_to_db()
except Exception, e:
alert = HIGAlertDialog(message_format = _("Can't save to database"),
secondary_text = _("Can't store unsaved scans to the recent scans database:\n%s") % str(e))
alert = HIGAlertDialog(
message_format=_("Can't save to database"),
secondary_text=_("Can't store unsaved scans to the "
"recent scans database:\n%s") % str(e))
alert.run()
alert.destroy()
log.debug(">>> Can't save result to database: %s." % str(e))
def get_recent_scans(self):
"""Gets seven most recent scans and appends them to the default UI definition."""
"""Gets seven most recent scans and appends them to the default UI
definition."""
r_scans = recent_scans.get_recent_scans_list()
new_rscan_xml = ''
for scan in r_scans[:7]:
scan = scan.replace('\n','')
if os.access(split(scan)[0],os.R_OK) and isfile(scan):
scan = scan.replace('\n','')
new_rscan = (scan, None, scan, None, scan, self._load_recent_scan)
new_rscan_xml += "<menuitem action=%s/>\n" % xml.sax.saxutils.quoteattr(scan)
scan = scan.replace('\n', '')
if os.access(split(scan)[0], os.R_OK) and isfile(scan):
scan = scan.replace('\n', '')
new_rscan = (
scan, None, scan, None, scan, self._load_recent_scan)
new_rscan_xml += "<menuitem action=%s/>\n" % (
xml.sax.saxutils.quoteattr(scan))
self.main_actions.append(new_rscan)
else:
@@ -501,16 +513,19 @@ class ScanWindow(UmitScanWindow):
self.menubar.show_all()
def _create_scan_interface(self):
self.scan_interface.scan_result.scan_result_notebook.scans_list.append_button.connect("clicked", self._append_scan_results_cb)
self.scan_interface.scan_result.scan_result_notebook.nmap_output.connect("changed", self._displayed_scan_change_cb)
notebook = self.scan_interface.scan_result.scan_result_notebook
notebook.scans_list.append_button.connect(
"clicked", self._append_scan_results_cb)
notebook.nmap_output.connect("changed", self._displayed_scan_change_cb)
self._displayed_scan_change_cb(None)
self.scan_interface.show_all()
self.vbox.pack_start(self.scan_interface, True, True, 0)
def show_open_dialog(self, title = None):
def show_open_dialog(self, title=None):
"""Show a load file chooser and return the filename chosen."""
if self._results_filechooser_dialog is None:
self._results_filechooser_dialog = ResultsFileChooserDialog(title = title)
self._results_filechooser_dialog = ResultsFileChooserDialog(
title=title)
filename = None
response = self._results_filechooser_dialog.run()
@@ -519,8 +534,9 @@ class ScanWindow(UmitScanWindow):
elif response == RESPONSE_OPEN_DIRECTORY:
filename = self._results_filechooser_dialog.get_filename()
# Check if the selected filename is a directory. If not, we take only the
# directory part of the path, omitting the actual name of the selected file.
# Check if the selected filename is a directory. If not, we take
# only the directory part of the path, omitting the actual name of
# the selected file.
if filename is not None and not os.path.isdir(filename):
filename = os.path.dirname(filename)
@@ -528,8 +544,9 @@ class ScanWindow(UmitScanWindow):
return filename
def _load_scan_results_cb(self, p):
"""'Open Scan' callback function. Displays a file chooser dialog and loads the
scan from the selected file or from the selected directory."""
"""'Open Scan' callback function. Displays a file chooser dialog and
loads the scan from the selected file or from the selected
directory."""
filename = self.show_open_dialog(p.get_name())
if filename is not None:
scan_interface = self.get_empty_interface()
@@ -539,8 +556,8 @@ class ScanWindow(UmitScanWindow):
self._load(scan_interface, filename)
def _append_scan_results_cb(self, p):
"""'Append Scan' callback function. Displays a file chooser dialog and appends the
scan from the selected file into the current window."""
"""'Append Scan' callback function. Displays a file chooser dialog and
appends the scan from the selected file into the current window."""
filename = self.show_open_dialog(p.get_name())
if filename is not None:
if os.path.isdir(filename):
@@ -559,7 +576,8 @@ class ScanWindow(UmitScanWindow):
widget.set_sensitive(entry is not None)
def _load_recent_scan(self, widget):
"""A helper function for loading a recent scan directly from the menu."""
"""A helper function for loading a recent scan directly from the
menu."""
self._load(self.get_empty_interface(), widget.get_name())
def _load(self, scan_interface, filename=None, parsed_result=None):
@@ -587,54 +605,59 @@ class ScanWindow(UmitScanWindow):
def _load_directory(self, scan_interface, directory):
for file in os.listdir(directory):
if os.path.isdir(os.path.join(directory,file)):
if os.path.isdir(os.path.join(directory, file)):
continue
self._load(scan_interface, filename = os.path.join(directory, file))
self._load(scan_interface, filename=os.path.join(directory, file))
def _save_scan_results_cb(self, widget):
"""'Save Scan' callback function. If it's OK to save the scan, it displays a
'Save File' dialog and saves the scan. If not, it displays an appropriate
alert dialog."""
"""'Save Scan' callback function. If it's OK to save the scan, it
displays a 'Save File' dialog and saves the scan. If not, it displays
an appropriate alert dialog."""
num_scans = len(self.scan_interface.inventory.get_scans())
if num_scans == 0:
alert = HIGAlertDialog(message_format=_('Nothing to save'),
secondary_text=_("""\
There are no scans with results to be saved. Run a scan with the "Scan" button \
first."""))
alert = HIGAlertDialog(
message_format=_('Nothing to save'),
secondary_text=_(
'There are no scans with results to be saved. '
'Run a scan with the "Scan" button first.'))
alert.run()
alert.destroy()
return
num_scans_running = self.scan_interface.num_scans_running()
if num_scans_running > 0:
if num_scans_running == 1:
text = _("There is a scan still running. Wait until it finishes and then save.")
text = _("There is a scan still running. "
"Wait until it finishes and then save.")
else:
text = _("There are %u scans still running. Wait until they finish and then save.")\
% num_scans_running
text = _("There are %u scans still running. Wait until they "
"finish and then save.") % num_scans_running
alert = HIGAlertDialog(message_format=_('Scan is running'),
secondary_text=text)
alert.run()
alert.destroy()
return
# If there's more than one scan in the inventory, display a warning dialog saying
# that only the most recent scan will be saved
# If there's more than one scan in the inventory, display a warning
# dialog saying that only the most recent scan will be saved
selected = 0
if num_scans > 1:
#text = _("You have %u scans loaded in the current view. Only the most recent scan " \
# "will be saved." % num_scans)
#alert = HIGAlertDialog(message_format=_("More than one scan loaded"),
# secondary_text=text)
#text = _("You have %u scans loaded in the current view. "
# "Only the most recent scan will be saved." % num_scans)
#alert = HIGAlertDialog(
# message_format=_("More than one scan loaded"),
# secondary_text=text)
#alert.run()
#alert.destroy()
dlg = HIGDialog(title="Choose a scan to save",
parent=self,
flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_SAVE, gtk.RESPONSE_OK))
dlg.vbox.pack_start(gtk.Label("You have %u scans loaded in the current view.\n" \
"Select the scan which you would like to save." \
% num_scans), False)
dlg = HIGDialog(
title="Choose a scan to save",
parent=self,
flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_SAVE, gtk.RESPONSE_OK))
dlg.vbox.pack_start(gtk.Label(
"You have %u scans loaded in the current view.\n"
"Select the scan which you would like to save." % num_scans),
False)
scan_combo = gtk.combo_box_new_text()
for scan in self.scan_interface.inventory.get_scans():
scan_combo.append_text(scan.nmap_command)
@@ -653,7 +676,8 @@ first."""))
SaveResultsFileChooserDialog(title=_('Save Scan'))
# Supply a default file name if this scan was previously saved.
if self.scan_interface.saved_filename:
self._save_results_filechooser_dialog.set_filename(self.scan_interface.saved_filename)
self._save_results_filechooser_dialog.set_filename(
self.scan_interface.saved_filename)
response = self._save_results_filechooser_dialog.run()
@@ -680,19 +704,21 @@ This scan has not been run yet. Start the scan with the "Scan" button first.'))
num_scans_running = self.scan_interface.num_scans_running()
if num_scans_running > 0:
if num_scans_running == 1:
text = _("There is a scan still running. Wait until it finishes and then save.")
text = _("There is a scan still running. "
"Wait until it finishes and then save.")
else:
text = _("There are %u scans still running. Wait until they finish and then save.")\
% num_scans_running
text = _("There are %u scans still running. Wait until they "
"finish and then save.") % num_scans_running
alert = HIGAlertDialog(message_format=_('Scan is running'),
secondary_text=text)
alert.run()
alert.destroy()
return
# We have multiple scans in our network inventory, so we need to display a directory
# chooser dialog
dir_chooser = SaveToDirectoryChooserDialog(title=_("Choose a directory to save scans into"))
# We have multiple scans in our network inventory, so we need to
# display a directory chooser dialog
dir_chooser = SaveToDirectoryChooserDialog(
title=_("Choose a directory to save scans into"))
if dir_chooser.run() == gtk.RESPONSE_OK:
self._save_all(self.scan_interface, dir_chooser.get_filename())
dir_chooser.destroy()
@@ -729,16 +755,20 @@ This scan has not been run yet. Start the scan with the "Scan" button first.'))
recent_scans.add_recent_scan(filename)
recent_scans.save()
def _save(self, scan_interface, saved_filename, selected_index, format = "xml"):
def _save(self, scan_interface, saved_filename, selected_index,
format="xml"):
"""Saves the scan into a file with a given filename. Displays an alert
dialog if the save fails."""
log.debug(">>> File being saved: %s" % saved_filename)
try:
scan_interface.inventory.save_to_file(saved_filename, selected_index, format)
scan_interface.inventory.get_scans()[selected_index].unsaved = False
scan_interface.inventory.save_to_file(
saved_filename, selected_index, format)
scan_interface.inventory.get_scans()[selected_index].unsaved = \
False
except (OSError, IOError), e:
alert = HIGAlertDialog(message_format=_('Can\'t save file'),
secondary_text=_('Can\'t open file to write.\n%s') % str(e))
alert = HIGAlertDialog(
message_format=_("Can't save file"),
secondary_text=_("Can't open file to write.\n%s") % str(e))
alert.run()
alert.destroy()
else:
@@ -766,12 +796,16 @@ This scan has not been run yet. Start the scan with the "Scan" button first.'))
return w
def _new_scan_profile_cb(self, p):
pe = ProfileEditor(command=self.scan_interface.command_toolbar.command, deletable=False)
pe = ProfileEditor(
command=self.scan_interface.command_toolbar.command,
deletable=False)
pe.set_scan_interface(self.scan_interface)
pe.show_all()
def _edit_scan_profile_cb(self, p):
pe = ProfileEditor(profile_name=self.scan_interface.toolbar.selected_profile,deletable=True,overwrite=True)
pe = ProfileEditor(
profile_name=self.scan_interface.toolbar.selected_profile,
deletable=True, overwrite=True)
pe.set_scan_interface(self.scan_interface)
pe.show_all()
@@ -779,18 +813,20 @@ This scan has not been run yet. Start the scan with the "Scan" button first.'))
show_help()
def _exit_cb(self, *args):
"""Closes the window, prompting for confirmation if necessary. If one of
the tabs couldn't be closed, the function returns True and doesn't exit
the application."""
"""Closes the window, prompting for confirmation if necessary. If one
of the tabs couldn't be closed, the function returns True and doesn't
exit the application."""
if self.scan_interface.changed:
log.debug("Found changes on closing window")
dialog = HIGDialog(buttons=(_('Close anyway').encode('utf-8'), gtk.RESPONSE_CLOSE,
gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
dialog = HIGDialog(
buttons=(_('Close anyway').encode('utf-8'),
gtk.RESPONSE_CLOSE, gtk.STOCK_CANCEL,
gtk.RESPONSE_CANCEL))
alert = HIGEntryLabel('<b>%s</b>' % _("Unsaved changes"))
text = HIGEntryLabel(_('The given scan has unsaved changes.\n\
What do you want to do?'))
text = HIGEntryLabel(_("The given scan has unsaved changes.\n"
"What do you want to do?"))
hbox = HIGHBox()
hbox.set_border_width(5)
hbox.set_spacing(12)
@@ -800,7 +836,8 @@ What do you want to do?'))
vbox.set_spacing(12)
image = gtk.Image()
image.set_from_stock(gtk.STOCK_DIALOG_QUESTION,gtk.ICON_SIZE_DIALOG)
image.set_from_stock(
gtk.STOCK_DIALOG_QUESTION, gtk.ICON_SIZE_DIALOG)
vbox.pack_start(alert)
vbox.pack_start(text)
@@ -822,13 +859,16 @@ What do you want to do?'))
elif self.scan_interface.num_scans_running() > 0:
log.debug("Trying to close a window with a running scan")
dialog = HIGDialog(buttons=(_('Close anyway').encode('utf-8'), gtk.RESPONSE_CLOSE,
gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
dialog = HIGDialog(
buttons=(_('Close anyway').encode('utf-8'),
gtk.RESPONSE_CLOSE, gtk.STOCK_CANCEL,
gtk.RESPONSE_CANCEL))
alert = HIGEntryLabel('<b>%s</b>' % _("Trying to close"))
text = HIGEntryLabel(_('The window you are trying to close has a scan \
running at the background.\nWhat do you want to do?'))
text = HIGEntryLabel(_(
"The window you are trying to close has a scan running in "
"the background.\nWhat do you want to do?"))
hbox = HIGHBox()
hbox.set_border_width(5)
hbox.set_spacing(12)
@@ -838,7 +878,8 @@ running at the background.\nWhat do you want to do?'))
vbox.set_spacing(12)
image = gtk.Image()
image.set_from_stock(gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG)
image.set_from_stock(
gtk.STOCK_DIALOG_WARNING, gtk.ICON_SIZE_DIALOG)
vbox.pack_start(alert)
vbox.pack_start(text)
@@ -865,7 +906,8 @@ running at the background.\nWhat do you want to do?'))
entry = self.scan_interface.scan_result.scan_result_notebook.nmap_output.get_active_entry()
if entry is None:
return False
zenmapGUI.Print.run_print_operation(self.scan_interface.inventory, entry)
zenmapGUI.Print.run_print_operation(
self.scan_interface.inventory, entry)
def _quit_cb(self, *args):
"""Close all open windows."""
@@ -874,12 +916,15 @@ running at the background.\nWhat do you want to do?'))
if window._exit_cb():
break
def _load_diff_compare_cb (self, widget=None, extra=None):
"""Loads all active scans into a dictionary, passes it to the DiffWindow
constructor, and then displays the 'Compare Results' window."""
self.diff_window = DiffWindow(self.scan_interface.inventory.get_scans())
def _load_diff_compare_cb(self, widget=None, extra=None):
"""Loads all active scans into a dictionary, passes it to the
DiffWindow constructor, and then displays the 'Compare Results'
window."""
self.diff_window = DiffWindow(
self.scan_interface.inventory.get_scans())
self.diff_window.show_all()
def show_help():
import urllib
import webbrowser