From 70049c47db7d41ad0044972b86858c14a6f4159e Mon Sep 17 00:00:00 2001 From: "dn337t@gmail.com" Date: Fri, 22 Jun 2012 12:02:19 +0000 Subject: [PATCH] refactored client/pm3_*.py to use with statements, contants and iterators --- client/pm3_eml2mfd.py | 18 +++++------------- client/pm3_mfd2eml.py | 17 ++++++----------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/client/pm3_eml2mfd.py b/client/pm3_eml2mfd.py index c583d267..5f9e7b23 100644 --- a/client/pm3_eml2mfd.py +++ b/client/pm3_eml2mfd.py @@ -6,6 +6,7 @@ # Converts PM3 Mifare Classic emulator EML text file to MFD binary dump file ''' +from __future__ import with_statement import sys import binascii @@ -14,22 +15,13 @@ def main(argv): if argc < 3: print 'Usage:', argv[0], 'input.eml output.mfd' sys.exit(1) - - try: - file_inp = open(argv[1], "r") - file_out = open(argv[2], "wb") - line = file_inp.readline() - while line: - line = line.rstrip('\n') - line = line.rstrip('\r') + + with file(argv[1], "r") as file_inp, file(argv[2], "wb") as file_out: + for line in file_inp: + line = line.rstrip('\n').rstrip('\r') print line data = binascii.unhexlify(line) file_out.write(data) - line = file_inp.readline() - - finally: - file_inp.close() - file_out.close() if __name__ == '__main__': main(sys.argv) diff --git a/client/pm3_mfd2eml.py b/client/pm3_mfd2eml.py index 5c5dc4dd..44d60e7d 100644 --- a/client/pm3_mfd2eml.py +++ b/client/pm3_mfd2eml.py @@ -6,31 +6,26 @@ # Converts PM3 Mifare Classic MFD binary dump file to emulator EML text file ''' +from __future__ import with_statement import sys import binascii +READ_BLOCKSIZE = 16 + def main(argv): argc = len(argv) if argc < 3: print 'Usage:', argv[0], 'input.mfd output.eml' sys.exit(1) - try: - file_inp = open(argv[1], "rb") - file_out = open(argv[2], "w") - - while 1: - # TODO: need to use defines instead of hardcoded 16, 64, etc. - byte_s = file_inp.read(16) + with file(argv[1], "rb") as file_inp, file(argv[2], "w") as file_out: + while True: + byte_s = file_inp.read(READ_BLOCKSIZE) if not byte_s: break hex_char_repr = binascii.hexlify(byte_s) file_out.write(hex_char_repr) file_out.write("\n") - - finally: - file_inp.close() - file_out.close() if __name__ == '__main__': main(sys.argv) -- 2.39.5