Package Ganga :: Package GPIDev :: Package Streamers :: Module utilities
[hide private]
[frames] | no frames]

Source Code for Module Ganga.GPIDev.Streamers.utilities

  1  ################################################################################ 
  2  # Ganga Project. http://cern.ch/ganga 
  3  # 
  4  # $Id: utilities.py,v 1.1 2008-07-17 16:40:56 moscicki Exp $ 
  5  ################################################################################ 
  6   
  7  from Ganga.Utility.Plugin import PluginManagerError, allPlugins 
  8  from Ganga.GPIDev.Base.Objects import GangaObject 
  9  from Ganga.GPIDev.Schema import Schema, Version 
 10  from Ganga.GPIDev.Lib.GangaList.GangaList import stripGangaList 
 11  from Ganga.GPIDev.Lib.GangaList.GangaList import makeGangaList 
 12   
 13  import Ganga.Utility.logging 
 14   
 15  logger = Ganga.Utility.logging.getLogger() 
 16   
 17  from Ganga.Utility.external.ordereddict import oDict 
 18  allConverters = oDict() 
 19   
 20  ################################################################################ 
 21  # helper to create a dictionary of simpleattributes 
 22  # according to the schema from a ganga object from 
23 -def serialize(obj):
24 """returns a (nested) dictionary of simple job attributes""" 25 schema = obj._schema 26 attrDict = {} 27 attrDict['name'] = schema.name 28 attrDict['category'] = schema.category 29 attrDict['version'] = (schema.version.major, schema.version.minor) 30 attrDict['simple'] = 0 31 attrDict['data'] = {} 32 def mapper(val): 33 if isinstance(val, GangaObject): 34 return serialize(val) 35 else: 36 for c in allConverters: 37 logger.debug('serialize: trying to apply %s converter',c) 38 vc = allConverters[c].serialize(val) 39 if vc: 40 val = vc 41 break 42 43 simpleDict = {} 44 simpleDict['simple'] = 1 45 simpleDict['data'] = val 46 return simpleDict
47 for attr, item in schema.allItems(): 48 if not item['transient']: 49 val = getattr(obj, attr) 50 if item['sequence']: 51 val = map(mapper, stripGangaList(val)) 52 else: 53 val = mapper(val) 54 attrDict['data'][attr] = val 55 return attrDict 56 57 ################################################################################ 58 # Exception raised by the GangaObjectFactory
59 -class GangaObjectFactoryError(Exception):
60 """ 61 Exception raised by the GangaObjectFactory 62 """
63 - def __init__(self, e = None, msg = None):
64 if msg == None: 65 msg = "GangaObjectFactoryError: see self.e for more info" 66 Exception.__init__(self, msg) 67 self.e = e
68 69 70 ################################################################################ 71 # Empty Ganga Object
72 -class EmptyGangaObject(GangaObject):
73 """Empty Ganga Object. Is used to construct incomplete jobs""" 74 _schema = Schema(Version(0,0), {}) 75 _name = "Unknown" 76 _category = "unknownObjects" 77 _hidden = 1
78 79 ################################################################################ 80 # helper to create a ganga object from dictionary of attributes 81 # according to the schema
82 -def gangaObjectFactory(attrDict, migration_class = None):
83 ## gangaObjectFactory(...) --> (object, migrated, [<list of errors>]) 84 migrated = [False] 85 errors = [] 86 if attrDict['simple']: 87 return (None, migrated[0], errors) 88 if migration_class: 89 cls = migration_class 90 else: 91 try: 92 cls = allPlugins.find(attrDict['category'], attrDict['name']) 93 except PluginManagerError, e: 94 msg = "Plugin Manager Error: %s" % str(e) 95 errors.append(GangaObjectFactoryError(e, msg = msg)) 96 return (EmptyGangaObject(), migrated[0], errors) 97 98 schema = cls._schema 99 major, minor = attrDict['version'] 100 version = Version(major, minor) 101 102 if not schema.version.isCompatible(version): 103 v1 = '.'.join(map(str, [major, minor])) 104 v2 = '.'.join(map(str, [schema.version.major, schema.version.minor])) 105 msg = "Incompatible schema versions of plugin %s in the category %s. Current version %s. Repository version %s." % (attrDict['name'], attrDict['category'],v2, v1) 106 if schema.version.major > version.major: #no forward migration 107 from MigrationControl import migration 108 if migration.isAllowed(attrDict['category'], attrDict['name'], attrDict['version'], msg = msg): 109 # try if migration provided by the plugin class 110 try: 111 old_cls = cls.getMigrationClass(version) 112 except: 113 old_cls = None 114 if old_cls: 115 old_obj, old_migrated, old_errors = gangaObjectFactory(attrDict, migration_class = old_cls) 116 if old_migrated: 117 migrated[0] = old_migrated 118 if not old_errors: 119 try: 120 obj = cls.getMigrationObject(old_obj) 121 #assert(isinstance(obj, cls)) 122 except Exception, e: 123 msg += ' Error in object migration: ' + str(e) 124 else: 125 obj.__setstate__(obj.__dict__) 126 migrated[0] = True 127 return (obj, migrated[0], errors) 128 else: 129 msg += ' Errors in object migration ' + str(map(str, old_errors)) 130 else: 131 msg += ' No migration class is provided.' 132 else: 133 msg += ' Migration was denied in the migration control object.' 134 errors.append(GangaObjectFactoryError(msg = msg)) 135 return (EmptyGangaObject(), migrated[0], errors) 136 137 obj = super(cls, cls).__new__(cls) 138 obj._data = {} 139 140 def mapper(attrDict): 141 if attrDict['simple']: 142 val = attrDict['data'] 143 else: 144 val, attr_migrated, attr_errors = gangaObjectFactory(attrDict) 145 if attr_migrated: 146 migrated[0] = attr_migrated 147 for err in attr_errors: 148 if str(err) not in map(str, errors): 149 # don't duplicate the same errors 150 errors.append(err) 151 return val
152 153 data = attrDict['data'] 154 for attr, item in schema.allItems(): 155 if attr in data: 156 val = data[attr] 157 if item['sequence']: 158 val = makeGangaList(val, mapper, parent = obj) 159 else: 160 val = mapper(val) 161 else: 162 val = schema.getDefaultValue(attr) 163 obj._data[attr] = val 164 obj.__setstate__(obj.__dict__) 165 return (obj, migrated[0], errors) 166