/usr/lib/python3/dist-packages/checkbox/resource.py is in python3-checkbox 0.17.6-0ubuntu6.
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 | #
# This file is part of Checkbox.
#
# Copyright 2010 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
#
import builtins
__all__ = ["ResourceMap"]
class ResourceObject:
__slots__ = ("_iterator", "_name", "_convert",)
def __init__(self, iterator, name, convert=lambda x: x):
self._iterator = iterator
self._name = name
self._convert = convert
def __cmp__(self, other):
return self._try(other, cmp, 0, 1)
def __lt__(self, other):
return self._try(other, lambda a, b: a < b)
def __le__(self, other):
return self._try(other, lambda a, b: a <= b)
def __gt__(self, other):
return self._try(other, lambda a, b: a > b)
def __ge__(self, other):
return self._try(other, lambda a, b: a >= b)
def __eq__(self, other):
return self._try(other, lambda a, b: a == b)
def __ne__(self, other):
return self._try(other, lambda a, b: a != b)
def __contains__(self, other):
return self._try(other, lambda a, b: b in a)
def _try(self, other, function, until=True, default=False):
found = False
for item in self._iterator:
if self._name in item:
value = self._convert(item[self._name])
if function(value, other) == until:
# Append item to results
self._iterator._map._results.append(item)
found = True
return until if found else default
class ResourceIterator:
__slots__ = ("_map", "_values",)
def __init__(self, map, values):
self._map = map
self._values = values
def __contains__(self, elt):
found = False
for value in self._values:
if elt in value:
self._map._results.append(value)
found = True
return found
def __iter__(self):
for value in self._values:
yield value
def __getattr__(self, name):
return ResourceObject(self, name)
class ResourceBuiltin:
__slots__ = ("_function",)
def __init__(self, function):
self._function = function
def __call__(self, object):
return ResourceObject(object._iterator, object._name, self._function)
class ResourceGlobals(dict):
def __init__(self, names, *args, **kwargs):
super(ResourceGlobals, self).__init__(*args, **kwargs)
for name in names:
function = getattr(builtins, name)
self[name] = ResourceBuiltin(function)
class ResourceMap(dict):
__slots__ = ("_results",)
def __getitem__(self, key):
value = super(ResourceMap, self).__getitem__(key)
if isinstance(value, (list, tuple)):
return ResourceIterator(self, value)
elif isinstance(value, dict):
return ResourceIterator(self, [value])
else:
return value
def eval(self, source):
self._results = []
resource_globals = ResourceGlobals(["bool", "float", "int", "str"])
try:
value = eval(source, resource_globals, self)
if (isinstance(value, (bool, int)) and value) \
or (isinstance(value, tuple) and True in value):
return self._results
except Exception:
pass
return None
|