From c68a10fc68b2f5a256694216ab44e180e9c52a9c Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Fri, 22 Aug 2008 22:20:51 -0300 Subject: [PATCH] Salir sin un traceback con el valor de retorno apropiado ante excepciones --- taller/ejercicios/ej1-20082/tp.py | 53 ++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/taller/ejercicios/ej1-20082/tp.py b/taller/ejercicios/ej1-20082/tp.py index 79c9b2c..05b1e6f 100755 --- a/taller/ejercicios/ej1-20082/tp.py +++ b/taller/ejercicios/ej1-20082/tp.py @@ -282,23 +282,54 @@ def decode_message(str, msg, indent_level=1): # Main ########################################################################## -def main(*args): +class MessageNotFoundError(Exception): + pass + +def main(schmfile, pbfile, msgname): msgs = [Message('int'), Message('string')] \ - + parse(file(args[0]).read()) + + parse(schmfile.read()) check_types(msgs) print msgs - msg = search(msgs, args[1]) + msg = search(msgs, msgname) if msg is None: - print "can't find message with name %s in %s" % (args[1], - args[0]) - return 7 - pbfile = sys.stdin - if len(args) > 2: - pbfile = file(args[2]) + raise MessageNotFoundError("can't find message %s" % msgname) decode_message(pbfile.read(), msg) - return 0 if __name__ == '__main__': import sys - sys.exit(main(*sys.argv[1:])) + try: + schmfile = sys.argv[1] + msgname = sys.argv[2] + except: + print 'wrong usage' + sys.exit(1) + try: + schmfile = file(args[0]) + except: + print "can't open schema file %s" % schmfile + sys.exit(2) + pbfile = sys.stdin + if len(sys.argv) > 3: + try: + pbfile = file(sys.argv[3]) + except: + print "can't open pb file %s" % sys.argv[3] + sys.exit(3) + try: + main(schmfile, pbfile, msgname) + except LexicError, e: + print e + sys.exit(4) + except ParserError, e: + print e + sys.exit(5) + except SemanticError, e: + print e + sys.exit(6) + except MessageNotFoundError, e: + print e + sys.exit(7) + except DecodeError, e: + print e + sys.exit(8) -- 2.43.0