From 1e7dba2f7b305e0ac218d99fdebde87cd83564a2 Mon Sep 17 00:00:00 2001 From: Chris Lamb Date: Mon, 26 Oct 2009 14:45:55 +0000 Subject: [PATCH] Port database.get_or_set_keyring_id => dbconn.get_or_set_keyring Signed-off-by: Chris Lamb --- daklib/dbconn.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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): -- 2.39.5