no change = prettyfied wrt spaces, commas and the like
Thierry Parmentelat [Tue, 2 Jun 2015 12:15:05 +0000 (14:15 +0200)]
sfa/client/common.py
sfa/managers/registry_manager.py
sfa/storage/model.py

index fb0fbcd..43a5b05 100644 (file)
@@ -67,22 +67,23 @@ def terminal_render_node (record, options):
 
 
 ### used in sfi list
-def terminal_render (records,options):
+def terminal_render (records, options):
     # sort records by type
-    grouped_by_type={}
+    grouped_by_type = {}
     for record in records:
-        type=record['type']
-        if type not in grouped_by_type: grouped_by_type[type]=[]
+        type = record['type']
+        if type not in grouped_by_type:
+            grouped_by_type[type]=[]
         grouped_by_type[type].append(record)
-    group_types=grouped_by_type.keys()
+    group_types = grouped_by_type.keys()
     group_types.sort()
     for type in group_types:
-        group=grouped_by_type[type]
+        group = grouped_by_type[type]
 #        print 20 * '-', type
-        try:    renderer=eval('terminal_render_'+type)
-        except: renderer=terminal_render_default
-        for record in group: renderer(record,options)
-
+        try:    renderer = eval('terminal_render_' + type)
+        except: renderer = terminal_render_default
+        for record in group:
+            renderer(record, options)
 
 ####################
 def filter_records(type, records):
index 5264adb..a65832f 100644 (file)
@@ -49,11 +49,11 @@ def _normalize_input (record, reg_key, driver_key):
         # as we're overwriting some user data here
         if driver_key in record:
             logger.warning ("normalize_input: incoming record has both values, using %s"%reg_key)
-        record[driver_key]=record[reg_key]
+        record[driver_key] = record[reg_key]
     # we only have one key set, duplicate for the other one
     elif driver_key in record:
         logger.warning ("normalize_input: you should use '%s' instead of '%s'"%(reg_key,driver_key))
-        record[reg_key]=record[driver_key]
+        record[reg_key] = record[driver_key]
 
 def normalize_input_record (record):
     _normalize_input (record, 'reg-researchers','researcher')
@@ -62,7 +62,8 @@ def normalize_input_record (record):
     # xxx the keys thing could use a little bit more attention:
     # some parts of the code are using 'keys' while they should use 'reg-keys' 
     # but I run out of time for now
-    if 'reg-keys' in record: record['keys']=record['reg-keys']
+    if 'reg-keys' in record:
+        record['keys'] = record['reg-keys']
     return record
 
 class RegistryManager:
@@ -243,7 +244,7 @@ class RegistryManager:
     
     def List (self, api, xrn, origin_hrn=None, options=None):
         if options is None: options={}
-        dbsession=api.dbsession()
+        dbsession = api.dbsession()
         # load all know registry names into a prefix tree and attempt to find
         # the longest matching prefix
         hrn, type = urn_to_hrn(xrn)
@@ -305,11 +306,11 @@ class RegistryManager:
         # Add the email of the user to SubjectAltName in the GID
         email = None
         hrn = Xrn(xrn).get_hrn()
-        dbsession=api.dbsession()
+        dbsession = api.dbsession()
         record=dbsession.query(RegUser).filter_by(hrn=hrn).first()
         if record:
-            email=getattr(record,'email',None)
-        gid = api.auth.hierarchy.create_gid(xrn, create_uuid(), pkey, email = email)
+            email = getattr(record,'email',None)
+        gid = api.auth.hierarchy.create_gid(xrn, create_uuid(), pkey, email=email)
         return gid.save_to_string(save_parents=True)
     
     ####################
@@ -330,7 +331,7 @@ class RegistryManager:
     # hrns is the list of hrns that should be linked to the subject from now on
     # target_type would be e.g. 'user' in the 'slice' x 'researcher' example
     def update_driver_relation (self, api, record_obj, hrns, target_type, relation_name):
-        dbsession=api.dbsession()
+        dbsession = api.dbsession()
         # locate the linked objects in our db
         subject_type=record_obj.type
         subject_id=record_obj.pointer
@@ -346,7 +347,7 @@ class RegistryManager:
         normalize_input_record (record_dict)
         logger.debug("Register: normalized record_dict=%s"%printable(record_dict))
 
-        dbsession=api.dbsession()
+        dbsession = api.dbsession()
         hrn, type = record_dict['hrn'], record_dict['type']
         urn = hrn_to_urn(hrn,type)
         # validate the type
@@ -375,7 +376,7 @@ class RegistryManager:
                 if pub_key and isinstance(pub_key, types.ListType): pub_key = pub_key[0]
                 pkey = convert_public_key(pub_key)
     
