File: const.py

package info (click to toggle)
offlineimap3 0.0~git20210225.1e7ef9e%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,328 kB
  • sloc: python: 7,974; sh: 548; makefile: 81
file content (35 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download | duplicates (3)
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
"""
Copyright (C) 2013-2014 Eygene A. Ryabinkin and contributors

Collection of classes that implement const-like behaviour
for various objects.
"""
import copy


class ConstProxy:
    """Implements read-only access to a given object
    that can be attached to each instance only once."""

    def __init__(self):
        self.__dict__['__source'] = None

    def __getattr__(self, name):
        src = self.__dict__['__source']
        if src is None:
            raise ValueError("using non-initialized ConstProxy() object")
        return copy.deepcopy(getattr(src, name))

    def __setattr__(self, name, value):
        raise AttributeError("tried to set '%s' to '%s' for constant object" %
                             (name, value))

    def __delattr__(self, name):
        raise RuntimeError("tried to delete field '%s' from constant object" %
                           name)

    def set_source(self, source):
        """ Sets source object for this instance. """
        if self.__dict__['__source'] is not None:
            raise ValueError("source object is already set")
        self.__dict__['__source'] = source