<commit_before>from __future__ import unicode_literals, division, absolute_import

from argparse import ArgumentParser, ArgumentTypeError

from sqlalchemy.orm.exc import NoResultFound

from flexget import options
from flexget.entry import Entry
from flexget.event import event
from flexget.logger import console
from flexget.manager import Session
from flexget.options import CLITable, table_parser, CLITableError
from flexget.plugin import PluginError
from flexget.plugins.list.movie_list import get_list_by_exact_name, get_movie_lists, get_movies_by_list_id, \
    get_movie_by_title, MovieListMovie, get_db_movie_identifiers, MovieListList, MovieListBase
from flexget.plugins.metainfo.imdb_lookup import ImdbLookup
from flexget.plugins.metainfo.tmdb_lookup import PluginTmdbLookup
from flexget.utils.tools import split_title_year

imdb_lookup = ImdbLookup().lookup
tmdb_lookup = PluginTmdbLookup().lookup


def lookup_movie(title, session, identifiers=None):
    entry = Entry(title=title)
    if identifiers:
        for identifier in identifiers:
            for key, value in identifier.items():
                entry[key] = value
    try:
        imdb_lookup(entry, session=session)
    except PluginError:
        tmdb_lookup(entry)
    if entry.get('movie_name'):
        return entry


def movie_list_keyword_type(identifier):
    if identifier.count('=') != 1:
        raise ArgumentTypeError('Received identifier in wrong format: %s, '
                                ' should be in keyword format like `imdb_id=tt1234567`' % identifier)
    name, value = identifier.split('=', 2)
    if name not in MovieListBase().supported_ids:
        raise ArgumentTypeError(
            'Received unsupported identifier ID %s. Should be one of %s' % (
                identifier, ' ,'.join(MovieListBase().supported_ids)))
    return {name: value}


def do_cli(manager, options):
    """Handle movie-list subcommand"""
    if options.list_action == 'all':
        movie_list_lists(options)
        return

    if options.list_action == 'list':
        movie_list_list(options)
        return

    if options.list_action == 'add':
        movie_list_add(options)
        return

    if options.list_action == 'del':
        movie_list_del(options)
        return

    if options.list_action == 'purge':
        movie_list_purge(options)
        return


def movie_list_lists(options):
    """ Show all movie lists """
    lists = get_movie_lists()
    header = ['#', 'List Name']
    table_data = [header]
    for movie_list in lists:
        table_data.append([movie_list.id, movie_list.name])
    table = CLITable(options.table_type, table_data)
    try:
        console(table.output)
    except CLITableError as e:
        console('ERROR: %s' % str(e))


def movie_list_list(options):
    """List movie list"""
    with Session() as session:
        try:
            movie_list = get_list_by_exact_name(options.list_name)
        except NoResultFound:
            console('Could not find movie list with name {}'.format(options.list_name))
            return
    header = ['Movie Name', 'Movie year']
    header += MovieListBase().supported_ids
    table_data = [header]
    movies = get_movies_by_list_id(movie_list.id, order_by='added', descending=True, session=session)
    for movie in movies:
        movie_row = [movie.title, movie.year or '']
        for identifier in MovieListBase().supported_ids:
            movie_row.append(movie.identifiers.get(identifier, ''))
        table_data.append(movie_row)
    title = '{} Movies in movie list: `{}`'.format(len(movies), options.list_name)
    table = CLITable(options.table_type, table_data, title)
    try:
        console(table.output)
    except CLITableError as e:
        console('ERROR: %s' % str(e))


def movie_list_add(options):
    with Session() as session:
        try:
            movie_list = get_list_by_exact_name(options.list_name)
        except NoResultFound:
            console('Could not find movie list with name {}, creating'.format(options.list_name))<commit_msg>Added word wrap support to movie list<commit_after>from __future__ import unicode_literals, division, absolute_import
from builtins import *  # pylint: disable=unused-import, redefined-builtin

from argparse import ArgumentParser, ArgumentTypeError
from functools import partial

from sqlalchemy.orm.exc import NoResultFound

from flexget import options
from flexget.entry import Entry
from flexget.event import event
from flexget.logger import console
from flexget.manager import Session
from flexget.options import CLITable, table_parser, CLITableError
from flexget.plugin import PluginError
from flexget.plugins.list.movie_list import get_list_by_exact_name, get_movie_lists, get_movies_by_list_id, \
    get_movie_by_title, MovieListMovie, get_db_movie_identifiers, MovieListList, MovieListBase
from flexget.plugins.metainfo.imdb_lookup import ImdbLookup
from flexget.plugins.metainfo.tmdb_lookup import PluginTmdbLookup
from flexget.utils.tools import split_title_year

imdb_lookup = ImdbLookup().lookup
tmdb_lookup = PluginTmdbLookup().lookup

ww = CLITable.word_wrap

def lookup_movie(title, session, identifiers=None):
    entry = Entry(title=title)
    if identifiers:
        for identifier in identifiers:
            for key, value in identifier.items():
                entry[key] = value
    try:
        imdb_lookup(entry, session=session)
    except PluginError:
        tmdb_lookup(entry)
    if entry.get('movie_name'):
        return entry


def movie_list_keyword_type(identifier):
    if identifier.count('=') != 1:
        raise ArgumentTypeError('Received identifier in wrong format: %s, '
                                ' should be in keyword format like `imdb_id=tt1234567`' % identifier)
    name, value = identifier.split('=', 2)
    if name not in MovieListBase().supported_ids:
        raise ArgumentTypeError(
            'Received unsupported identifier ID %s. Should be one of %s' % (
                identifier, ' ,'.join(MovieListBase().supported_ids)))
    return {name: value}


def do_cli(manager, options):
    """Handle movie-list subcommand"""

    # Handle globally setting value for word wrap method
    global ww
    ww = partial(CLITable.word_wrap, max_length=options.max_column_width)

    if options.list_action == 'all':
        movie_list_lists(options)
        return

    if options.list_action == 'list':
        movie_list_list(options)
        return

    if options.list_action == 'add':
        movie_list_add(options)
        return

    if options.list_action == 'del':
        movie_list_del(options)
        return

    if options.list_action == 'purge':
        movie_list_purge(options)
        return


def movie_list_lists(options):
    """ Show all movie lists """
    lists = get_movie_lists()
    header = ['#', 'List Name']
    table_data = [header]
    for movie_list in lists:
        table_data.append([movie_list.id, movie_list.name])
    table = CLITable(options.table_type, table_data)
    try:
        console(table.output)
    except CLITableError as e:
        console('ERROR: %s' % str(e))


def movie_list_list(options):
    """List movie list"""
    with Session() as session:
        try:
            movie_list = get_list_by_exact_name(options.list_name)
        except NoResultFound:
            console('Could not find movie list with name {}'.format(options.list_name))
            return
    header = ['Movie Name', 'Movie year']
    header += MovieListBase().supported_ids
    table_data = [header]
    movies = get_movies_by_list_id(movie_list.id, order_by='added', descending=True, session=session)
    for movie in movies:
        movie_row = [ww(movie.title), movie.year or '']
        for identifier in MovieListBase().supported_ids:
            movie_row.append(movie.identifiers.get(identifier, ''))
        table_data.append(movie_row)
    title = '{} Movies in movie list: `{}`'.format(len(movies), options.list_name)
    table = CLITable(options.table_type, table_data, title)
    try:
        console(table.output)
    except CLITableError as e:
        console('ERROR: %s' % str(e))


def movie_list_add(options):
    with Session() as session:
        try:
            movie_list = get_list_by_exact_name(options.list_name)
        except NoResultFound:
            console('Could not find movie list with name {}, creating'.format(options.list_name))
{'fraction_non_alphanumeric': 0.09347463681829007, 'fraction_numerical': 0.0021433674684448677, 'mean_word_length': 3.3093894304771676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1344051', 'n_tokens_mistral': 2546, 'n_tokens_neox': 2435, 'n_words': 628}