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