-            email=getattr(record,'email',None)
+            email = getattr(record,'email',None)
             gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey, email = email)
             gid = gid_object.save_to_string(save_parents=True)
             record.gid = gid
@@ -425,15 +426,15 @@ class RegistryManager:
         normalize_input_record (record_dict)
         logger.debug("Update: normalized record_dict=%s"%printable(record_dict))
 
-        dbsession=api.dbsession()
+        dbsession = api.dbsession()
         assert ('type' in record_dict)
-        new_record=make_record(dict=record_dict)
-        (type,hrn) = (new_record.type, new_record.hrn)
+        new_record = make_record(dict=record_dict)
+        (type, hrn) = (new_record.type, new_record.hrn)
         
         # make sure the record exists
-        record = dbsession.query(RegRecord).filter_by(type=type,hrn=hrn).first()
+        record = dbsession.query(RegRecord).filter_by(type=type, hrn=hrn).first()
         if not record:
-            raise RecordNotFound("hrn=%s, type=%s"%(hrn,type))
+            raise RecordNotFound("hrn={}, type={}".format(hrn, type))
         record.just_updated()
     
         # Use the pointer from the existing record, not the one that the user
@@ -441,12 +442,12 @@ class RegistryManager:
         pointer = record.pointer
 
         # is there a change in keys ?
-        new_key=None
-        if type=='user':
-            if getattr(new_record,'keys',None):
-                new_key=new_record.keys
-                if isinstance (new_key,types.ListType):
-                    new_key=new_key[0]
+        new_key = None
+        if type == 'user':
+            if getattr(new_record, 'keys', None):
+                new_key = new_record.keys
+                if isinstance (new_key, types.ListType):
+                    new_key = new_key[0]
 
         # take new_key into account
         if new_key:
@@ -455,9 +456,9 @@ class RegistryManager:
             uuid = create_uuid()
             urn = hrn_to_urn(hrn,type)
 
-            email=getattr(new_record,'email',None)
+            email = getattr(new_record, 'email', None)
             if email is None:
-                email=getattr(record,'email',None)
+                email = getattr(record, 'email', None)
             gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey, email = email)
             gid = gid_object.save_to_string(save_parents=True)
         
@@ -486,18 +487,18 @@ class RegistryManager:
         except:
            pass
         if new_key and new_key_pointer:
-            record.reg_keys=[ RegKey (new_key, new_key_pointer)]
+            record.reg_keys = [ RegKey (new_key, new_key_pointer)]
             record.gid = gid
 
         dbsession.commit()
         # update membership for researchers, pis, owners, operators
-        self.update_driver_relations (api, record, new_record)
+        self.update_driver_relations(api, record, new_record)
         
         return 1 
     
     # expecting an Xrn instance
     def Remove(self, api, xrn, origin_hrn=None):
-        dbsession=api.dbsession()
+        dbsession = api.dbsession()
         hrn=xrn.get_hrn()
         type=xrn.get_type()
         request=dbsession.query(RegRecord).filter_by(hrn=hrn)
@@ -540,7 +541,7 @@ class RegistryManager:
 
     # This is a PLC-specific thing, won't work with other platforms
     def get_key_from_incoming_ip (self, api):
-        dbsession=api.dbsession()
+        dbsession = api.dbsession()
         # verify that the callers's ip address exist in the db and is an interface
         # for a node in the db
         (ip, port) = api.remote_addr
@@ -562,7 +563,7 @@ class RegistryManager:
         pkey = Keypair(create=True)
         urn = hrn_to_urn(record.hrn, record.type)
 
-        email=getattr(record,'email',None)
+        email = getattr(record, 'email', None)
         gid_object = api.auth.hierarchy.create_gid(urn, uuid, pkey, email)
         gid = gid_object.save_to_string(save_parents=True)
         record.gid = gid
index f5bf71d..c040852 100644 (file)
@@ -18,7 +18,7 @@ from sfa.util.xml import XML
 from sfa.trust.gid import GID
 
 ##############################
-Base=declarative_base()
+Base = declarative_base()
 
 ####################
 # dicts vs objects
@@ -75,7 +75,7 @@ class AlchemyObj(Record):
 # but we had to define another more internal column (classtype) so we 
 # accomodate variants in types like authority+am and the like
 
-class RegRecord (Base,AlchemyObj):
+class RegRecord (Base, AlchemyObj):
     __tablename__       = 'records'
     record_id           = Column (Integer, primary_key=True)
     # this is the discriminator that tells which class to use
