VirtualMailManager/maillocation.py
author Pascal Volk <neverseen@users.sourceforge.net>
Tue, 08 Nov 2011 23:25:30 +0000
branchv0.6.x
changeset 471 5d0114f7bb99
parent 450 fd4aa073015f
child 568 14abdd04ddf5
permissions -rw-r--r--
VMM/cli/subcommands: Use the common output formatting in list_pwschemes().
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
366
d6573da35b5f Updated copyright notices to include the year 2011.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 352
diff changeset
     2
# Copyright (c) 2008 - 2011, Pascal Volk
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     4
"""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     5
    VirtualMailManager.maillocation
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     7
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     8
    Virtual Mail Manager's maillocation module to handle Dovecot's
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     9
    mail_location setting for accounts.
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    11
"""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    12
450
fd4aa073015f VMM/{maillocation,quotalimit}: Unified object initialization code.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    13
from VirtualMailManager.constants import MAILLOCATION_INIT
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    14
from VirtualMailManager.errors import MailLocationError as MLErr
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    15
from VirtualMailManager.pycompat import all
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    16
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    17
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    18
__all__ = ('MailLocation', 'known_format')
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    20
_ = lambda msg: msg
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    21
_format_info = {
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    22
    'maildir': dict(dovecot_version=0x10000f00, postfix=True),
307
217b419d6561 VMM/maillocation: Dovecot >= 2.0.beta5 is required for `doveadm mailbox create -s …`
Pascal Volk <neverseen@users.sourceforge.net>
parents: 305
diff changeset
    23
    'mdbox': dict(dovecot_version=0x20000b05, postfix=False),
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    24
    'sdbox': dict(dovecot_version=0x20000c03, postfix=False),
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    25
}
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    26
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    27
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    28
class MailLocation(object):
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    29
    """Class to handle mail_location relevant information."""
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    30
    __slots__ = ('_directory', '_mbfmt', '_mid', '_dbh')
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    31
    _kwargs = ('mid', 'mbfmt', 'directory')
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    32
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    33
    def __init__(self, dbh, **kwargs):
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        """Creates a new MailLocation instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    36
        Either the mid keyword or the mbfmt and directory keywords must be
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    37
        specified.
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    38
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    39
        Arguments:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    40
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    41
        `dbh` : pyPgSQL.PgSQL.Connection
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    42
          A database connection for the database access.
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    43
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
        Keyword arguments:
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    45
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    46
        `mid` : int
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    47
          the id of a mail_location
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    48
        `mbfmt` : str
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    49
          the mailbox format of the mail_location. One out of: ``maildir``,
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    50
          ``sdbox`` and ``mdbox``.
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    51
        `directory` : str
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    52
          name of the mailbox root directory.
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
        """
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    54
        self._dbh = dbh
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    55
        self._directory = None
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    56
        self._mbfmt = None
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    57
        self._mid = 0
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    58
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    59
        for key in kwargs.iterkeys():
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    60
            if key not in self.__class__._kwargs:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    61
                raise ValueError('unrecognized keyword: %r' % key)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    62
        mid = kwargs.get('mid')
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    63
        if mid:
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    64
            assert isinstance(mid, (int, long))
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    65
            self._load_by_mid(mid)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
        else:
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    67
            args = kwargs.get('mbfmt'), kwargs.get('directory')
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    68
            assert all(isinstance(arg, basestring) for arg in args)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    69
            if args[0].lower() not in _format_info:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    70
                raise MLErr(_(u"Unsupported mailbox format: '%s'") % args[0],
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    71
                            MAILLOCATION_INIT)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    72
            directory = args[1].strip()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    73
            if not directory:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    74
                raise MLErr(_(u"Empty directory name"), MAILLOCATION_INIT)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    75
            if len(directory) > 20:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    76
                raise MLErr(_(u"Directory name is too long: '%s'") % directory,
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    77
                            MAILLOCATION_INIT)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    78
            self._load_by_names(args[0].lower(), directory)
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    79
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    80
    def __str__(self):
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    81
        return u'%s:~/%s' % (self._mbfmt, self._directory)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    83
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    84
    def directory(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    85
        """The mail_location's directory name."""
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    86
        return self._directory
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    87
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    88
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    89
    def dovecot_version(self):
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    90
        """The required Dovecot version for this mailbox format."""
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    91
        return _format_info[self._mbfmt]['dovecot_version']
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    93
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    94
    def postfix(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    95
        """`True` if Postfix supports this mailbox format, else `False`."""
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    96
        return _format_info[self._mbfmt]['postfix']
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    98
    @property
305
3c62f581d17a VMM/maillocation: Renamed MailLocation's property prefix to mbformat.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
    99
    def mbformat(self):
3c62f581d17a VMM/maillocation: Renamed MailLocation's property prefix to mbformat.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
   100
        """The mail_location's mailbox format."""
3c62f581d17a VMM/maillocation: Renamed MailLocation's property prefix to mbformat.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
   101
        return self._mbfmt
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   102
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   103
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   104
    def mail_location(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   105
        """The mail_location, e.g. ``maildir:~/Maildir``"""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   106
        return self.__str__()
227
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   107
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   108
    @property
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   109
    def mid(self):
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   110
        """The mail_location's unique ID."""
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   111
        return self._mid
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   112
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   113
    def _load_by_mid(self, mid):
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   114
        """Load mail_location relevant information by *mid*"""
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   115
        dbc = self._dbh.cursor()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   116
        dbc.execute('SELECT format, directory FROM mailboxformat, '
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   117
                    'maillocation WHERE mid = %u AND '
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   118
                    'maillocation.fid = mailboxformat.fid' % mid)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   119
        result = dbc.fetchone()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   120
        dbc.close()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   121
        if not result:
450
fd4aa073015f VMM/{maillocation,quotalimit}: Unified object initialization code.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   122
            raise ValueError('Unknown mail_location id specified: %r' % mid)
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   123
        self._mid = mid
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   124
        self._mbfmt, self._directory = result
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   125
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   126
    def _load_by_names(self, mbfmt, directory):
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   127
        """Try to load mail_location relevant information by *mbfmt* and
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   128
        *directory* name. If it fails goto _save()."""
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   129
        dbc = self._dbh.cursor()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   130
        dbc.execute("SELECT mid FROM maillocation WHERE fid = (SELECT fid "
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   131
                    "FROM mailboxformat WHERE format = %s) AND directory = %s",
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   132
                    (mbfmt, directory))
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   133
        result = dbc.fetchone()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   134
        dbc.close()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   135
        if not result:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   136
            self._save(mbfmt, directory)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   137
        else:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   138
            self._mid = result[0]
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   139
            self._mbfmt = mbfmt
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   140
            self._directory = directory
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   141
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   142
    def _save(self, mbfmt, directory):
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   143
        """Save a new mail_location in the database."""
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   144
        dbc = self._dbh.cursor()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   145
        dbc.execute("SELECT nextval('maillocation_id')")
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   146
        mid = dbc.fetchone()[0]
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   147
        dbc.execute("INSERT INTO maillocation (fid, mid, directory) VALUES ("
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   148
                    "(SELECT fid FROM mailboxformat WHERE format = %s), %s, "
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   149
                    "%s)",  (mbfmt, mid, directory))
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   150
        self._dbh.commit()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   151
        dbc.close()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   152
        self._mid = mid
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   153
        self._mbfmt = mbfmt
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   154
        self._directory = directory
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   155
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   156
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   157
def known_format(mbfmt):
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   158
    """Checks if the mailbox format *mbfmt* is known, returns bool."""
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   159
    return mbfmt.lower() in _format_info
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   160
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   161
del _