1"""0MQ Constants."""
2
3# Copyright (c) PyZMQ Developers.
4# Distributed under the terms of the Modified BSD License.
5
6from zmq.backend import constants
7from zmq.utils.constant_names import (
8    base_names,
9    switched_sockopt_names,
10    int_sockopt_names,
11    int64_sockopt_names,
12    bytes_sockopt_names,
13    fd_sockopt_names,
14    ctx_opt_names,
15    msg_opt_names,
16)
17
18#-----------------------------------------------------------------------------
19# Python module level constants
20#-----------------------------------------------------------------------------
21
22__all__ = [
23    'int_sockopts',
24    'int64_sockopts',
25    'bytes_sockopts',
26    'ctx_opts',
27    'ctx_opt_names',
28    ]
29
30int_sockopts    = set()
31int64_sockopts  = set()
32bytes_sockopts  = set()
33fd_sockopts     = set()
34ctx_opts        = set()
35msg_opts        = set()
36
37
38if constants.VERSION < 30000:
39    int64_sockopt_names.extend(switched_sockopt_names)
40else:
41    int_sockopt_names.extend(switched_sockopt_names)
42
43_UNDEFINED = -9999
44
45def _add_constant(name, container=None):
46    """add a constant to be defined
47
48    optionally add it to one of the sets for use in get/setopt checkers
49    """
50    c = getattr(constants, name, _UNDEFINED)
51    if c == _UNDEFINED:
52        return
53    globals()[name] = c
54    __all__.append(name)
55    if container is not None:
56        container.add(c)
57    return c
58
59for name in base_names:
60    _add_constant(name)
61
62for name in int_sockopt_names:
63    _add_constant(name, int_sockopts)
64
65for name in int64_sockopt_names:
66    _add_constant(name, int64_sockopts)
67
68for name in bytes_sockopt_names:
69    _add_constant(name, bytes_sockopts)
70
71for name in fd_sockopt_names:
72    _add_constant(name, fd_sockopts)
73
74for name in ctx_opt_names:
75    _add_constant(name, ctx_opts)
76
77for name in msg_opt_names:
78    _add_constant(name, msg_opts)
79
80# ensure some aliases are always defined
81aliases = [
82    ('DONTWAIT', 'NOBLOCK'),
83    ('XREQ', 'DEALER'),
84    ('XREP', 'ROUTER'),
85]
86for group in aliases:
87    undefined = set()
88    found = None
89    for name in group:
90        value = getattr(constants, name, -1)
91        if value != -1:
92            found = value
93        else:
94            undefined.add(name)
95    if found is not None:
96        for name in undefined:
97            globals()[name] = found
98            __all__.append(name)
99