aboutsummaryrefslogtreecommitdiffstats
path: root/cards
diff options
context:
space:
mode:
authorhenryk <henryk@f711b948-2313-0410-aaa9-d29f33439f0b>2005-10-10 23:38:29 +0000
committerhenryk <henryk@f711b948-2313-0410-aaa9-d29f33439f0b>2005-10-10 23:38:29 +0000
commit6003cc1fc01413cd7825ea0c9cdb9dd5d60a3828 (patch)
treeb94acc4fc34aa9bba335d8cfbd2e32696a5231e3 /cards
parent32b0e2150762ba4267b8ec6a758343121fe816c2 (diff)
Dynamically load all modules and their card classes from the cards directory
git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@42 f711b948-2313-0410-aaa9-d29f33439f0b
Diffstat (limited to 'cards')
-rw-r--r--cards/__init__.py35
1 files changed, 28 insertions, 7 deletions
diff --git a/cards/__init__.py b/cards/__init__.py
index cbbf745..2e535de 100644
--- a/cards/__init__.py
+++ b/cards/__init__.py
@@ -1,14 +1,35 @@
-"""This package contains different card-specific modules."""
-from generic_card import Card
-from java_card import Java_Card
-from cyberflex_card import Cyberflex_Card
-from sys import modules
+"""This package contains different card-specific modules.
+
+The __init__.py file will automatically load all modules in the cards directory
+and import all classes that have a DRIVER_NAME attribute. If you want to write
+your own classes you should therefore make sure it has a DRIVER_NAME and then
+put it in the same directory as the other classes. Preferably you should derive
+from the generic_card.Card class."""
+
+from sys import modules as _modules
+from dircache import listdir as _listdir
+
+for filename in _listdir(_modules[__name__].__path__[0]):
+ if filename[-3:].lower() == ".py":
+ possible_module = filename[:-3]
+ if possible_module.lower() == "__init__":
+ continue
+ try:
+ module = __import__(possible_module, globals(), locals(), [])
+ for possible_class in dir(module):
+ if hasattr(getattr(module, possible_class), "DRIVER_NAME"):
+ setattr(_modules[__name__], possible_class, getattr(module, possible_class))
+ except ImportError:
+ pass
+
+
+del filename, possible_module, module, possible_class
def find_class(ATR):
"""Find a card class that supports the card identified by ATR.
Returns the generic card class when no better match is found."""
- for card_class in dir(modules[__name__]):
- card_class = getattr(modules[__name__], card_class)
+ for card_class in dir(_modules[__name__]):
+ card_class = getattr(_modules[__name__], card_class)
if hasattr(card_class, "can_handle"):
if card_class.can_handle(ATR):
return card_class