<commit_before>'''
    Gmvault: a tool to backup and restore your gmail account.
    Copyright (C) <2011-2012>  <<NAME> (<EMAIL>)>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Contains the class monkey patching IMAPClient and imaplib
'''

import zlib
import time
import datetime
import imaplib  #for the exception
import imapclient

#need to monkey patch _convert_INTERNALDATE to work with imaplib2
def mod_convert_INTERNALDATE(date_string, normalise_times=True):
    
        print("MOD CONVERTDATE")
        mo = imaplib.InternalDate.match('INTERNALDATE "%s"' % date_string)
        if not mo:
            raise ValueError("couldn't parse date %r" % date_string)
    
        zoneh = int(mo.group('zoneh'))
        zonem = (zoneh * 60) + int(mo.group('zonem'))
        if mo.group('zonen') == '-':
            zonem = -zonem
        tz = imapclient.fixed_offset.FixedOffset(zonem)
   
        year = int(mo.group('year'))
        mon = imaplib.Mon2num[mo.group('mon')]
        day = int(mo.group('day'))
        hour = int(mo.group('hour'))
        minute = int(mo.group('min'))
        sec = int(mo.group('sec'))
    
        dt = datetime.datetime(year, mon, day, hour, minute, sec, 0, tz)
    
        if normalise_times:
            # Normalise to host system's timezone
            return dt.astimezone(imapclient.fixed_offset.FixedOffset.for_system()).replace(tzinfo=None)
        return dt

imapclient.response_parser._convert_INTERNALDATE = mod_convert_INTERNALDATE

#monkey patching add compress in COMMANDS of imap
imaplib.Commands['COMPRESS'] = ('AUTH', 'SELECTED')

class IMAP4COMPSSL(imaplib.IMAP4_SSL): #pylint:disable-msg=R0904
    """
       Add support for compression inspired by inspired by http://www.janeelix.com/piers/python/py2html.cgi/piers/python/imaplib2
    """
    def __init__(self, host = '', port = imaplib.IMAP4_SSL_PORT, keyfile = None, certfile = None):
        """
           constructor
        """
        self.compressor = None
        self.decompressor = None
        <commit_msg>update monkey patch<commit_after>'''
    Gmvault: a tool to backup and restore your gmail account.
    Copyright (C) <2011-2012>  <<NAME> (guillaume dot aubert at gmail do com)>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Contains the class monkey patching IMAPClient and imaplib
'''

import zlib
import time
import datetime
import imaplib  #for the exception
import imapclient

#need to monkey patch _convert_INTERNALDATE to work with imaplib2
def mod_convert_INTERNALDATE(date_string, normalise_times=True):
    """
       monkey patched convert_INTERNALDATE
    """
    mo = imaplib.InternalDate.match('INTERNALDATE "%s"' % date_string)
    if not mo:
        raise ValueError("couldn't parse date %r" % date_string)
    
    zoneh = int(mo.group('zoneh'))
    zonem = (zoneh * 60) + int(mo.group('zonem'))
    if mo.group('zonen') == '-':
        zonem = -zonem
    tz = imapclient.fixed_offset.FixedOffset(zonem)
    
    year = int(mo.group('year'))
    mon = imaplib.Mon2num[mo.group('mon')]
    day = int(mo.group('day'))
    hour = int(mo.group('hour'))
    minute = int(mo.group('min'))
    sec = int(mo.group('sec'))
    
    dt = datetime.datetime(year, mon, day, hour, minute, sec, 0, tz)
    
    if normalise_times:
        # Normalise to host system's timezone
        return dt.astimezone(imapclient.fixed_offset.FixedOffset.for_system()).replace(tzinfo=None)
    return dt

imapclient.response_parser._convert_INTERNALDATE = mod_convert_INTERNALDATE

#monkey patching add compress in COMMANDS of imap
imaplib.Commands['COMPRESS'] = ('AUTH', 'SELECTED')

class IMAP4COMPSSL(imaplib.IMAP4_SSL): #pylint:disable-msg=R0904
    """
       Add support for compression inspired by inspired by http://www.janeelix.com/piers/python/py2html.cgi/piers/python/imaplib2
    """
    def __init__(self, host = '', port = imaplib.IMAP4_SSL_PORT, keyfile = None, certfile = None):
        """
           constructor
        """
        self.compressor = None
        self.decompressor = None
        
{'fraction_non_alphanumeric': 0.09807288685365388, 'fraction_numerical': 0.008776950963556573, 'mean_word_length': 3.1308116627265563, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4077503', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1587, 'n_words': 501}