imip-agent

imiptools/stores/database/postgresql.py

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