Convert lazy singleton functions to Singleton object
- simplify the singleton pattern across the codebase - reduce lines of code needed for crufty initialization - reduce functions that need to mess with a global - Singletons whose semantics changed: - spack.store.store() -> spack.store - spack.repo.path() -> spack.repo.path - spack.config.config() -> spack.config.config - spack.caches.fetch_cache() -> spack.caches.fetch_cache - spack.caches.misc_cache() -> spack.caches.misc_cache
This commit is contained in:
@@ -531,3 +531,48 @@ def __init__(self, wrapped_object):
|
||||
self.__class__ = type(wrapped_name, (wrapped_cls,), {})
|
||||
|
||||
self.__dict__ = wrapped_object.__dict__
|
||||
|
||||
|
||||
class Singleton(object):
|
||||
"""Simple wrapper for lazily initialized singleton objects."""
|
||||
|
||||
def __init__(self, factory):
|
||||
"""Create a new singleton to be inited with the factory function.
|
||||
|
||||
Args:
|
||||
factory (function): function taking no arguments that
|
||||
creates the singleton instance.
|
||||
"""
|
||||
self.factory = factory
|
||||
self._instance = None
|
||||
|
||||
@property
|
||||
def instance(self):
|
||||
if self._instance is None:
|
||||
self._instance = self.factory()
|
||||
return self._instance
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self.instance, name)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.instance)
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.instance)
|
||||
|
||||
|
||||
class LazyReference(object):
|
||||
"""Lazily evaluated reference to part of a singleton."""
|
||||
|
||||
def __init__(self, ref_function):
|
||||
self.ref_function = ref_function
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self.ref_function(), name)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.ref_function())
|
||||
|
||||
def __repr__(self):
|
||||
return repr(self.ref_function())
|
||||
|
Reference in New Issue
Block a user