Package Ganga :: Package GPIDev :: Package Lib :: Package Registry :: Module BoxRegistry'
[hide private]
[frames] | no frames]

Source Code for Module Ganga.GPIDev.Lib.Registry.BoxRegistry'

  1   
  2   
  3  import Ganga.Utility.logging 
  4  logger = Ganga.Utility.logging.getLogger() 
  5   
  6  # add display default values for the box 
  7  from RegistrySlice import config 
  8  config.addOption('box_columns', 
  9                   ("id","type","name","application"), 
 10                   'list of job attributes to be printed in separate columns') 
 11   
 12  config.addOption('box_columns_width', 
 13                   {'id': 5, 'type':20, 'name':40, 'application':15}, 
 14                   'width of each column') 
 15   
 16  config.addOption('box_columns_functions', 
 17                   {'application': "lambda obj: obj.application._name"}, 
 18                   'optional converter functions') 
 19   
 20  config.addOption('box_columns_show_empty', 
 21                   ['id'], 
 22                   'with exception of columns mentioned here, hide all values which evaluate to logical false (so 0,"",[],...)') 
 23   
 24  from Ganga.Core import GangaException 
25 -class BoxTypeError(GangaException,TypeError):
26 - def __init__(self,what):
27 GangaException.__init__(self,what) 28 self.what=what
29 - def __str__(self):
30 return "BoxTypeError: %s"%self.what
31 32 from Ganga.GPIDev.Base.Objects import GangaObject 33 from Ganga.GPIDev.Schema import Schema, Version, SimpleItem 34 from Ganga.GPIDev.Lib.GangaList.GangaList import makeGangaList 35
36 -class BoxMetadataObject(GangaObject):
37 """Internal object to store names""" 38 _schema = Schema(Version(1,0), {"name": SimpleItem(defvalue="",copyable=1,doc='the name of this object',typelist=["str"])}) 39 _name = "BoxMetadataObject" 40 _category = "internal" 41 _enable_plugin = True 42 _hidden = 1
43 44 from Ganga.Core.GangaRepository.Registry import Registry, RegistryKeyError
45 -class BoxRegistry(Registry):
46 - def _setName(self,obj,name):
47 nobj = self.metadata[self.find(obj)] 48 obj._getWriteAccess() 49 nobj._getWriteAccess() 50 nobj.name = name 51 nobj._setDirty() 52 obj._setDirty()
53
54 - def _getName(self,obj):
55 nobj = self.metadata[self.find(obj)] 56 nobj._getReadAccess() 57 return nobj.name
58
59 - def _remove(self, obj, auto_removed=0):
60 nobj = self.metadata[self.find(obj)] 61 super(BoxRegistry,self)._remove(obj,auto_removed) 62 self.metadata._remove(nobj,auto_removed)
63
64 - def getIndexCache(self,obj):
65 cached_values = ['status','id','name'] 66 c = {} 67 for cv in cached_values: 68 if cv in obj._data: 69 c[cv] = obj._data[cv] 70 slice = BoxRegistrySlice("tmp") 71 for dpv in slice._display_columns: 72 c["display:"+dpv] = slice._get_display_value(obj, dpv) 73 return c
74 75 # Methods for the "box" proxy (but not for slice proxies)
76 - def _get_obj(self,obj_id):
77 if type(obj_id) == str: 78 return self[self._getIDByName(obj_id)] 79 elif type(obj_id) == int: 80 return self[obj_id] 81 else: 82 obj = _unwrap(obj_id) 83 return self[self.find(obj)]
84
85 - def proxy_add(self,obj,name):
86 obj = _unwrap(obj) 87 if isinstance(obj,list): 88 obj = makeGangaList(obj) 89 if not isinstance(obj,GangaObject): 90 raise BoxTypeError("The Box can only contain Ganga Objects (i.e. Applications, Datasets or Backends). Check that the object is first in box.add(obj,'name')") 91 obj = obj.clone() 92 nobj = BoxMetadataObject() 93 nobj.name = name 94 self._add(obj) 95 self.metadata._add(nobj,self.find(obj)) 96 nobj._setDirty() 97 obj._setDirty()
98
99 - def proxy_rename(self,obj_id,name):
100 self._setName(self._get_obj(obj_id), name)
101
102 - def proxy_remove(self,obj_id):
103 self._remove(self._get_obj(obj_id))
104
105 - def getProxy(self):
106 slice = BoxRegistrySlice(self.name) 107 slice.objects = self 108 proxy = BoxRegistrySliceProxy(slice) 109 proxy.add = self.proxy_add 110 proxy.rename = self.proxy_rename 111 proxy.remove = self.proxy_remove 112 return proxy
113
114 - def startup(self):
115 self._needs_metadata = True 116 super(BoxRegistry,self).startup()
117 118 from RegistrySlice import RegistrySlice 119 from Ganga.Core.GangaRepository import getRegistry
120 -class BoxRegistrySlice(RegistrySlice):
121 - def __init__(self,name):
122 super(BoxRegistrySlice,self).__init__(name,display_prefix="box") 123 self._display_columns_functions["id"] = lambda obj : obj._getRegistry().find(obj) 124 self._display_columns_functions["type"] = lambda obj : obj._name 125 self._display_columns_functions["name"] = lambda obj : obj._getRegistry()._getName(obj) 126 from Ganga.Utility.ColourText import Foreground, Background, Effects 127 fg = Foreground() 128 fx = Effects() 129 bg = Background() 130 self.fx = fx 131 self.status_colours = { 'default' : fx.normal, 132 'backends' : fg.orange, 133 'applications': fg.green, 134 'jobs' : fg.blue} 135 self._proxyClass = BoxRegistrySliceProxy
136 137
138 - def _getColour(self,obj):
139 return self.status_colours.get(obj._category,self.fx.normal)
140
141 - def __getitem__(self,id):
142 if isinstance(id,str): 143 matches = [] 144 for o in self.objects: 145 if o._getRegistry()._getName(o) == id: 146 return o 147 matches.append(o) 148 if len(matches) == 1: 149 return matches[0] 150 elif len(matches) > 1: 151 raise RegistryKeyError("Multiple objects with name '%s' found in the box - use IDs!" % id) 152 else: 153 raise RegistryKeyError("No object with name '%s' found in the box!" % id) 154 else: 155 return super(BoxRegistrySlice,self).__getitem__(id)
156 157 158 from RegistrySliceProxy import RegistrySliceProxy, _wrap, _unwrap
159 -class BoxRegistrySliceProxy(RegistrySliceProxy):
160 """This object is a list of objects in the box 161 """
162 - def __call__(self,x):
163 """ Access individual object. Examples: 164 box(10) : get object with id 10 or raise exception if it does not exist. 165 """ 166 return _wrap(self._impl.__call__(x))
167
168 - def __getitem__(self,x):
169 """ Get an item by positional index. Examples: 170 box[-1] : get last object, 171 box[0] : get first object, 172 box[1] : get second object. 173 """ 174 return _wrap(self._impl.__getitem__(x))
175
176 - def __getslice__(self, i1,i2):
177 """ Get a slice. Examples: 178 box[2:] : get first two objects, 179 box[:-10] : get last 10 objects. 180 """ 181 return _wrap(self._impl.__getslice__(i1,i2))
182
183 - def remove_all(self):
184 items = self._impl.objects.items() 185 for id,obj in items: 186 reg = obj._getRegistry() 187 if not reg is None: 188 reg._remove(obj)
189