# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/email.py
# def create_message(request, template, context, subject, recipients,
#                    attachments=None, extra_headers=None):
#     text_body = render(template + '.txt', context, request=request)
#     # chamaleon txt templates are rendered as utf-8 bytestrings
#     text_body = text_body.decode('utf-8')
# 
#     html_body = render(template + '.pt', context, request=request)
# 
#     extra_headers = extra_headers or {}
# 
#     message = Message(
#         subject=subject,
#         recipients=recipients,
#         body=text_body,
#         html=html_body,
#         extra_headers=extra_headers,
#     )
# 
#     if attachments is not None:
#         for attachment in attachments:
#             message.attach(attachment)
# 
#     return message
# 
# Path: yithlibraryserver/scripts/reports.py
# def get_passwords_map(passwords):
#     # make a password dict keyed by users_id
#     passwords_map = {}
#     for password in passwords:
#         owner = password['owner']
#         if owner in passwords_map:
#             passwords_map[owner] += 1
#         else:
#             passwords_map[owner] = 1
# 
#     return passwords_map
# 
# Path: yithlibraryserver/scripts/utils.py
# def safe_print(value):
#     if PY3:  # pragma: no cover
#         print(value)
#     else:  # pragma: no cover
#         print(value.encode('utf-8'))
# 
# Path: yithlibraryserver/scripts/utils.py
# def get_user_display_name(user):
#     return '%s %s <%s>' % (user.get('first_name', ''),
#                            user.get('last_name', ''),
#                            user.get('email', ''))


import optparse
import textwrap
import sys
import transaction
from pyramid.paster import bootstrap
from pyramid_mailer import get_mailer
from yithlibraryserver.compat import urlparse
from yithlibraryserver.email import create_message
from yithlibraryserver.scripts.reports import get_passwords_map
from yithlibraryserver.scripts.utils import safe_print
from yithlibraryserver.scripts.utils import get_user_display_name
        if not user['email']:
            continue

        if not user['_id'] in passwords_map:
            continue

        yield user


def send_email(request, email_template, user, preferences_link):
    safe_print('Sending email to %s' % get_user_display_name(user))
    context = {'user': user, 'preferences_link': preferences_link}
    return create_message(
        request,
        'yithlibraryserver.scripts:templates/%s' % email_template,
        context,
        "Yith Library announcement",
        [user['email']],
    )


def announce():
    usage = "migrate: %prog config_uri migration_name"
    description = "Add a 'send_email_periodically' preference to every user."
    parser = optparse.OptionParser(
        usage=usage,
        description=textwrap.dedent(description)
        )
    options, args = parser.parse_args(sys.argv[1:])
    if len(args) != 2:

# Path: yithlibraryserver/compat.py
# PY3 = sys.version_info[0] == 3
#     def encode_header(obj):  # pragma: no cover
#     def encode_header(obj):  # pragma: no cover
# 
# Path: yithlibraryserver/email.py
# def create_message(request, template, context, subject, recipients,
#                    attachments=None, extra_headers=None):
#     text_body = render(template + '.txt', context, request=request)
#     # chamaleon txt templates are rendered as utf-8 bytestrings
#     text_body = text_body.decode('utf-8')
# 
#     html_body = render(template + '.pt', context, request=request)
# 
#     extra_headers = extra_headers or {}
# 
#     message = Message(
#         subject=subject,
#         recipients=recipients,
#         body=text_body,
#         html=html_body,
#         extra_headers=extra_headers,
#     )
# 
#     if attachments is not None:
#         for attachment in attachments:
#             message.attach(attachment)
# 
#     return message
# 
# Path: yithlibraryserver/scripts/reports.py
# def get_passwords_map(passwords):
#     # make a password dict keyed by users_id
#     passwords_map = {}
#     for password in passwords:
#         owner = password['owner']
#         if owner in passwords_map:
#             passwords_map[owner] += 1
#         else:
#             passwords_map[owner] = 1
# 
#     return passwords_map
# 
# Path: yithlibraryserver/scripts/utils.py
# def safe_print(value):
#     if PY3:  # pragma: no cover
#         print(value)
#     else:  # pragma: no cover
#         print(value.encode('utf-8'))
# 
# Path: yithlibraryserver/scripts/utils.py
# def get_user_display_name(user):
#     return '%s %s <%s>' % (user.get('first_name', ''),
#                            user.get('last_name', ''),
#                            user.get('email', ''))


# Path: yithlibraryserver/scripts/announce.py
import optparse
import textwrap
import sys
import transaction
from pyramid.paster import bootstrap
from pyramid_mailer import get_mailer
from yithlibraryserver.compat import urlparse
from yithlibraryserver.email import create_message
from yithlibraryserver.scripts.reports import get_passwords_map
from yithlibraryserver.scripts.utils import safe_print
from yithlibraryserver.scripts.utils import get_user_display_name

        if not user['email']:
            continue

        if not user['_id'] in passwords_map:
            continue

        yield user


def send_email(request, email_template, user, preferences_link):
    safe_print('Sending email to %s' % get_user_display_name(user))
    context = {'user': user, 'preferences_link': preferences_link}
    return create_message(
        request,
        'yithlibraryserver.scripts:templates/%s' % email_template,
        context,
        "Yith Library announcement",
        [user['email']],
    )


def announce():
    usage = "migrate: %prog config_uri migration_name"
    description = "Add a 'send_email_periodically' preference to every user."
    parser = optparse.OptionParser(
        usage=usage,
        description=textwrap.dedent(description)
        )
    options, args = parser.parse_args(sys.argv[1:])
    if len(args) != 2:

        safe_print('You must provide two arguments. '