1#
2# This file is part of pyasn1 software.
3#
4# Copyright (c) 2005-2019, 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 *OpenType* object models an untyped field of a constructed ASN.1
15    type. In ASN.1 syntax it is usually represented by the
16    `ANY DEFINED BY` for scalars or `SET OF ANY DEFINED BY`,
17    `SEQUENCE OF ANY DEFINED BY` for container types clauses. Typically
18    used together with :class:`~pyasn1.type.univ.Any` object.
19
20    OpenType objects duck-type a read-only Python :class:`dict` objects,
21    however the passed `typeMap` is not copied, but stored by reference.
22    That means the user can manipulate `typeMap` at run time having this
23    reflected on *OpenType* object behavior.
24
25    The |OpenType| class models an untyped field of a constructed ASN.1
26    type. In ASN.1 syntax it is usually represented by the
27    `ANY DEFINED BY` for scalars or `SET OF ANY DEFINED BY`,
28    `SEQUENCE OF ANY DEFINED BY` for container types clauses. Typically
29    used with :class:`~pyasn1.type.univ.Any` type.
30
31    Parameters
32    ----------
33    name: :py:class:`str`
34        Field name
35
36    typeMap: :py:class:`dict`
37        A map of value->ASN.1 type. It's stored by reference and can be
38        mutated later to register new mappings.
39
40    Examples
41    --------
42
43    For untyped scalars:
44
45    .. code-block:: python
46
47        openType = OpenType(
48            'id', {1: Integer(),
49                   2: OctetString()}
50        )
51        Sequence(
52            componentType=NamedTypes(
53                NamedType('id', Integer()),
54                NamedType('blob', Any(), openType=openType)
55            )
56        )
57
58    For untyped `SET OF` or `SEQUENCE OF` vectors:
59
60    .. code-block:: python
61
62        openType = OpenType(
63            'id', {1: Integer(),
64                   2: OctetString()}
65        )
66        Sequence(
67            componentType=NamedTypes(
68                NamedType('id', Integer()),
69                NamedType('blob', SetOf(componentType=Any()),
70                          openType=openType)
71            )
72        )
73    """
74
75    def __init__(self, name, typeMap=None):
76        self.__name = name
77        if typeMap is None:
78            self.__typeMap = {}
79        else:
80            self.__typeMap = typeMap
81
82    @property
83    def name(self):
84        return self.__name
85
86    # Python dict protocol
87
88    def values(self):
89        return self.__typeMap.values()
90
91    def keys(self):
92        return self.__typeMap.keys()
93
94    def items(self):
95        return self.__typeMap.items()
96
97    def __contains__(self, key):
98        return key in self.__typeMap
99
100    def __getitem__(self, key):
101        return self.__typeMap[key]
102
103    def __iter__(self):
104        return iter(self.__typeMap)
105