1
2
3
4
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
22
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
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
78
79
80
81
83
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:
107 from MigrationControl import migration
108 if migration.isAllowed(attrDict['category'], attrDict['name'], attrDict['version'], msg = msg):
109
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
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
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