View Single Post
Old 08-15-2014, 09:27 AM   #28
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,455
Karma: 27757438
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Quote:
Originally Posted by eschwartz View Post
(Course, I do have to manually setup a lot of xdg-open file associations. Archlinux, you see. )
Just FYI, a handy trick I use is to create my own ~/bin/xdg-open much nicer than having to configure file associations, for the crazy xdg-utils. This allows me to do fun things like change the virtual desktop to the desktop I use for my web-browser when opening http links and to create associations for every file type I use in just a few lines of code.

Code:
#!/usr/bin/env python2
# vim:fileencoding=utf-8
from __future__ import (unicode_literals, division, absolute_import,
                        print_function)

__license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'

BROWSER = 'firefox'
PDF = 'okular'
EBOOK = 'ebook-viewer'
EDITOR = ('gvim', '-f')
IMAGE = 'gwenview'
OFFICE = ('libreoffice', '--nologo')
DIRECTORY = 'dolphin'
COMIC = 'okular'
ARCHIVE = 'ark'

import os
import sys
import mimetypes
import urlparse
import subprocess
import urllib

arg = sys.argv[-1]
base = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
custom_mimetypes = os.path.join(base, 'conf/mime.types')
url = urlparse.urlparse(arg)


def call(args, path):
    if isinstance(args, basestring):
        args = [args]
    raise SystemExit(subprocess.Popen(list(args) + [path]).wait())

if url.scheme in {'http', 'https', 'ftp'}:
    from libqtile.command import Client
    c = Client()
    if c.group.info()['name'] != '2':
        c.screen.togglegroup('2')
    call(BROWSER, arg)

if url.scheme in {'file', ''}:
    path = url.path
    if path and not os.path.exists(path):
        path = urllib.unquote(path)

    ext = path.rpartition('.')[-1] if path else ''

    if not path or not os.path.exists(path):
        print (path, 'does not exist')
        raise SystemExit(1)

    if os.path.isdir(path):
        call(DIRECTORY, path)

    if ext in {'epub', 'mobi', 'azw', 'azw3', 'fb2', 'lrf', 'pdb', 'prc', 'lit', 'txtz', 'htmlz'}:
        call(EBOOK, path)

    if ext == 'pdf':
        call(PDF, path)

    if ext in {'cbz', 'cbr'}:
        call(COMIC, path)

    if ext in {'gz', 'bz2', 'xz', 'txz', 'tgz', 'tbz2', 'rar', '7z'}:
        call(ARCHIVE, path)

    if ext in {'opf', 'xml', 'html', 'htm', 'xhtm', 'xhtml', 'zip'}:
        call(EDITOR, path)

    mimetypes.init(mimetypes.knownfiles + [custom_mimetypes])

    mt = mimetypes.guess_type(path)[0]
    if mt:
        if mt.startswith('text/'):
            call(EDITOR,  path)
        if mt.startswith('image/'):
            call(IMAGE, path)
        if mt.startswith('application/vnd.openxmlformats.') or mt.startswith('application/vnd.oasis.opendocument.'):
            call(OFFICE, path)
        for x in {'application/msword', 'application/vnd.ms-'}:
            call(OFFICE, path)
kovidgoyal is offline   Reply With Quote