From: Michael Tremer Date: Fri, 26 Nov 2010 12:45:17 +0000 (+0100) Subject: Change System to be a singleton pattern class and connect it to the hypervisor class. X-Git-Tag: v0.3~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32aeec7300ee16572e407d279e0c6244f4d8cbae;p=oddments%2Ffireinfo.git Change System to be a singleton pattern class and connect it to the hypervisor class. --- diff --git a/fireinfo/hypervisor.py b/fireinfo/hypervisor.py index dc0d373..4b8f873 100644 --- a/fireinfo/hypervisor.py +++ b/fireinfo/hypervisor.py @@ -1,11 +1,22 @@ #!/usr/bin/python import _fireinfo +import system class Hypervisor(object): def __init__(self): self.__info = _fireinfo.get_hypervisor() + @property + def system(self): + """ + Return the current instance of the System class. + + We need to do that as a property because otherwise + we get a recursion. + """ + return system.System() + @property def vendor(self): """ diff --git a/fireinfo/system.py b/fireinfo/system.py index 50eddfe..43807c3 100644 --- a/fireinfo/system.py +++ b/fireinfo/system.py @@ -9,7 +9,20 @@ import cpu import device import hypervisor +class Singleton(type): + def __init__(cls, name, bases, dict): + super(Singleton, cls).__init__(name, bases, dict) + cls.instance = None + + def __call__(cls, *args, **kw): + if cls.instance is None: + cls.instance = super(Singleton, cls).__call__(*args, **kw) + + return cls.instance + + class System(object): + __metaclass__ = Singleton def __init__(self): # find all devices