<s> """<STR_LIT>""" <EOL> __version__ = '<STR_LIT>' <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> import os <EOL> import sys <EOL> import webbrowser <EOL> import subprocess <EOL> from email . Utils import encode_rfc2231 <EOL> _controllers = { } <EOL> _open = None <EOL> class BaseController ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def open ( self , filename ) : <EOL> raise NotImplementedError <EOL> _is_windows = sys . platform . startswith ( '<STR_LIT>' ) <EOL> _is_linux = sys . platform . startswith ( '<STR_LIT>' ) <EOL> _is_osx = sys . platform == '<STR_LIT>' <EOL> class Controller ( BaseController ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> super ( Controller , self ) . __init__ ( os . path . basename ( args [ <NUM_LIT:0> ] ) ) <EOL> self . args = list ( args ) <EOL> def _invoke ( self , cmdline ) : <EOL> if _is_windows : <EOL> closefds = False <EOL> startupinfo = subprocess . STARTUPINFO ( ) <EOL> startupinfo . dwFlags |= subprocess . STARTF_USESHOWWINDOW <EOL> else : <EOL> closefds = True <EOL> startupinfo = None <EOL> if os . environ . get ( '<STR_LIT>' ) or _is_windows or _is_osx : <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> pipe = subprocess . Popen ( cmdline , stdin = inout , stdout = inout , <EOL> stderr = inout , close_fds = closefds , <EOL> preexec_fn = setsid , startupinfo = startupinfo ) <EOL> returncode = pipe . wait ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> returncode = self . fixreturncode ( returncode ) <EOL> return not returncode <EOL> def open ( self , filename ) : <EOL> if isinstance ( filename , basestring ) : <EOL> cmdline = self . args + [ filename ] <EOL> else : <EOL> cmdline = self . args + filename <EOL> try : <EOL> return self . _invoke ( cmdline ) <EOL> except OSError : <EOL> return False <EOL> if _is_windows : <EOL> class Start ( BaseController ) : <EOL> """<STR_LIT>""" <EOL> def open ( self , filename ) : <EOL> try : <EOL> os . startfile ( filename ) <EOL> except WindowsError : <EOL> return False <EOL> else : <EOL> return True <EOL> _controllers [ '<STR_LIT>' ] = Start ( '<STR_LIT:start>' ) <EOL> _open = _controllers [ '<STR_LIT>' ] . open <EOL> elif _is_osx : <EOL> _controllers [ '<STR_LIT>' ] = Controller ( '<STR_LIT>' ) <EOL> _open = _controllers [ '<STR_LIT>' ] . open <EOL> else : <EOL> import commands <EOL> from webbrowser import _iscommand <EOL> class KfmClient ( Controller ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , kfmclient = '<STR_LIT>' ) : <EOL> super ( KfmClient , self ) . __init__ ( kfmclient , '<STR_LIT>' ) <EOL> self . kde_version = self . detect_kde_version ( ) <EOL> def detect_kde_version ( self ) : <EOL> kde_version = None <EOL> try : <EOL> info = commands . getoutput ( '<STR_LIT>' ) <EOL> for line in info . splitlines ( ) : <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> kde_version = line . split ( '<STR_LIT::>' ) [ - <NUM_LIT:1> ] . strip ( ) <EOL> break <EOL> except ( OSError , RuntimeError ) : <EOL> pass <EOL> return kde_version <EOL> def fixreturncode ( self , returncode ) : <EOL> if returncode is not None and self . kde_version > '<STR_LIT>' : <EOL> return returncode <EOL> else : <EOL> return os . EX_OK <EOL> def detect_desktop_environment ( ) : <EOL> """<STR_LIT>""" <EOL> desktop_environment = '<STR_LIT>' <EOL> if os . environ . get ( '<STR_LIT>' ) == '<STR_LIT:true>' : <EOL> desktop_environment = '<STR_LIT>' <EOL> elif os . environ . get ( '<STR_LIT>' ) : <EOL> desktop_environment = '<STR_LIT>' <EOL> else : <EOL> try : <EOL> info = commands . getoutput ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in info : <EOL> desktop_environment = '<STR_LIT>' <EOL> except ( OSError , RuntimeError ) : <EOL> pass <EOL> return desktop_environment <EOL> def register_X_controllers ( ) : <EOL> if _iscommand ( '<STR_LIT>' ) : <EOL> _controllers [ '<STR_LIT>' ] = KfmClient ( ) <EOL> for command in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if _iscommand ( command ) : <EOL> _controllers [ command ] = Controller ( command ) <EOL> def get ( ) : <EOL> controllers_map = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> desktop_environment = detect_desktop_environment ( ) <EOL> try : <EOL> controller_name = controllers_map [ desktop_environment ] <EOL> return _controllers [ controller_name ] . open <EOL> except KeyError : <EOL> if _controllers . has_key ( '<STR_LIT>' ) : <EOL> return _controllers [ '<STR_LIT>' ] . open <EOL> else : <EOL> return webbrowser . open <EOL> if os . environ . get ( "<STR_LIT>" ) : <EOL> register_X_controllers ( ) <EOL> _open = get ( ) <EOL> def open ( filename ) : <EOL> """<STR_LIT>""" <EOL> return _open ( filename ) <EOL> def _fix_addersses ( ** kwargs ) : <EOL> for headername in ( '<STR_LIT:address>' , '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> headervalue = kwargs [ headername ] <EOL> if not headervalue : <EOL> del kwargs [ headername ] <EOL> continue <EOL> elif not isinstance ( headervalue , basestring ) : <EOL> headervalue = '<STR_LIT:U+002C>' . join ( headervalue ) <EOL> except KeyError : <EOL> pass <EOL> except TypeError : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( headername , <EOL> type ( headervalue ) . __name__ ) ) <EOL> else : <EOL> translation_map = { '<STR_LIT:%>' : '<STR_LIT>' , '<STR_LIT:&>' : '<STR_LIT>' , '<STR_LIT:?>' : '<STR_LIT>' } <EOL> for char , replacement in translation_map . items ( ) : <EOL> headervalue = headervalue . replace ( char , replacement ) <EOL> kwargs [ headername ] = headervalue <EOL> return kwargs <EOL> def mailto_format ( ** kwargs ) : <EOL> kwargs = _fix_addersses ( ** kwargs ) <EOL> parts = [ ] <EOL> for headername in ( '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:body>' , '<STR_LIT>' ) : <EOL> if kwargs . has_key ( headername ) : <EOL> headervalue = kwargs [ headername ] <EOL> if not headervalue : <EOL> continue <EOL> if headername in ( '<STR_LIT:address>' , '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> parts . append ( '<STR_LIT>' % ( headername , headervalue ) ) <EOL> else : <EOL> headervalue = encode_rfc2231 ( headervalue ) <EOL> parts . append ( '<STR_LIT>' % ( headername , headervalue ) ) <EOL> mailto_string = '<STR_LIT>' % kwargs . get ( '<STR_LIT:address>' , '<STR_LIT>' ) <EOL> if parts : <EOL> mailto_string = '<STR_LIT>' % ( mailto_string , '<STR_LIT:&>' . join ( parts ) ) <EOL> return mailto_string <EOL> def mailto ( address , to = None , cc = None , bcc = None , subject = None , body = None , <EOL> attach = None ) : <EOL> """<STR_LIT>""" <EOL> mailto_string = mailto_format ( ** locals ( ) ) <EOL> return open ( mailto_string ) </s>
