From 10135f6d1563ccd01c9afc90613f7dd16ca4c541 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 30 Jan 2021 16:42:23 +0100 Subject: db/attachment: move to mail/ It has nothing whatsoever to do with the database. --- alot/commands/thread.py | 2 +- alot/db/attachment.py | 56 ------------------------------------------------- alot/db/message.py | 2 +- alot/mail/attachment.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ alot/mail/envelope.py | 13 ++++++------ alot/widgets/globals.py | 2 +- 6 files changed, 65 insertions(+), 66 deletions(-) delete mode 100644 alot/db/attachment.py create mode 100644 alot/mail/attachment.py diff --git a/alot/commands/thread.py b/alot/commands/thread.py index e4334a28..72c607b1 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -27,8 +27,8 @@ from .common import RetagPromptCommand from .envelope import SendCommand from ..completion.contacts import ContactsCompleter from ..completion.path import PathCompleter +from ..mail.attachment import Attachment from ..mail.envelope import Envelope -from ..db.attachment import Attachment from ..db.errors import DatabaseROError from ..settings.const import settings from ..helper import formataddr diff --git a/alot/db/attachment.py b/alot/db/attachment.py deleted file mode 100644 index 5c993a68..00000000 --- a/alot/db/attachment.py +++ /dev/null @@ -1,56 +0,0 @@ -# Copyright (C) 2011-2012 Patrick Totzke -# Copyright © 2018 Dylan Baker -# This file is released under the GNU GPL, version 3 or a later revision. -# For further details see the COPYING file - -import os.path - -def _humanize_size(size): - """Create a nice human readable representation of the given number - (understood as bytes) using the "KiB" and "MiB" suffixes to indicate - kibibytes and mebibytes. A kibibyte is defined as 1024 bytes (as opposed to - a kilobyte which is 1000 bytes) and a mibibyte is 1024**2 bytes (as opposed - to a megabyte which is 1000**2 bytes). - - :param size: the number to convert - :type size: int - :returns: the human readable representation of size - :rtype: str - """ - for factor, format_string in ((1, '%i'), - (1024, '%iKiB'), - (1024 * 1024, '%.1fMiB')): - if size / factor < 1024: - return format_string % (size / factor) - return format_string % (size / factor) - - - -class Attachment: - """represents a mail attachment""" - - data = None - content_type = None - filename = None - params = None - - def __init__(self, data, ctype, filename, params): - self.data = data - self.content_type = ctype - self.params = params - - # make sure the filename is a relative path - # that does not go upwards - filename = os.path.normpath(filename) - if filename.startswith('/') or filename.startswith('..'): - raise ValueError('Dangerous attachment filename: %s' % filename) - - self.filename = filename - - def __str__(self): - ret = self.content_type - if self.filename: - ret += ':' + self.filename - ret += ' (%s)' % _humanize_size(len(self.data)) - - return ret diff --git a/alot/db/message.py b/alot/db/message.py index 5db42c0a..29ca11dc 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -13,7 +13,7 @@ from datetime import datetime from urwid.util import detected_encoding -from .attachment import Attachment +from ..mail.attachment import Attachment from .. import crypto from .. import helper from ..errors import GPGProblem diff --git a/alot/mail/attachment.py b/alot/mail/attachment.py new file mode 100644 index 00000000..5c993a68 --- /dev/null +++ b/alot/mail/attachment.py @@ -0,0 +1,56 @@ +# Copyright (C) 2011-2012 Patrick Totzke +# Copyright © 2018 Dylan Baker +# This file is released under the GNU GPL, version 3 or a later revision. +# For further details see the COPYING file + +import os.path + +def _humanize_size(size): + """Create a nice human readable representation of the given number + (understood as bytes) using the "KiB" and "MiB" suffixes to indicate + kibibytes and mebibytes. A kibibyte is defined as 1024 bytes (as opposed to + a kilobyte which is 1000 bytes) and a mibibyte is 1024**2 bytes (as opposed + to a megabyte which is 1000**2 bytes). + + :param size: the number to convert + :type size: int + :returns: the human readable representation of size + :rtype: str + """ + for factor, format_string in ((1, '%i'), + (1024, '%iKiB'), + (1024 * 1024, '%.1fMiB')): + if size / factor < 1024: + return format_string % (size / factor) + return format_string % (size / factor) + + + +class Attachment: + """represents a mail attachment""" + + data = None + content_type = None + filename = None + params = None + + def __init__(self, data, ctype, filename, params): + self.data = data + self.content_type = ctype + self.params = params + + # make sure the filename is a relative path + # that does not go upwards + filename = os.path.normpath(filename) + if filename.startswith('/') or filename.startswith('..'): + raise ValueError('Dangerous attachment filename: %s' % filename) + + self.filename = filename + + def __str__(self): + ret = self.content_type + if self.filename: + ret += ':' + self.filename + ret += ' (%s)' % _humanize_size(len(self.data)) + + return ret diff --git a/alot/mail/envelope.py b/alot/mail/envelope.py index 540c0342..db907340 100644 --- a/alot/mail/envelope.py +++ b/alot/mail/envelope.py @@ -14,7 +14,7 @@ from urllib.parse import unquote import gpg import magic -from ..db.attachment import Attachment +from .attachment import Attachment from .. import __version__ from .. import helper from .. import crypto @@ -99,7 +99,7 @@ class Envelope: tmpfile = None """template text for initial content""" attachments = None - """list of :class:`Attachments `""" + """list of :class:`Attachments `""" tags = None """tags to add after successful sendout""" account = None @@ -119,7 +119,7 @@ class Envelope: :param headers: unencoded header values :type headers: dict (str -> [unicode]) :param attachments: file attachments to include - :type attachments: list of :class:`~alot.db.attachment.Attachment` + :type attachments: list of :class:`~alot.mail.attachment.Attachment` :param tags: tags to add after successful sendout and saving this msg :type tags: set of str :param replied: message being replied to @@ -231,11 +231,10 @@ class Envelope: def attach(self, attachment): """ - attach a file + attach data - :param attachment: File to attach, given as - :class:`~alot.db.attachment.Attachment` object - :type attachment: :class:`~alot.db.attachment.Attachment` + :param attachment: data to attach + :type attachment: :class:`~alot.mail.attachment.Attachment` """ self.attachments.append(attachment) if self.sent_time: diff --git a/alot/widgets/globals.py b/alot/widgets/globals.py index 7f2a9dcd..66e51406 100644 --- a/alot/widgets/globals.py +++ b/alot/widgets/globals.py @@ -15,7 +15,7 @@ from ..errors import CompletionError class AttachmentWidget(urwid.WidgetWrap): """ - one-line summary of an :class:`~alot.db.attachment.Attachment`. + one-line summary of an :class:`~alot.mail.attachment.Attachment`. """ def __init__(self, attachment, selectable=True): -- cgit v1.2.3