imip-agent

Annotated imiptools/stores/database/postgresql.py

1408:64a4eacec2ae
2017-12-03 Paul Boddie Make WeekDayFilter an Enum subclass, ensuring that such selectors can be wrapped with Pattern selectors where no frequency qualifier is given. client-editing-simplification
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@1340 23
from imiptools.stores.common import StoreInitialisationError
paul@1088 24
from imiptools.stores.database.common import DatabaseStore, DatabaseJournal
paul@1088 25
import psycopg2
paul@1088 26
paul@1210 27
STORE_DIR = settings["STORE_DIR"]
paul@1210 28
JOURNAL_DIR = settings["JOURNAL_DIR"]
paul@1210 29
paul@1088 30
class Store(DatabaseStore):
paul@1088 31
paul@1088 32
    "A PostgreSQL database store of calendar objects and free/busy data."
paul@1088 33
paul@1098 34
    def __init__(self, store_dir=None):
paul@1088 35
paul@1088 36
        "Interpret 'store_dir' as a connection string."
paul@1088 37
paul@1340 38
        try:
paul@1340 39
            connection = psycopg2.connect(store_dir or STORE_DIR)
paul@1340 40
        except psycopg2.OperationalError, exc:
paul@1340 41
            raise StoreInitialisationError, exc
paul@1340 42
paul@1088 43
        connection.autocommit = True
paul@1088 44
        DatabaseStore.__init__(self, connection, psycopg2.paramstyle)
paul@1088 45
paul@1089 46
    def acquire_lock(self, user, timeout=None):
paul@1089 47
        query = "select pg_advisory_lock(20160311)"
paul@1089 48
        self.cursor.execute(query)
paul@1089 49
paul@1089 50
    def release_lock(self, user):
paul@1089 51
        query = "select pg_advisory_unlock(20160311)"
paul@1089 52
        self.cursor.execute(query)
paul@1089 53
paul@1088 54
class Journal(DatabaseJournal):
paul@1088 55
paul@1088 56
    "A PostgreSQL journal system supporting quotas."
paul@1088 57
paul@1098 58
    def __init__(self, store_dir=None):
paul@1088 59
paul@1088 60
        "Interpret 'store_dir' as a connection string."
paul@1088 61
paul@1340 62
        try:
paul@1340 63
            connection = psycopg2.connect(store_dir or JOURNAL_DIR)
paul@1340 64
        except psycopg2.OperationalError, exc:
paul@1340 65
            raise StoreInitialisationError, exc
paul@1340 66
paul@1088 67
        connection.autocommit = True
paul@1088 68
        DatabaseJournal.__init__(self, connection, psycopg2.paramstyle)
paul@1088 69
paul@1089 70
    def acquire_lock(self, user, timeout=None):
paul@1089 71
        query = "select pg_advisory_lock(20160312)"
paul@1089 72
        self.cursor.execute(query)
paul@1089 73
paul@1089 74
    def release_lock(self, user):
paul@1089 75
        query = "select pg_advisory_unlock(20160312)"
paul@1089 76
        self.cursor.execute(query)
paul@1089 77
paul@1088 78
# vim: tabstop=4 expandtab shiftwidth=4