Skip to content

Commit

Permalink
Update desktop module for Python 3
Browse files Browse the repository at this point in the history
Shamelessly ripped from @revolunet's markdown preview package.
  • Loading branch information
FichteFoll committed Mar 8, 2014
1 parent c03d7e5 commit 7c6d940
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
52 changes: 32 additions & 20 deletions desktop/__init__.py
Expand Up @@ -42,6 +42,7 @@
desktop.open("http://www.python.org", "KDE") # Insists on KDE
desktop.open("http://www.python.org", "GNOME") # Insists on GNOME
desktop.open("http://www.python.org", "MATE") # Insists on MATE
Without overriding using the desktop parameter, the open function will attempt
to use the "standard" desktop opening mechanism which is controlled by the
Expand Down Expand Up @@ -115,7 +116,7 @@ def _status(cmd, shell):
opener.wait()
return opener.poll() == 0

import commands
import subprocess

# Private functions.

Expand All @@ -135,15 +136,15 @@ def _is_xfce():
# XFCE detection involves testing the output of a program.

try:
return _readfrom(_get_x11_vars() + "xprop -root _DT_SAVE_MODE", shell=1).strip().endswith(' = "xfce4"')
return _readfrom(_get_x11_vars() + "xprop -root _DT_SAVE_MODE", shell=1).decode("utf-8").strip().endswith(' = "xfce4"')
except OSError:
return 0

def _is_x11():

"Return whether the X Window System is in use."

return os.environ.has_key("DISPLAY")
return "DISPLAY" in os.environ

# Introspection functions.

Expand All @@ -154,20 +155,23 @@ def get_desktop():
environment. If no environment could be detected, None is returned.
"""

if os.environ.has_key("KDE_FULL_SESSION") or \
os.environ.has_key("KDE_MULTIHEAD"):
if "KDE_FULL_SESSION" in os.environ or \
"KDE_MULTIHEAD" in os.environ:
return "KDE"
elif os.environ.has_key("GNOME_DESKTOP_SESSION_ID") or \
os.environ.has_key("GNOME_KEYRING_SOCKET"):
elif "GNOME_DESKTOP_SESSION_ID" in os.environ or \
"GNOME_KEYRING_SOCKET" in os.environ:
return "GNOME"
elif "MATE_DESKTOP_SESSION_ID" in os.environ or \
"MATE_KEYRING_SOCKET" in os.environ:
return "MATE"
elif sys.platform == "darwin":
return "Mac OS X"
elif hasattr(os, "startfile"):
return "Windows"
elif _is_xfce():
return "XFCE"

# KDE, GNOME and XFCE run on X11, so we have to test for X11 last.
# KDE, GNOME, MATE and XFCE run on X11, so we have to test for X11 last.

if _is_x11():
return "X11"
Expand Down Expand Up @@ -200,6 +204,8 @@ def use_desktop(desktop):
return "KDE"
elif (desktop or detected) == "GNOME":
return "GNOME"
elif (desktop or detected) == "MATE":
return "MATE"
elif (desktop or detected) == "XFCE":
return "XFCE"
elif (desktop or detected) == "Mac OS X":
Expand All @@ -216,7 +222,7 @@ def is_standard():
launching.
"""

return os.environ.has_key("DESKTOP_LAUNCH")
return "DESKTOP_LAUNCH" in os.environ

# Activity functions.

Expand All @@ -228,11 +234,11 @@ def open(url, desktop=None, wait=0):
particular desktop environment's mechanisms to open the 'url' instead of
guessing or detecting which environment is being used.
Suggested values for 'desktop' are "standard", "KDE", "GNOME", "XFCE",
"Mac OS X", "Windows" where "standard" employs a DESKTOP_LAUNCH environment
variable to open the specified 'url'. DESKTOP_LAUNCH should be a command,
possibly followed by arguments, and must have any special characters
shell-escaped.
Suggested values for 'desktop' are "standard", "KDE", "GNOME", "GNOME",
"XFCE", "Mac OS X", "Windows" where "standard" employs a DESKTOP_LAUNCH
environment variable to open the specified 'url'. DESKTOP_LAUNCH should
be a command, possibly followed by arguments, and must have any special
characters shell-escaped.
The process identifier of the "opener" (ie. viewer, editor, browser or
program) associated with the 'url' is returned by this function. If the
Expand All @@ -249,32 +255,38 @@ def open(url, desktop=None, wait=0):
desktop_in_use = use_desktop(desktop)

