X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Ftrust%2Fcredential_factory.py;h=07cd6da1e87d1d107c58209b9bc66ee971bd4170;hb=4a9e6751f9f396f463932133b9d62fc925a99ef6;hp=47ddc662a29bf539c164b0aab8d3bd17c8902631;hpb=5038558d9964a097582210037fa10f151044f275;p=sfa.git diff --git a/sfa/trust/credential_factory.py b/sfa/trust/credential_factory.py index 47ddc662..07cd6da1 100644 --- a/sfa/trust/credential_factory.py +++ b/sfa/trust/credential_factory.py @@ -21,6 +21,8 @@ # IN THE WORK. #---------------------------------------------------------------------- + + from sfa.util.sfalogging import logger from sfa.trust.credential import Credential from sfa.trust.abac_credential import ABACCredential @@ -32,6 +34,7 @@ import re # Specifically, this factory can create standard SFA credentials # and ABAC credentials from XML strings based on their identifying content + class CredentialFactory: UNKNOWN_CREDENTIAL_TYPE = 'geni_unknown' @@ -56,22 +59,25 @@ class CredentialFactory: @staticmethod def createCred(credString=None, credFile=None): if not credString and not credFile: - raise Exception("CredentialFactory.createCred called with no argument") + raise Exception( + "CredentialFactory.createCred called with no argument") if credFile: try: credString = open(credFile).read() - except Exception, e: - logger.info("Error opening credential file %s: %s" % credFile, e) + except Exception as e: + logger.info("Error opening credential file %s: %s" % + credFile, e) return None # Try to treat the file as JSON, getting the cred_type from the struct try: credO = json.loads(credString, encoding='ascii') - if credO.has_key('geni_value') and credO.has_key('geni_type'): + if 'geni_value' in credO and 'geni_type' in credO: cred_type = credO['geni_type'] credString = credO['geni_value'] - except Exception, e: - # It wasn't a struct. So the credString is XML. Pull the type directly from the string + except Exception as e: + # It wasn't a struct. So the credString is XML. Pull the type + # directly from the string logger.debug("Credential string not JSON: %s" % e) cred_type = CredentialFactory.getType(credString) @@ -79,22 +85,26 @@ class CredentialFactory: try: cred = Credential(string=credString) return cred - except Exception, e: + except Exception as e: if credFile: msg = "credString started: %s" % credString[:50] - raise Exception("%s not a parsable SFA credential: %s. " % (credFile, e) + msg) + raise Exception( + "%s not a parsable SFA credential: %s. " % (credFile, e) + msg) else: - raise Exception("SFA Credential not parsable: %s. Cred start: %s..." % (e, credString[:50])) + raise Exception( + "SFA Credential not parsable: %s. Cred start: %s..." % (e, credString[:50])) elif cred_type == ABACCredential.ABAC_CREDENTIAL_TYPE: try: cred = ABACCredential(string=credString) return cred - except Exception, e: + except Exception as e: if credFile: - raise Exception("%s not a parsable ABAC credential: %s" % (credFile, e)) + raise Exception( + "%s not a parsable ABAC credential: %s" % (credFile, e)) else: - raise Exception("ABAC Credential not parsable: %s. Cred start: %s..." % (e, credString[:50])) + raise Exception( + "ABAC Credential not parsable: %s. Cred start: %s..." % (e, credString[:50])) else: raise Exception("Unknown credential type '%s'" % cred_type) @@ -103,8 +113,8 @@ if __name__ == "__main__": cred1 = CredentialFactory.createCred(credFile='/tmp/cred.xml') cred2 = CredentialFactory.createCred(credString=c2) - print "C1 = %s" % cred1 - print "C2 = %s" % cred2 + print("C1 = %s" % cred1) + print("C2 = %s" % cred2) c1s = cred1.dump_string() - print "C1 = %s" % c1s + print("C1 = %s" % c1s) # print "C2 = %s" % cred2.dump_string()