aboutsummaryrefslogtreecommitdiffstats
path: root/utils.py
diff options
context:
space:
mode:
authorHenryk Plötz <henryk@ploetzli.ch>2010-02-25 16:43:54 +0100
committerHenryk Plötz <henryk@ploetzli.ch>2010-02-26 02:27:26 +0100
commit331bfc4d476e9ce215e2100b43c75d7be408ae6c (patch)
treefe4094b7a8ca03bdcd52197ac574e1420b078c50 /utils.py
parent63d7c9d0a8fc13a79a22f8e6c23498ee97e697ca (diff)
Move all PC/SC specific stuff to a separate class structure for readers (thus preparing the way for non-PC/SC-readers)
This commit should be known as "Holy excessive layering, batman!"
Diffstat (limited to 'utils.py')
-rw-r--r--utils.py68
1 files changed, 4 insertions, 64 deletions
diff --git a/utils.py b/utils.py
index 0162c46..df93196 100644
--- a/utils.py
+++ b/utils.py
@@ -1,13 +1,4 @@
-import string, binascii, sys, re, getopt
-try:
- import smartcard, smartcard.CardRequest
-except ImportError:
- print >>sys.stderr, """Could not import smartcard module. Please install pyscard
-from http://pyscard.sourceforge.net/
-If you can't install pyscard and want to continue using
-pycsc you'll need to downgrade to SVN revision 246.
-"""
- raise
+import string, binascii, sys, re, getopt, readers
class CommandLineArgumentHelper:
OPTIONS = "r:l"
@@ -15,65 +6,13 @@ class CommandLineArgumentHelper:
exit_now = False
reader = None
- def list_readers():
- for index, name in enumerate(smartcard.System.readers()):
- print "%i: %s" % (index, name)
- list_readers = staticmethod(list_readers)
-
def connect(self):
"Open the connection to a card"
if self.reader is None:
self.reader = 0
- return self.connect_to(self.reader)
-
- def connect_to(reader):
- "Open the connection to a reader"
-
- readerObject = None
-
- if isinstance(reader, int) or reader.isdigit():
- reader = int(reader)
- readerObject = smartcard.System.readers()[reader]
- else:
- for r in smartcard.System.readers():
- if str(r).startswith(reader):
- readerObject = r
-
- if readerObject is None:
- readerObject = smartcard.System.readers()[0]
-
- print "Using reader: %s" % readerObject
- unpatched = False
- printed = False
- while True:
- try:
- if not unpatched:
- cardrequest = smartcard.CardRequest.CardRequest( readers=[readerObject], timeout=0.1 )
- else:
- cardrequest = smartcard.CardRequest.CardRequest( readers=[readerObject], timeout=1 )
-
- cardservice = cardrequest.waitforcard()
- cardservice.connection.connect()
- break
- except TypeError:
- unpatched = True
- except (KeyboardInterrupt, SystemExit):
- raise
- except smartcard.Exceptions.CardRequestException:
- if sys.exc_info()[1].message.endswith("Command timeout."):
- if not printed:
- print "Please insert card ..."
- printed = True
- else:
- raise
- except smartcard.Exceptions.NoCardException:
- print "Card is mute or absent. Please retry."
-
- print "ATR: %s" % hexdump(smartcard.util.toASCIIString(cardservice.connection.getATR()), short = True)
- return cardservice
- connect_to = staticmethod(connect_to)
+ return readers.connect_to(self.reader)
def getopt(self, argv, opts="", long_opts=[]):
"Wrapper around getopt.gnu_getopt. Handles common arguments, returns everything else."
@@ -85,7 +24,8 @@ class CommandLineArgumentHelper:
if option in ("-r","--reader"):
self.reader = value
elif option in ("-l","--list-readers"):
- self.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) )