@@ -110,8 +110,10 @@ class RegRecord (Base,AlchemyObj):
         result="<Record id=%s, type=%s, hrn=%s, authority=%s, pointer=%s" % \
                 (self.record_id, self.type, self.hrn, self.authority, self.pointer)
         # skip the uniform '--- BEGIN CERTIFICATE --' stuff
-        if self.gid: result+=" gid=%s..."%self.gid[28:36]
-        else: result+=" nogid"
+        if self.gid:
+            result+=" gid=%s..."%self.gid[28:36]
+        else:
+            result+=" nogid"
         result += ">"
         return result
 
@@ -126,30 +128,35 @@ class RegRecord (Base,AlchemyObj):
         else:                               return gid.save_to_string(save_parents=True)
 
     def validate_datetime (self, key, incoming):
-        if isinstance (incoming, datetime):     return incoming
-        elif isinstance (incoming, (int,float)):return datetime.fromtimestamp (incoming)
-        else: logger.info("Cannot validate datetime for key %s with input %s"%\
-                              (key,incoming))
+        if isinstance (incoming, datetime):
+            return incoming
+        elif isinstance (incoming, (int, float)):
+            return datetime.fromtimestamp (incoming)
+        else:
+            logger.info("Cannot validate datetime for key %s with input %s"%\
+                        (key,incoming))
 
     @validates ('date_created')
-    def validate_date_created (self, key, incoming): return self.validate_datetime (key, incoming)
+    def validate_date_created (self, key, incoming):
+        return self.validate_datetime (key, incoming)
 
     @validates ('last_updated')
-    def validate_last_updated (self, key, incoming): return self.validate_datetime (key, incoming)
+    def validate_last_updated (self, key, incoming):
+        return self.validate_datetime (key, incoming)
 
     # xxx - there might be smarter ways to handle get/set'ing gid using validation hooks 
     def get_gid_object (self):
-        if not self.gid: return None
-        else: return GID(string=self.gid)
+        if not self.gid:        return None
+        else:                   return GID(string=self.gid)
 
     def just_created (self):
-        now=datetime.utcnow()
-        self.date_created=now
-        self.last_updated=now
+        now = datetime.utcnow()
+        self.date_created = now
+        self.last_updated = now
 
     def just_updated (self):
-        now=datetime.utcnow()
-        self.last_updated=now
+        now = datetime.utcnow()
+        self.last_updated = now
 
 #################### cross-relations tables
 # authority x user (pis) association
@@ -178,17 +185,19 @@ class RegAuthority (RegRecord):
     #### extensions come here
     reg_pis             = relationship \
         ('RegUser',
-         secondary=authority_pi_table,
-         primaryjoin=RegRecord.record_id==authority_pi_table.c.authority_id,
-         secondaryjoin=RegRecord.record_id==authority_pi_table.c.pi_id,
-         backref='reg_authorities_as_pi')
+         secondary = authority_pi_table,
+         primaryjoin = RegRecord.record_id==authority_pi_table.c.authority_id,
+         secondaryjoin = RegRecord.record_id==authority_pi_table.c.pi_id,
+         backref = 'reg_authorities_as_pi',
+        )
     
     def __init__ (self, **kwds):
         # handle local settings
         if 'name' in kwds:
             self.name = kwds.pop('name')
         # fill in type if not previously set
-        if 'type' not in kwds: kwds['type']='authority'
+        if 'type' not in kwds:
+            kwds['type']='authority'
         # base class constructor
         RegRecord.__init__(self, **kwds)
 
@@ -202,8 +211,9 @@ class RegAuthority (RegRecord):
         # strip that in case we have <researcher> words </researcher>
         pi_hrns = [ x.strip() for x in pi_hrns ]
         request = dbsession.query (RegUser).filter(RegUser.hrn.in_(pi_hrns))
-        logger.info ("RegAuthority.update_pis: %d incoming pis, %d matches found"%(len(pi_hrns),request.count()))
-        pis = dbsession.query (RegUser).filter(RegUser.hrn.in_(pi_hrns)).all()
+        logger.info("RegAuthority.update_pis: %d incoming pis, %d matches found"\
+                    % (len(pi_hrns), request.count()))
+        pis = dbsession.query(RegUser).filter(RegUser.hrn.in_(pi_hrns)).all()
         self.reg_pis = pis
 
 ####################
@@ -217,20 +227,23 @@ class RegSlice (RegRecord):
          secondary=slice_researcher_table,
          primaryjoin=RegRecord.record_id==slice_researcher_table.c.slice_id,
          secondaryjoin=RegRecord.record_id==slice_researcher_table.c.researcher_id,
