/usr/lib/python2.7/dist-packages/chef/base.py is in python-chef 0.2.3-2.
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 | import collections
import pkg_resources
from chef.api import ChefAPI
from chef.exceptions import *
class ChefQuery(collections.Mapping):
def __init__(self, obj_class, names, api):
self.obj_class = obj_class
self.names = names
self.api = api
def __len__(self):
return len(self.names)
def __contains__(self, key):
return key in self.names
def __iter__(self):
return iter(self.names)
def __getitem__(self, name):
if name not in self:
raise KeyError('%s not found'%name)
return self.obj_class(name, api=self.api)
class ChefObjectMeta(type):
def __init__(cls, name, bases, d):
super(ChefObjectMeta, cls).__init__(name, bases, d)
if name != 'ChefObject':
ChefObject.types[name.lower()] = cls
cls.api_version_parsed = pkg_resources.parse_version(cls.api_version)
class ChefObject(object):
"""A base class for Chef API objects."""
__metaclass__ = ChefObjectMeta
types = {}
url = ''
attributes = {}
api_version = '0.9'
def __init__(self, name, api=None, skip_load=False):
self.name = name
self.api = api or ChefAPI.get_global()
self._check_api_version(self.api)
self.url = self.__class__.url + '/' + self.name
self.exists = False
data = {}
if not skip_load:
try:
data = self.api[self.url]
except ChefServerNotFoundError:
pass
else:
self.exists = True
self._populate(data)
def _populate(self, data):
for name, cls in self.__class__.attributes.iteritems():
if name in data:
value = cls(data[name])
else:
value = cls()
setattr(self, name, value)
@classmethod
def from_search(cls, data, api=None):
obj = cls(data.get('name'), api=api, skip_load=True)
obj.exists = True
obj._populate(data)
return obj
@classmethod
def list(cls, api=None):
"""Return a :class:`ChefQuery` with the available objects of this type.
"""
api = api or ChefAPI.get_global()
cls._check_api_version(api)
names = [name for name, url in api[cls.url].iteritems()]
return ChefQuery(cls, names, api)
@classmethod
def create(cls, name, api=None, **kwargs):
"""Create a new object of this type. Pass the initial value for any
attributes as keyword arguments.
"""
api = api or ChefAPI.get_global()
cls._check_api_version(api)
obj = cls(name, api, skip_load=True)
for key, value in kwargs.iteritems():
setattr(obj, key, value)
api.api_request('POST', cls.url, data=obj)
return obj
def save(self, api=None):
"""Save this object to the server. If the object does not exist it
will be created.
"""
api = api or self.api
try:
api.api_request('PUT', self.url, data=self)
except ChefServerNotFoundError, e:
# If you get a 404 during a save, just create it instead
# This mirrors the logic in the Chef code
api.api_request('POST', self.__class__.url, data=self)
def delete(self, api=None):
"""Delete this object from the server."""
api = api or self.api
api.api_request('DELETE', self.url)
def to_dict(self):
d = {
'name': self.name,
'json_class': 'Chef::'+self.__class__.__name__,
'chef_type': self.__class__.__name__.lower(),
}
for attr in self.__class__.attributes.iterkeys():
d[attr] = getattr(self, attr)
return d
def __str__(self):
return self.name
def __repr__(self):
return '<%s %s>'%(type(self).__name__, self)
@classmethod
def _check_api_version(cls, api):
# Don't enforce anything if api is None, since there is sometimes a
# use for creating Chef objects without an API connection (just for
# serialization perhaps).
if api and cls.api_version_parsed > api.version_parsed:
raise ChefAPIVersionError, "Class %s is not compatible with API version %s" % (cls.__name__, api.version)
|