1d3907f0dSYaroslav Brustinov# coding: utf-8
2d3907f0dSYaroslav Brustinov"""Mixin for mapping set/getattr to self.set/get"""
3d3907f0dSYaroslav Brustinov
4d3907f0dSYaroslav Brustinov# Copyright (C) PyZMQ Developers
5d3907f0dSYaroslav Brustinov# Distributed under the terms of the Modified BSD License.
6d3907f0dSYaroslav Brustinov
7d3907f0dSYaroslav Brustinov
8d3907f0dSYaroslav Brustinovfrom . import constants
9d3907f0dSYaroslav Brustinov
10d3907f0dSYaroslav Brustinovclass AttributeSetter(object):
11d3907f0dSYaroslav Brustinov
12d3907f0dSYaroslav Brustinov    def __setattr__(self, key, value):
13d3907f0dSYaroslav Brustinov        """set zmq options by attribute"""
14d3907f0dSYaroslav Brustinov
15d3907f0dSYaroslav Brustinov        # regular setattr only allowed for class-defined attributes
16d3907f0dSYaroslav Brustinov        for obj in [self] + self.__class__.mro():
17d3907f0dSYaroslav Brustinov            if key in obj.__dict__:
18d3907f0dSYaroslav Brustinov                object.__setattr__(self, key, value)
19d3907f0dSYaroslav Brustinov                return
20d3907f0dSYaroslav Brustinov
21d3907f0dSYaroslav Brustinov        upper_key = key.upper()
22d3907f0dSYaroslav Brustinov        try:
23d3907f0dSYaroslav Brustinov            opt = getattr(constants, upper_key)
24d3907f0dSYaroslav Brustinov        except AttributeError:
25d3907f0dSYaroslav Brustinov            raise AttributeError("%s has no such option: %s" % (
26d3907f0dSYaroslav Brustinov                self.__class__.__name__, upper_key)
27d3907f0dSYaroslav Brustinov            )
28d3907f0dSYaroslav Brustinov        else:
29d3907f0dSYaroslav Brustinov            self._set_attr_opt(upper_key, opt, value)
30d3907f0dSYaroslav Brustinov
31d3907f0dSYaroslav Brustinov    def _set_attr_opt(self, name, opt, value):
32d3907f0dSYaroslav Brustinov        """override if setattr should do something other than call self.set"""
33d3907f0dSYaroslav Brustinov        self.set(opt, value)
34d3907f0dSYaroslav Brustinov
35d3907f0dSYaroslav Brustinov    def __getattr__(self, key):
36d3907f0dSYaroslav Brustinov        """get zmq options by attribute"""
37d3907f0dSYaroslav Brustinov        upper_key = key.upper()
38d3907f0dSYaroslav Brustinov        try:
39d3907f0dSYaroslav Brustinov            opt = getattr(constants, upper_key)
40d3907f0dSYaroslav Brustinov        except AttributeError:
41d3907f0dSYaroslav Brustinov            raise AttributeError("%s has no such option: %s" % (
42d3907f0dSYaroslav Brustinov                self.__class__.__name__, upper_key)
43d3907f0dSYaroslav Brustinov            )
44d3907f0dSYaroslav Brustinov        else:
45d3907f0dSYaroslav Brustinov            return self._get_attr_opt(upper_key, opt)
46d3907f0dSYaroslav Brustinov
47d3907f0dSYaroslav Brustinov    def _get_attr_opt(self, name, opt):
48d3907f0dSYaroslav Brustinov        """override if getattr should do something other than call self.get"""
49d3907f0dSYaroslav Brustinov        return self.get(opt)
50d3907f0dSYaroslav Brustinov
51d3907f0dSYaroslav Brustinov
52d3907f0dSYaroslav Brustinov__all__ = ['AttributeSetter']
53