-         backref='reg_slices_as_researcher')
+         backref='reg_slices_as_researcher',
+        )
 
     def __init__ (self, **kwds):
-        if 'type' not in kwds: kwds['type']='slice'
+        if 'type' not in kwds:
+            kwds['type']='slice'
         RegRecord.__init__(self, **kwds)
 
     def __repr__ (self):
-        return RegRecord.__repr__(self).replace("Record","Slice")
+        return RegRecord.__repr__(self).replace("Record", "Slice")
 
     def update_researchers (self, researcher_hrns, dbsession):
         # strip that in case we have <researcher> words </researcher>
         researcher_hrns = [ x.strip() for x in researcher_hrns ]
         request = dbsession.query (RegUser).filter(RegUser.hrn.in_(researcher_hrns))
-        logger.info ("RegSlice.update_researchers: %d incoming researchers, %d matches found"%(len(researcher_hrns),request.count()))
+        logger.info ("RegSlice.update_researchers: %d incoming researchers, %d matches found"\
+                     % (len(researcher_hrns), request.count()))
         researchers = dbsession.query (RegUser).filter(RegUser.hrn.in_(researcher_hrns)).all()
         self.reg_researchers = researchers
 
@@ -239,15 +252,16 @@ class RegSlice (RegRecord):
     # helper function is called from the trust/ area that
     def get_pis (self):
         from sqlalchemy.orm import sessionmaker
-        Session=sessionmaker()
-        dbsession=Session.object_session(self)
+        Session = sessionmaker()
+        dbsession = Session.object_session(self)
         from sfa.util.xrn import get_authority
         authority_hrn = get_authority(self.hrn)
         auth_record = dbsession.query(RegAuthority).filter_by(hrn=authority_hrn).first()
         return auth_record.reg_pis
         
     @validates ('expires')
-    def validate_expires (self, key, incoming): return self.validate_datetime (key, incoming)
+    def validate_expires (self, key, incoming):
+        return self.validate_datetime (key, incoming)
 
 ####################
 class RegNode (RegRecord):
@@ -255,12 +269,13 @@ class RegNode (RegRecord):
     __mapper_args__     = { 'polymorphic_identity' : 'node' }
     record_id           = Column (Integer, ForeignKey ("records.record_id"), primary_key=True)
     
-    def __init__ (self, **kwds):
-        if 'type' not in kwds: kwds['type']='node'
+    def __init__(self, **kwds):
+        if 'type' not in kwds:
+            kwds['type']='node'
         RegRecord.__init__(self, **kwds)
 
     def __repr__ (self):
-        return RegRecord.__repr__(self).replace("Record","Node")
+        return RegRecord.__repr__(self).replace("Record", "Node")
 
 ####################
 class RegUser (RegRecord):
@@ -274,20 +289,22 @@ class RegUser (RegRecord):
     # a 'keys' tag, and assigning a list of strings in a reference column like this crashes
     reg_keys            = relationship \
         ('RegKey', backref='reg_user',
-         cascade="all, delete, delete-orphan")
+         cascade = "all, delete, delete-orphan",
+        )
     
     # so we can use RegUser (email=.., hrn=..) and the like
     def __init__ (self, **kwds):
         # handle local settings
-        if 'email' in kwds: self.email=kwds.pop('email')
-        if 'type' not in kwds: kwds['type']='user'
+        if 'email' in kwds:
+            self.email = kwds.pop('email')
+        if 'type' not in kwds:
+            kwds['type'] = 'user'
         RegRecord.__init__(self, **kwds)
 
     # append stuff at the end of the record __repr__
     def __repr__ (self): 
-        result = RegRecord.__repr__(self).replace("Record","User")
-        result.replace (">"," email=%s"%self.email)
-        result += ">"
+        result = RegRecord.__repr__(self).replace("Record", "User")
+        result.replace(">", " email={}>".format(self.email))
         return result
 
     @validates('email') 
@@ -303,17 +320,18 @@ class RegUser (RegRecord):
 class RegKey (Base):
     __tablename__       = 'keys'
     key_id              = Column (Integer, primary_key=True)
-    record_id             = Column (Integer, ForeignKey ("records.record_id"))
+    record_id           = Column (Integer, ForeignKey ("records.record_id"))
     key                 = Column (String)
     pointer             = Column (Integer, default = -1)
     
     def __init__ (self, key, pointer=None):
-        self.key=key
-        if pointer: self.pointer=pointer
+        self.key = key
+        if pointer:
+            self.pointer = pointer
 
     def __repr__ (self):
