VirtualMailManager/maillocation.py
author Pascal Volk <user@localhost.localdomain.org>
Sun, 23 Feb 2014 18:00:35 +0000
branchv0.7.x
changeset 735 ddcad86f7386
parent 711 2a75058fc064
permissions -rw-r--r--
VMM/account: Account.modify accepts a password hash.
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 -*-
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 675
diff changeset
     2
# Copyright (c) 2008 - 2014, 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
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    15
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    16
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    17
__all__ = ('MailLocation', 'known_format')
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    19
_ = lambda msg: msg
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    20
_format_info = {
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    21
    '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
    22
    '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
    23
    'sdbox': dict(dovecot_version=0x20000c03, postfix=False),
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    24
}
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    25
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    26
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    27
class MailLocation(object):
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    28
    """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
    29
    __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
    30
    _kwargs = ('mid', 'mbfmt', 'directory')
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    31
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    32
    def __init__(self, dbh, **kwargs):
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        """Creates a new MailLocation instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    35
        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
    36
        specified.
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    37
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    38
        Arguments:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    39
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    40
        `dbh` : psycopg2._psycopg.connection
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    41
          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
    42
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
        Keyword arguments:
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    44
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    45
        `mid` : int
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    46
          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
    47
        `mbfmt` : str
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    48
          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
    49
          ``sdbox`` and ``mdbox``.
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    50
        `directory` : str
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    51
          name of the mailbox root directory.
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
        """
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    53
        self._dbh = dbh
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    54
        self._directory = None
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    55
        self._mbfmt = None
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    56
        self._mid = 0
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    57
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    58
        for key in kwargs.keys():
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    59
            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
    60
                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
    61
        mid = kwargs.get('mid')
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    62
        if mid:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    63
            assert isinstance(mid, int)
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    64
            self._load_by_mid(mid)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        else:
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    66
            args = kwargs.get('mbfmt'), kwargs.get('directory')
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    67
            assert all(isinstance(arg, str) for arg in args)
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    68
            if args[0].lower() not in _format_info:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    69
                raise MLErr(_("Unsupported mailbox format: '%s'") % args[0],
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    70
                            MAILLOCATION_INIT)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    71
            directory = args[1].strip()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    72
            if not directory:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    73
                raise MLErr(_("Empty directory name"), MAILLOCATION_INIT)
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    74
            if len(directory) > 20:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    75
                raise MLErr(_("Directory name is too long: '%s'") % directory,
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    76
                            MAILLOCATION_INIT)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    77
            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
    78
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    79
    def __str__(self):
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    80
        return '%s:~/%s' % (self._mbfmt, self._directory)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    82
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    83
    def directory(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    84
        """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
    85
        return self._directory
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    86
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    87
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    88
    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
    89
        """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
    90
        return _format_info[self._mbfmt]['dovecot_version']
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    92
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    93
    def postfix(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    94
        """`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
    95
        return _format_info[self._mbfmt]['postfix']
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    97
    @property
305
3c62f581d17a VMM/maillocation: Renamed MailLocation's property prefix to mbformat.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
    98
    def mbformat(self):
3c62f581d17a VMM/maillocation: Renamed MailLocation's property prefix to mbformat.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
    99
        """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
   100
        return self._mbfmt
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   102
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   103
    def mail_location(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   104
        """The mail_location, e.g. ``maildir:~/Maildir``"""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   105
        return self.__str__()
227
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   106
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   107
    @property
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   108
    def mid(self):
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   109
        """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
   110
        return self._mid
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   111
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   112
    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
   113
        """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
   114
        dbc = self._dbh.cursor()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   115
        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
   116
                    '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
   117
                    '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
   118
        result = dbc.fetchone()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   119
        dbc.close()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   120
        if not result:
450
fd4aa073015f VMM/{maillocation,quotalimit}: Unified object initialization code.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   121
            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
   122
        self._mid = mid
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   123
        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
   124
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   125
    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
   126
        """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
   127
        *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
   128
        dbc = self._dbh.cursor()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   129
        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
   130
                    "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
   131
                    (mbfmt, directory))
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   132
        result = dbc.fetchone()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   133
        dbc.close()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   134
        if not result:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   135
            self._save(mbfmt, directory)
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   136
        else:
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   137
            self._mid = result[0]
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   138
            self._mbfmt = mbfmt
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   139
            self._directory = directory
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   140
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   141
    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
   142
        """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
   143
        dbc = self._dbh.cursor()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   144
        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
   145
        mid = dbc.fetchone()[0]
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   146
        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
   147
                    "(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
   148
                    "%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
   149
        self._dbh.commit()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   150
        dbc.close()
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   151
        self._mid = mid
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   152
        self._mbfmt = mbfmt
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   153
        self._directory = directory
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   154
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   155
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   156
def known_format(mbfmt):
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   157
    """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
   158
    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
   159
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
   160
del _