1from pyasn1 import error
2
3class TagMap:
4    def __init__(self, posMap={}, negMap={}, defType=None):
5        self.__posMap = posMap.copy()
6        self.__negMap = negMap.copy()
7        self.__defType = defType
8
9    def __contains__(self, tagSet):
10        return tagSet in self.__posMap or \
11               self.__defType is not None and tagSet not in self.__negMap
12
13    def __getitem__(self, tagSet):
14        if tagSet in self.__posMap:
15            return self.__posMap[tagSet]
16        elif tagSet in self.__negMap:
17            raise error.PyAsn1Error('Key in negative map')
18        elif self.__defType is not None:
19            return self.__defType
20        else:
21            raise KeyError()
22
23    def __repr__(self):
24        s = '%r/%r' % (self.__posMap, self.__negMap)
25        if self.__defType is not None:
26            s = s + '/%r' % (self.__defType,)
27        return s
28
29    def clone(self, parentType, tagMap, uniq=False):
30        if self.__defType is not None and tagMap.getDef() is not None:
31            raise error.PyAsn1Error('Duplicate default value at %s' % (self,))
32        if tagMap.getDef() is not None:
33            defType = tagMap.getDef()
34        else:
35            defType = self.__defType
36
37        posMap = self.__posMap.copy()
38        for k in tagMap.getPosMap():
39            if uniq and k in posMap:
40                raise error.PyAsn1Error('Duplicate positive key %s' % (k,))
41            posMap[k] = parentType
42
43        negMap = self.__negMap.copy()
44        negMap.update(tagMap.getNegMap())
45
46        return self.__class__(
47            posMap, negMap, defType,
48            )
49
50    def getPosMap(self): return self.__posMap.copy()
51    def getNegMap(self): return self.__negMap.copy()
52    def getDef(self): return self.__defType
53