-        result="<key id=%s key=%s..."%(self.key_id,self.key[8:16],)
-        try:    result += " user=%s"%self.reg_user.record_id
+        result = "<key id=%s key=%s..." % (self.key_id, self.key[8:16],)
+        try:    result += " user=%s" % self.reg_user.record_id
         except: result += " no-user"
         result += ">"
         return result
@@ -339,8 +357,8 @@ class SliverAllocation(Base,AlchemyObj):
             self.allocation_state = kwds['allocation_state']
 
     def __repr__(self):
-        result = "<sliver_allocation sliver_id=%s allocation_state=%s" % \
-                  (self.sliver_id, self.allocation_state)
+        result = "<sliver_allocation sliver_id=%s allocation_state=%s"\
+                 % (self.sliver_id, self.allocation_state)
         return result
 
     @validates('allocation_state')
@@ -381,7 +399,7 @@ class SliverAllocation(Base,AlchemyObj):
         dbsession.commit()
     
     def sync(self, dbsession):
-        constraints = [SliverAllocation.sliver_id==self.sliver_id]
+        constraints = [SliverAllocation.sliver_id == self.sliver_id]
         results = dbsession.query(SliverAllocation).filter(and_(*constraints))
         records = []
         for result in results:
@@ -426,20 +444,20 @@ def make_record (dict=None, xml=""):
 # convert an incoming record - typically from xmlrpc - into an object
 def make_record_dict (record_dict):
     assert ('type' in record_dict)
-    type=record_dict['type'].split('+')[0]
-    if type=='authority':
-        result=RegAuthority (dict=record_dict)
-    elif type=='user':
-        result=RegUser (dict=record_dict)
-    elif type=='slice':
-        result=RegSlice (dict=record_dict)
-    elif type=='node':
-        result=RegNode (dict=record_dict)
+    type = record_dict['type'].split('+')[0]
+    if type == 'authority':
+        result = RegAuthority (dict=record_dict)
+    elif type == 'user':
+        result = RegUser (dict=record_dict)
+    elif type == 'slice':
+        result = RegSlice (dict=record_dict)
+    elif type == 'node':
+        result = RegNode (dict=record_dict)
     else:
         logger.debug("Untyped RegRecord instance")
-        result=RegRecord (dict=record_dict)
-    logger.info ("converting dict into Reg* with type=%s"%type)
-    logger.info ("returning=%s"%result)
+        result = RegRecord (dict=record_dict)
+    logger.info("converting dict into Reg* with type=%s"%type)
+    logger.info("returning=%s"%result)
     # xxx todo
     # register non-db attributes in an extensions field
     return result
@@ -457,27 +475,27 @@ def make_record_xml (xml):
 # were the relationships data came from the testbed side
 # for each type, a dict of the form {<field-name-exposed-in-record>:<alchemy_accessor_name>}
 # so after that, an 'authority' record will e.g. have a 'reg-pis' field with the hrns of its pi-users
-augment_map={'authority': {'reg-pis':'reg_pis',},
-             'slice': {'reg-researchers':'reg_researchers',},
-             'user': {'reg-pi-authorities':'reg_authorities_as_pi',
-                      'reg-slices':'reg_slices_as_researcher',},
+augment_map={'authority': {'reg-pis' : 'reg_pis',},
+             'slice': {'reg-researchers' : 'reg_researchers',},
+             'user': {'reg-pi-authorities' : 'reg_authorities_as_pi',
+                      'reg-slices' : 'reg_slices_as_researcher',},
              }
 
-def augment_with_sfa_builtins (local_record):
+def augment_with_sfa_builtins(local_record):
     # don't ruin the import of that file in a client world
     from sfa.util.xrn import Xrn
     # add a 'urn' field
-    setattr(local_record,'reg-urn',Xrn(xrn=local_record.hrn,type=local_record.type).urn)
+    setattr(local_record,'reg-urn',Xrn(xrn=local_record.hrn, type=local_record.type).urn)
     # users have keys and this is needed to synthesize 'users' sent over to CreateSliver
-    if local_record.type=='user':
+    if local_record.type == 'user':
         user_keys = [ key.key for key in local_record.reg_keys ]
         setattr(local_record, 'reg-keys', user_keys)
     # search in map according to record type
-    type_map=augment_map.get(local_record.type,{})
+    type_map=augment_map.get(local_record.type, {})
     # use type-dep. map to do the job
-    for (field_name,attribute) in type_map.items():
+    for (field_name, attribute) in type_map.items():
         # get related objects
-        related_records = getattr(local_record,attribute,[])
+        related_records = getattr(local_record, attribute, [])
         hrns = [ r.hrn for r in related_records ]
         setattr (local_record, field_name, hrns)