VirtualMailManager/serviceset.py
author Pascal Volk <user@localhost.localdomain.org>
Sun, 09 Mar 2014 18:52:27 +0000
branchv0.7.x
changeset 740 aa346db76ee2
parent 711 2a75058fc064
permissions -rw-r--r--
merged changes from default(b65c3abf9ca8).
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
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 675
diff changeset
     2
# Copyright (c) 2011 - 2014, 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
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
671
a9d86ccac044 VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    20
    `ssid` : int
438
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
443
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
    30
    `services` : dict
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
    31
      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
    32
    """
691
932cd76bf879 VMM: Dropped support for Dovecot versions before v1.2.0.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    33
    __slots__ = ('_ssid', '_services', '_dbh')
439
39036f5e6387 VMM/serviceset: Added module constant SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 438
diff changeset
    34
    _kwargs = (('ssid',) + SERVICES)
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
    def __init__(self, dbh, **kwargs):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        """Creates a new ServiceSet instance.
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        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
    40
        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
    41
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
        Arguments:
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 691
diff changeset
    43
        `dbh` : psycopg2.extensions.connection
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
          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
    45
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
        Keyword arguments:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
        `ssid` : int
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
          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
    49
        `smtp` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
          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
    51
        `pop3` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
          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
    53
        `imap` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
          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
    55
        `sieve` : bool
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
          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
    57
        """
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        self._dbh = dbh
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        self._ssid = 0
440
1a08fe35b496 VMM/serviceset: Use SERVICES inside of ServiceSet - avoid slicing.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 439
diff changeset
    60
        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
    61
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
    62
        for key in kwargs.keys():
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
            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
    64
                raise ValueError('unrecognized keyword: %r' % key)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
            if key == 'ssid':
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
                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
    67
                       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
    68
                self._load_by_ssid(kwargs[key])
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
                break
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
            else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
                assert isinstance(kwargs[key], bool)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
                if not kwargs[key]:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
                    self._services[key] = kwargs[key]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
        if not self._ssid:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
            self._load_by_services()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
    def __eq__(self, other):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
        if isinstance(other, self.__class__):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
            return self._ssid == other._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        return NotImplemented
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    def __ne__(self, other):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        if isinstance(other, self.__class__):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
            return self._ssid != other._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        return NotImplemented
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
    def __getattr__(self, name):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        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
    89
            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
    90
                                 self.__class__.__name__, name))
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        if name == 'ssid':
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
            return self._ssid
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
            return self._services[name]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
    def __repr__(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        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
    98
                  ', '.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
    99
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
    def _load_by_services(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
        """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
   102
        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
   103
               ' AND '.join('%s = %s' %
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 638
diff changeset
   104
               (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
   105
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
        dbc.execute(sql)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
        result = dbc.fetchone()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   108
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
        if result:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   110
            self._ssid = result[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   111
        else:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   112
            self._save()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
    def _load_by_ssid(self, ssid):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
        """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
   116
        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
   117
        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
   118
                    '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
   119
        result = dbc.fetchone()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   120
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
        if not result:
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   122
            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
   123
        self._ssid = result[0]
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 691
diff changeset
   124
        self._services.update(list(zip(SERVICES, result[1:])))
438
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   125
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   126
    def _save(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   127
        """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
   128
        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
   129
               '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
   130
        self._set_ssid()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   131
        values = {'ssid': self._ssid}
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   132
        values.update(self._services)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   133
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   134
        dbc.execute(sql, values)
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   135
        self._dbh.commit()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   136
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   137
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   138
    def _set_ssid(self):
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   139
        """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
   140
        assert self._ssid == 0
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   141
        dbc = self._dbh.cursor()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   142
        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
   143
        self._ssid = dbc.fetchone()[0]
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   144
        dbc.close()
e35efe931af3 VMM: Added new module serviceset - provides class ServiceSet.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   145
443
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   146
    @property
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   147
    def services(self):
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   148
        """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
   149
        boolean values."""
e2b9e3de2b51 VMM/serviceset: Added @property ServiceSet.services -> dict.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 440
diff changeset
   150
        return self._services.copy()