Created
June 3, 2013 16:26
-
-
Save iwinux/5699353 to your computer and use it in GitHub Desktop.
sudo ln -s $PWD/xdg-open.py /usr/bin/xdg-open
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from os.path import isdir, splitext | |
from urlparse import urlparse | |
from urllib import unquote | |
import sys | |
import os | |
APP_MAPPINGS = { | |
'html': 'google-chrome', | |
'pdf': '/opt/Adobe/Reader9/bin/acroread' | |
} | |
FILE_MANAGER = 'pcmanfm' | |
BROWSER = 'google-chrome' | |
def open_local(url): | |
path = unquote(url.path) | |
app = '' | |
if isdir(path): | |
app = FILE_MANAGER | |
else: | |
ext = splitext(path)[1] | |
app = APP_MAPPINGS.get(ext[1:]) | |
if not app: | |
raise TypeError("Don't know how to open %s." % path) | |
cmd = '%s "%s"' % (app, path) | |
os.system(cmd) | |
def open_in_browser(url): | |
cmd = '%s "%s"' % (BROWSER, url.geturl()) | |
os.system(cmd) | |
SCHEME_HANDLERS = { | |
'file': open_local, | |
'http': open_in_browser, | |
'https': open_in_browser, | |
} | |
def open_url(url): | |
scheme = url.scheme or 'file' | |
handler = SCHEME_HANDLERS.get(scheme) | |
if not handler: | |
raise TypeError('Unknown URL scheme.') | |
handler(url) | |
def main(): | |
if len(sys.argv) < 2: | |
sys.exit("usage: open.py <filename or url>") | |
open_url(urlparse(sys.argv[1])) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment