VirtualMailManager/serviceset.py
author Pascal Volk <user@localhost.localdomain.org>
Sun, 16 Sep 2012 17:05:33 +0000
changeset 614 1ec3497a6733
parent 594 cdbe200c0ecc
child 638 0de0b9e75c9f
child 675 d24f094d1cb5
permissions -rw-r--r--
VMM: Moved transport-validation to common.
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
568
14abdd04ddf5 Updated copyright notices to include the year 2012.
Pascal Volk <user@localhost.localdomain.org>
parents: 482
diff changeset
     2
# Copyright (c) 2011 - 2012, Pascal Volk
438
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
439
39036f5e6387 VMM/serviceset: Added module constant SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 438
diff changeset
    12
SERVICES = ('smtp', 'pop3', 'imap', 'sieve')
39036f5e6387 VMM/serviceset: Added module constant SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 438
diff changeset
    13
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
cfg_dget = lambda option: None
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
class ServiceSet(object):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
    """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
    19
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
    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
    21
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
    `ssid` : long
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
      The id of the service set
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
    `smtp` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
      Boolean flag for service smtp
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    `pop3` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
      Boolean flag for service pop3
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
    `imap` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
      Boolean flag for service imap
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
    `sieve` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
      Boolean flag for service sieve
443
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
    32
    `services` : dict
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
    33
      The four services above with boolean values
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
    """
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
    __slots__ = ('_ssid', '_services', '_sieve_col', '_dbh')
439
39036f5e6387 VMM/serviceset: Added module constant SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 438
diff changeset
    36
    _kwargs = (('ssid',) + SERVICES)
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
    def __init__(self, dbh, **kwargs):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        """Creates a new ServiceSet instance.
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        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
    42
        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
    43
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
        Arguments:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
        `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
    46
          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
    47
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
        Keyword arguments:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
        `ssid` : int
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
          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
    51
        `smtp` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
          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
    53
        `pop3` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
          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
    55
        `imap` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
          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
    57
        `sieve` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
          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
    59
        """
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        self._dbh = dbh
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        self._ssid = 0
440
1a08fe35b496 VMM/serviceset: Use SERVICES inside of ServiceSet - avoid slicing.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 439
diff changeset
    62
        self._services = dict.fromkeys(SERVICES, True)
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
        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
    64
            self._sieve_col = 'managesieve'
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
            self._sieve_col = 'sieve'
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
        for key in kwargs.iterkeys():
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
            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
    70
                raise ValueError('unrecognized keyword: %r' % key)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
            if key == 'ssid':
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
                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
    73
                       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
    74
                self._load_by_ssid(kwargs[key])
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
                break
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
            else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
                assert isinstance(kwargs[key], bool)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
                if not kwargs[key]:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
                    self._services[key] = kwargs[key]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        if not self._ssid:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
            self._load_by_services()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
    def __eq__(self, other):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        if isinstance(other, self.__class__):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
            return self._ssid == other._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        return NotImplemented
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
    def __ne__(self, other):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        if isinstance(other, self.__class__):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
            return self._ssid != other._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        return NotImplemented
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
    def __getattr__(self, name):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
        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
    95
            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
    96
                                 self.__class__.__name__, name))
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        if name == 'ssid':
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
            return self._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
            return self._services[name]
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 __repr__(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
        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
   104
                  ', '.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
   105
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
    def _load_by_services(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
        """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
   108
        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
   109
               ' AND '.join('%s = %s' %
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   110
               (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
   111
        if self._sieve_col == 'managesieve':
594
cdbe200c0ecc VMM/serviceset: Fixed a conditional ProgrammingError.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   112
            sql = sql.replace('sieve', self._sieve_col)
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
        dbc.execute(sql)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
        result = dbc.fetchone()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
        if result:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   118
            self._ssid = result[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   119
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   120
            self._save()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   122
    def _load_by_ssid(self, ssid):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   123
        """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
   124
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   125
        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
   126
                    ' 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
   127
        result = dbc.fetchone()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   128
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   129
        if not result:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   130
            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
   131
        self._ssid = result[0]
482
428ee9cdf1b2 VMM: The pyPgSQL compatibility commit.
Pascal Volk <user@localhost.localdomain.org>
parents: 443
diff changeset
   132
        #self._services.update(zip(SERVICES, result[1:]))
428ee9cdf1b2 VMM: The pyPgSQL compatibility commit.
Pascal Volk <user@localhost.localdomain.org>
parents: 443
diff changeset
   133
        for key, value in zip(SERVICES, result[1:]):  # pyPgSQL compatible
428ee9cdf1b2 VMM: The pyPgSQL compatibility commit.
Pascal Volk <user@localhost.localdomain.org>
parents: 443
diff changeset
   134
            if value:
428ee9cdf1b2 VMM: The pyPgSQL compatibility commit.
Pascal Volk <user@localhost.localdomain.org>
parents: 443
diff changeset
   135
                self._services[key] = True
428ee9cdf1b2 VMM: The pyPgSQL compatibility commit.
Pascal Volk <user@localhost.localdomain.org>
parents: 443
diff changeset
   136
            else:
428ee9cdf1b2 VMM: The pyPgSQL compatibility commit.
Pascal Volk <user@localhost.localdomain.org>
parents: 443
diff changeset
   137
                self._services[key] = False
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   138
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   139
    def _save(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   140
        """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
   141
        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
   142
               (self._sieve_col,) +
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   143
               '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
   144
        self._set_ssid()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   145
        values = {'ssid': self._ssid}
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   146
        values.update(self._services)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   147
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   148
        dbc.execute(sql, values)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   149
        self._dbh.commit()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   150
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   151
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   152
    def _set_ssid(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   153
        """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
   154
        assert self._ssid == 0
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   155
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   156
        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
   157
        self._ssid = dbc.fetchone()[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   158
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   159
443
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   160
    @property
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   161
    def services(self):
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   162
        """A dictionary: Keys: `smtp`, `pop3`, `imap` and `sieve` with
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   163
        boolean values."""
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   164
        return self._services.copy()
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   165
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   166
del cfg_dget