VirtualMailManager/serviceset.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sun, 30 Oct 2011 11:41:11 +0000
branchv0.6.x
changeset 438 e35efe931af3
child 439 39036f5e6387
permissions -rw-r--r--
VMM: Added new module serviceset - provides class ServiceSet.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# coding: utf-8
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
# Copyright (c) 2011, Pascal Volk
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
"""
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
    VirtualMailManager.serviceset
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
    Virtual Mail Manager's ServiceSet class for simplified database access
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
    to the service_set table.
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
"""
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
cfg_dget = lambda option: None
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
class ServiceSet(object):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
    """A wrapper class that provides access to the service_set table.
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
    Each ServiceSet object provides following - read only - attributes:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
    `ssid` : long
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
      The id of the service set
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
    `smtp` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
      Boolean flag for service smtp
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
    `pop3` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
      Boolean flag for service pop3
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    `imap` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
      Boolean flag for service imap
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
    `sieve` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
      Boolean flag for service sieve
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
    """
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
    __slots__ = ('_ssid', '_services', '_sieve_col', '_dbh')
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
    _kwargs = ('ssid', 'smtp', 'pop3', 'imap', 'sieve')
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
    def __init__(self, dbh, **kwargs):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
        """Creates a new ServiceSet instance.
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        Either the 'ssid' keyword argument or one or more of the service
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
        arguments ('smtp', 'pop3',  'imap', 'sieve') must be provided.
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        Arguments:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        `dbh` : pyPgSQL.PgSQL.Connection or psycopg2.extensions.connection
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
          A database connection for the database access.
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
        Keyword arguments:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
        `ssid` : int
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
          The id of the service set (>0)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
        `smtp` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
          Boolean flag for service smtp - default `True`
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
        `pop3` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
          Boolean flag for service pop3 - default `True`
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
        `imap` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
          Boolean flag for service imap - default `True`
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
        `sieve` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
          Boolean flag for service sieve - default `True`
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
        """
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
        self._dbh = dbh
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        self._ssid = 0
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        self._services = dict.fromkeys(self.__class__._kwargs[1:], True)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        if cfg_dget('misc.dovecot_version') < 0x10200b02:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
            self._sieve_col = 'managesieve'
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
            self._sieve_col = 'sieve'
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        for key in kwargs.iterkeys():
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
            if key not in self.__class__._kwargs:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
                raise ValueError('unrecognized keyword: %r' % key)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
            if key == 'ssid':
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
                assert not isinstance(kwargs[key], bool) and \
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
                       isinstance(kwargs[key], (int, long)) and kwargs[key] > 0
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
                self._load_by_ssid(kwargs[key])
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
                break
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
            else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
                assert isinstance(kwargs[key], bool)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
                if not kwargs[key]:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
                    self._services[key] = kwargs[key]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
        if not self._ssid:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
            self._load_by_services()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
    def __eq__(self, other):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        if isinstance(other, self.__class__):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
            return self._ssid == other._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
        return NotImplemented
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    def __ne__(self, other):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        if isinstance(other, self.__class__):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
            return self._ssid != other._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        return NotImplemented
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
    def __getattr__(self, name):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        if name not in self.__class__._kwargs:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
            raise AttributeError('%r object has no attribute %r' % (
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
                                 self.__class__.__name__, name))
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        if name == 'ssid':
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
            return self._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
            return self._services[name]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
    def __repr__(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
        return '%s(%s, %s)' % (self.__class__.__name__, self._dbh,
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
                  ', '.join('%s=%r' % s for s in self._services.iteritems()))
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
    def _load_by_services(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
        """Try to load the service_set by it's service combination."""
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
        sql = ('SELECT ssid FROM service_set WHERE %s' %
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
               ' AND '.join('%s = %s' %
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
               (k, str(v).upper()) for k, v in self._services.iteritems()))
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
        if self._sieve_col == 'managesieve':
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   108
            sql.replace('sieve', self._sieve_col)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   110
        dbc.execute(sql)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   111
        result = dbc.fetchone()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   112
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
        if result:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
            self._ssid = result[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
            self._save()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   118
    def _load_by_ssid(self, ssid):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   119
        """Try to load the service_set by it's primary key."""
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   120
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
        dbc.execute('SELECT ssid, smtp, pop3, imap, %s' % (self._sieve_col,) +
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   122
                    ' FROM service_set WHERE ssid = %s', (ssid,))
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   123
        result = dbc.fetchone()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   124
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   125
        if not result:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   126
            raise ValueError('Unknown service_set id specified: %r' % ssid)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   127
        self._ssid = result[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   128
        self._services.update(zip(self.__class__._kwargs[1:], result[1:]))
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   129
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   130
    def _save(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   131
        """Store a new service_set in the database."""
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   132
        sql = ('INSERT INTO service_set (ssid, smtp, pop3, imap, %s) ' %
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   133
               (self._sieve_col,) +
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   134
               'VALUES (%(ssid)s, %(smtp)s, %(pop3)s, %(imap)s, %(sieve)s)')
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   135
        if self._sieve_col == 'managesieve':
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   136
            sql.replace('sieve', self._sieve_col)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   137
        self._set_ssid()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   138
        values = {'ssid': self._ssid}
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   139
        values.update(self._services)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   140
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   141
        dbc.execute(sql, values)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   142
        self._dbh.commit()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   143
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   144
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   145
    def _set_ssid(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   146
        """Set the unique ID for the new service_set."""
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   147
        assert self._ssid == 0
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   148
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   149
        dbc.execute("SELECT nextval('service_set_id')")
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   150
        self._ssid = dbc.fetchone()[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   151
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   152
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   153
del cfg_dget