Changeset 3318
- Timestamp:
- 02/02/08 15:33:52 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pykota/trunk/pykota/utils.py
r3310 r3318 27 27 import gettext 28 28 from types import UnicodeType 29 30 from pykota.errors import PyKotaCommandLineError 29 31 30 32 def initlocale(lang="", cset=None) : … … 117 119 "replace")) 118 120 sys.stderr.flush() 121 122 def 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) 119 132 120 133 def crashed(message="Bug in PyKota") : … … 122 135 import traceback 123 136 from pykota.version import __version__ 137 charset = sys.stdout.encoding or locale.getlocale()[1] or "ANSI_X3.4-1968" 124 138 lines = [] 125 139 for line in traceback.format_exception(*sys.exc_info()) : 140 line = line.decode(charset, "replace") 126 141 lines.extend([l for l in line.split("\n") if l]) 127 142 msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKota v%s" % __version__, message] + lines)]) 128 143 logerr(msg) 129 144 return msg 145 146 def 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)