<s> """<STR_LIT>""" <EOL> import os <EOL> import shlex <EOL> import sys <EOL> import stat <EOL> import subprocess <EOL> import time <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Error ( Exception ) : <EOL> pass <EOL> _browsers = { } <EOL> _tryorder = [ ] <EOL> def register ( name , klass , instance = None , update_tryorder = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> _browsers [ name . lower ( ) ] = [ klass , instance ] <EOL> if update_tryorder > <NUM_LIT:0> : <EOL> _tryorder . append ( name ) <EOL> elif update_tryorder < <NUM_LIT:0> : <EOL> _tryorder . insert ( <NUM_LIT:0> , name ) <EOL> def get ( using = None ) : <EOL> """<STR_LIT>""" <EOL> if using is not None : <EOL> alternatives = [ using ] <EOL> else : <EOL> alternatives = _tryorder <EOL> for browser in alternatives : <EOL> if '<STR_LIT:%s>' in browser : <EOL> browser = shlex . split ( browser ) <EOL> if browser [ - <NUM_LIT:1> ] == '<STR_LIT:&>' : <EOL> return BackgroundBrowser ( browser [ : - <NUM_LIT:1> ] ) <EOL> else : <EOL> return GenericBrowser ( browser ) <EOL> else : <EOL> try : <EOL> command = _browsers [ browser . lower ( ) ] <EOL> except KeyError : <EOL> command = _synthesize ( browser ) <EOL> if command [ <NUM_LIT:1> ] is not None : <EOL> return command [ <NUM_LIT:1> ] <EOL> elif command [ <NUM_LIT:0> ] is not None : <EOL> return command [ <NUM_LIT:0> ] ( ) <EOL> raise Error ( "<STR_LIT>" ) <EOL> def open ( url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> for name in _tryorder : <EOL> browser = get ( name ) <EOL> if browser . open ( url , new , autoraise ) : <EOL> return True <EOL> return False <EOL> def open_new ( url ) : <EOL> return open ( url , <NUM_LIT:1> ) <EOL> def open_new_tab ( url ) : <EOL> return open ( url , <NUM_LIT:2> ) <EOL> def _synthesize ( browser , update_tryorder = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> cmd = browser . split ( ) [ <NUM_LIT:0> ] <EOL> if not _iscommand ( cmd ) : <EOL> return [ None , None ] <EOL> name = os . path . basename ( cmd ) <EOL> try : <EOL> command = _browsers [ name . lower ( ) ] <EOL> except KeyError : <EOL> return [ None , None ] <EOL> controller = command [ <NUM_LIT:1> ] <EOL> if controller and name . lower ( ) == controller . basename : <EOL> import copy <EOL> controller = copy . copy ( controller ) <EOL> controller . name = browser <EOL> controller . basename = os . path . basename ( browser ) <EOL> register ( browser , None , controller , update_tryorder ) <EOL> return [ None , controller ] <EOL> return [ None , None ] <EOL> if sys . platform [ : <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> def _isexecutable ( cmd ) : <EOL> cmd = cmd . lower ( ) <EOL> if os . path . isfile ( cmd ) and cmd . endswith ( ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> return True <EOL> for ext in "<STR_LIT>" , "<STR_LIT>" : <EOL> if os . path . isfile ( cmd + ext ) : <EOL> return True <EOL> return False <EOL> else : <EOL> def _isexecutable ( cmd ) : <EOL> if os . path . isfile ( cmd ) : <EOL> mode = os . stat ( cmd ) [ stat . ST_MODE ] <EOL> if mode & stat . S_IXUSR or mode & stat . S_IXGRP or mode & stat . S_IXOTH : <EOL> return True <EOL> return False <EOL> def _iscommand ( cmd ) : <EOL> """<STR_LIT>""" <EOL> if _isexecutable ( cmd ) : <EOL> return True <EOL> path = os . environ . get ( "<STR_LIT>" ) <EOL> if not path : <EOL> return False <EOL> for d in path . split ( os . pathsep ) : <EOL> exe = os . path . join ( d , cmd ) <EOL> if _isexecutable ( exe ) : <EOL> return True <EOL> return False <EOL> class BaseBrowser ( object ) : <EOL> """<STR_LIT>""" <EOL> args = [ '<STR_LIT:%s>' ] <EOL> def __init__ ( self , name = "<STR_LIT>" ) : <EOL> self . name = name <EOL> self . basename = name <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> raise NotImplementedError <EOL> def open_new ( self , url ) : <EOL> return self . open ( url , <NUM_LIT:1> ) <EOL> def open_new_tab ( self , url ) : <EOL> return self . open ( url , <NUM_LIT:2> ) <EOL> class GenericBrowser ( BaseBrowser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> if isinstance ( name , basestring ) : <EOL> self . name = name <EOL> self . args = [ "<STR_LIT:%s>" ] <EOL> else : <EOL> self . name = name [ <NUM_LIT:0> ] <EOL> self . args = name [ <NUM_LIT:1> : ] <EOL> self . basename = os . path . basename ( self . name ) <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> cmdline = [ self . name ] + [ arg . replace ( "<STR_LIT:%s>" , url ) <EOL> for arg in self . args ] <EOL> try : <EOL> if sys . platform [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> p = subprocess . Popen ( cmdline ) <EOL> else : <EOL> p = subprocess . Popen ( cmdline , close_fds = True ) <EOL> return not p . wait ( ) <EOL> except OSError : <EOL> return False <EOL> class BackgroundBrowser ( GenericBrowser ) : <EOL> """<STR_LIT>""" <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> cmdline = [ self . name ] + [ arg . replace ( "<STR_LIT:%s>" , url ) <EOL> for arg in self . args ] <EOL> try : <EOL> if sys . platform [ : <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> p = subprocess . Popen ( cmdline ) <EOL> else : <EOL> setsid = getattr ( os , '<STR_LIT>' , None ) <EOL> if not setsid : <EOL> setsid = getattr ( os , '<STR_LIT>' , None ) <EOL> p = subprocess . Popen ( cmdline , close_fds = True , preexec_fn = setsid ) <EOL> return ( p . poll ( ) is None ) <EOL> except OSError : <EOL> return False <EOL> class UnixBrowser ( BaseBrowser ) : <EOL> """<STR_LIT>""" <EOL> raise_opts = None <EOL> remote_args = [ '<STR_LIT>' , '<STR_LIT:%s>' ] <EOL> remote_action = None <EOL> remote_action_newwin = None <EOL> remote_action_newtab = None <EOL> background = False <EOL> redirect_stdout = True <EOL> def _invoke ( self , args , remote , autoraise ) : <EOL> raise_opt = [ ] <EOL> if remote and self . raise_opts : <EOL> autoraise = int ( autoraise ) <EOL> opt = self . raise_opts [ autoraise ] <EOL> if opt : raise_opt = [ opt ] <EOL> cmdline = [ self . name ] + raise_opt + args <EOL> if remote or self . background : <EOL> inout = file ( os . devnull , "<STR_LIT>" ) <EOL> else : <EOL> inout = None <EOL> setsid = getattr ( os , '<STR_LIT>' , None ) <EOL> if not setsid : <EOL> setsid = getattr ( os , '<STR_LIT>' , None ) <EOL> p = subprocess . Popen ( cmdline , close_fds = True , stdin = inout , <EOL> stdout = ( self . redirect_stdout and inout or None ) , <EOL> stderr = inout , preexec_fn = setsid ) <EOL> if remote : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> rc = p . poll ( ) <EOL> if rc is None : <EOL> time . sleep ( <NUM_LIT:4> ) <EOL> rc = p . poll ( ) <EOL> if rc is None : <EOL> return True <EOL> return not rc <EOL> elif self . background : <EOL> if p . poll ( ) is None : <EOL> return True <EOL> else : <EOL> return False <EOL> else : <EOL> return not p . wait ( ) <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> if new == <NUM_LIT:0> : <EOL> action = self . remote_action <EOL> elif new == <NUM_LIT:1> : <EOL> action = self . remote_action_newwin <EOL> elif new == <NUM_LIT:2> : <EOL> if self . remote_action_newtab is None : <EOL> action = self . remote_action_newwin <EOL> else : <EOL> action = self . remote_action_newtab <EOL> else : <EOL> raise Error ( "<STR_LIT>" + <EOL> "<STR_LIT>" % new ) <EOL> args = [ arg . replace ( "<STR_LIT:%s>" , url ) . replace ( "<STR_LIT>" , action ) <EOL> for arg in self . remote_args ] <EOL> success = self . _invoke ( args , True , autoraise ) <EOL> if not success : <EOL> args = [ arg . replace ( "<STR_LIT:%s>" , url ) for arg in self . args ] <EOL> return self . _invoke ( args , False , False ) <EOL> else : <EOL> return True <EOL> class Mozilla ( UnixBrowser ) : <EOL> """<STR_LIT>""" <EOL> raise_opts = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> remote_args = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> remote_action = "<STR_LIT>" <EOL> remote_action_newwin = "<STR_LIT>" <EOL> remote_action_newtab = "<STR_LIT>" <EOL> background = True <EOL> Netscape = Mozilla <EOL> class Galeon ( UnixBrowser ) : <EOL> """<STR_LIT>""" <EOL> raise_opts = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> remote_args = [ '<STR_LIT>' , '<STR_LIT:%s>' ] <EOL> remote_action = "<STR_LIT>" <EOL> remote_action_newwin = "<STR_LIT>" <EOL> background = True <EOL> class Chrome ( UnixBrowser ) : <EOL> "<STR_LIT>" <EOL> remote_args = [ '<STR_LIT>' , '<STR_LIT:%s>' ] <EOL> remote_action = "<STR_LIT>" <EOL> remote_action_newwin = "<STR_LIT>" <EOL> remote_action_newtab = "<STR_LIT>" <EOL> background = True <EOL> Chromium = Chrome <EOL> class Opera ( UnixBrowser ) : <EOL> "<STR_LIT>" <EOL> raise_opts = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> remote_args = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> remote_action = "<STR_LIT>" <EOL> remote_action_newwin = "<STR_LIT>" <EOL> remote_action_newtab = "<STR_LIT>" <EOL> background = True <EOL> class Elinks ( UnixBrowser ) : <EOL> "<STR_LIT>" <EOL> remote_args = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> remote_action = "<STR_LIT>" <EOL> remote_action_newwin = "<STR_LIT>" <EOL> remote_action_newtab = "<STR_LIT>" <EOL> background = False <EOL> redirect_stdout = False <EOL> class Konqueror ( BaseBrowser ) : <EOL> """<STR_LIT>""" <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> if new == <NUM_LIT:2> : <EOL> action = "<STR_LIT>" <EOL> else : <EOL> action = "<STR_LIT>" <EOL> devnull = file ( os . devnull , "<STR_LIT>" ) <EOL> setsid = getattr ( os , '<STR_LIT>' , None ) <EOL> if not setsid : <EOL> setsid = getattr ( os , '<STR_LIT>' , None ) <EOL> try : <EOL> p = subprocess . Popen ( [ "<STR_LIT>" , action , url ] , <EOL> close_fds = True , stdin = devnull , <EOL> stdout = devnull , stderr = devnull ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> p . wait ( ) <EOL> return True <EOL> try : <EOL> p = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" , url ] , <EOL> close_fds = True , stdin = devnull , <EOL> stdout = devnull , stderr = devnull , <EOL> preexec_fn = setsid ) <EOL> except OSError : <EOL> pass <EOL> else : <EOL> if p . poll ( ) is None : <EOL> return True <EOL> try : <EOL> p = subprocess . Popen ( [ "<STR_LIT>" , "<STR_LIT>" , url ] , <EOL> close_fds = True , stdin = devnull , <EOL> stdout = devnull , stderr = devnull , <EOL> preexec_fn = setsid ) <EOL> except OSError : <EOL> return False <EOL> else : <EOL> return ( p . poll ( ) is None ) <EOL> class Grail ( BaseBrowser ) : <EOL> def _find_grail_rc ( self ) : <EOL> import glob <EOL> import pwd <EOL> import socket <EOL> import tempfile <EOL> tempdir = os . path . join ( tempfile . gettempdir ( ) , <EOL> "<STR_LIT>" ) <EOL> user = pwd . getpwuid ( os . getuid ( ) ) [ <NUM_LIT:0> ] <EOL> filename = os . path . join ( tempdir , user + "<STR_LIT>" ) <EOL> maybes = glob . glob ( filename ) <EOL> if not maybes : <EOL> return None <EOL> s = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> for fn in maybes : <EOL> try : <EOL> s . connect ( fn ) <EOL> except socket . error : <EOL> try : <EOL> os . unlink ( fn ) <EOL> except IOError : <EOL> pass <EOL> else : <EOL> return s <EOL> def _remote ( self , action ) : <EOL> s = self . _find_grail_rc ( ) <EOL> if not s : <EOL> return <NUM_LIT:0> <EOL> s . send ( action ) <EOL> s . close ( ) <EOL> return <NUM_LIT:1> <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> if new : <EOL> ok = self . _remote ( "<STR_LIT>" + url ) <EOL> else : <EOL> ok = self . _remote ( "<STR_LIT>" + url ) <EOL> return ok <EOL> def register_X_browsers ( ) : <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , BackgroundBrowser ( "<STR_LIT>" ) ) <EOL> if "<STR_LIT>" in os . environ and _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , BackgroundBrowser ( "<STR_LIT>" ) ) <EOL> if "<STR_LIT>" in os . environ and _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , BackgroundBrowser ( "<STR_LIT>" ) ) <EOL> if "<STR_LIT>" in os . environ and _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , Konqueror , Konqueror ( "<STR_LIT>" ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , BackgroundBrowser ( "<STR_LIT>" ) ) <EOL> for browser in ( "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if _iscommand ( browser ) : <EOL> register ( browser , None , Mozilla ( browser ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , Konqueror , Konqueror ( "<STR_LIT>" ) ) <EOL> elif _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , Konqueror , Konqueror ( "<STR_LIT>" ) ) <EOL> for browser in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if _iscommand ( browser ) : <EOL> register ( browser , None , Galeon ( browser ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , BackgroundBrowser ( "<STR_LIT>" ) ) <EOL> for browser in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if _iscommand ( browser ) : <EOL> register ( browser , None , Chrome ( browser ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , Opera ( "<STR_LIT>" ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , BackgroundBrowser ( "<STR_LIT>" ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , Grail , None ) <EOL> if os . environ . get ( "<STR_LIT>" ) : <EOL> register_X_browsers ( ) <EOL> if os . environ . get ( "<STR_LIT>" ) : <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , GenericBrowser ( "<STR_LIT>" ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , GenericBrowser ( "<STR_LIT>" ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , Elinks ( "<STR_LIT>" ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , GenericBrowser ( "<STR_LIT>" ) ) <EOL> if _iscommand ( "<STR_LIT>" ) : <EOL> register ( "<STR_LIT>" , None , GenericBrowser ( "<STR_LIT>" ) ) <EOL> if sys . platform [ : <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> class WindowsDefault ( BaseBrowser ) : <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> try : <EOL> os . startfile ( url ) <EOL> except WindowsError : <EOL> return False <EOL> else : <EOL> return True <EOL> _tryorder = [ ] <EOL> _browsers = { } <EOL> register ( "<STR_LIT>" , WindowsDefault ) <EOL> iexplore = os . path . join ( os . environ . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) <EOL> for browser in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , iexplore ) : <EOL> if _iscommand ( browser ) : <EOL> register ( browser , None , BackgroundBrowser ( browser ) ) <EOL> if sys . platform == '<STR_LIT>' : <EOL> class MacOSX ( BaseBrowser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> assert "<STR_LIT:'>" not in url <EOL> if not '<STR_LIT::>' in url : <EOL> url = '<STR_LIT>' + url <EOL> new = int ( bool ( new ) ) <EOL> if self . name == "<STR_LIT:default>" : <EOL> script = '<STR_LIT>' % url . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> else : <EOL> if self . name == "<STR_LIT>" : <EOL> toWindow = "<STR_LIT>" <EOL> else : <EOL> toWindow = "<STR_LIT>" % ( new - <NUM_LIT:1> ) <EOL> cmd = '<STR_LIT>' % url . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> script = '''<STR_LIT>''' % ( self . name , cmd , toWindow ) <EOL> osapipe = os . popen ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> if osapipe is None : <EOL> return False <EOL> osapipe . write ( script ) <EOL> rc = osapipe . close ( ) <EOL> return not rc <EOL> class MacOSXOSAScript ( BaseBrowser ) : <EOL> def __init__ ( self , name ) : <EOL> self . _name = name <EOL> def open ( self , url , new = <NUM_LIT:0> , autoraise = True ) : <EOL> if self . _name == '<STR_LIT:default>' : <EOL> script = '<STR_LIT>' % url . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> else : <EOL> script = '''<STR_LIT>''' % ( self . _name , url . replace ( '<STR_LIT:">' , '<STR_LIT>' ) ) <EOL> osapipe = os . popen ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> if osapipe is None : <EOL> return False <EOL> osapipe . write ( script ) <EOL> rc = osapipe . close ( ) <EOL> return not rc <EOL> register ( "<STR_LIT>" , None , MacOSXOSAScript ( '<STR_LIT>' ) , - <NUM_LIT:1> ) <EOL> register ( "<STR_LIT>" , None , MacOSXOSAScript ( '<STR_LIT>' ) , - <NUM_LIT:1> ) <EOL> register ( "<STR_LIT>" , None , MacOSXOSAScript ( '<STR_LIT:default>' ) , - <NUM_LIT:1> ) <EOL> if sys . platform [ : <NUM_LIT:3> ] == "<STR_LIT>" and _iscommand ( "<STR_LIT>" ) : <EOL> _tryorder = [ ] <EOL> _browsers = { } <EOL> register ( "<STR_LIT>" , None , <EOL> GenericBrowser ( [ "<STR_LIT:start>" , "<STR_LIT>" , "<STR_LIT:%s>" ] ) , - <NUM_LIT:1> ) <EOL> if "<STR_LIT>" in os . environ : <EOL> _userchoices = os . environ [ "<STR_LIT>" ] . split ( os . pathsep ) <EOL> _userchoices . reverse ( ) <EOL> for cmdline in _userchoices : <EOL> if cmdline != '<STR_LIT>' : <EOL> cmd = _synthesize ( cmdline , - <NUM_LIT:1> ) <EOL> if cmd [ <NUM_LIT:1> ] is None : <EOL> register ( cmdline , None , GenericBrowser ( cmdline ) , - <NUM_LIT:1> ) <EOL> cmdline = None <EOL> del cmdline <EOL> del _userchoices <EOL> def main ( ) : <EOL> import getopt <EOL> usage = """<STR_LIT>""" % sys . argv [ <NUM_LIT:0> ] <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' ) <EOL> except getopt . error , msg : <EOL> print >> sys . stderr , msg <EOL> print >> sys . stderr , usage <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> new_win = <NUM_LIT:0> <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : new_win = <NUM_LIT:1> <EOL> elif o == '<STR_LIT>' : new_win = <NUM_LIT:2> <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> print >> sys . stderr , usage <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> url = args [ <NUM_LIT:0> ] <EOL> open ( url , new_win ) <EOL> print "<STR_LIT>" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
