blob: 3d445103a9bebac4265e4c1c64431fe4ce360a21 (
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
|
"""
Base class for any serializable list of things...
Michael DeHaan <mdehaan@redhat.com>
"""
import serializable
import utils
import msg
class Collection(serializable.Serializable):
def __init__(self,config):
"""
Constructor.
"""
self.config = config
self.clear()
def factory_produce(self):
raise exceptions.NotImplementedError
def filename(self):
raise exceptions.NotImplementedError
def clear(self):
if utils.app_debug:
print "Collection::clear"
self.listing = {}
def find(self,name):
"""
Return anything named 'name' in the collection, else return None if
no objects can be found.
"""
if utils.app_debug:
print "Collection::find(%s)" % name
if name in self.listing.keys():
return self.listing[name]
return None
def to_datastruct(self):
"""
Serialize the collection
"""
if utils.app_debug:
print "Collection::to_datastruct"
datastruct = [x.to_datastruct() for x in self.listing.values()]
return datastruct
def from_datastruct(self,datastruct):
if utils.app_debug:
print "Collection::from_datastruct(%s)" % datastruct
if datastruct is None:
print "DEBUG: from_datastruct -> None, skipping"
return
print "DEBUG: from_datastruct: %s" % datastruct
for seed_data in datastruct:
item = self.factory_produce(self.config,seed_data)
self.add(item)
def add(self,ref):
"""
Add an object to the collection, if it's valid. Returns True
if the object was added to the collection. Returns False if the
object specified by ref deems itself invalid (and therefore
won't be added to the collection).
"""
if utils.app_debug:
print "Collection::add(%s)" % ref
if ref is None or not ref.is_valid():
if utils.last_error() is None or utils.last_error() == "":
utils.set_error("bad_param")
return False
self.listing[ref.name] = ref
return True
def printable(self):
"""
Creates a printable representation of the collection suitable
for reading by humans or parsing from scripts. Actually scripts
would be better off reading the YAML in the config files directly.
"""
if utils.app_debug:
print "Collection::printable"
values = map(lambda(a): a.printable(), sorted(self.listing.values()))
if len(values) > 0:
return "\n\n".join(values)
else:
return msg.m("empty_list")
def __iter__(self):
"""
Iterator for the collection. Allows list comprehensions, etc
"""
if utils.app_debug:
print "Collection::__iter__"
for a in self.listing.values():
yield a
def __len__(self):
"""
Returns size of the collection
"""
if utils.app_debug:
print "Collection::__len__"
return len(self.listing.values())
|