/usr/share/pyshared/juju/lib/schema.py is in juju-0.7 0.7-0ubuntu2.
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | """A schema system for validation of dict-based values."""
import re
class SchemaError(Exception):
"""Raised when invalid input is received."""
def __init__(self, path, message):
self.path = path
self.message = message
info = "%s: %s" % ("".join(self.path), self.message)
super(Exception, self).__init__(info)
class SchemaExpectationError(SchemaError):
"""Raised when an expected value is not found."""
def __init__(self, path, expected, got):
self.expected = expected
self.got = got
message = "expected %s, got %s" % (expected, got)
super(SchemaExpectationError, self).__init__(path, message)
class Constant(object):
"""Something that must be equal to a constant value."""
def __init__(self, value):
self.value = value
def coerce(self, value, path):
if value != self.value:
raise SchemaExpectationError(path, repr(self.value), repr(value))
return value
class Any(object):
"""Anything at all."""
def coerce(self, value, path):
return value
class OneOf(object):
"""Must necessarily match one of the given schemas."""
def __init__(self, *schemas):
"""
@param schemas: Any number of other schema objects.
"""
self.schemas = schemas
def coerce(self, value, path):
"""
The result of the first schema which doesn't raise
L{SchemaError} from its C{coerce} method will be returned.
"""
best_error = None
for i, schema in enumerate(self.schemas):
try:
return schema.coerce(value, path)
except SchemaError, be:
if not best_error or len(be.path) > len(best_error.path):
best_error = be
raise best_error
class Bool(object):
"""Something that must be a C{bool}."""
def coerce(self, value, path):
if not isinstance(value, bool):
raise SchemaExpectationError(path, "bool", repr(value))
return value
class Int(object):
"""Something that must be an C{int} or C{long}."""
def coerce(self, value, path):
if not isinstance(value, (int, long)):
raise SchemaExpectationError(path, "int", repr(value))
return value
class Float(object):
"""Something that must be an C{int}, C{long}, or C{float}."""
def coerce(self, value, path):
if not isinstance(value, (int, long, float)):
raise SchemaExpectationError(path, "number", repr(value))
return value
class String(object):
"""Something that must be a C{str}."""
def coerce(self, value, path):
if not isinstance(value, str):
raise SchemaExpectationError(path, "string", repr(value))
return value
class Unicode(object):
"""Something that must be a C{unicode}."""
def coerce(self, value, path):
if not isinstance(value, unicode):
raise SchemaExpectationError(path, "unicode", repr(value))
return value
class Regex(object):
"""Something that must be a valid Python regular expression."""
def coerce(self, value, path):
try:
regex = re.compile(value)
except re.error:
raise SchemaExpectationError(path,
"regex",
repr(value))
return regex
class UnicodeOrString(object):
"""Something that must be a C{unicode} or {str}.
If the value is a C{str}, it will automatically be decoded.
"""
def __init__(self, encoding):
"""
@param encoding: The encoding to automatically decode C{str}s with.
"""
self.encoding = encoding
def coerce(self, value, path):
if isinstance(value, str):
try:
value = value.decode(self.encoding)
except UnicodeDecodeError:
raise SchemaExpectationError(
path, "unicode or %s string" % self.encoding,
repr(value))
elif not isinstance(value, unicode):
raise SchemaExpectationError(
path, "unicode or %s string" % self.encoding,
repr(value))
return value
class List(object):
"""Something which must be a C{list}."""
def __init__(self, schema):
"""
@param schema: The schema that all values of the list must match.
"""
self.schema = schema
def coerce(self, value, path):
if not isinstance(value, list):
raise SchemaExpectationError(path, "list", repr(value))
new_list = list(value)
path.extend(["[", "?", "]"])
try:
for i, subvalue in enumerate(value):
path[-2] = str(i)
new_list[i] = self.schema.coerce(subvalue, path)
finally:
del path[-3:]
return new_list
class Tuple(object):
"""Something which must be a fixed-length tuple."""
def __init__(self, *schema):
"""
@param schema: A sequence of schemas, which will be applied to
each value in the tuple respectively.
"""
self.schema = schema
def coerce(self, value, path):
if not isinstance(value, tuple):
raise SchemaExpectationError(path, "tuple", repr(value))
if len(value) != len(self.schema):
raise SchemaExpectationError(
path, "tuple with %d elements" % len(self.schema),
repr(value))
new_value = []
path.extend(["[", "?", "]"])
try:
for i, (schema, value) in enumerate(zip(self.schema, value)):
path[-2] = str(i)
new_value.append(schema.coerce(value, path))
finally:
del path[-3:]
return tuple(new_value)
class Dict(object):
"""Something which must be a C{dict} with arbitrary keys."""
def __init__(self, key_schema, value_schema):
"""
@param key_schema: The schema that keys must match.
@param value_schema: The schema that values must match.
"""
self.key_schema = key_schema
self.value_schema = value_schema
def coerce(self, value, path):
if not isinstance(value, dict):
raise SchemaExpectationError(path, "dict", repr(value))
new_dict = {}
key_path = path
if not path:
value_path = ["?"]
else:
value_path = path + [".", "?"]
for key, subvalue in value.items():
new_key = self.key_schema.coerce(key, key_path)
try:
value_path[-1] = str(key)
except ValueError:
value_path[-1] = repr(key)
new_subvalue = self.value_schema.coerce(subvalue, value_path)
new_dict[new_key] = new_subvalue
return new_dict
class KeyDict(object):
"""Something which must be a C{dict} with defined keys.
The keys must be constant and the values must match a per-key schema.
"""
def __init__(self, schema, optional=None):
"""
@param schema: A dict mapping keys to schemas that the values
of those keys must match.
"""
self.optional = set(optional or ())
self.schema = schema
def coerce(self, value, path):
new_dict = {}
if not isinstance(value, dict):
raise SchemaExpectationError(path, "dict", repr(value))
path = path[:]
if path:
path.append(".")
path.append("?")
for k, v in value.iteritems():
if k in self.schema:
try:
path[-1] = str(k)
except ValueError:
path[-1] = repr(k)
new_dict[k] = self.schema[k].coerce(v, path)
else:
# Just preserve entries which are not in the schema.
# This is less likely to eat important values due to
# different app versions being used, for instance.
new_dict[k] = v
for k in self.schema:
if k not in value and k not in self.optional:
path[-1] = k
raise SchemaError(path, "required value not found")
# No need to restore path. It was copied.
return new_dict
class SelectDict(object):
"""Something that must be a C{dict} whose schema depends on some value."""
def __init__(self, key, schemas):
"""
@param key: a key we expect to be in each of the possible schemas,
which we use to select which schema to coerce to.
@param schemas: a dictionary mapping values for C{key} to schemas,
to which the eventual value should be coerced.
"""
self.key = key
self.schemas = schemas
def coerce(self, value, path):
if self.key not in value:
raise SchemaError(
path + ['.', self.key], "required value not found")
selected = value[self.key]
return self.schemas[selected].coerce(value, path)
class OAuthString(String):
"""A L{String} containing OAuth information, colon-separated.
The string should contain three parts::
consumer-key:resource-key:resource-secret
Each part is stripped of leading and trailing whitespace.
@return: A 3-tuple of C{consumer-key}, C{resource-key},
C{resource-secret}.
"""
def coerce(self, value, path):
value = super(OAuthString, self).coerce(value, path)
parts = tuple(part.strip() for part in value.split(":"))
if len(parts) != 3:
raise SchemaError(
path, "does not contain three colon-separated parts")
if "" in parts:
raise SchemaError(
path, "one or more parts are empty")
return parts
|