Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tagmap.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. from pyasn1 import error
  8. __all__ = ['TagMap']
  9. class TagMap(object):
  10. """Map *TagSet* objects to ASN.1 types
  11. Create an object mapping *TagSet* object to ASN.1 type.
  12. *TagMap* objects are immutable and duck-type read-only Python
  13. :class:`dict` objects holding *TagSet* objects as keys and ASN.1
  14. type objects as values.
  15. Parameters
  16. ----------
  17. presentTypes: :py:class:`dict`
  18. Map of :class:`~pyasn1.type.tag.TagSet` to ASN.1 objects considered
  19. as being unconditionally present in the *TagMap*.
  20. skipTypes: :py:class:`dict`
  21. A collection of :class:`~pyasn1.type.tag.TagSet` objects considered
  22. as absent in the *TagMap* even when *defaultType* is present.
  23. defaultType: ASN.1 type object
  24. An ASN.1 type object callee *TagMap* returns for any *TagSet* key not present
  25. in *presentTypes* (unless given key is present in *skipTypes*).
  26. """
  27. def __init__(self, presentTypes=None, skipTypes=None, defaultType=None):
  28. self.__presentTypes = presentTypes or {}
  29. self.__skipTypes = skipTypes or {}
  30. self.__defaultType = defaultType
  31. def __contains__(self, tagSet):
  32. return (tagSet in self.__presentTypes or
  33. self.__defaultType is not None and tagSet not in self.__skipTypes)
  34. def __getitem__(self, tagSet):
  35. try:
  36. return self.__presentTypes[tagSet]
  37. except KeyError:
  38. if self.__defaultType is None:
  39. raise KeyError()
  40. elif tagSet in self.__skipTypes:
  41. raise error.PyAsn1Error('Key in negative map')
  42. else:
  43. return self.__defaultType
  44. def __iter__(self):
  45. return iter(self.__presentTypes)
  46. def __repr__(self):
  47. representation = '%s object at 0x%x' % (self.__class__.__name__, id(self))
  48. if self.__presentTypes:
  49. representation += ' present %s' % repr(self.__presentTypes)
  50. if self.__skipTypes:
  51. representation += ' skip %s' % repr(self.__skipTypes)
  52. if self.__defaultType is not None:
  53. representation += ' default %s' % repr(self.__defaultType)
  54. return '<%s>' % representation
  55. @property
  56. def presentTypes(self):
  57. """Return *TagSet* to ASN.1 type map present in callee *TagMap*"""
  58. return self.__presentTypes
  59. @property
  60. def skipTypes(self):
  61. """Return *TagSet* collection unconditionally absent in callee *TagMap*"""
  62. return self.__skipTypes
  63. @property
  64. def defaultType(self):
  65. """Return default ASN.1 type being returned for any missing *TagSet*"""
  66. return self.__defaultType
  67. # Backward compatibility
  68. def getPosMap(self):
  69. return self.presentTypes
  70. def getNegMap(self):
  71. return self.skipTypes
  72. def getDef(self):
  73. return self.defaultType