if desktop_in_use == "standard":
arg = "".join([os.environ["DESKTOP_LAUNCH"], commands.mkarg(url)])
arg = "".join([os.environ["DESKTOP_LAUNCH"], subprocess.mkarg(url)])
return _run(arg, 1, wait)

elif desktop_in_use == "Windows":
# NOTE: This returns None in current implementations.
return os.startfile(url)

elif desktop_in_use == "KDE":
cmd = ["kfmclient", "exec", url]
cmd = ["xdg-open", url]

elif desktop_in_use == "GNOME":
cmd = ["gnome-open", url]
cmd = ["xdg-open", url]

elif desktop_in_use == "MATE":
cmd = ["xdg-open", url]

elif desktop_in_use == "XFCE":
cmd = ["exo-open", url]
cmd = ["xdg-open", url]

elif desktop_in_use == "Mac OS X":
cmd = ["open", url]

elif desktop_in_use == "X11" and os.environ.has_key("BROWSER"):
elif desktop_in_use == "X11" and "BROWSER" in os.environ:
cmd = [os.environ["BROWSER"], url]

elif desktop_in_use == "X11":
cmd = ["xdg-open", url]

# Finish with an error where no suitable desktop was identified.

else:
raise OSError, "Desktop '%s' not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" % desktop_in_use
raise OSError("Desktop '%s' not supported (neither DESKTOP_LAUNCH nor os.startfile could be used)" % desktop_in_use)

return _run(cmd, 0, wait)

Expand Down
10 changes: 6 additions & 4 deletions desktop/dialog.py
Expand Up @@ -35,6 +35,7 @@
question.open("KDE") # Insists on KDE
question.open("GNOME") # Insists on GNOME
question.open("MATE") # Insists on MATE
The dialogue box options are documented in each class's docstring.
Expand Down Expand Up @@ -241,6 +242,7 @@ class Dialogue:
commands = {
"KDE" : "kdialog",
"GNOME" : "zenity",
"MATE" : "zenity",
"XFCE" : "zenity", # NOTE: Based on observations with Xubuntu.
"X11" : "Xdialog"
}
Expand All @@ -256,7 +258,7 @@ def open(self, desktop=None):
instead of guessing or detecting which environment is being used.
Suggested values for 'desktop' are "standard", "KDE", "GNOME",
"Mac OS X", "Windows".
"MATE", "Mac OS X", "Windows".
The result of the dialogue interaction may be a string indicating user
input (for Input, Password, Menu, Pulldown), a list of strings
Expand All @@ -278,7 +280,7 @@ def open(self, desktop=None):
try:
program = self.commands[desktop_in_use]
except KeyError:
raise OSError, "Desktop '%s' not supported (no known dialogue box command could be suggested)" % desktop_in_use
raise OSError("Desktop '%s' not supported (no known dialogue box command could be suggested)" % desktop_in_use)

# The handler is one of the functions communicating with the subprocess.
# Some handlers return boolean values, others strings.
Expand Down Expand Up @@ -473,7 +475,7 @@ class Pulldown(Menu):
"Xdialog" : (_readvalue(_readfrom),
["--stdout", "--combobox", String("text"), Integer("height"), Integer("width"), Strings("items")]),
}
item = unicode
item = str
number_of_titles = 2

class Input(Simple):
Expand Down Expand Up @@ -544,6 +546,6 @@ def __init__(self, filename, text="", width=None, height=None):

# Supported desktop environments.

supported = Dialogue.commands.keys()
supported = list(Dialogue.commands.keys())

# vim: tabstop=4 expandtab shiftwidth=4
4 changes: 2 additions & 2 deletions desktop/windows.py
Expand Up @@ -141,7 +141,7 @@ def _get_descendant_handle_and_name(self, line):
if match:
return self._get_handle_and_name(line[:match.start()].strip())
else:
raise OSError, "Window information from %r did not contain window details." % line
raise OSError("Window information from %r did not contain window details." % line)

def _descendants(self, s, fn):
handles = []
Expand Down Expand Up @@ -258,7 +258,7 @@ def root(desktop=None):
if _is_x11():
return Window(None)
else:
raise OSError, "Desktop '%s' not supported" % use_desktop(desktop)
raise OSError("Desktop '%s' not supported" % use_desktop(desktop))

def find(callable, desktop=None):

Expand Down

0 comments on commit 7c6d940

Please sign in to comment.