Home | Trees | Indices | Help |
---|
|
1 # Customization of GPI component object assignment: Component Filters 2 # 3 # Example of usage: 4 # 5 # j.f = 'myfile' <=> j.f = File('myfile') 6 # j.application = 'DaVinci' <=> DaVinci() 7 # 8 # j = Job() 9 # j2 = Job(j) <=> copy constructor 10 # 11 # 12 # 13 # Semantics: 14 # gpi_proxy.x = v --> f = select_filter(category_of(x)); f(v,schema_item_of(x)) 15 # gpi_proxy.y = [v1,v2] --> f = select_filter(category_of(x)); [f(v,schema_item_of(y)) for v in [v1,v2]] 16 # x = X(y,...) --> f = select_filter(X._category); f(y,None) 17 # 18 # Component filters are applied *only* to component properties and they are applied *before* attribute filters. 19 20 # Component filter has the following signature: 21 # filter(val,item) --> return a GangaObject instance or None if no conversion performed 22 # 23 # item is None if the filter is called outside of the attribute assignment context 24 # 25 # If conversion takes place filter MUST return an object which is an instance (derived) of GangaObject. 26 27 # Void filter does nothing. This is the default filter if no other default has been defined. 28 29 from Ganga.Utility.Config import makeConfig 30 from Ganga.Utility.Config.Config import ConfigError 31 32 # test configuration properties 33 config = makeConfig('GPIComponentFilters',"""Customization of GPI component object assignment 34 for each category there may be multiple filters registered, the one used being defined 35 in the configuration file in [GPIComponentFilters] 36 e.g: {'datasets':{'lhcbdatasets':lhcbFilter, 'testdatasets':testFilter}...} 37 """,is_open=False) 38 417444 #for each category there may be multiple filters registered, the one used being defined 45 #in the configuration file in [GPIComponentFilters] 46 #e.g: {'datasets':{'lhcbdatasets':lhcbFilter, 'testdatasets':testFilter}...} 47 self._dict = {} 48 self.default = None49 51 52 if not self._dict.has_key(category): 53 self._dict[category] = {} 54 55 #the filter can be registered as a tuple: ('filtername',filterfunction) 56 #or just as a function in which case the function name is used as an alias 57 if type(filter)==type(()) and len(filter)>=2: 58 filtername = filter[0] 59 filterfunc = filter[1] 60 else: 61 try: 62 filtername = filter.__name__ 63 filterfunc = filter 64 except AttributeError,e: 65 raise ValueError('FilterManager: Invalid component filter %s.'%filter) 66 67 if self._dict[category].has_key(filtername): 68 raise ValueError('FilterManager: %s component filter already exists for %s category '%(filtername,category)) 69 70 if not config.options.has_key(category): 71 config.addOption(category, "", "") 72 config.overrideDefaultValue(category,filtername) 73 self._dict[category][filtername] = filterfunc76 if self.default: 77 raise ValueError('FilterManager: default filter already exists') 78 79 self.default = filter8082 try: 83 filters = self._dict[category] 84 except KeyError: 85 #no filters registered for this category 86 if self.default: return self.default 87 return void_filter 88 89 try: 90 filtername = config[category] 91 return filters[filtername] 92 except ConfigError: 93 #if we have only one filter registered for this category we use it 94 if len(filters)==1: 95 return filters.values()[0] 96 else: #ambiguity 97 raise ValueError('FilterManager: Multiple filters detected for %s category: %s, '\ 98 'but no one has be set as default in [GPIComponentFilters] section of the configuration file' 99 % (category,str(filters.keys()))) 100 except KeyError: 101 #wrong filter name in configuration for this category 102 raise ValueError('FilterManager: %s filter is not registered for %s category.'\ 103 'Check your [GPIComponentFilters] section of the configuration file' 104 % (filtername,category))105 106 107 # all filters register here... 108 allComponentFilters = _ComponentFilterManager() 109
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Mon Jun 25 10:35:26 2012 | http://epydoc.sourceforge.net |