summaryrefslogtreecommitdiffstats
path: root/scripts/external_libs/pyzmq-14.5.0/python3/ucs4/32bit/zmq/sugar/attrsettr.py
blob: 4bbd36d6e6afb3778d622ce00b37fc08cf2db889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# coding: utf-8
"""Mixin for mapping set/getattr to self.set/get"""

# Copyright (C) PyZMQ Developers
# Distributed under the terms of the Modified BSD License.


from . import constants

class AttributeSetter(object):
    
    def __setattr__(self, key, value):
        """set zmq options by attribute"""
        
        # regular setattr only allowed for class-defined attributes
        for obj in [self] + self.__class__.mro():
            if key in obj.__dict__:
                object.__setattr__(self, key, value)
                return
        
        upper_key = key.upper()
        try:
            opt = getattr(constants, upper_key)
        except AttributeError:
            raise AttributeError("%s has no such option: %s" % (
                self.__class__.__name__, upper_key)
            )
        else:
            self._set_attr_opt(upper_key, opt, value)
    
    def _set_attr_opt(self, name, opt, value):
        """override if setattr should do something other than call self.set"""
        self.set(opt, value)
    
    def __getattr__(self, key):
        """get zmq options by attribute"""
        upper_key = key.upper()
        try:
            opt = getattr(constants, upper_key)
        except AttributeError:
            raise AttributeError("%s has no such option: %s" % (
                self.__class__.__name__, upper_key)
            )
        else:
            return self._get_attr_opt(upper_key, opt)

    def _get_attr_opt(self, name, opt):
        """override if getattr should do something other than call self.get"""
        return self.get(opt)
    

__all__ = ['AttributeSetter']