Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date handling improvement and various fixes #218

Merged
merged 4 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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