Added the Renderable mixin because DRY

This commit is contained in:
Daniel Quinn 2016-02-11 22:05:38 +00:00
parent ef1639208c
commit 7aadab23cc
5 changed files with 27 additions and 23 deletions

View File

@ -11,6 +11,7 @@ from dateutil import parser
from django.conf import settings from django.conf import settings
from .consumer import Consumer from .consumer import Consumer
from .mixins import Renderable
from .models import Sender from .models import Sender
@ -22,7 +23,7 @@ class InvalidMessageError(Exception):
pass pass
class Message(object): class Message(Renderable):
""" """
A crude, but simple email message class. We assume that there's a subject A crude, but simple email message class. We assume that there's a subject
and n attachments, and that we don't care about the message body. and n attachments, and that we don't care about the message body.
@ -37,7 +38,7 @@ class Message(object):
except (ValueError, AttributeError): except (ValueError, AttributeError):
pass # We assume that "now" is ok pass # We assume that "now" is ok
def __init__(self, data): def __init__(self, data, verbosity=1):
""" """
Cribbed heavily from Cribbed heavily from
https://www.ianlewis.org/en/parsing-email-attachments-python https://www.ianlewis.org/en/parsing-email-attachments-python
@ -57,7 +58,7 @@ class Message(object):
if not Sender.SAFE_REGEX.match(self.subject): if not Sender.SAFE_REGEX.match(self.subject):
raise InvalidMessageError("Message subject is unsafe") raise InvalidMessageError("Message subject is unsafe")
print('Fetching email: "{}"'.format(self.subject)) self._render('Fetching email: "{}"'.format(self.subject), 1)
attachments = [] attachments = []
for part in message.walk(): for part in message.walk():
@ -116,9 +117,9 @@ class Attachment(object):
return self.data return self.data
class MailFetcher(object): class MailFetcher(Renderable):
def __init__(self): def __init__(self, verbosity=1):
self._connection = None self._connection = None
self._host = settings.MAIL_CONSUMPTION["HOST"] self._host = settings.MAIL_CONSUMPTION["HOST"]
@ -130,6 +131,7 @@ class MailFetcher(object):
self._enabled = bool(self._host) self._enabled = bool(self._host)
self.last_checked = datetime.datetime.now() self.last_checked = datetime.datetime.now()
self.verbosity = verbosity
def pull(self): def pull(self):
""" """
@ -142,7 +144,7 @@ class MailFetcher(object):
for message in self._get_messages(): for message in self._get_messages():
print("Storing email: \"{}\"".format(message.subject)) self._render("Storing email: \"{}\"".format(message.subject), 1)
t = int(time.mktime(message.time.timetuple())) t = int(time.mktime(message.time.timetuple()))
file_name = os.path.join(Consumer.CONSUME, message.file_name) file_name = os.path.join(Consumer.CONSUME, message.file_name)
@ -189,9 +191,9 @@ class MailFetcher(object):
message = None message = None
try: try:
message = Message(data[0][1]) message = Message(data[0][1], self.verbosity)
except InvalidMessageError as e: except InvalidMessageError as e:
print(e) self._render(e, 0)
pass pass
self._connection.store(num, "+FLAGS", "\\Deleted") self._connection.store(num, "+FLAGS", "\\Deleted")

View File

@ -7,9 +7,10 @@ from django.core.management.base import BaseCommand, CommandError
from ...consumer import Consumer, ConsumerError from ...consumer import Consumer, ConsumerError
from ...mail import MailFetcher, MailFetcherError from ...mail import MailFetcher, MailFetcherError
from ...mixins import Renderable
class Command(BaseCommand): class Command(Renderable, BaseCommand):
""" """
On every iteration of an infinite loop, consume what we can from the On every iteration of an infinite loop, consume what we can from the
consumption directory, and fetch any mail available. consumption directory, and fetch any mail available.
@ -62,7 +63,3 @@ class Command(BaseCommand):
delta = self.mail_fetcher.last_checked + self.MAIL_DELTA delta = self.mail_fetcher.last_checked + self.MAIL_DELTA
if delta > datetime.datetime.now(): if delta > datetime.datetime.now():
self.mail_fetcher.pull() self.mail_fetcher.pull()
def _render(self, text, verbosity):
if self.verbosity >= verbosity:
print(text)

View File

@ -8,8 +8,10 @@ from django.core.management.base import BaseCommand, CommandError
from documents.models import Document from documents.models import Document
from paperless.db import GnuPG from paperless.db import GnuPG
from ...mixins import Renderable
class Command(BaseCommand):
class Command(Renderable, BaseCommand):
help = """ help = """
Decrypt and rename all files in our collection into a given target Decrypt and rename all files in our collection into a given target
@ -50,7 +52,3 @@ class Command(BaseCommand):
f.write(GnuPG.decrypted(document.source_file)) f.write(GnuPG.decrypted(document.source_file))
t = int(time.mktime(document.created.timetuple())) t = int(time.mktime(document.created.timetuple()))
os.utime(target, times=(t, t)) os.utime(target, times=(t, t))
def _render(self, text, verbosity):
if self.verbosity >= verbosity:
print(text)

View File

@ -2,8 +2,10 @@ from django.core.management.base import BaseCommand
from documents.models import Document, Tag from documents.models import Document, Tag
from ...mixins import Renderable
class Command(BaseCommand):
class Command(Renderable, BaseCommand):
help = """ help = """
Using the current set of tagging rules, apply said rules to all Using the current set of tagging rules, apply said rules to all
@ -28,7 +30,3 @@ class Command(BaseCommand):
self._render( self._render(
'Tagging {} with "{}"'.format(document, tag), 1) 'Tagging {} with "{}"'.format(document, tag), 1)
document.tags.add(tag) document.tags.add(tag)
def _render(self, text, verbosity):
if self.verbosity >= verbosity:
print(text)

9
src/documents/mixins.py Normal file
View File

@ -0,0 +1,9 @@
class Renderable(object):
"""
A handy mixin to make it easier/cleaner to print output based on a verbosity
value.
"""
def _render(self, text, verbosity):
if self.verbosity >= verbosity:
print(text)