/usr/share/pyshared/melange/common/utils.py is in python-melange 2012.1~e4~20120126.11502-0ubuntu1.
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 | # vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# 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.
"""System-level utilities and helper functions."""
import datetime
import inspect
import re
import uuid
from melange.openstack.common import utils as openstack_utils
from melange.common import exception
import_class = openstack_utils.import_class
import_object = openstack_utils.import_object
bool_from_string = openstack_utils.bool_from_string
def parse_int(subject):
try:
return int(subject)
except (ValueError, TypeError):
return None
def utcnow():
return datetime.datetime.utcnow()
def underscore(string):
return re.sub(r'\B((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))',
r'_\1', string).lower()
def exclude(key_values, *exclude_keys):
if key_values is None:
return None
return dict((key, value) for key, value in key_values.iteritems()
if key not in exclude_keys)
def filter_dict(key_values, *include_keys):
if key_values is None:
return None
return dict((key, value) for key, value in key_values.iteritems()
if key in include_keys)
def stringify_keys(dictionary):
if dictionary is None:
return None
return dict((str(key), value) for key, value in dictionary.iteritems())
def find(predicate, items):
for item in items:
if predicate(item) is True:
return item
def generate_uuid():
return str(uuid.uuid4())
def remove_nones(hash):
return dict((key, value)
for key, value in hash.iteritems() if value is not None)
class cached_property(object):
"""A decorator that converts a function into a lazy property.
Taken from : https://github.com/nshah/python-memoize
The function wrapped is called the first time to retrieve the result
and than that calculated result is used the next time you access
the value:
class Foo(object):
@cached_property
def bar(self):
# calculate something important here
return 42
"""
def __init__(self, func, name=None, doc=None):
self.func = func
self.__name__ = name or func.__name__
self.__doc__ = doc or func.__doc__
def __get__(self, obj, type=None):
if obj is None:
return self
value = self.func(obj)
setattr(obj, self.__name__, value)
return value
class MethodInspector(object):
def __init__(self, func):
self._func = func
@cached_property
def required_args(self):
return self.args[0:self.required_args_count]
@cached_property
def optional_args(self):
keys = self.args[self.required_args_count: len(self.args)]
return zip(keys, self.defaults)
@cached_property
def defaults(self):
return self.argspec.defaults or ()
@cached_property
def required_args_count(self):
return len(self.args) - len(self.defaults)
@cached_property
def args(self):
args = self.argspec.args
if inspect.ismethod(self._func):
args.pop(0)
return args
@cached_property
def argspec(self):
return inspect.getargspec(self._func)
def __str__(self):
optionals = ["[{0}=<{0}>]".format(k) for k, v in self.optional_args]
required = ["{0}=<{0}>".format(arg) for arg in self.required_args]
args_str = ' '.join(required + optionals)
return "%s %s" % (self._func.__name__, args_str)
|