1 | #============================================================================ |
---|
2 | # This library is free software; you can redistribute it and/or |
---|
3 | # modify it under the terms of version 2.1 of the GNU Lesser General Public |
---|
4 | # License as published by the Free Software Foundation. |
---|
5 | # |
---|
6 | # This library is distributed in the hope that it will be useful, |
---|
7 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
8 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
9 | # Lesser General Public License for more details. |
---|
10 | # |
---|
11 | # You should have received a copy of the GNU Lesser General Public |
---|
12 | # License along with this library; if not, write to the Free Software |
---|
13 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
14 | #============================================================================ |
---|
15 | # Copyright (C) 2007 Tom Wilkie <tom.wilkie@gmail.com> |
---|
16 | #============================================================================ |
---|
17 | """ |
---|
18 | This is a place to put instances of XenAPI objects, |
---|
19 | instead of just holding them in arbitrary places. |
---|
20 | |
---|
21 | All objects which subclass XendBase should use this |
---|
22 | mechanism. |
---|
23 | |
---|
24 | You must register both the uuid and type, and get objects |
---|
25 | by type, to ensure safety |
---|
26 | """ |
---|
27 | |
---|
28 | __classes = {} |
---|
29 | |
---|
30 | def register(uuid, type, inst): |
---|
31 | __classes[(uuid, type)] = inst |
---|
32 | return inst |
---|
33 | |
---|
34 | def deregister(uuid, type): |
---|
35 | old = get(uuid, type) |
---|
36 | del __classes[(uuid, type)] |
---|
37 | return old |
---|
38 | |
---|
39 | def get(uuid, type): |
---|
40 | """ |
---|
41 | Get the instances by uuid and type |
---|
42 | """ |
---|
43 | return __classes.get((uuid, type), None) |
---|
44 | |
---|
45 | def get_all(all_type): |
---|
46 | """ |
---|
47 | Get all instances by type |
---|
48 | """ |
---|
49 | return [inst |
---|
50 | for ((uuid, t), inst) in __classes.items() |
---|
51 | if t == all_type] |
---|
52 | |
---|
53 | def get_all_uuid(all_type): |
---|
54 | """ |
---|
55 | Get all uuids by type |
---|
56 | """ |
---|
57 | return [uuid |
---|
58 | for (uuid, t) in __classes.keys() |
---|
59 | if t == all_type] |
---|