X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fdbconn.py;h=c0b7d0e841498097bf2fc6cc32522317f98f1717;hb=e4fd019394c15b807aa88510e01496b5a2c03e50;hp=23aeda6fdad5ae52109eac5fb4f5989d2eb28463;hpb=97f565f26006d3866daaf39ee6512567b20cb01b;p=dak.git diff --git a/daklib/dbconn.py b/daklib/dbconn.py index 23aeda6f..c0b7d0e8 100755 --- a/daklib/dbconn.py +++ b/daklib/dbconn.py @@ -981,6 +981,42 @@ class Keyring(object): __all__.append('Keyring') +def get_or_set_keyring(keyring, session=None): + """ + If C{keyring} does not have an entry in the C{keyrings} table yet, create one + and return the new Keyring + If C{keyring} already has an entry, simply return the existing Keyring + + @type keyring: string + @param keyring: the keyring name + + @rtype: Keyring + @return: the Keyring object for this keyring + + """ + privatetrans = False + if session is None: + session = DBConn().session() + privatetrans = True + + try: + obj = session.query(Keyring).filter_by(keyring_name=keyring).first() + + if obj is None: + obj = Keyring(keyring_name=keyring) + session.add(obj) + if privatetrans: + session.commit() + else: + session.flush() + + return obj + finally: + if privatetrans: + session.close() + +__all__.append('get_or_set_keyring') + ################################################################################ class Location(object):