[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-changelog] [xen-3.0.5-testing] Add new base class and instance store for new autoplugged api classes
# HG changeset patch # User Tom Wilkie <tom.wilkie@xxxxxxxxx> # Date 1177511884 -3600 # Node ID ca5bfcd32e138388b2a548c18d0ade2c8d1cff46 # Parent 2b994e23f96b58831e28f29857bf6a4fd1459a9e Add new base class and instance store for new autoplugged api classes signed-off-by: Tom Wilkie <tom.wilkie@xxxxxxxxx> --- tools/python/xen/xend/XendAPIStore.py | 59 ++++++++++++++++ tools/python/xen/xend/XendBase.py | 123 ++++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) diff -r 2b994e23f96b -r ca5bfcd32e13 tools/python/xen/xend/XendAPIStore.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/python/xen/xend/XendAPIStore.py Wed Apr 25 15:38:04 2007 +0100 @@ -0,0 +1,59 @@ +#============================================================================ +# This library is free software; you can redistribute it and/or +# modify it under the terms of version 2.1 of the GNU Lesser General Public +# License as published by the Free Software Foundation. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +#============================================================================ +# Copyright (C) 2007 Tom Wilkie <tom.wilkie@xxxxxxxxx> +#============================================================================ +""" +This is a place to put instances of XenAPI objects, +instead of just holding them in arbitrary places. + +All objects which subclass XendBase should use this +mechanism. + +You must register both the uuid and type, and get objects +by type, to ensure safety +""" + +__classes = {} + +def register(uuid, type, inst): + __classes[(uuid, type)] = inst + return inst + +def deregister(uuid, type): + old = get(uuid, type) + del __classes[(uuid, type)] + return old + +def get(uuid, type): + """ + Get the instances by uuid and type + """ + return __classes.get((uuid, type), None) + +def get_all(all_type): + """ + Get all instances by type + """ + return [inst + for ((uuid, t), inst) in __classes.items() + if t == all_type] + +def get_all_uuid(all_type): + """ + Get all uuids by type + """ + return [uuid + for (uuid, t) in __classes.keys() + if t == all_type] diff -r 2b994e23f96b -r ca5bfcd32e13 tools/python/xen/xend/XendBase.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/python/xen/xend/XendBase.py Wed Apr 25 15:38:04 2007 +0100 @@ -0,0 +1,123 @@ +#!/usr/bin/python +#============================================================================ +# This library is free software; you can redistribute it and/or +# modify it under the terms of version 2.1 of the GNU Lesser General Public +# License as published by the Free Software Foundation. +# +# This library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +#============================================================================ +# Copyright (C) 2007 Tom Wilkie <tom.wilkie@xxxxxxxxx> +#============================================================================ +""" +Base class for all XenAPI classes +""" + +from xen.xend.XendError import * +from xen.xend import XendAPIStore + +class XendBase: + # + # These functions describe the object, and what is exposed via the API + # + def getClass(self): + return "Base" + + def getAttrRO(self): + return ['uuid'] + + def getAttrRW(self): + return [] + + def getAttrInst(self): + return [] + + def getMethods(self): + return ["get_record"] + + def getFuncs(self): + return ["get_all", "get_by_uuid", "get_all_records"] + + getClass = classmethod(getClass) + getAttrRO = classmethod(getAttrRO) + getAttrRW = classmethod(getAttrRW) + getAttrInst = classmethod(getAttrInst) + getMethods = classmethod(getMethods) + getFuncs = classmethod(getFuncs) + + def __init__(self, uuid, record): + self.__uuid = uuid + + # First check this class implements all the correct methods: + for attr_ro in self.getAttrRO() + self.getAttrRW(): + if not hasattr(self, "get_%s" % attr_ro): + raise ImplementationError(self.getClass(), + "get_%s" % attr_ro) + + for attr_rw in self.getAttrRW(): + if not hasattr(self, "set_%s" % attr_rw): + raise ImplementationError(self.getClass(), + "set_%s" % attr_rw) + + for method in self.getMethods(): + if not hasattr(self, method): + raise ImplementationError(self.getClass(), + method) + + for func in self.getFuncs(): + if not hasattr(self.__class__, func): + raise ImplementationError(self.getClass(), + func) + + # Next check that the class is being created with the correct + # parameters + if not isinstance(record, dict): + raise CreateUnspecifiedAttributeError( + "record" , self.getClass()) + + for attr_inst in self.getAttrInst(): + if attr_inst not in record: + raise CreateUnspecifiedAttributeError( + attr_inst, self.getClass()) + setattr(self, attr_inst, record[attr_inst]) + + # Finally register it + XendAPIStore.register(uuid, self.getClass(), self) + + def get_uuid(self): + return self.__uuid + + def get_record(self): + keys = self.getAttrRO() + self.getAttrRW() + return dict([(key, getattr(self, "get_%s" % key)()) + for key in keys]) + + # + # Class methods + # + + def get_all(cls): + return XendAPIStore.get_all_uuid(cls.getClass()) + + def get_by_uuid(cls, uuid): + # Sanity check the uuid is one of us + me = XendAPIStore.get(uuid, cls.getClass()) + if me is not None and me.getClass() == cls.getClass(): + # In OSS, ref == uuid + return uuid + else: + raise "Big Error.. TODO!" + + def get_all_records(cls): + return [inst.get_record() + for inst in XendAPIStore.get_all(cls.getClass())] + + get_all = classmethod(get_all) + get_by_uuid = classmethod(get_by_uuid) + get_all_records = classmethod(get_all_records) _______________________________________________ Xen-changelog mailing list Xen-changelog@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/xen-changelog
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |