VirtualMailManager/serviceset.py
author Pascal Volk <user@localhost.localdomain.org>
Wed, 23 Jan 2013 22:16:38 +0000
branchv0.7.x
changeset 691 932cd76bf879
parent 676 2bc11dada296
child 694 b1bfd4d1d9c0
permissions -rw-r--r--
VMM: Dropped support for Dovecot versions before v1.2.0.
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
675
d24f094d1cb5 Updated copyright notices to include the year 2013.
Pascal Volk <user@localhost.localdomain.org>
parents: 594
diff changeset
     2
# Copyright (c) 2011 - 2013, 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
671
a9d86ccac044 VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    22
    `ssid` : int
438
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
    """
691
932cd76bf879 VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    35
    __slots__ = ('_ssid', '_services', '_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
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
    64
        for key in kwargs.keys():
438
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 \
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
    69
                       isinstance(kwargs[key], int) and kwargs[key] > 0
438
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,
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
   100
                  ', '.join('%s=%r' % s for s in self._services.items()))
438
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' %
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
   106
               (k, str(v).upper()) for k, v in self._services.items()))
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   108
        dbc.execute(sql)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
        result = dbc.fetchone()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   110
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   111
        if result:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   112
            self._ssid = result[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
            self._save()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
    def _load_by_ssid(self, ssid):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
        """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
   118
        dbc = self._dbh.cursor()
691
932cd76bf879 VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
   119
        dbc.execute('SELECT ssid, smtp, pop3, imap, sieve '
932cd76bf879 VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
   120
                    'FROM service_set WHERE ssid = %s', (ssid,))
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
        result = dbc.fetchone()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   122
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   123
        if not result:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   124
            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
   125
        self._ssid = result[0]
482
428ee9cdf1b2 VMM: The pyPgSQL compatibility commit.
Pascal Volk <user@localhost.localdomain.org>
parents: 443
diff changeset
   126
        #self._services.update(zip(SERVICES, result[1:]))
428ee9cdf1b2 VMM: The pyPgSQL compatibility commit.
Pascal Volk <user@localhost.localdomain.org>
parents: 443
diff changeset
   127
        for key, value in zip(SERVICES, result[1:]):  # pyPgSQL compatible
638
0de0b9e75c9f VMM: Partial PEP-308-ification.
Pascal Volk <user@localhost.localdomain.org>
parents: 594
diff changeset
   128
            self._services[key] = True if value else False
438
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."""
691
932cd76bf879 VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
   132
        sql = ('INSERT INTO service_set (ssid, smtp, pop3, imap, sieve) '
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   133
               '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
   134
        self._set_ssid()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   135
        values = {'ssid': self._ssid}
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   136
        values.update(self._services)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   137
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   138
        dbc.execute(sql, values)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   139
        self._dbh.commit()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   140
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   141
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   142
    def _set_ssid(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   143
        """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
   144
        assert self._ssid == 0
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   145
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   146
        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
   147
        self._ssid = dbc.fetchone()[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   148
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   149
443
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   150
    @property
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   151
    def services(self):
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   152
        """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
   153
        boolean values."""
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   154
        return self._services.copy()
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   155
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   156
del cfg_dget