/usr/lib/python3/dist-packages/commando/conf.py is in python3-commando 1.0.0-0.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | """
A few wrappers and utilities for handling complex
configuration objects.
"""
from collections import defaultdict
from ._compat import with_metaclass, iteritems
SEQS = (tuple, list, set, frozenset)
class ConfigDict(defaultdict):
"""
A patchable dictionary like object that allows accessing items
as attributes.
"""
def __init__(self, initial=None):
super(ConfigDict, self).__init__(ConfigDict)
initial = initial or {}
for key, value in iteritems(initial):
self.__setitem__(key, value)
def __setitem__(self, key, value):
# pylint: disable-msg=C0111
def transform(primitive):
if isinstance(primitive, dict):
return ConfigDict(primitive)
elif isinstance(primitive, SEQS):
seq = type(primitive)
return seq(transform(v) for v in primitive)
else:
return primitive
super(ConfigDict, self).__setitem__(key, transform(value))
def __getitem__(self, key):
return super(ConfigDict, self).__getitem__(key)
def copy(self):
"""
Returns a copy of the config dict object.
"""
return ConfigDict(self)
def patch(self, overrides):
"""
Patches the config with the given overrides.
Example:
If the current dictionary looks like this:
a: 1,
b: {
c: 3,
d: 4
}
and `patch` is called with the following overrides:
b: {
d: 2,
e: 4
},
c: 5
then, the following will be the resulting dictionary:
a: 1,
b: {
c: 3,
d: 2,
e: 4
},
c: 5
"""
overrides = overrides or {}
for key, value in iteritems(overrides):
current = self.get(key)
if isinstance(value, dict) and isinstance(current, dict):
current.patch(value)
else:
self[key] = value
__setattr__ = __setitem__
__getattr__ = __getitem__
# pylint: disable-msg=R0903
class AutoPropDescriptor(object):
"""
Descriptor for providing default values.
"""
def __init__(self, default_prop):
self.default_prop = default_prop
self.name = default_prop.__name__
self.assigned = '_' + self.name
def __get_assigned__(self, instance):
return getattr(instance, self.assigned, None)
def __set_assigned__(self, instance, value):
return setattr(instance, self.assigned, value)
def __get__(self, instance, owner):
value = self.__get_assigned__(instance)
return value or self.default_prop(instance)
def __set__(self, instance, value):
self.__set_assigned__(instance, value)
# pylint: disable-msg=R0903
class AutoPropMetaClass(type):
"""
Meta class for enabling autoprops.
"""
def __new__(mcs, cname, cbases, cattrs):
autoprops = {name: member
for name, member in iteritems(cattrs)
if getattr(member, 'autoprop', False)}
for name, member in iteritems(autoprops):
cattrs[name] = AutoPropDescriptor(member)
return super(AutoPropMetaClass, mcs).__new__(
mcs, cname, cbases, cattrs)
# pylint: disable-msg=R0903
class AutoProp(with_metaclass(AutoPropMetaClass, object)):
"""
The base class for all objects supporting autoprops.
Usage:
class Project(AutoProp):
def __init__(self, config=None):
self.config = config or {}
@AutoProp.default
def source_dir(self):
return self.config.get('source')
p = Project({'source': 'test'})
p.source_dir
>>> 'test'
p.source_dir = 'xyz'
p.source_dir
>>> 'xyz'
"""
@staticmethod
def default(function):
"""
Decorator for autoprops.
"""
function.autoprop = True
return function
|