summaryrefslogtreecommitdiffstats
path: root/ipalib/base.py
blob: ae9dfae49f048f72b3840f0f1718d6028a0bbc7a (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# Authors:
#   Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2008  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Base classes for plug-in architecture and generative API.
"""

import re
import inspect
import errors


class NameSpace(object):
	"""
	A read-only namespace of (key, value) pairs that can be accessed
	both as instance attributes and as dictionary items.  For example:

	>>> ns = NameSpace(dict(my_message='Hello world!'))
	>>> ns.my_message
	'Hello world!'
	>>> ns['my_message']
	'Hello world!'

	Keep in mind that Python doesn't offer true ready-only attributes. A
	NameSpace is read-only in that it prevents programmers from
	*accidentally* setting its attributes, but a motivated programmer can
	still set them.

	For example, setting an attribute the normal way will raise an exception:

	>>> ns.my_message = 'some new value'
	(raises errors.SetError)

	But a programmer could still set the attribute like this:

	>>> ns.__dict__['my_message'] = 'some new value'

	You should especially not implement a security feature that relies upon
	NameSpace being strictly read-only.
	"""

	__locked = False # Whether __setattr__ has been locked

	def __init__(self, kw, order=None):
		"""
		The `kw` argument is a dict of the (key, value) pairs to be in this
		NameSpace instance.  The optional `order` keyword argument specifies
		the order of the keys in this namespace; if omitted, the default is
		to sort the keys in ascending order.
		"""
		assert isinstance(kw, dict)
		self.__kw = dict(kw)
		for (key, value) in self.__kw.items():
			assert not key.startswith('_')
			setattr(self, key, value)
		if order is None:
			self.__keys = sorted(self.__kw)
		else:
			self.__keys = list(order)
			assert set(self.__keys) == set(self.__kw)
		self.__locked = True

	def __setattr__(self, name, value):
		"""
		Raises an exception if trying to set an attribute after the
		NameSpace has been locked; otherwise calls object.__setattr__().
		"""
		if self.__locked:
			raise errors.SetError(name)
		super(NameSpace, self).__setattr__(name, value)

	def __getitem__(self, key):
		"""
		Returns item from namespace named `key`.
		"""
		return self.__kw[key]

	def __hasitem__(self, key):
		"""
		Returns True if namespace has an item named `key`.
		"""
		return bool(key in self.__kw)

	def __iter__(self):
		"""
		Yields the names in this NameSpace in ascending order, or in the
		the order specified in `order` kw arg.

		For example:

		>>> ns = NameSpace(dict(attr_b='world', attr_a='hello'))
		>>> list(ns)
		['attr_a', 'attr_b']
		>>> [ns[k] for k in ns]
		['hello', 'world']
		"""
		for key in self.__keys:
			yield key

	def __call__(self):
		"""
		Iterates through the values in this NameSpace in the same order as
		the keys.
		"""
		for key in self.__keys:
			yield self.__kw[key]

	def __len__(self):
		"""
		Returns number of items in this NameSpace.
		"""
		return len(self.__keys)



class Named(object):
	__name = None

	def _get_name(self):
		return self.__class__.__name__

	def __get_loc(self):
		cls = self.__class__
		return '%s.%s' % (cls.__module__, cls.__name__)
	loc = property(__get_loc)

	def __get_name(self):
		if self.__name is None:
			self.__name = self._get_name()
		return self.__name
	name = property(__get_name)

	def __get_cli_name(self):
		return self.name.replace('_', '-')
	cli_name = property(__get_cli_name)


class AbstractCommand(object):
	def __call__(self):
		print 'You called %s.%s()' % (
			self.__class__.__module__,
			self.__class__.__name__
		)

	def get_doc(self, _):
		"""
		This should return a gettext translated summarary of the command.

		For example, if you were documenting the 'add-user' command, you're
		method would look something like this.

		>>> def get_doc(self, _):
		>>>		return _('add new user')
		"""
		raise NotImplementedError('%s.%s.%s()' % (
				self.__class__.__module__,
				self.__class__.__name__,
				'get_doc',
			)
		)


class Attribute(Named):
	__locked = False
	__obj = None

	def __init__(self):
		m = re.match('^([a-z]+)__([a-z]+)$', self.__class__.__name__)
		assert m
		self.__obj_name = m.group(1)
		self.__attr_name = m.group(2)

	def __get_obj(self):
		return self.__obj
	def __set_obj(self, obj):
		if self.__obj is not None:
			raise errors.TwiceSetError(self.__class__.__name__, 'obj')
		assert isinstance(obj, Object)
		self.__obj = obj
		assert self.obj is obj
	obj = property(__get_obj, __set_obj)

	def __get_obj_name(self):
		return self.__obj_name
	obj_name = property(__get_obj_name)

	def __get_attr_name(self):
		return self.__attr_name
	attr_name = property(__get_attr_name)


class Method(AbstractCommand, Attribute):
	def _get_name(self):
		return '%s_%s' % (self.attr_name, self.obj_name)


class Property(Attribute):
	def _get_name(self):
		return self.attr_name


class Command(AbstractCommand, Named):
	pass


class Object(Named):
	__methods = None
	__properties = None

	def __get_methods(self):
		return self.__methods
	def __set_methods(self, methods):
		if self.__methods is not None:
			raise errors.TwiceSetError(
				self.__class__.__name__, 'methods'
			)
		assert type(methods) is NameSpace
		self.__methods = methods
		assert self.methods is methods
	methods = property(__get_methods, __set_methods)

	def __get_properties(self):
		return self.__properties
	def __set_properties(self, properties):
		if self.__properties is not None:
			raise errors.TwiceSetError(
				self.__class__.__name__, 'properties'
			)
		assert type(properties) is NameSpace
		self.__properties = properties
		assert self.properties is properties
	properties = property(__get_properties, __set_properties)



class AttributeCollector(object):
	def __init__(self):
		self.__d = {}

	def __getitem__(self, key):
		assert isinstance(key, str)
		if key not in self.__d:
			self.__d[key] = {}
		return self.__d[key]

	def __iter__(self):
		for key in self.__d:
			yield key

	def add(self, i):
		assert isinstance(i, Attribute)
		self[i.obj_name][i.attr_name] = i

	def namespaces(self):
		for key in self:
			yield (key, NameSpace(self[key]))


class Collector(object):
	def __init__(self):
		self.__d = {}

	def __get_d(self):
		return dict(self.__d)
	d = property(__get_d)

	def __iter__(self):
		for key in self.__d:
			yield key

	def add(self, i):
		assert isinstance(i, Named)
		self.__d[i.name] = i

	def ns(self):
		return NameSpace(self.__d)


class Proxy(object):
	def __init__(self, d):
		self.__d = d

	def __getattr__(self, name):
		if name not in self.__d:
			raise AttributeError(name)
		return self.__d[name]



class Registrar(object):
	__allowed = (
		Command,
		Object,
		Method,
		Property,
	)

	def __init__(self, d=None):
		if d is None:
			self.__d = {}
		else:
			assert isinstance(d, dict)
			assert d == {}
			self.__d = d
		for base in self.__allowed:
			assert inspect.isclass(base)
			assert base.__name__ not in self.__d
			sub_d = {}
			self.__d[base.__name__] = sub_d
			setattr(self, base.__name__, Proxy(sub_d))

	def __iter__(self):
		for key in self.__d:
			yield key

	def __getitem__(self, key):
		return dict(self.__d[key])

	def items(self):
		for key in self:
			yield (key, self[key])

	def __findbase(self, cls):
		if not inspect.isclass(cls):
			raise errors.RegistrationError('not a class', cls)
		for base in self.__allowed:
			if issubclass(cls, base):
				return base
		raise errors.RegistrationError(
			'not subclass of an allowed base',
			cls,
		)

	def __call__(self, cls):
		base = self.__findbase(cls)
		ns = self.__d[base.__name__]
		assert cls.__name__ not in ns
		ns[cls.__name__] = cls


	def get_instances(self, base_name):
		for cls in self[base_name].values():
			yield cls()

	def get_attrs(self, base_name):
		d = {}
		for i in self.get_instances(base_name):
			if i.obj_name not in d:
				d[i.obj_name] = []
			d[i.obj_name].append(i)
		return d






class RegistrarOld(object):


	def __init__(self):
		self.__tmp_commands = Collector()
		self.__tmp_objects = Collector()
		self.__tmp_methods = AttributeCollector()
		self.__tmp_properties = AttributeCollector()

	def __get_objects(self):
		return self.__objects
	objects = property(__get_objects)

	def __get_commands(self):
		return self.__commands
	commands = property(__get_commands)


	def __get_target(self, i):
		if isinstance(i, Command):
			return self.__tmp_commands
		if isinstance(i, Object):
			return self.__tmp_objects
		if isinstance(i, Method):
			return self.__tmp_methods
		assert isinstance(i, Property)
		return self.__tmp_properties


	def register(self, cls):
		assert inspect.isclass(cls)
		assert issubclass(cls, Named)
		i = cls()
		self.__get_target(i).add(i)


	def finalize(self):
		self.__objects = self.__tmp_objects.ns()
		for (key, ns) in self.__tmp_methods.namespaces():
			self.__objects[key].methods = ns
		for (key, ns) in self.__tmp_properties.namespaces():
			self.__objects[key].properties = ns
		commands = self.__tmp_commands.d
		for obj in self.__objects():
			assert isinstance(obj, Object)
			if obj.methods is None:
				obj.methods = NameSpace({})
			if obj.properties is None:
				obj.properties = NameSpace({})
			for m in obj.methods():
				m.obj = obj
				assert m.name not in commands
				commands[m.name] = m
			for p in obj.properties():
				p.obj = obj
		self.__commands = NameSpace(commands)



class API(object):
	__max_cmd_len = None
	__objects = None
	__commands = None

	def __init__(self, registrar):
		assert isinstance(registrar, Registrar)
		self.__r = registrar

	def __get_objects(self):
		return self.__objects
	objects = property(__get_objects)

	def __get_commands(self):
		return self.__commands
	commands = property(__get_commands)

	def __get_max_cmd_len(self):
		if self.__max_cmd_len is None:
			if self.commands is None:
				return None
			self.__max_cmd_len = max(len(n) for n in self.commands)
		return self.__max_cmd_len
	max_cmd_len = property(__get_max_cmd_len)

	def __items(self, base, name):
		for cls in self.__r[base].values():
			i = cls()
			yield (getattr(i, name), i)

	def __namespace(self, base, name):
		return NameSpace(dict(self.__items(base, name)))



	def finalize(self):
		self.__objects = self.__namespace('Object', 'name')

		m = {}
		for obj in self.__objects():
			if obj.name not in m:
				m[obj.name] = {}

		for cls in self.__r['Method'].values():
			meth = cls()
			assert meth.obj_name in m

		return

		for (key, ns) in self.__tmp_methods.namespaces():
			self.__objects[key].methods = ns
		for (key, ns) in self.__tmp_properties.namespaces():
			self.__objects[key].properties = ns
		commands = self.__tmp_commands.d
		for obj in self.__objects():
			assert isinstance(obj, Object)
			if obj.methods is None:
				obj.methods = NameSpace({})
			if obj.properties is None:
				obj.properties = NameSpace({})
			for m in obj.methods():
				m.obj = obj
				assert m.name not in commands
				commands[m.name] = m
			for p in obj.properties():
				p.obj = obj
		self.__commands = NameSpace(commands)