Skip to content

Commit

Permalink
Merge pull request #218 from AT0myks/master
Browse files Browse the repository at this point in the history
Date handling improvement and various fixes
  • Loading branch information
martinrusev committed Jan 18, 2022
2 parents c24b14a + 6cdc0f0 commit 38aa133
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include LICENSE
include MANIFEST.in
include README.rst
include README.md
include CHANGELOG.md
graft tests
4 changes: 3 additions & 1 deletion imbox/query.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import datetime

from imbox.utils import date_to_date_text


def build_search_query(imap_attribute_lookup, **kwargs):
query = []
for name, value in kwargs.items():
if value is not None:
if isinstance(value, datetime.date):
value = value.strftime('%d-%b-%Y')
value = date_to_date_text(value)
if isinstance(value, str) and '"' in value:
value = value.replace('"', "'")
query.append(imap_attribute_lookup[name].format(value))
Expand Down
14 changes: 13 additions & 1 deletion imbox/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import datetime
import logging
from imaplib import Time2Internaldate
logger = logging.getLogger(__name__)


def str_encode(value='', encoding=None, errors='strict'):
logger.debug("Encode str {} with and errors {}".format(value, encoding, errors))
logger.debug("Encode str {value} with encoding {encoding} and errors {errors}".format(
value=value,
encoding=encoding,
errors=errors))
return str(value, encoding, errors)


Expand All @@ -14,3 +19,10 @@ def str_decode(value='', encoding=None, errors='strict'):
return value.decode(encoding or 'utf-8', errors=errors)
else:
raise TypeError("Cannot decode '{}' object".format(value.__class__))


def date_to_date_text(date):
"""Return a date in the RFC 3501 date-text syntax"""
tzutc = datetime.timezone.utc
dt = datetime.datetime.combine(date, datetime.time.min, tzutc)
return Time2Internaldate(dt)[1:12]
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ def get_version():


def read(filename):
return open(os.path.join(os.path.dirname(__file__), filename)).read()
with open(os.path.join(os.path.dirname(__file__), filename)) as f:
return f.read()


setup(
name='imbox',
version=get_version(),
description="Python IMAP for Human beings",
long_description=read('README.rst'),
long_description=read('README.md'),
keywords='email, IMAP, parsing emails',
author='Martin Rusev',
author_email='martin@amon.cx',
Expand Down

0 comments on commit 38aa133

Please sign in to comment.