paul@1088 | 1 | #!/usr/bin/env python |
paul@1088 | 2 | |
paul@1088 | 3 | """ |
paul@1088 | 4 | A PostgreSQL database store of calendar data. |
paul@1088 | 5 | |
paul@1210 | 6 | Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@1088 | 7 | |
paul@1088 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1088 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1088 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1088 | 11 | version. |
paul@1088 | 12 | |
paul@1088 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1088 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1088 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1088 | 16 | details. |
paul@1088 | 17 | |
paul@1088 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1088 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1088 | 20 | """ |
paul@1088 | 21 | |
paul@1210 | 22 | from imiptools.config import settings |
paul@1088 | 23 | from imiptools.stores.database.common import DatabaseStore, DatabaseJournal |
paul@1088 | 24 | import psycopg2 |
paul@1088 | 25 | |
paul@1210 | 26 | STORE_DIR = settings["STORE_DIR"] |
paul@1210 | 27 | JOURNAL_DIR = settings["JOURNAL_DIR"] |
paul@1210 | 28 | |
paul@1088 | 29 | class Store(DatabaseStore): |
paul@1088 | 30 | |
paul@1088 | 31 | "A PostgreSQL database store of calendar objects and free/busy data." |
paul@1088 | 32 | |
paul@1098 | 33 | def __init__(self, store_dir=None): |
paul@1088 | 34 | |
paul@1088 | 35 | "Interpret 'store_dir' as a connection string." |
paul@1088 | 36 | |
paul@1098 | 37 | connection = psycopg2.connect(store_dir or STORE_DIR) |
paul@1088 | 38 | connection.autocommit = True |
paul@1088 | 39 | DatabaseStore.__init__(self, connection, psycopg2.paramstyle) |
paul@1088 | 40 | |
paul@1089 | 41 | def acquire_lock(self, user, timeout=None): |
paul@1089 | 42 | query = "select pg_advisory_lock(20160311)" |
paul@1089 | 43 | self.cursor.execute(query) |
paul@1089 | 44 | |
paul@1089 | 45 | def release_lock(self, user): |
paul@1089 | 46 | query = "select pg_advisory_unlock(20160311)" |
paul@1089 | 47 | self.cursor.execute(query) |
paul@1089 | 48 | |
paul@1088 | 49 | class Journal(DatabaseJournal): |
paul@1088 | 50 | |
paul@1088 | 51 | "A PostgreSQL journal system supporting quotas." |
paul@1088 | 52 | |
paul@1098 | 53 | def __init__(self, store_dir=None): |
paul@1088 | 54 | |
paul@1088 | 55 | "Interpret 'store_dir' as a connection string." |
paul@1088 | 56 | |
paul@1098 | 57 | connection = psycopg2.connect(store_dir or JOURNAL_DIR) |
paul@1088 | 58 | connection.autocommit = True |
paul@1088 | 59 | DatabaseJournal.__init__(self, connection, psycopg2.paramstyle) |
paul@1088 | 60 | |
paul@1089 | 61 | def acquire_lock(self, user, timeout=None): |
paul@1089 | 62 | query = "select pg_advisory_lock(20160312)" |
paul@1089 | 63 | self.cursor.execute(query) |
paul@1089 | 64 | |
paul@1089 | 65 | def release_lock(self, user): |
paul@1089 | 66 | query = "select pg_advisory_unlock(20160312)" |
paul@1089 | 67 | self.cursor.execute(query) |
paul@1089 | 68 | |
paul@1088 | 69 | # vim: tabstop=4 expandtab shiftwidth=4 |