Commit 2aa575f0 authored by Ulrich Sibiller's avatar Ulrich Sibiller Committed by Mike Gabriel

nxdialog: pylint improvements

parent 486cc6f5
...@@ -76,294 +76,301 @@ VALID_DLG_TYPES = frozenset([ ...@@ -76,294 +76,301 @@ VALID_DLG_TYPES = frozenset([
DLG_TYPE_QUIT, DLG_TYPE_QUIT,
DLG_TYPE_YESNO, DLG_TYPE_YESNO,
DLG_TYPE_YESNOSUSPEND, DLG_TYPE_YESNOSUSPEND,
]) ])
class PullDownMenu(object): class PullDownMenu(object):
""" Shows a popup menu to disconnect/terminate session. """ """ Shows a popup menu to disconnect/terminate session. """
def __init__(self, window_id): def __init__(self, window_id):
""" Initializes this class. """ Initializes this class.
@type window_id: int @type window_id: int
@param window_id: X11 window id of target window @param window_id: X11 window id of target window
""" """
self._window_id = window_id self.window_id = window_id
self._result = None self.result = None
def Show(self): def show(self):
""" Shows popup and returns result. """ """ Shows popup and returns result. """
win = gtk.gdk.window_foreign_new(self._window_id) win = gtk.gdk.window_foreign_new(self.window_id)
menu = gtk.Menu() menu = gtk.Menu()
menu.connect("deactivate", self._MenuDeactivate) menu.connect("deactivate", self.menu_deactivate)
# TODO: Show title item in bold font # TODO: Show title item in bold font
title = gtk.MenuItem(label="Session control") title = gtk.MenuItem(label="Session control")
title.set_sensitive(False) title.set_sensitive(False)
menu.append(title) menu.append(title)
disconnect = gtk.MenuItem(label=DISCONNECT_TEXT) disconnect = gtk.MenuItem(label=DISCONNECT_TEXT)
disconnect.connect("activate", self._ItemActivate, DISCONNECT) disconnect.connect("activate", self.item_activate, DISCONNECT)
menu.append(disconnect) menu.append(disconnect)
terminate = gtk.MenuItem(label=TERMINATE_TEXT) terminate = gtk.MenuItem(label=TERMINATE_TEXT)
terminate.connect("activate", self._ItemActivate, TERMINATE) terminate.connect("activate", self.item_activate, TERMINATE)
menu.append(terminate) menu.append(terminate)
menu.append(gtk.SeparatorMenuItem()) menu.append(gtk.SeparatorMenuItem())
cancel = gtk.MenuItem(label=CANCEL_TEXT) cancel = gtk.MenuItem(label=CANCEL_TEXT)
menu.append(cancel) menu.append(cancel)
menu.show_all() menu.show_all()
menu.popup(parent_menu_shell=None, parent_menu_item=None, menu.popup(parent_menu_shell=None, parent_menu_item=None,
func=self._PosMenu, data=win, func=self.pos_menu, data=win,
button=0, activate_time=gtk.get_current_event_time()) button=0, activate_time=gtk.get_current_event_time())
gtk.main() gtk.main()
return self._result return self.result
def _ItemActivate(self, _, result): def item_activate(self, _, result):
""" called when a menu item is selected """ """ called when a menu item is selected """
self._result = result self.result = result
gtk.main_quit() gtk.main_quit()
def _MenuDeactivate(self, _): @staticmethod
""" called when menu is deactivated """ def menu_deactivate(_):
gtk.main_quit() """ called when menu is deactivated """
gtk.main_quit()
def _PosMenu(self, menu, parent): @staticmethod
""" Positions menu at the top center of the parent window. """ def pos_menu(menu, parent):
# Get parent geometry and origin """ Positions menu at the top center of the parent window. """
(_, _, win_width, _, _) = parent.get_geometry() # Get parent geometry and origin
(win_x, win_y) = parent.get_origin() (_, _, win_width, _, _) = parent.get_geometry()
(win_x, win_y) = parent.get_origin()
# Calculate width of menu # Calculate width of menu
(menu_width, _) = menu.size_request() (menu_width, _) = menu.size_request()
# Calculate center # Calculate center
x = win_x + ((win_width - menu_width) / 2) center_x = win_x + ((win_width - menu_width) / 2)
return (x, win_y, True) return (center_x, win_y, True)
def ShowYesNoSuspendBox(title, text): def show_yes_no_suspend_box(title, text):
""" Shows a message box to disconnect/terminate session. """ Shows a message box to disconnect/terminate session.
@type title: str @type title: str
@param title: Message box title @param title: Message box title
@type text: str @type text: str
@param text: Message box text @param text: Message box text
@return: Choosen action @return: Choosen action
""" """
dlg = gtk.MessageDialog(type=gtk.MESSAGE_QUESTION, flags=gtk.DIALOG_MODAL) dlg = gtk.MessageDialog(type=gtk.MESSAGE_QUESTION, flags=gtk.DIALOG_MODAL)
dlg.set_title(title) dlg.set_title(title)
dlg.set_markup(text) dlg.set_markup(text)
dlg.add_button(DISCONNECT_TEXT, DISCONNECT) dlg.add_button(DISCONNECT_TEXT, DISCONNECT)
dlg.add_button(TERMINATE_TEXT, TERMINATE) dlg.add_button(TERMINATE_TEXT, TERMINATE)
dlg.add_button(CANCEL_TEXT, gtk.RESPONSE_CANCEL) dlg.add_button(CANCEL_TEXT, gtk.RESPONSE_CANCEL)
res = dlg.run() res = dlg.run()
if res in (DISCONNECT, TERMINATE): if res in (DISCONNECT, TERMINATE):
return res return res
# Everything else is cancel # Everything else is cancel
return None return None
def ShowYesNoBox(title, text): def show_yes_no_box(title, text):
""" Shows a message box with answers yes and no. """ Shows a message box with answers yes and no.
@type title: str @type title: str
@param title: Message box title @param title: Message box title
@type text: str @type text: str
@param text: Message box text @param text: Message box text
@return: Choosen action @return: Choosen action
""" """
dlg = gtk.MessageDialog(type=gtk.MESSAGE_QUESTION, flags=gtk.DIALOG_MODAL) dlg = gtk.MessageDialog(type=gtk.MESSAGE_QUESTION, flags=gtk.DIALOG_MODAL)
dlg.set_title(title) dlg.set_title(title)
dlg.set_markup(text) dlg.set_markup(text)
dlg.add_button(YES_TEXT, TERMINATE) dlg.add_button(YES_TEXT, TERMINATE)
dlg.add_button(NO_TEXT, gtk.RESPONSE_CANCEL) dlg.add_button(NO_TEXT, gtk.RESPONSE_CANCEL)
res = dlg.run() res = dlg.run()
if res == TERMINATE: if res == TERMINATE:
return res return res
# Everything else is cancel # Everything else is cancel
return None return None
def HandleSessionAction(agentpid, action): def handle_session_action(agentpid, action):
""" Execute session action choosen by user. """ Execute session action choosen by user.
@type agentpid: int @type agentpid: int
@param agentpid: Nxagent process id as passed by command line @param agentpid: Nxagent process id as passed by command line
@type action: int or None @type action: int or None
@param action: Choosen action @param action: Choosen action
""" """
if action == DISCONNECT: if action == DISCONNECT:
print "Disconnecting from session, sending SIGHUP to %s" % (agentpid) print "Disconnecting from session, sending SIGHUP to %s" % (agentpid)
os.kill(agentpid, signal.SIGHUP) os.kill(agentpid, signal.SIGHUP)
elif action == TERMINATE: elif action == TERMINATE:
print "Terminating session, sending SIGTERM to process %s" % (agentpid) print "Terminating session, sending SIGTERM to process %s" % (agentpid)
os.kill(agentpid, signal.SIGTERM) os.kill(agentpid, signal.SIGTERM)
elif action is None: elif action is None:
pass pass
else: else:
raise NotImplementedError() raise NotImplementedError()
def ShowSimpleMessageBox(icon, title, text): def show_simple_message_box(icon, title, text):
""" Shows a simple message box. """ Shows a simple message box.
@type icon: QMessageBox.Icon @type icon: QMessageBox.Icon
@param icon: Icon for message box @param icon: Icon for message box
@type title: str @type title: str
@param title: Message box title @param title: Message box title
@type text: str @type text: str
@param text: Message box text @param text: Message box text
""" """
dlg = gtk.MessageDialog(type=icon, flags=gtk.DIALOG_MODAL, dlg = gtk.MessageDialog(type=icon, flags=gtk.DIALOG_MODAL,
buttons=gtk.BUTTONS_OK) buttons=gtk.BUTTONS_OK)
dlg.set_title(title) dlg.set_title(title)
dlg.set_markup(text) dlg.set_markup(text)
dlg.run() dlg.run()
class NxDialogProgram(object): class NxDialogProgram(object):
""" the main program """ """ the main program """
def __init__(self):
self.args = None def __init__(self):
self.options = None self.args = None
self.options = None
def Main(self):
""" let's do something """ def main(self):
try: """ let's do something """
(self.options, self.args) = self.ParseArgs() try:
(self.options, self.args) = self.parse_args()
self.Run()
self.run()
except (SystemExit, KeyboardInterrupt):
raise except (SystemExit, KeyboardInterrupt):
raise
except Exception, e:
sys.stderr.write("Caught exception: %s\n" % (e)) except Exception, expt:
sys.exit(EXIT_FAILURE) sys.stderr.write("Caught exception: %s\n" % (expt))
sys.exit(EXIT_FAILURE)
def ParseArgs(self):
""" init parser """ def parse_args(self):
parser = optparse.OptionParser(option_list=self.BuildOptions(), """ init parser """
formatter=optparse.TitledHelpFormatter()) parser = optparse.OptionParser(option_list=self.build_options(),
return parser.parse_args() formatter=optparse.TitledHelpFormatter())
return parser.parse_args()
def BuildOptions(self):
""" build options for the parser """ @staticmethod
return [ def build_options():
# nxagent 3.5.99.18 only uses yesno, ok, pulldown and yesnosuspend """ build options for the parser """
# yesno dialogs will always kill the session if "yes" is selected return [
optparse.make_option("--dialog", type="string", dest="dialog_type", # nxagent 3.5.99.18 only uses yesno, ok, pulldown and yesnosuspend
help='type of dialog to show, one of "yesno", \ # yesno dialogs will always kill the session if "yes" is selected
"ok", "error", "panic", "quit", "pulldown", \ optparse.make_option("--dialog", type="string", dest="dialog_type",
"yesnosuspend"'), help='type of dialog to show, one of "yesno", \
optparse.make_option("--message", type="string", dest="text", "ok", "error", "panic", "quit", "pulldown", \
help="message text to display in the dialog"), "yesnosuspend"'),
optparse.make_option("--caption", type="string", dest="caption", optparse.make_option("--message", type="string", dest="text",
help="window title of the dialog"), help="message text to display in the dialog"),
optparse.make_option("--display", type="string", dest="display", optparse.make_option("--caption", type="string", dest="caption",
help="X11 display where the dialog should be \ help="window title of the dialog"),
shown"), optparse.make_option("--display", type="string", dest="display",
optparse.make_option("--parent", type="int", dest="agentpid", help="X11 display where the dialog should be \
help="pid of the nxagent"), shown"),
optparse.make_option("--window", type="int", dest="window", optparse.make_option("--parent", type="int", dest="agentpid",
help="id of window where to embed the \ help="pid of the nxagent"),
pulldown dialog type"), optparse.make_option("--window", type="int", dest="window",
# -class, -local, -allowmultiple are unused in nxlibs 3.5.99.18 help="id of window where to embed the \
optparse.make_option("--class", type="string", dest="dlgclass", pulldown dialog type"),
default="info", # -class, -local, -allowmultiple are unused in nxlibs 3.5.99.18
help="class of the message (info, warning, error) \ optparse.make_option("--class", type="string", dest="dlgclass",
default: info) [currently unimplemented]"), default="info",
optparse.make_option("--local", action="store_true", dest="local", help="class of the message (info, warning, error) \
help="specify that proxy mode is used \ default: info) [currently unimplemented]"),
optparse.make_option("--local", action="store_true", dest="local",
help="specify that proxy mode is used \
[currently unimplemented]"), [currently unimplemented]"),
optparse.make_option("--allowmultiple", action="store_true", optparse.make_option("--allowmultiple", action="store_true",
dest="allowmultiple", dest="allowmultiple",
help="allow launching more than one dialog with \ help="allow launching more than one dialog with \
the same message [currently unimplemented]"), the same message [currently unimplemented]"),
] ]
def Run(self): def run(self):
""" Disconnect/terminate NX session upon user's request. """ """ Disconnect/terminate NX session upon user's request. """
if not self.options.dialog_type: if not self.options.dialog_type:
sys.stderr.write("Dialog type not supplied via --type\n") sys.stderr.write("Dialog type not supplied via --type\n")
sys.exit(EXIT_FAILURE) sys.exit(EXIT_FAILURE)
dlgtype = self.options.dialog_type dlgtype = self.options.dialog_type
if dlgtype not in VALID_DLG_TYPES: if dlgtype not in VALID_DLG_TYPES:
sys.stderr.write("Invalid dialog type '%s'\n" % (dlgtype)) sys.stderr.write("Invalid dialog type '%s'\n" % (dlgtype))
sys.exit(EXIT_FAILURE) sys.exit(EXIT_FAILURE)
if dlgtype in (DLG_TYPE_PULLDOWN, if dlgtype in (DLG_TYPE_PULLDOWN,
DLG_TYPE_YESNOSUSPEND, DLG_TYPE_YESNOSUSPEND,
DLG_TYPE_YESNO) and not self.options.agentpid: DLG_TYPE_YESNO) and not self.options.agentpid:
sys.stderr.write("Agent pid not supplied via --parent\n") sys.stderr.write("Agent pid not supplied via --parent\n")
sys.exit(EXIT_FAILURE) sys.exit(EXIT_FAILURE)
if dlgtype == DLG_TYPE_PULLDOWN and not self.options.window: if dlgtype == DLG_TYPE_PULLDOWN and not self.options.window:
sys.stderr.write("Window id not supplied via --window\n") sys.stderr.write("Window id not supplied via --window\n")
sys.exit(EXIT_FAILURE) sys.exit(EXIT_FAILURE)
if self.options.caption: if self.options.caption:
message_caption = self.options.caption message_caption = self.options.caption
else: else:
message_caption = sys.argv[0] message_caption = sys.argv[0]
if self.options.text: if self.options.text:
message_text = self.options.text message_text = self.options.text
else: else:
message_text = "" message_text = ""
if self.options.display: if self.options.display:
os.environ["DISPLAY"] = self.options.display os.environ["DISPLAY"] = self.options.display
if dlgtype == DLG_TYPE_OK: if dlgtype == DLG_TYPE_OK:
ShowSimpleMessageBox(gtk.MESSAGE_INFO, message_caption, message_text) show_simple_message_box(
gtk.MESSAGE_INFO, message_caption, message_text)
elif dlgtype in (DLG_TYPE_ERROR, DLG_TYPE_PANIC): elif dlgtype in (DLG_TYPE_ERROR, DLG_TYPE_PANIC):
ShowSimpleMessageBox(gtk.MESSAGE_ERROR, message_caption, message_text) show_simple_message_box(
gtk.MESSAGE_ERROR, message_caption, message_text)
elif dlgtype == DLG_TYPE_PULLDOWN: elif dlgtype == DLG_TYPE_PULLDOWN:
HandleSessionAction(self.options.agentpid, handle_session_action(self.options.agentpid,
PullDownMenu(self.options.window).Show()) PullDownMenu(self.options.window).show())
elif dlgtype == DLG_TYPE_YESNOSUSPEND: elif dlgtype == DLG_TYPE_YESNOSUSPEND:
HandleSessionAction(self.options.agentpid, handle_session_action(self.options.agentpid,
ShowYesNoSuspendBox(message_caption, message_text)) show_yes_no_suspend_box(message_caption, message_text))
elif dlgtype == DLG_TYPE_YESNO: elif dlgtype == DLG_TYPE_YESNO:
HandleSessionAction(self.options.agentpid, handle_session_action(self.options.agentpid,
ShowYesNoBox(message_caption, message_text)) show_yes_no_box(message_caption, message_text))
else: else:
# TODO: Implement all dialog types # TODO: Implement all dialog types
sys.stderr.write("Dialog type '%s' not implemented" % (dlgtype)) sys.stderr.write("Dialog type '%s' not implemented" % (dlgtype))
sys.exit(EXIT_FAILURE) sys.exit(EXIT_FAILURE)
NxDialogProgram().Main() NxDialogProgram().main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment