<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-


"""Use IMAP CLI to gt a summary of IMAP account state."""


import argparse
import cmd
import datetime
import logging
import os
import sys

import docopt

import imap_cli
from imap_cli import config
from imap_cli import fetch
from imap_cli import flag
from imap_cli import search


app_name = os.path.splitext(os.path.basename(__file__))[0]
log = logging.getLogger(app_name)


class ImapShell(cmd.Cmd):
    completekey = 'Tab'
    intro = 'IMAP interactive Command Line Interpreter.  Type help or ? to list commands.\n'
    prompt = '(imap-cli "INBOX") '
    stdout = sys.stdout
    cmdqueue = []

    def __init__(self, imap_account):
        self.imap_account = imap_account

    def do_cd(self, arg):
        '''Change selected IMAP folder.'''
        args = docopt.docopt('Usage: cd <directory>', arg)
        cd_result = imap_cli.change_dir(self.imap_account, directory=args['<directory>'])
        if cd_result == -1:
            sys.stdout.write('IMAP Folder can\'t be found\n')
        else:
            self.prompt = '(imap-cli "{}") '.format(args['<directory>'])

    def do_flag(self, arg):
        '''Set or Unset flag on mails.'''
        args = docopt.docopt('\n'.join([
            'Usage: flag [options] <mail_id> <flag>',
            '',
            'Options:',
            '    -u, --unset    Remove flag instead of setting them',
            '    -h, --help     Show help options',
        ]), argv=arg)
        flag.flag(self.imap_account, [args['<mail_id>']], args['<flag>'], unset=args['--unset'])

    def do_list(self, arg):
        '''List mail in specified folder.'''

        args = docopt.docopt('\n'.join([
            'Usage: list [options] [<directory>]',
            '',
            'Options:',
            '    -l, --limit=<LIMIT>    Limit number of mail displayed',
            '    -h, --help             Show this message',
        ]), argv=arg)

        try:
            limit = int(args['--limit'] or 10)
        except ValueError:
            limit = 10
        for mail_info in search.fetch_mails_info(self.imap_account, limit=limit):
            sys.stdout.write(u'UID : {:<10} From : {:<40} Subject : {}\n'.format(
                mail_info['uid'][0],
                truncate_string(mail_info['from'], 33),
                truncate_string(mail_info['subject'], 50),
            ))

    def do_read(self, arg):
        '''Read mail by uid.'''
        args = docopt.docopt('Usage: read <mail_uid> [<save_directory>]', arg)
        fetched_mail = fetch.read(self.imap_account, args['<mail_uid>'], save_directory=args['<save_directory>'])
        if fetched_mail is None:
            log.error("Mail was not fetched, an error occured")

        sys.stdout.write(fetch.display(fetched_mail))

    def do_search(self, arg):
        '''Search mail.'''
        args = docopt.docopt('\n'.join([
            'Usage: search [options]',
            '',
            'Options:',
            '    -a, --address=<address>     Search for specified "FROM" address',
            '    -d, --date=<date>           Search mail receive since the specified date (format YYYY-MM-DD)',
            '    -s, --size=<SIZE>           Search mails larger than specified size (in bytes)',
            '    -S, --subject=<subject>     Search by subject',
            '    -t, --tags=<tags>           Searched tags (Comma separated values)',
            '    -T, --full-text=<text>      Searched tags (Comma separated values)',
            '    -h, --help                  Show help options.',
        ]), argv=arg)

        if args.get('--tags') is not None:
            args['--tags'] = args['--tags'].split(',')
        if args['--date'] is not None:
            try:
                date = datetime.datetime.strptime(args['--date'], '%Y-%m-%d')
            except ValueError:
                date = None
        else:
            date = None

        search_criterion = search.create_search_criterion(
            address=args['--address'],
            date=date,
            subject=args['--subject'],
            size=args['--size'],
            tags=args['--tags'],
            text=args['--full-text'],
        )
        mail_set = search.fetch_uids(self.imap_account, search_criterion=search_criterion)
        if len(mail_set) == 0:
            log.error('No mail found')
            return 0
        for mail_info in search.fetch_mails_info(self.imap_account, mail_set=mail_set):
            sys.stdout.write(u'UID : {:<10} From : {:<40} Subject : {}\n'.format(
                mail_info['uid'][0],
                truncate_string(mail_info['from'], 33),
                truncate_string(mail_info['subject'], 50),
            ))

    def do_status(self, arg):
        'Print status of all IMAP folder in this account'
        directory_statuses = sorted(imap_cli.status(self.imap_account), key=lambda obj: obj['directory'])
        for directory_status in directory_statuses:
            sys.stdout.write(u'{:<30} : Unseen {:<6}   Recent {:<6}   Total {:<6}\n'.format(
                directory_status['directory'],
                directory_status['unseen'],
                directory_status['recent'],
                directory_status['count'],
            ))

    def do_quit(self, arg):
        'Exit this shell'
        print('Bye')
        return True
<commit_msg>Add unseen method in repl commands<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-


"""Use IMAP CLI to gt a summary of IMAP account state."""


import argparse
import cmd
import datetime
import logging
import os
import sys

import docopt

import imap_cli
from imap_cli import config
from imap_cli import fetch
from imap_cli import flag
from imap_cli import search


app_name = os.path.splitext(os.path.basename(__file__))[0]
log = logging.getLogger(app_name)


class ImapShell(cmd.Cmd):
    completekey = 'Tab'
    intro = 'IMAP interactive Command Line Interpreter.  Type help or ? to list commands.\n'
    prompt = '(imap-cli "INBOX") '
    stdout = sys.stdout
    cmdqueue = []

    def __init__(self, imap_account):
        self.imap_account = imap_account

    def do_cd(self, arg):
        '''Change selected IMAP folder.'''
        args = docopt.docopt('Usage: cd <directory>', arg)
        cd_result = imap_cli.change_dir(self.imap_account, directory=args['<directory>'])
        if cd_result == -1:
            sys.stdout.write('IMAP Folder can\'t be found\n')
        else:
            self.prompt = '(imap-cli "{}") '.format(args['<directory>'])

    def do_flag(self, arg):
        '''Set or Unset flag on mails.'''
        args = docopt.docopt('\n'.join([
            'Usage: flag [options] <mail_id> <flag>',
            '',
            'Options:',
            '    -u, --unset    Remove flag instead of setting them',
            '    -h, --help     Show help options',
        ]), argv=arg)
        flag.flag(self.imap_account, [args['<mail_id>']], args['<flag>'], unset=args['--unset'])

    def do_list(self, arg):
        '''List mail in specified folder.'''

        args = docopt.docopt('\n'.join([
            'Usage: list [options] [<directory>]',
            '',
            'Options:',
            '    -l, --limit=<LIMIT>    Limit number of mail displayed',
            '    -h, --help             Show this message',
        ]), argv=arg)

        try:
            limit = int(args['--limit'] or 10)
        except ValueError:
            limit = 10
        for mail_info in search.fetch_mails_info(self.imap_account, limit=limit):
            sys.stdout.write(u'UID : {:<10} From : {:<40} Subject : {}\n'.format(
                mail_info['uid'][0],
                truncate_string(mail_info['from'], 33),
                truncate_string(mail_info['subject'], 50),
            ))

    def do_quit(self, arg):
        'Exit this shell'
        print('Bye')
        return True

    def do_read(self, arg):
        '''Read mail by uid.'''
        args = docopt.docopt('Usage: read <mail_uid> [<save_directory>]', arg)
        fetched_mail = fetch.read(self.imap_account, args['<mail_uid>'], save_directory=args['<save_directory>'])
        if fetched_mail is None:
            log.error("Mail was not fetched, an error occured")

        sys.stdout.write(fetch.display(fetched_mail))

    def do_search(self, arg):
        '''Search mail.'''
        args = docopt.docopt('\n'.join([
            'Usage: search [options]',
            '',
            'Options:',
            '    -a, --address=<address>     Search for specified "FROM" address',
            '    -d, --date=<date>           Search mail receive since the specified date (format YYYY-MM-DD)',
            '    -s, --size=<SIZE>           Search mails larger than specified size (in bytes)',
            '    -S, --subject=<subject>     Search by subject',
            '    -t, --tags=<tags>           Searched tags (Comma separated values)',
            '    -T, --full-text=<text>      Searched tags (Comma separated values)',
            '    -h, --help                  Show help options.',
        ]), argv=arg)

        if args.get('--tags') is not None:
            args['--tags'] = args['--tags'].split(',')
        if args['--date'] is not None:
            try:
                date = datetime.datetime.strptime(args['--date'], '%Y-%m-%d')
            except ValueError:
                date = None
        else:
            date = None

        search_criterion = search.create_search_criterion(
            address=args['--address'],
            date=date,
            subject=args['--subject'],
            size=args['--size'],
            tags=args['--tags'],
            text=args['--full-text'],
        )
        mail_set = search.fetch_uids(self.imap_account, search_criterion=search_criterion)
        if len(mail_set) == 0:
            log.error('No mail found')
            return 0
        for mail_info in search.fetch_mails_info(self.imap_account, mail_set=mail_set):
            sys.stdout.write(u'UID : {:<10} From : {:<40} Subject : {}\n'.format(
                mail_info['uid'][0],
                truncate_string(mail_info['from'], 33),
                truncate_string(mail_info['subject'], 50),
            ))

    def do_status(self, arg):
        'Print status of all IMAP folder in this account'
        directory_statuses = sorted(imap_cli.status(self.imap_account), key=lambda obj: obj['directory'])
        for directory_status in directory_statuses:
            sys.stdout.write(u'{:<30} : Unseen {:<6}   Recent {:<6}   Total {:<6}\n'.format(
                directory_status['directory'],
                directory_status['unseen'],
                directory_status['recent'],
                directory_status['count'],
            ))

    def do_unseen(self, arg):
        '''List Unseen mail (equivalent to "search -t unseen").'''
        search_criterion = search.create_search_criterion(tags=['unseen'])
        mail_set = search.fetch_uids(self.imap_account, search_criterion=search_criterion)
        if len(mail_set) == 0:
            log.error('No unseen mail found')
        else:
            for mail_info in search.fetch_mails_info(self.imap_account, mail_set=mail_set):
                sys.stdout.write(u'UID : {:<10} From : {:<40} Subject : {}\n'.format(
                    mail_info['uid'][0],
                    truncate_string(mail_info['from'], 33),
                    truncate_string(mail_info['subject'], 50),
                ))

{'fraction_non_alphanumeric': 0.14449200663929415, 'fraction_numerical': 0.006464575871407355, 'mean_word_length': 1.9921589127025614, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3370534', 'n_tokens_mistral': 3533, 'n_tokens_neox': 3397, 'n_words': 817}