pySim-read.py: Added a common card detection function for both pySim-prog.py and pySim-read.py

This function is used to detect the card type and return Card class/Card subclasses object
if its a know card or else None. Also, an initial step towards refactoring of code.

Change-Id: I71f57c6403dc933bd9d54f90df3d3fe105b4f66f
diff --git a/pySim/cards.py b/pySim/cards.py
index fe7f0fd..a43da9c 100644
--- a/pySim/cards.py
+++ b/pySim/cards.py
@@ -1104,3 +1104,31 @@
 			card.reset()
 			return card
 	return None
+
+def card_detect(ctype, scc):
+	# Detect type if needed
+	card = None
+	ctypes = dict([(kls.name, kls) for kls in _cards_classes])
+
+	if ctype in ("auto", "auto_once"):
+		for kls in _cards_classes:
+			card = kls.autodetect(scc)
+			if card:
+				print("Autodetected card type: %s" % card.name)
+				card.reset()
+				break
+
+		if card is None:
+			print("Autodetection failed")
+			return None
+
+		if ctype == "auto_once":
+			ctype = card.name
+
+	elif ctype in ctypes:
+		card = ctypes[ctype](scc)
+
+	else:
+		raise ValueError("Unknown card type: %s" % ctype)
+
+	return card