From bc07262d8043a189dd8f19df904c233b1358c230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Wed, 3 Mar 2010 00:06:20 +0100 Subject: [PATCH] API CHANGE: Move CommandLineArgumentHelper to readers module to allow proper import order. (CommandLineArgumentHelper references the readers module, which references the utils module, so having CLAH in utils would create a circular reference.) --- brutefid.py | 4 ++-- cyberflex-shell.py | 2 +- fingerpass.py | 4 ++-- gui/ireadyou.py | 6 +++--- readers.py | 43 ++++++++++++++++++++++++++++++++++++++++--- readpass.py | 4 ++-- utils.py | 38 +------------------------------------- 7 files changed, 51 insertions(+), 50 deletions(-) diff --git a/brutefid.py b/brutefid.py index a882aab..2aae0d7 100755 --- a/brutefid.py +++ b/brutefid.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: iso-8859-1 -*- -import utils, cards, TLV_utils, sys, binascii, time, traceback, smartcard +import utils, cards, TLV_utils, sys, binascii, time, traceback, smartcard, readers OPTIONS = "m:x:dD" LONG_OPTIONS = ["min-fid", "max-fid", "with-dirs", "dump-contents"] @@ -35,7 +35,7 @@ def dump(data): pass if __name__ == "__main__": - c = utils.CommandLineArgumentHelper() + c = readers.CommandLineArgumentHelper() (options, arguments) = c.getopt(sys.argv[1:], OPTIONS, LONG_OPTIONS) for option, value in options: diff --git a/cyberflex-shell.py b/cyberflex-shell.py index 35521da..1232709 100755 --- a/cyberflex-shell.py +++ b/cyberflex-shell.py @@ -406,7 +406,7 @@ reader = None if __name__ == "__main__": - helper = utils.CommandLineArgumentHelper() + helper = readers.CommandLineArgumentHelper() (options, arguments) = helper.getopt(sys.argv[1:], OPTIONS, LONG_OPTIONS) diff --git a/fingerpass.py b/fingerpass.py index 52e8d41..1a2d22b 100755 --- a/fingerpass.py +++ b/fingerpass.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: iso-8859-1 -*- -import utils, cards, TLV_utils, sys, binascii, time, traceback, re +import utils, cards, TLV_utils, sys, binascii, time, traceback, re, readers def fingerprint_rfid(card): # Need RFID @@ -180,7 +180,7 @@ def match_fingerprint(fingerprint, database="fingerprints.txt"): return ["\n".join(e) for e in results] if __name__ == "__main__": - c = utils.CommandLineArgumentHelper() + c = readers.CommandLineArgumentHelper() (options, arguments) = c.getopt(sys.argv[1:]) card_object = c.connect() diff --git a/gui/ireadyou.py b/gui/ireadyou.py index 820723e..62adeb5 100644 --- a/gui/ireadyou.py +++ b/gui/ireadyou.py @@ -4,11 +4,11 @@ import gtk,gtk.glade,gobject import sys, os, time try: - import utils, TLV_utils, cards + import utils, TLV_utils, cards, readers except ImportError, e: try: sys.path.append(".") - import utils, TLV_utils, cards + import utils, TLV_utils, cards, readers except ImportError: raise e @@ -360,7 +360,7 @@ OPTIONS = "" LONG_OPTIONS = [] if __name__ == "__main__": -## c = utils.CommandLineArgumentHelper() +## c = readers.CommandLineArgumentHelper() ## ## (options, arguments) = c.getopt(sys.argv[1:], OPTIONS, LONG_OPTIONS) ## diff --git a/readers.py b/readers.py index f34ebd9..cefaf3c 100644 --- a/readers.py +++ b/readers.py @@ -7,6 +7,9 @@ If you can't install pyscard and want to continue using pycsc you'll need to downgrade to SVN revision 246. """ raise + +import sys, utils, getopt + class Smartcard_Reader(object): def list_readers(cls): "Return a list of tuples: (reader name, implementing object)" @@ -219,11 +222,45 @@ def connect_to(reader): readerObject.connect() - from utils import hexdump - - print "ATR: %s" % hexdump(readerObject.get_ATR(), short = True) + print "ATR: %s" % utils.hexdump(readerObject.get_ATR(), short = True) return readerObject +class CommandLineArgumentHelper: + OPTIONS = "r:l" + LONG_OPTIONS = ["reader=", "list-readers"] + exit_now = False + reader = None + + def connect(self): + "Open the connection to a card" + + if self.reader is None: + self.reader = 0 + + return connect_to(self.reader) + + def getopt(self, argv, opts="", long_opts=[]): + "Wrapper around getopt.gnu_getopt. Handles common arguments, returns everything else." + (options, arguments) = getopt.gnu_getopt(sys.argv[1:], self.OPTIONS+opts, self.LONG_OPTIONS+long_opts) + + unrecognized = [] + + for (option, value) in options: + if option in ("-r","--reader"): + self.reader = value + elif option in ("-l","--list-readers"): + for i, (name, obj) in enumerate(list_readers()): + print "%i: %s" % (i,name) + self.exit_now = True + else: + unrecognized.append( (option, value) ) + + if self.exit_now: + sys.exit() + + return unrecognized, arguments + + if __name__ == "__main__": list_readers() diff --git a/readpass.py b/readpass.py index be188f1..2d68e27 100755 --- a/readpass.py +++ b/readpass.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: iso-8859-1 -*- -import utils, cards, TLV_utils, sys, binascii, time, traceback +import utils, cards, TLV_utils, sys, binascii, time, traceback, readers OPTIONS = "iGW:R:" LONG_OPTIONS = ["interactive","no-gui", "write-files-basename", "read-files-basename"] @@ -12,7 +12,7 @@ read_files = None start_interactive = False if __name__ == "__main__": - c = utils.CommandLineArgumentHelper() + c = readers.CommandLineArgumentHelper() (options, arguments) = c.getopt(sys.argv[1:], OPTIONS, LONG_OPTIONS) diff --git a/utils.py b/utils.py index df93196..57883a2 100644 --- a/utils.py +++ b/utils.py @@ -1,40 +1,4 @@ -import string, binascii, sys, re, getopt, readers - -class CommandLineArgumentHelper: - OPTIONS = "r:l" - LONG_OPTIONS = ["reader=", "list-readers"] - exit_now = False - reader = None - - def connect(self): - "Open the connection to a card" - - if self.reader is None: - self.reader = 0 - - return readers.connect_to(self.reader) - - def getopt(self, argv, opts="", long_opts=[]): - "Wrapper around getopt.gnu_getopt. Handles common arguments, returns everything else." - (options, arguments) = getopt.gnu_getopt(sys.argv[1:], self.OPTIONS+opts, self.LONG_OPTIONS+long_opts) - - unrecognized = [] - - for (option, value) in options: - if option in ("-r","--reader"): - self.reader = value - elif option in ("-l","--list-readers"): - for i, (name, obj) in enumerate(readers.list_readers()): - print "%i: %s" % (i,name) - self.exit_now = True - else: - unrecognized.append( (option, value) ) - - if self.exit_now: - sys.exit() - - return unrecognized, arguments - +import string, binascii, sys, re def represent_binary_fancy(len, value, mask = 0): result = []