1#
2# This file is part of pyasn1 software.
3#
4# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
5# License: http://snmplabs.com/pyasn1/license.html
6#
7
8__all__ = ['OpenType']
9
10
11class OpenType(object):
12    """Create ASN.1 type map indexed by a value
13
14    The *DefinedBy* object models the ASN.1 *DEFINED BY* clause which maps
15    values to ASN.1 types in the context of the ASN.1 SEQUENCE/SET type.
16
17    OpenType objects are duck-type a read-only Python :class:`dict` objects,
18    however the passed `typeMap` is stored by reference.
19
20    Parameters
21    ----------
22    name: :py:class:`str`
23        Field name
24
25    typeMap: :py:class:`dict`
26        A map of value->ASN.1 type. It's stored by reference and can be
27        mutated later to register new mappings.
28
29    Examples
30    --------
31    .. code-block:: python
32
33        openType = OpenType(
34            'id',
35            {1: Integer(),
36             2: OctetString()}
37        )
38        Sequence(
39            componentType=NamedTypes(
40                NamedType('id', Integer()),
41                NamedType('blob', Any(), openType=openType)
42            )
43        )
44    """
45
46    def __init__(self, name, typeMap=None):
47        self.__name = name
48        if typeMap is None:
49            self.__typeMap = {}
50        else:
51            self.__typeMap = typeMap
52
53    @property
54    def name(self):
55        return self.__name
56
57    # Python dict protocol
58
59    def values(self):
60        return self.__typeMap.values()
61
62    def keys(self):
63        return self.__typeMap.keys()
64
65    def items(self):
66        return self.__typeMap.items()
67
68    def __contains__(self, key):
69        return key in self.__typeMap
70
71    def __getitem__(self, key):
72        return self.__typeMap[key]
73
74    def __iter__(self):
75        return iter(self.__typeMap)
76