Consolidate and simplify common command line processing code
git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@223 f711b948-2313-0410-aaa9-d29f33439f0b
This commit is contained in:
parent
d7aa71ca8d
commit
918eaf3f40
79
brutefid.py
79
brutefid.py
|
@ -2,7 +2,7 @@
|
|||
# -*- coding: iso-8859-1 -*-
|
||||
|
||||
from utils import pycsc
|
||||
import utils, cards, TLV_utils, sys, binascii, time, getopt, traceback
|
||||
import utils, cards, TLV_utils, sys, binascii, time, traceback
|
||||
|
||||
STATUS_INTERVAL = 10
|
||||
|
||||
|
@ -12,85 +12,16 @@ top_level = None
|
|||
start_time = time.time()
|
||||
loop = 0
|
||||
|
||||
OPTIONS = "r:l"
|
||||
LONG_OPTIONS = ["reader=", "list-readers"]
|
||||
exit_now = False
|
||||
reader = None
|
||||
|
||||
def list_readers():
|
||||
for index, name in enumerate(pycsc.listReader()):
|
||||
print "%i: %s" % (index, name)
|
||||
|
||||
def connect(reader = None):
|
||||
"Open the connection to a card"
|
||||
|
||||
if reader is None:
|
||||
reader = 0
|
||||
|
||||
if isinstance(reader, int) or reader.isdigit():
|
||||
reader = int(reader)
|
||||
readerName = pycsc.listReader()[reader]
|
||||
else:
|
||||
readerName = reader
|
||||
|
||||
newState = pycsc.getStatusChange(ReaderStates=[
|
||||
{'Reader': readerName, 'CurrentState':pycsc.SCARD_STATE_UNAWARE}
|
||||
]
|
||||
)
|
||||
|
||||
print "Using reader: %s" % readerName
|
||||
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
|
||||
|
||||
if not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT:
|
||||
print "Please insert card ..."
|
||||
|
||||
last_was_mute = False
|
||||
|
||||
while not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT \
|
||||
or newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
|
||||
|
||||
try:
|
||||
newState = pycsc.getStatusChange(ReaderStates=[
|
||||
{'Reader': readerName, 'CurrentState':newState[0]['EventState']}
|
||||
], Timeout = 100
|
||||
) ## 100 ms latency from Ctrl-C to abort should be almost unnoticeable by the user
|
||||
except pycsc.PycscException, e:
|
||||
if e.args[0] == 'Command timeout.': pass ## ugly
|
||||
else: raise
|
||||
|
||||
if newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
|
||||
if not last_was_mute:
|
||||
print "Card is mute, please retry ..."
|
||||
last_was_mute = True
|
||||
else:
|
||||
last_was_mute = False
|
||||
|
||||
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
|
||||
|
||||
print "ATR: %s" % utils.hexdump(newState[0]['Atr'], short = True)
|
||||
return pycsc.pycsc(reader = readerName, protocol = pycsc.SCARD_PROTOCOL_ANY)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
(options, arguments) = getopt.gnu_getopt(sys.argv[1:], OPTIONS, LONG_OPTIONS)
|
||||
c = utils.CommandLineArgumentHelper()
|
||||
|
||||
(options, arguments) = c.getopt(sys.argv[1:])
|
||||
|
||||
for (option, value) in options:
|
||||
if option in ("-r","--reader"):
|
||||
reader = value
|
||||
if option in ("-l","--list-readers"):
|
||||
list_readers()
|
||||
exit_now = True
|
||||
|
||||
if exit_now:
|
||||
sys.exit()
|
||||
del exit_now
|
||||
|
||||
if len(arguments) > 0:
|
||||
top_level = ("".join( ["".join(e.split()) for e in arguments] )).split("/")
|
||||
top_level = [binascii.unhexlify(e) for e in top_level]
|
||||
|
||||
pycsc_card = connect(reader)
|
||||
pycsc_card = c.connect()
|
||||
card = cards.new_card_object(pycsc_card)
|
||||
cards.generic_card.DEBUG = False
|
||||
|
||||
|
|
|
@ -2,68 +2,10 @@
|
|||
# -*- coding: iso-8859-1 -*-
|
||||
|
||||
from utils import pycsc
|
||||
import utils, cards, TLV_utils, sys, binascii, time, getopt, traceback, re
|
||||
import utils, cards, TLV_utils, sys, binascii, time, traceback, re
|
||||
|
||||
STATUS_INTERVAL = 10
|
||||
|
||||
OPTIONS = "r:l"
|
||||
LONG_OPTIONS = ["reader=", "list-readers"]
|
||||
exit_now = False
|
||||
reader = None
|
||||
|
||||
def list_readers():
|
||||
for index, name in enumerate(pycsc.listReader()):
|
||||
print "%i: %s" % (index, name)
|
||||
|
||||
def connect(reader = None):
|
||||
"Open the connection to a card"
|
||||
|
||||
if reader is None:
|
||||
reader = 0
|
||||
|
||||
if isinstance(reader, int) or reader.isdigit():
|
||||
reader = int(reader)
|
||||
readerName = pycsc.listReader()[reader]
|
||||
else:
|
||||
readerName = reader
|
||||
|
||||
newState = pycsc.getStatusChange(ReaderStates=[
|
||||
{'Reader': readerName, 'CurrentState':pycsc.SCARD_STATE_UNAWARE}
|
||||
]
|
||||
)
|
||||
|
||||
print "Using reader: %s" % readerName
|
||||
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
|
||||
|
||||
if not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT:
|
||||
print "Please insert card ..."
|
||||
|
||||
last_was_mute = False
|
||||
|
||||
while not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT \
|
||||
or newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
|
||||
|
||||
try:
|
||||
newState = pycsc.getStatusChange(ReaderStates=[
|
||||
{'Reader': readerName, 'CurrentState':newState[0]['EventState']}
|
||||
], Timeout = 100
|
||||
) ## 100 ms latency from Ctrl-C to abort should be almost unnoticeable by the user
|
||||
except pycsc.PycscException, e:
|
||||
if e.args[0] == 'Command timeout.': pass ## ugly
|
||||
else: raise
|
||||
|
||||
if newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
|
||||
if not last_was_mute:
|
||||
print "Card is mute, please retry ..."
|
||||
last_was_mute = True
|
||||
else:
|
||||
last_was_mute = False
|
||||
|
||||
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
|
||||
|
||||
print "ATR: %s" % utils.hexdump(newState[0]['Atr'], short = True)
|
||||
return pycsc.pycsc(reader = readerName, protocol = pycsc.SCARD_PROTOCOL_ANY)
|
||||
|
||||
def fingerprint_rfid(card):
|
||||
# Need RFID
|
||||
if not isinstance(card, cards.rfid_card.RFID_Card):
|
||||
|
@ -241,21 +183,10 @@ def match_fingerprint(fingerprint, database="fingerprints.txt"):
|
|||
return ["\n".join(e) for e in results]
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
(options, arguments) = getopt.gnu_getopt(sys.argv[1:], OPTIONS, LONG_OPTIONS)
|
||||
c = utils.CommandLineArgumentHelper()
|
||||
(options, arguments) = c.getopt(sys.argv[1:])
|
||||
|
||||
for (option, value) in options:
|
||||
if option in ("-r","--reader"):
|
||||
reader = value
|
||||
if option in ("-l","--list-readers"):
|
||||
list_readers()
|
||||
exit_now = True
|
||||
|
||||
if exit_now:
|
||||
sys.exit()
|
||||
del exit_now
|
||||
|
||||
pycsc_card = connect(reader)
|
||||
pycsc_card = c.connect()
|
||||
card = cards.new_card_object(pycsc_card)
|
||||
cards.generic_card.DEBUG = False
|
||||
|
||||
|
|
83
utils.py
83
utils.py
|
@ -8,7 +8,88 @@ except ImportError,e:
|
|||
except ImportError:
|
||||
raise e # raise the original exception, masking the windows-only attempt
|
||||
|
||||
import string, binascii, sys, re
|
||||
import string, binascii, sys, re, getopt
|
||||
|
||||
class CommandLineArgumentHelper:
|
||||
OPTIONS = "r:l"
|
||||
LONG_OPTIONS = ["reader=", "list-readers"]
|
||||
exit_now = False
|
||||
reader = None
|
||||
|
||||
def list_readers():
|
||||
for index, name in enumerate(pycsc.listReader()):
|
||||
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
|
||||
|
||||
if isinstance(self.reader, int) or self.reader.isdigit():
|
||||
self.reader = int(self.reader)
|
||||
readerName = pycsc.listReader()[self.reader]
|
||||
else:
|
||||
readerName = self.reader
|
||||
|
||||
newState = pycsc.getStatusChange(ReaderStates=[
|
||||
{'Reader': readerName, 'CurrentState':pycsc.SCARD_STATE_UNAWARE}
|
||||
]
|
||||
)
|
||||
|
||||
print "Using reader: %s" % readerName
|
||||
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
|
||||
|
||||
if not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT:
|
||||
print "Please insert card ..."
|
||||
|
||||
last_was_mute = False
|
||||
|
||||
while not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT \
|
||||
or newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
|
||||
|
||||
try:
|
||||
newState = pycsc.getStatusChange(ReaderStates=[
|
||||
{'Reader': readerName, 'CurrentState':newState[0]['EventState']}
|
||||
], Timeout = 100
|
||||
) ## 100 ms latency from Ctrl-C to abort should be almost unnoticeable by the user
|
||||
except pycsc.PycscException, e:
|
||||
if e.args[0] == 'Command timeout.': pass ## ugly
|
||||
else: raise
|
||||
|
||||
if newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
|
||||
if not last_was_mute:
|
||||
print "Card is mute, please retry ..."
|
||||
last_was_mute = True
|
||||
else:
|
||||
last_was_mute = False
|
||||
|
||||
print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
|
||||
|
||||
print "ATR: %s" % hexdump(newState[0]['Atr'], short = True)
|
||||
return pycsc.pycsc(reader = readerName, protocol = pycsc.SCARD_PROTOCOL_ANY)
|
||||
|
||||
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"):
|
||||
self.list_readers()
|
||||
self.exit_now = True
|
||||
else:
|
||||
unrecognized.append( (option, value) )
|
||||
|
||||
if self.exit_now:
|
||||
sys.exit()
|
||||
|
||||
return unrecognized, arguments
|
||||
|
||||
|
||||
def represent_binary_fancy(len, value, mask = 0):
|
||||
result = []
|
||||
|
|
Loading…
Reference in New Issue