#!/usr/bin/env python """Script for starting browsers in a convenient way.""" import getopt import os import sys import choices import popen5 # Change known_browsers if you want to add/remove a browser or change the # defaults for them. # # Has this structure: # "browser": (executable, grep-string, url_arg_func, environ) # # - browser -- the name the user sees of the browser # - executable -- the actual executable. # - grep-string -- a string to be used when searching for existing # processes of the browser. # - url_arg_func -- A function returning a list of arguments used # when opening the url. # - environ -- a mapping to list extra environment variables that # should be set before execution. known_browsers = { "galeon": ('/usr/bin/galeon', 'galeon', (lambda url: ["--new-tab", url]), {"LANGUAGE": "sv_SE", "LANG": "sv_SE"}), "opera": ('/usr/bin/opera', 'opera', (lambda url: ["-newpage", url]), {}), "mozilla": ('/usr/bin/mozilla', 'mozilla', (lambda url: ["-remote", "openURL(%s, new-tab)" % url]), {}), "mozilla-firebird": ('/usr/bin/mozilla-firebird', 'mozilla-firebird', (lambda url: ["-remote", "openURL(%s, new-tab)" % url]), {}), } urls = [] browsers = [] debug = False reuse = 1 quiet = False def unique(the_list): """Removes all duplicates in the list.""" res = [] for l in the_list: if not l in res: res.append(l) return res def help(): """Returns the usage string.""" return """Usage: %s [options] urls Options: -h --help Show this help text. -b STRING --browser=STRING Specify browser. -d --debug Don't suppress browser output. -f --force-start Don't reuse existing browser. -o --use-old Must reuse existing browser. -q --quiet Be rather quiet. """ % sys.argv[0] def get_default_browsers(): """Get the preferred browsers from a config file. The config file uses the choices system, so check your $CHOICESPATH. The file should be called in start-browser/preferred_browsers somewhere in that path and contain whitespace separated names of browsers.""" global browsers browserfile = choices.find_first("start-browser", "preferred_browsers") if browserfile is None: return new_browsers = open(browserfile, 'r').read().split() browsers = new_browsers + browsers def commandline(): """Parses the commandline and sets values according to what was given on the commandline.""" global browsers, urls, debug, reuse try: (options, args) = getopt.getopt(sys.argv[1:], "b:dfhoq", ["help", "browser=", "debug", "force-start", "use-old", "quiet"]) except getopt.GetoptError, err: sys.stderr.write(str(err) + "\n\n") sys.stderr.write(help()) sys.exit(2) new_browsers = [] for o, a in options: if o in ["-h", "--help"]: sys.stdout.write(help()) sys.exit() elif o in ["-b", "--browser"]: new_browsers.append(a) elif o in ["-d", "--debug"]: debug = True elif o in ["-f", "--force-start"]: reuse = 0 elif o in ["-o", "--use-old"]: reuse = 2 elif o in ["-q", "--quiet"]: quiet = True browsers = new_browsers + browsers urls += args def select_browser_and_start(): """Check if any of the browsers are up, then open url in that, else start a new browser.""" if reuse > 0 and len(urls) > 0: for browser in browsers: if browser not in known_browsers.keys(): sys.stderr.write("Unknown browser '%s'.\n" % b) sys.exit(2) grepping = known_browsers[browser][1] res = not os.system("ps -e | grep %s &> /dev/null" % grepping) if res: start_browser(browser, False) return if reuse < 2: if len(browsers) > 0: browser = browsers[0] if browser not in known_browsers.keys(): sys.stderr.write("Unknown browser '%s'.\n" % b) sys.exit(2) start_browser(browser, True) return else: sys.stderr.write("No browser specified.\n") sys.stderr.write(help()) sys.exit(2) if len(urls) == 0: sys.stderr.write("You didn't specify any urls.\n") sys.exit(2) else: sys.stderr.write("No old browser found.\n") sys.exit(2) def start_browser(browser, spawn = False): """Start specified browser.""" executable = known_browsers[browser][0] url_func = known_browsers[browser][2] extra_environ = known_browsers[browser][3] environ = os.environ.copy() environ.update(extra_environ) sout = serr = None if not debug: sout = open("/dev/null", "w") serr = popen5.STDOUT if spawn: if not quiet: sys.stdout.write("Starting %s.\n" % browser) popen5.Popen([executable] + urls, stdout=sout, stderr=serr, env = environ) else: for url in urls: if not quiet: sys.stdout.write("Opening %s in %s.\n" % (url, browser)) popen5.Popen([executable] + url_func(url), stdout=sout, stderr=serr, env = environ).wait() if __name__ == "__main__": get_default_browsers() commandline() select_browser_and_start()