ATLAS Offline Software
Control/StoreGateBindings/python/Bindings.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
3 # @file: StoreGateBindings/python/Bindings.py
4 # @author: Wim Lavrijsen <WLavrijsen@lbl.gov>
5 # @author: Sebastien Binet <binet@cern.ch>
6 
7 
8 
9 __author__ = """
10 Wim Lavrijsen (WLavrijsen@lbl.gov),
11 Sebastien Binet (binet@cern.ch)
12 """
13 
14 
15 def _setup():
16  import cppyy
17  # StoreGate bindings from dictionary
18  cppyy.load_library( "libAthenaPythonDict" ) # for clidsvc
19  cppyy.load_library( "libStoreGateBindingsDict" ) # for storegatesvc
20  cppyy.load_library( "libStoreGateBindings" ) # not linked from libStoreGateBindingsDict in ROOT6
21 
22  global py_retrieve
23  py_retrieve = cppyy.gbl.AthenaInternal.retrieveObjectFromStore
24 
25  global py_record
26  py_record = cppyy.gbl.AthenaInternal.recordObjectToStore
27 
28  global py_sg_contains
29  py_sg_contains = cppyy.gbl.AthenaInternal.py_sg_contains
30 
31  global py_sg_getitem
32  py_sg_getitem = cppyy.gbl.AthenaInternal.py_sg_getitem
33 
34  # retrieve the StoreGateSvc class
35  global StoreGateSvc
36  StoreGateSvc = cppyy.gbl.StoreGateSvc
37 
38  # add specialized retrieve method
39  def retrieve( self, klass, key = None ):
40  ret = py_retrieve( self, klass, key )
41  if ret and hasattr(ret,'setStore') and not ret.hasStore():
42  if not hasattr(ret,'trackIndices') or ret.trackIndices():
43  if py_sg_contains (self, 'SG::IConstAuxStore', key + 'Aux.'):
44  aux = py_retrieve (self, 'SG::IConstAuxStore', key + 'Aux.')
45  ret.setStore (aux)
46  return ret
47  StoreGateSvc.retrieve = retrieve
48 
49  # add specialized record method
50  def record( self, obj, key, allowMods=True, resetOnly=True, noHist=False ):
51  return py_record( self, obj, key, allowMods, resetOnly, noHist )
52  StoreGateSvc.record = record
53 
54  # add specialized contains method
55  def contains( self, klass_or_clid, key ):
56  from builtins import int
57  if isinstance(klass_or_clid, str):
58  try:
59  clid = int(klass_or_clid)
60  klass = self._pyclidsvc.typename(clid)
61  except ValueError:
62  klass = str(klass_or_clid)
63  pass
64  elif isinstance(klass_or_clid, int):
65  klass = self._pyclidsvc.typename(klass_or_clid)
66  elif isinstance(klass_or_clid, type):
67  klass = klass_or_clid.__name__
68  else:
69  raise TypeError(
70  'argument 2 must be a typename, a clid or a type (got %r)' %
71  type(klass_or_clid))
72  return py_sg_contains( self, klass, key )
73  StoreGateSvc.contains = contains
74 
75  # dict-pythonization of storegate: __setitem__
76  def __setitem__ (self, key, obj):
77  return py_record( self, obj, key, True, True, False )
78  StoreGateSvc.__setitem__ = __setitem__
79 
80  # dict-pythonization of storegate: __getitem__
81  def __getitem__ (self, key):
82  try:
83  ret = py_sg_getitem(self, key)
84  except LookupError as err:
85  raise KeyError(str(err))
86  if ret and hasattr(ret,'setStore') and hasattr(ret,'hasStore') and not ret.hasStore():
87  if not hasattr(ret,'trackIndices') or ret.trackIndices():
88  if py_sg_contains (self, 'SG::IConstAuxStore', key + 'Aux.'):
89  aux = py_retrieve (self, 'SG::IConstAuxStore', key + 'Aux.')
90  ret.setStore (aux)
91  return ret
92  StoreGateSvc.__getitem__ = __getitem__
93 
94  # dict-pythonization of storegate: __len__
95  def __len__ (self):
96  return len(self.keys())
97  StoreGateSvc.__len__ = __len__
98 
99  # make dump print, rather than return string
100  def dump(self, fd = None):
101  if fd is None:
102  import sys
103  fd = sys.stdout
104  print (self.__class__._dump( self ), file=fd)
105  StoreGateSvc._dump = StoreGateSvc.dump
106  StoreGateSvc.dump = dump
107 
108  # allow the use of the pythonized properties interface
109  def __getattr__( self, attr ):
110  try: from GaudiPython.Bindings import iProperty
111  except ImportError: from gaudimodule import iProperty
112  return getattr( iProperty("StoreGateSvc", self), attr )
113 
114  def __setattr__( self, attr, value ):
115  try: from GaudiPython.Bindings import iProperty
116  except ImportError: from gaudimodule import iProperty
117  return setattr( iProperty("StoreGateSvc", self), attr, value )
118 
119  StoreGateSvc.__getattr__ = __getattr__
120  StoreGateSvc.__setattr__ = __setattr__
121 
122  import AthenaPython.PyAthena as PyAthena
123  StoreGateSvc._pyclidsvc = PyAthena.py_svc('ClassIDSvc')
124 
125  def keys(self, clid=None, allKeys=False):
126  """return the list of keys for a given CLID (int, type or class_name)
127  if clid is None, it will return the list of all keys.
128  When 'allKeys' is True it will also return the key aliases.
129  """
130  if isinstance(clid, str):
131  clid = self._pyclidsvc.clid(clid)
132  if isinstance(clid, type):
133  clid = self._pyclidsvc.clid(clid.__name__)
134  if clid is None:
135  return [p.name() for p in self.proxies()]
136  return [str(x) for x in self._cpp_keys(clid, allKeys)]
137  StoreGateSvc._cpp_keys = StoreGateSvc.keys
138  StoreGateSvc.keys = keys
139 
140 
141  #The cppyy version that comes with ROOT v6.22 checks also __len__!=0
142  #when casting to bool. Since we defined a __len__ method, newly-created
143  #(empty) StoreGate instances are always casted to False and therefore
144  #considered invalid.
145  #Work-around by implementing our own __bool__ method
146  StoreGateSvc.__bool__ = lambda self : True
147 
148  return
149 
150 
151 _setup()
152 
153 
154 del _setup
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
python.Bindings._setup
def _setup()
initialize the bindings' registration
Definition: Control/AthenaPython/python/Bindings.py:829
run_Egamma1_LArStrip_Fex.dump
dump
Definition: run_Egamma1_LArStrip_Fex.py:87
AthenaInternal::py_sg_contains
PyObject * py_sg_contains(PyObject *storeGateSvc, PyObject *tp, PyObject *key)
Definition: StoreGatePyExt.cxx:346
contains
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition: hcg.cxx:111
AthenaInternal::py_sg_getitem
PyObject * py_sg_getitem(PyObject *storeGateSvc, PyObject *key)
Definition: StoreGatePyExt.cxx:414
python.Dumpers.typename
def typename(t)
Definition: Dumpers.py:193
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
python.Bindings.__setitem__
__setitem__
Definition: Control/AthenaPython/python/Bindings.py:774
str
Definition: BTagTrackIpAccessor.cxx:11
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:801
python.PyAthenaComps.__setattr__
__setattr__
Definition: PyAthenaComps.py:39
python.Bindings.__getitem__
__getitem__
Definition: Control/AthenaPython/python/Bindings.py:782