aboutsummaryrefslogtreecommitdiffstats
path: root/cards/generic_application.py
blob: 5b663b45c2de5d2a35a4b89ca0e85f00769506f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import binascii, re, sys, cards

class Application:
    
    # This must be a sequence of regular expressions
    # When an application is selected through a matching AID
    # then all correponding classes are merged into the card
    # object.
    # The application classes themselves are responsible for
    # unmerging, should their application become deselected.
    # However, the default implementation in the generic
    # Application class which triggers unmerging on a card reset
    # and a successfull SELECT APPLICATION should be sufficient
    # in most cases.
    # (Still haven't thought this through, though...)
    ## FIXME Unloading is not implemented yet
    #
    # NOTE: Implementing classes MUST derive from Application
    AID_LIST = []
    
    def load_applications(card, aid):
        classes_to_load = []
        for i in dir(cards):
            possible_class = getattr(cards, i)
            if not hasattr(possible_class, "DRIVER_NAME") or not issubclass(possible_class, Application):
                continue
            if possible_class.can_handle_aid(card, aid):
                classes_to_load.append(possible_class)
                print ".oO(Loading application '%s')" % possible_class.DRIVER_NAME
        
        card.add_classes(classes_to_load)
    load_applications = staticmethod(load_applications)
    
    def can_handle_aid(cls, card, aid):
        for i in cls.AID_LIST:
            if re.match(i+"$", binascii.b2a_hex(aid), re.I):
                return True
        return False
    can_handle_aid = classmethod(can_handle_aid)