comparison python/hexutil.py @ 246:f254b87258e6

Added hexfile to zcc
author Windel Bouwman
date Thu, 25 Jul 2013 08:11:30 +0200
parents 58155c7c4a8e
children 7b38782ed496
comparison
equal deleted inserted replaced
245:66912720d712 246:f254b87258e6
16 description='possible commands', dest='command') 16 description='possible commands', dest='command')
17 17
18 p = subparsers.add_parser('info', help='dump info about hexfile') 18 p = subparsers.add_parser('info', help='dump info about hexfile')
19 p.add_argument('hexfile', type=argparse.FileType('r')) 19 p.add_argument('hexfile', type=argparse.FileType('r'))
20 20
21 p = subparsers.add_parser('new', help='create empty hexfile') 21 p = subparsers.add_parser('new', help='create a hexfile')
22 p.add_argument('hexfile', type=argparse.FileType('x')) 22 p.add_argument('hexfile', type=argparse.FileType('w'))
23 p.add_argument('address', type=hex2int, help="hex address of the data")
24 p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add')
23 25
24 p = subparsers.add_parser('add', help='add binary data from file to hexfile') 26 p = subparsers.add_parser('merge', help='merge two hexfiles into a third')
25 p.add_argument('hexfile', type=argparse.FileType('r+'), help="the hexfile to add the data to") 27 p.add_argument('hexfile1', type=argparse.FileType('r'), help="hexfile 1")
26 p.add_argument('address', type=hex2int, help="hex address") 28 p.add_argument('hexfile2', type=argparse.FileType('r'), help="hexfile 2")
27 p.add_argument('datafile', type=argparse.FileType('rb'), help='binary file to add') 29 p.add_argument('rhexfile', type=argparse.FileType('w'), help="resulting hexfile")
28 30
29 def main(args): 31 def main(args):
30 if args.command == 'info': 32 if args.command == 'info':
31 hf = HexFile() 33 hf = HexFile()
32 hf.load(args.hexfile) 34 hf.load(args.hexfile)
33 print(hf) 35 print(hf)
34 for region in hf.regions: 36 for region in hf.regions:
35 print(region) 37 print(region)
36 elif args.command == 'new': 38 elif args.command == 'new':
37 hf = HexFile() 39 hf = HexFile()
38 hf.save(args.hexfile)
39 elif args.command == 'add':
40 hf = HexFile()
41 hf.load(args.hexfile)
42 data = args.datafile.read() 40 data = args.datafile.read()
43 hf.addRegion(args.address, data) 41 hf.addRegion(args.address, data)
42 hf.save(args.hexfile)
43 elif args.command == 'merge':
44 hf = HexFile()
45 hf.load(args.hexfile1)
46 hf2 = HexFile()
47 hf2.load(args.hexfile2)
48 hf.merge(hf2)
49 hf.save(args.rhexfile)
44 else: 50 else:
45 raise NotImplementedError() 51 raise NotImplementedError()
46 52
47 if __name__ == '__main__': 53 if __name__ == '__main__':
48 args = parser.parse_args() 54 args = parser.parse_args()