/usr/lib/python2.7/dist-packages/twext/who/util.py is in python-twext 0.1.b2.dev15059-1.
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | # -*- test-case-name: twext.who.test.test_util -*-
##
# Copyright (c) 2013-2015 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
"""
Directory service module utilities.
"""
__all__ = [
"ConstantsContainer",
"uniqueResult",
"describe",
]
from inspect import getmembers, isclass, isfunction
from twisted.python.constants import (
Names, Values, Flags, NamedConstant, ValueConstant, FlagConstant,
)
from .idirectory import DirectoryServiceError
class ConstantsContainer(object):
"""
A container for constants.
"""
def __init__(self, sources):
self._constants = {}
self._methods = {}
for source in sources:
if isclass(source):
if issubclass(source, CONTAINER_CLASSES):
self._addConstants(source.iterconstants())
self._addMethods(getmembers(source, isfunction))
else:
raise TypeError(
"Unknown constants type: {0}".format(source)
)
elif isinstance(source, ConstantsContainer):
self._addConstants(source.iterconstants())
self._addMethods(source._methods.iteritems())
elif isinstance(source, CONSTANT_CLASSES):
self._addConstants((source,))
else:
self._addConstants(source)
def _addConstants(self, constants):
for constant in constants:
if hasattr(self, "_constantsClass"):
if constant.__class__ != self._constantsClass:
raise TypeError(
"Can't mix constants classes in the "
"same constants container: {0} != {1}"
.format(constant.__class__, self._constantsClass)
)
else:
self._constantsClass = constant.__class__
if issubclass(self._constantsClass, ValueConstant):
self.lookupByValue = self._lookupByValue
if (
constant.name in self._constants and
self._constants[constant.name] is not constant
):
raise ValueError("Name conflict: {0}".format(constant.name))
self._constants[constant.name] = constant
def _addMethods(self, methods):
for name, method in methods:
if name[0] == "_":
continue
if (
name in self._constants or
(name in self._methods and self._methods[name] is not method)
):
raise ValueError("Name conflict: {0}".format(name))
self._methods[name] = method
def __getattr__(self, name):
attr = self._constants.get(name, None)
if attr is not None:
return attr
attr = self._methods.get(name, None)
if attr is not None:
return attr
raise AttributeError(name)
def iterconstants(self):
return self._constants.itervalues()
def lookupByName(self, name):
try:
return self._constants[name]
except KeyError:
raise ValueError(name)
def _lookupByValue(self, value):
for constant in self.iterconstants():
if constant.value == value:
return constant
raise ValueError(value)
def uniqueResult(values):
result = None
for value in values:
if result is None:
result = value
else:
raise DirectoryServiceError(
"Multiple values found where one expected."
)
return result
def firstResult(values):
for value in values:
return value
return None
def describe(constant):
if isinstance(constant, FlagConstant):
return "|".join(
sorted(getattr(flag, "description", flag.name) for flag in constant)
)
if isinstance(constant, (NamedConstant, ValueConstant)):
for attr in ("description", "name"):
description = getattr(constant, attr, None)
if description is not None:
return description
return unicode(constant)
CONTAINER_CLASSES = (Names, Values, Flags)
CONSTANT_CLASSES = (NamedConstant, ValueConstant, FlagConstant)
|