3
3
# Copyright 2010 United States Government as represented by the
4
4
# Administrator of the National Aeronautics and Space Administration.
5
# Copyright 2011 Justin Santa Barbara
5
6
# All Rights Reserved.
7
8
# Licensed under the Apache License, Version 2.0 (the "License"); you may
53
57
__import__(mod_str)
54
58
return getattr(sys.modules[mod_str], class_str)
55
59
except (ImportError, ValueError, AttributeError), exc:
56
logging.debug(_('Inner Exception: %s'), exc)
60
LOG.debug(_('Inner Exception: %s'), exc)
57
61
raise exception.NotFound(_('Class %s cannot be found') % class_str)
235
239
return ':'.join(map(lambda x: "%02x" % x, mac))
242
def generate_password(length=20):
243
"""Generate a random sequence of letters and digits
244
to be used as a password. Note that this is not intended
245
to represent the ultimate in security.
247
chrs = string.letters + string.digits
248
return "".join([random.choice(chrs) for i in xrange(length)])
238
251
def last_octet(address):
239
252
return int(address.split(".")[-1])
478
491
return json.loads(s)
494
def ensure_b64_encoding(val):
495
"""Safety method to ensure that values expected to be base64-encoded
496
actually are. If they are, the value is returned unchanged. Otherwise,
497
the encoded value is returned.
500
dummy = base64.decode(val)
503
return base64.b64encode(val)
506
def get_from_path(items, path):
507
""" Returns a list of items matching the specified path. Takes an
508
XPath-like expression e.g. prop1/prop2/prop3, and for each item in items,
509
looks up items[prop1][prop2][prop3]. Like XPath, if any of the
510
intermediate results are lists it will treat each list item individually.
511
A 'None' in items or any child expressions will be ignored, this function
512
will not throw because of None (anywhere) in items. The returned list
513
will contain no None values."""
516
raise exception.Error("Invalid mini_xpath")
518
(first_token, sep, remainder) = path.partition("/")
520
if first_token == "":
521
raise exception.Error("Invalid mini_xpath")
528
if not isinstance(items, types.ListType):
529
# Wrap single objects in a list
535
get_method = getattr(item, "get", None)
536
if get_method is None:
538
child = get_method(first_token)
541
if isinstance(child, types.ListType):
542
# Flatten intermediate lists
546
results.append(child)
552
return get_from_path(results, remainder)