<commit_before># -*- coding: utf-8 -*-

# Copyright(C) 2010-2011  <NAME>
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


import sys
import os
import datetime
import hashlib

from tempfile import NamedTemporaryFile
from lxml import etree

from weboob.core import CallErrors
from weboob.capabilities.messages import ICapMessages, Message, Thread
from weboob.capabilities.account import ICapAccount
from weboob.capabilities.contact import ICapContact, Contact
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter
from weboob.tools.misc import html2text


__all__ = ['Boobmsg']


class AtomFormatter(IFormatter):
    MANDATORY_FIELDS = ('title', 'date', 'sender', 'content')

    def start_format(self, **kwargs):
        self.output(u'<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"')
        self.output(u'xmlns:dc="http://purl.org/dc/elements/1.1/">\n')
        self.output(u'<title type="text">Atom feed by Weboob</title>')  # TODO : get backend name
        self.output(u'<updated>%s</updated>' % datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"))
        m = hashlib.md5()
        m.update(datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"))
        self.output(u'<id>urn:md5:%s</id>' % m.hexdigest())

    def format_obj(self, obj, alias):
        elem = etree.Element('entry')

        title = etree.Element('title')
        title.text = obj.title
        elem.append(title)

        id = etree.Element('id')
        m = hashlib.md5()
        m.update(obj.content.encode('utf8', 'ascii'))
        id.text = "urn:md5:%s" % m.hexdigest()
        elem.append(id)

        link = etree.Element('link')
        link.attrib["href"] = obj.thread.id
        link.attrib["title"] = obj.title
        link.attrib["type"] = "text/html"
        elem.append(link)

        author = etree.Element('author')
        name = etree.Element('name')
        if obj.sender:
            name.text = obj.sender
        else:
            name.text = obj.backend
        author.append(name)
        elem.append(author)

        date = etree.Element('updated')
        date.text = obj.date.strftime("%Y-%m-%dT%H:%M:%SZ")
        elem.append(date)

        content = etree.Element('content')
        content.text = obj.content
        content.attrib["type"] = "html"
        elem.append(content)

        return etree.tostring(elem, pretty_print=True)

    def flush(self):
        self.output(u'</feed>')


class XHtmlFormatter(IFormatter):
    MANDATORY_FIELDS = ('title', 'date', 'sender', 'signature', 'content')

    def format_obj(self, obj, alias):
        result  = "<div>\n"
        result += "<h1>%s</h1>" % (obj.title)
        result += "<dl>"
        result += "<dt>Date</dt><dd>%s</dd>" % (obj.date)
        result += "<dt>Sender</dt><dd>%s</dd>" % (obj.sender)
        result += "<dt>Signature</dt><dd>%s</dd>" % (obj.signature)
        result += "</dl>"
        result += "<div>%s</div>" % (obj.content)
        result += "</div>\n"
        return result


class MessageFormatter(IFormatter):
    MANDATORY_FIELDS = ('title', 'date', 'sender', 'signature', 'content')

    def format_obj(self, obj, alias):
        result = u'%sTitle:%s %s\n' % (self.BOLD,
                                       self.NC, obj.title)
        result += u'%sDate:%s %s\n' % (self.BOLD,
                                       self.NC, obj.date)
        result += u'%sFrom:%s %s\n' % (self.BOLD,
                                       self.NC, obj.sender)
        if hasattr(obj, 'receivers') and obj.receivers:
            result += u'%sTo:%s %s\n' % (self.BOLD,
                                         self.NC,
                                         ', '.join(obj.receivers))

        if obj.flags & Message.IS_HTML:
            content = html2text(obj.content)
        else:
            content = obj.content

        result += '\n%s' % content

        if obj.signature:
            if obj.flags & Message.IS_HTML:
                signature = html2text(obj.signature)
            else:
                signature = obj.signature

            result += '\n-- \n%s' % signature
        return result


class MessagesListFormatter(IFormatter):
    MANDATORY_FIELDS = ()
    count = 0
    _list_messages = False
<commit_msg>Datetime human readable<commit_after># -*- coding: utf-8 -*-

# Copyright(C) 2010-2011  <NAME>
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


import sys
import os
import datetime
import hashlib

from tempfile import NamedTemporaryFile
from lxml import etree

from weboob.core import CallErrors
from weboob.capabilities.messages import ICapMessages, Message, Thread
from weboob.capabilities.account import ICapAccount
from weboob.capabilities.contact import ICapContact, Contact
from weboob.tools.application.repl import ReplApplication
from weboob.tools.application.formatters.iformatter import IFormatter
from weboob.tools.misc import html2text


__all__ = ['Boobmsg']


class AtomFormatter(IFormatter):
    MANDATORY_FIELDS = ('title', 'date', 'sender', 'content')

    def start_format(self, **kwargs):
        self.output(u'<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"')
        self.output(u'xmlns:dc="http://purl.org/dc/elements/1.1/">\n')
        self.output(u'<title type="text">Atom feed by Weboob</title>')  # TODO : get backend name
        self.output(u'<updated>%s</updated>' % datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"))
        m = hashlib.md5()
        m.update(datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"))
        self.output(u'<id>urn:md5:%s</id>' % m.hexdigest())

    def format_obj(self, obj, alias):
        elem = etree.Element('entry')

        title = etree.Element('title')
        title.text = obj.title
        elem.append(title)

        id = etree.Element('id')
        m = hashlib.md5()
        m.update(obj.content.encode('utf8', 'ascii'))
        id.text = "urn:md5:%s" % m.hexdigest()
        elem.append(id)

        link = etree.Element('link')
        link.attrib["href"] = obj.thread.id
        link.attrib["title"] = obj.title
        link.attrib["type"] = "text/html"
        elem.append(link)

        author = etree.Element('author')
        name = etree.Element('name')
        if obj.sender:
            name.text = obj.sender
        else:
            name.text = obj.backend
        author.append(name)
        elem.append(author)

        date = etree.Element('updated')
        date.text = obj.date.strftime("%Y-%m-%dT%H:%M:%SZ")
        elem.append(date)

        content = etree.Element('content')
        content.text = obj.content
        content.attrib["type"] = "html"
        elem.append(content)

        return etree.tostring(elem, pretty_print=True)

    def flush(self):
        self.output(u'</feed>')


class XHtmlFormatter(IFormatter):
    MANDATORY_FIELDS = ('title', 'date', 'sender', 'signature', 'content')

    def format_obj(self, obj, alias):
        result  = "<div>\n"
        result += "<h1>%s</h1>" % (obj.title)
        result += "<dl>"
        result += "<dt>Date</dt><dd>%s</dd>" % (obj.date.strftime('%Y-%m-%d %H:%M'))
        result += "<dt>Sender</dt><dd>%s</dd>" % (obj.sender)
        result += "<dt>Signature</dt><dd>%s</dd>" % (obj.signature)
        result += "</dl>"
        result += "<div>%s</div>" % (obj.content)
        result += "</div>\n"
        return result


class MessageFormatter(IFormatter):
    MANDATORY_FIELDS = ('title', 'date', 'sender', 'signature', 'content')

    def format_obj(self, obj, alias):
        result = u'%sTitle:%s %s\n' % (self.BOLD,
                                       self.NC, obj.title)
        result += u'%sDate:%s %s\n' % (self.BOLD,
                                       self.NC, obj.date.strftime('%Y-%m-%d %H:%M'))
        result += u'%sFrom:%s %s\n' % (self.BOLD,
                                       self.NC, obj.sender)
        if hasattr(obj, 'receivers') and obj.receivers:
            result += u'%sTo:%s %s\n' % (self.BOLD,
                                         self.NC,
                                         ', '.join(obj.receivers))

        if obj.flags & Message.IS_HTML:
            content = html2text(obj.content)
        else:
            content = obj.content

        result += '\n%s' % content

        if obj.signature:
            if obj.flags & Message.IS_HTML:
                signature = html2text(obj.signature)
            else:
                signature = obj.signature

            result += '\n-- \n%s' % signature
        return result


class MessagesListFormatter(IFormatter):
    MANDATORY_FIELDS = ()
    count = 0
    _list_messages = False

{'fraction_non_alphanumeric': 0.1463365539452496, 'fraction_numerical': 0.006239935587761675, 'mean_word_length': 2.38799863620866, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 2, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4769227', 'n_tokens_mistral': 3173, 'n_tokens_neox': 3085, 'n_words': 732}