forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_aliases.py
99 lines (80 loc) · 3.99 KB
/
_aliases.py
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
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
"""
Defines a set of aliases for the kernel module to make
accessing certain objects easier.
"""
from mantid.kernel import (ConfigServiceImpl, Logger, PropertyManagerDataServiceImpl, UnitFactoryImpl, UsageServiceImpl)
def lazy_instance_access(cls, key_as_str=False):
"""
Takes a singleton class and wraps it in an LazySingletonHolder
that constructs the instance on first access.
Historically, mantid <= v3.13, the singleton aliases mapped to the
instances rather than the class types, i.e. UsageService is the instance and not the type.
To preserve compatibility with existing scripts, where users have not called UsageService.Instance(),
but also allow the singleton instance startup to be delayed we create a thin wrapper that
delays the first .Instance() call until an attribute is accessed on the wrapper.
:param cls: The singleton class type
:param key_as_str: When ``True``, the key supplied to ``__getitem__``, ``__setitem__``,
``__delitem__``, and ``__contains__`` will be converted to a str. ``None`` wil return ``False``
for ``__contains__``
:return: A new LazySingletonHolder wrapping cls
"""
class LazySingletonHolder(object):
"""
Delays construction of a singleton instance until the
first attribute access.
"""
def __getattribute__(self, item):
# Called for each attribute access. cls.Instance() constructs
# the singleton at first access
return cls.__getattribute__(cls.Instance(), item)
def __len__(self):
return cls.__getattribute__(cls.Instance(), "__len__")()
def __getitem__(self, item):
if key_as_str:
if item is None:
raise KeyError(item)
else:
cls.__getattribute__(cls.Instance(), "__getitem__")(str(item))
return cls.__getattribute__(cls.Instance(), "__getitem__")(item)
def __setitem__(self, item, value):
if key_as_str:
if item is None:
raise KeyError(item)
else:
return cls.__getattribute__(cls.Instance(), "__setitem__")(str(item), value)
return cls.__getattribute__(cls.Instance(), "__setitem__")(item, value)
def __delitem__(self, item):
if key_as_str:
if item is None:
raise KeyError(item)
else:
return cls.__getattribute__(cls.Instance(), "__delitem__")(str(item))
return cls.__getattribute__(cls.Instance(), "__delitem__")(item)
def __contains__(self, item):
if key_as_str:
# check for things that evaluate to False
if (item is None) or (not bool(item)):
return False
else:
# convert the ``item`` to a string
return cls.__getattribute__(cls.Instance(), "__contains__")(str(item))
else:
return cls.__getattribute__(cls.Instance(), "__contains__")(item)
return LazySingletonHolder()
UsageService = lazy_instance_access(UsageServiceImpl)
ConfigService = lazy_instance_access(ConfigServiceImpl)
PropertyManagerDataService = lazy_instance_access(PropertyManagerDataServiceImpl)
UnitFactory = lazy_instance_access(UnitFactoryImpl)
config = ConfigService
pmds = PropertyManagerDataService
###############################################################################
# Set up a general Python logger. Others can be created as they are required
# if a user wishes to be more specific
###############################################################################
logger = Logger("Python")