1 from types import StringTypes
2 from datetime import datetime
4 from sqlalchemy import Column, Integer, String, DateTime
5 from sqlalchemy import Table, Column, MetaData, join, ForeignKey
6 from sqlalchemy.orm import relationship, backref
7 from sqlalchemy.orm import column_property
8 from sqlalchemy.orm import object_mapper
9 from sqlalchemy.orm import validates
10 from sqlalchemy.ext.declarative import declarative_base
12 from sfa.util.sfalogging import logger
13 from sfa.util.xml import XML
15 from sfa.trust.gid import GID
17 ##############################
18 Base=declarative_base()
23 # historically the front end to the db dealt with dicts, so the code was only dealing with dicts
24 # sqlalchemy however offers an object interface, meaning that you write obj.id instead of obj['id']
25 # which is admittedly much nicer
26 # however we still need to deal with dictionaries if only for the xmlrpc layer
28 # here are a few utilities for this
30 # (*) first off, when an old pieve of code needs to be used as-is, if only temporarily, the simplest trick
31 # is to use obj.__dict__
32 # this behaves exactly like required, i.e. obj.__dict__['field']='new value' does change obj.field
33 # however this depends on sqlalchemy's implementation so it should be avoided
35 # (*) second, when an object needs to be exposed to the xmlrpc layer, we need to convert it into a dict
36 # remember though that writing the resulting dictionary won't change the object
37 # essentially obj.__dict__ would be fine too, except that we want to discard alchemy private keys starting with '_'
38 # 2 ways are provided for that:
41 # the former dict(obj) relies on __iter__() and next() below, and does not rely on the fields names
42 # although it seems to work fine, I've found cases where it issues a weird python error that I could not get right
43 # so the latter obj.todict() seems more reliable but more hacky as is relies on the form of fields, so this can probably be improved
45 # (*) finally for converting a dictionary into an sqlalchemy object, we provide
46 # obj.load_from_dict(dict)
50 self._i = iter(object_mapper(self).columns)
53 n = self._i.next().name
54 return n, getattr(self, n)
57 keys=[k for k in d.keys() if not k.startswith('_')]
58 return dict ( [ (k,d[k]) for k in keys ] )
59 def load_from_dict (self, d):
60 for (k,v) in d.iteritems():
62 if isinstance(v, StringTypes) and v.lower() in ['true']: v=True
63 if isinstance(v, StringTypes) and v.lower() in ['false']: v=False
66 # in addition we provide convenience for converting to and from xml records
67 # for this purpose only, we need the subclasses to define 'fields' as either
68 # a list or a dictionary
69 def xml_fields (self):
71 if isinstance(fields,dict): fields=fields.keys()
74 def save_as_xml (self):
75 # xxx not sure about the scope here
76 input_dict = dict( [ (key, getattr(self.key), ) for key in self.xml_fields() if getattr(self,key,None) ] )
77 xml_record=XML("<record />")
78 xml_record.parse_dict (input_dict)
79 return xml_record.toxml()
81 def dump(self, format=None, dump_parents=False):
85 format = format.lower()
87 self.dump_text(dump_parents)
89 print self.save_to_string()
90 elif format == 'simple':
91 print self.dump_simple()
93 raise Exception, "Invalid format %s" % format
95 def dump_text(self, dump_parents=False):
96 # print core fields in this order
97 core_fields = ['hrn', 'type', 'authority', 'gid', 'date_created', 'last_updated']
98 print "".join(['=' for i in range(40)])
100 print " hrn:", self.hrn
101 print " type:", self.type
102 print " authority:", self.authority
103 date_created = utcparse(datetime_to_string(self.date_created))
104 print " date created:", date_created
105 last_updated = utcparse(datetime_to_string(self.last_updated))
106 print " last updated:", last_updated
108 print "\t\t", self.get_gid_object().dump_string(8, dump_parents)
110 # print remaining fields
111 for attrib_name in dir(self):
113 if attrib_name in core_fields:
116 attrib = getattr(self, attrib_name)
119 print " %s: %s" % (attrib_name, attrib)
121 def dump_simple(self):
122 return "Record(record_id=%s, hrn=%s, type=%s, authority=%s, pointer=%s)" % \
123 (self.record_id, self.hrn, self.type, self.authority, self.pointer)
125 # # only intended for debugging
126 # def inspect (self, logger, message=""):
127 # logger.info("%s -- Inspecting AlchemyObj -- attrs"%message)
128 # for k in dir(self):
129 # if not k.startswith('_'):
130 # logger.info (" %s: %s"%(k,getattr(self,k)))
131 # logger.info("%s -- Inspecting AlchemyObj -- __dict__"%message)
133 # for (k,v) in d.iteritems():
134 # logger.info("[%s]=%s"%(k,v))
137 ##############################
138 # various kinds of records are implemented as an inheritance hierarchy
139 # RegRecord is the base class for all actual variants
140 # a first draft was using 'type' as the discriminator for the inheritance
141 # but we had to define another more internal column (classtype) so we
142 # accomodate variants in types like authority+am and the like
144 class RegRecord (Base,AlchemyObj):
145 __tablename__ = 'records'
146 record_id = Column (Integer, primary_key=True)
147 # this is the discriminator that tells which class to use
148 classtype = Column (String)
149 # in a first version type was the discriminator
150 # but that could not accomodate for 'authority+sa' and the like
151 type = Column (String)
152 hrn = Column (String)
153 gid = Column (String)
154 authority = Column (String)
155 peer_authority = Column (String)
156 pointer = Column (Integer, default=-1)
157 date_created = Column (DateTime)
158 last_updated = Column (DateTime)
159 # use the 'type' column to decide which subclass the object is of
160 __mapper_args__ = { 'polymorphic_on' : classtype }
162 fields = [ 'type', 'hrn', 'gid', 'authority', 'peer_authority' ]
163 def __init__ (self, type=None, hrn=None, gid=None, authority=None, peer_authority=None,
164 pointer=None, dict=None):
165 if type: self.type=type
168 if isinstance(gid, StringTypes): self.gid=gid
169 else: self.gid=gid.save_to_string(save_parents=True)
170 if authority: self.authority=authority
171 if peer_authority: self.peer_authority=peer_authority
172 if pointer: self.pointer=pointer
173 if dict: self.load_from_dict (dict)
176 result="<Record id=%s, type=%s, hrn=%s, authority=%s, pointer=%s" % \
177 (self.record_id, self.type, self.hrn, self.authority, self.pointer)
178 # skip the uniform '--- BEGIN CERTIFICATE --' stuff
179 if self.gid: result+=" gid=%s..."%self.gid[28:36]
180 else: result+=" nogid"
185 def validate_gid (self, key, gid):
186 if gid is None: return
187 elif isinstance(gid, StringTypes): return gid
188 else: return gid.save_to_string(save_parents=True)
190 # xxx - there might be smarter ways to handle get/set'ing gid using validation hooks
191 def get_gid_object (self):
192 if not self.gid: return None
193 else: return GID(string=self.gid)
195 def just_created (self):
197 self.date_created=now
198 self.last_updated=now
200 def just_updated (self):
202 self.last_updated=now
204 ##############################
205 # all subclasses define a convenience constructor with a default value for type,
206 # and when applicable a way to define local fields in a kwd=value argument
208 class RegAuthority (RegRecord):
209 __tablename__ = 'authorities'
210 __mapper_args__ = { 'polymorphic_identity' : 'authority' }
211 record_id = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
213 def __init__ (self, **kwds):
214 # fill in type if not previously set
215 if 'type' not in kwds: kwds['type']='authority'
216 # base class constructor
217 RegRecord.__init__(self, **kwds)
219 # no proper data yet, just hack the typename
221 return RegRecord.__repr__(self).replace("Record","Authority")
224 # slice x user (researchers) association
225 slice_researcher_table = \
226 Table ( 'slice_researcher', Base.metadata,
227 Column ('slice_id', Integer, ForeignKey ('records.record_id'), primary_key=True),
228 Column ('researcher_id', Integer, ForeignKey ('records.record_id'), primary_key=True),
232 class RegSlice (RegRecord):
233 __tablename__ = 'slices'
234 __mapper_args__ = { 'polymorphic_identity' : 'slice' }
235 record_id = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
236 #### extensions come here
237 reg_researchers = relationship \
239 secondary=slice_researcher_table,
240 primaryjoin=RegRecord.record_id==slice_researcher_table.c.slice_id,
241 secondaryjoin=RegRecord.record_id==slice_researcher_table.c.researcher_id,
242 backref="reg_slices_as_researcher")
244 def __init__ (self, **kwds):
245 if 'type' not in kwds: kwds['type']='slice'
246 RegRecord.__init__(self, **kwds)
249 return RegRecord.__repr__(self).replace("Record","Slice")
252 class RegNode (RegRecord):
253 __tablename__ = 'nodes'
254 __mapper_args__ = { 'polymorphic_identity' : 'node' }
255 record_id = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
257 def __init__ (self, **kwds):
258 if 'type' not in kwds: kwds['type']='node'
259 RegRecord.__init__(self, **kwds)
262 return RegRecord.__repr__(self).replace("Record","Node")
265 class RegUser (RegRecord):
266 __tablename__ = 'users'
267 # these objects will have type='user' in the records table
268 __mapper_args__ = { 'polymorphic_identity' : 'user' }
269 record_id = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
270 #### extensions come here
271 email = Column ('email', String)
272 # can't use name 'keys' here because when loading from xml we're getting
273 # a 'keys' tag, and assigning a list of strings in a reference column like this crashes
274 reg_keys = relationship \
275 ('RegKey', backref='reg_user',
276 cascade="all, delete, delete-orphan")
278 # so we can use RegUser (email=.., hrn=..) and the like
279 def __init__ (self, **kwds):
280 # handle local settings
281 if 'email' in kwds: self.email=kwds.pop('email')
282 if 'type' not in kwds: kwds['type']='user'
283 RegRecord.__init__(self, **kwds)
285 # append stuff at the end of the record __repr__
287 result = RegRecord.__repr__(self).replace("Record","User")
288 result.replace (">"," email=%s"%self.email)
293 def validate_email(self, key, address):
294 assert '@' in address
298 # xxx tocheck : not sure about eager loading of this one
299 # meaning, when querying the whole records, we expect there should
300 # be a single query to fetch all the keys
301 # or, is it enough that we issue a single query to retrieve all the keys
303 __tablename__ = 'keys'
304 key_id = Column (Integer, primary_key=True)
305 record_id = Column (Integer, ForeignKey ("records.record_id"))
306 key = Column (String)
307 pointer = Column (Integer, default = -1)
309 def __init__ (self, key, pointer=None):
311 if pointer: self.pointer=pointer
314 result="<key id=%s key=%s..."%(self.key_id,self.key[8:16],)
315 try: result += " user=%s"%self.reg_user.record_id
316 except: result += " no-user"
320 ##############################
321 # although the db needs of course to be reachable for the following functions
322 # the schema management functions are here and not in alchemy
323 # because the actual details of the classes need to be known
324 # migrations: this code has no notion of the previous versions
325 # of the data model nor of migrations
326 # sfa.storage.migrations.db_init uses this when starting from
328 def init_tables(engine):
329 logger.info("Initializing db schema from current/latest model")
330 Base.metadata.create_all(engine)
332 def drop_tables(engine):
333 logger.info("Dropping tables from current/latest model")
334 Base.metadata.drop_all(engine)
336 ##############################
337 # create a record of the right type from either a dict or an xml string
338 def make_record (dict={}, xml=""):
339 if dict: return make_record_dict (dict)
340 elif xml: return make_record_xml (xml)
341 else: raise Exception("make_record has no input")
343 # convert an incoming record - typically from xmlrpc - into an object
344 def make_record_dict (record_dict):
345 assert ('type' in record_dict)
346 type=record_dict['type'].split('+')[0]
347 if type=='authority':
348 result=RegAuthority (dict=record_dict)
350 result=RegUser (dict=record_dict)
352 result=RegSlice (dict=record_dict)
354 result=RegNode (dict=record_dict)
356 logger.debug("Untyped RegRecord instance")
357 result=RegRecord (dict=record_dict)
358 logger.info ("converting dict into Reg* with type=%s"%type)
359 logger.info ("returning=%s"%result)
361 # register non-db attributes in an extensions field
364 def make_record_xml (xml):
365 xml_record = XML(xml)
366 xml_dict = xml_record.todict()
367 logger.info("load from xml, keys=%s"%xml_dict.keys())
368 return make_record_dict (xml_dict)