# 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)