Changeset 3318

Show
Ignore:
Timestamp:
02/02/08 15:33:52 (16 years ago)
Author:
jerome
Message:

Added some utility functions.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/pykota/utils.py

    r3310 r3318  
    2727import gettext 
    2828from types import UnicodeType 
     29 
     30from pykota.errors import PyKotaCommandLineError 
    2931 
    3032def initlocale(lang="", cset=None) : 
     
    117119                                 "replace")) 
    118120    sys.stderr.flush() 
     121     
     122def loginvalidparam(opt, value, defaultvalue, additionalinfo=None) : 
     123    """Logs an error when an invalid parameter to a command line option 
     124       is encountered. 
     125    """    
     126    message = _("Invalid value '%(value)s' for the %(opt)s command line option, using default '%(defaultvalue)s' instead") \ 
     127                                % locals() 
     128    if additionalinfo : 
     129        logerr("%s (%s)\n" % (message, additionalinfo)) 
     130    else :     
     131        logerr("%s\n" % message) 
    119132             
    120133def crashed(message="Bug in PyKota") :     
     
    122135    import traceback 
    123136    from pykota.version import __version__ 
     137    charset = sys.stdout.encoding or locale.getlocale()[1] or "ANSI_X3.4-1968" 
    124138    lines = [] 
    125139    for line in traceback.format_exception(*sys.exc_info()) : 
     140        line = line.decode(charset, "replace") 
    126141        lines.extend([l for l in line.split("\n") if l]) 
    127142    msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)]) 
    128143    logerr(msg) 
    129144    return msg 
     145     
     146def run(optparser, workclass, requireargs=False) : 
     147    """Runs a PyKota command line tool.""" 
     148    appname = os.path.basename(sys.argv[0]) 
     149    retcode = 0 
     150    (options, arguments) = optparser.parse_args()                    
     151    if requireargs and not arguments : 
     152        logerr("%s\n" % (_("%(appname)s requires arguments, please use --help") \ 
     153                            % locals())) 
     154        retcode = -1 
     155    try : 
     156        application = workclass() 
     157        application.deferredInit() 
     158        retcode = application.main(arguments, options) 
     159    except KeyboardInterrupt :         
     160        logerr("\nInterrupted with Ctrl+C !\n") 
     161        retcode = -3 
     162    except PyKotaCommandLineError, msg :     
     163        logerr("%s : %s\n" % (sys.argv[0], msg)) 
     164        retcode = -2 
     165    except SystemExit :         
     166        pass 
     167    except : 
     168        title = _("%(appname)s failed") % locals() 
     169        try : 
     170            application.crashed(title) 
     171        except :     
     172            crashed(title) 
     173        retcode = -1 
     174         
     175    sys.exit(retcode)