# Path: animanager/animecmd.py
# class AnimeCmd:
# 
#     prompt = 'A> '
#     commands = {
#         '?': commands.help,
#         'a': commands.add,
#         'add': commands.add,
#         'addrule': commands.addrule,
#         'as': commands.asearch,
#         'asearch': commands.asearch,
#         'deleterule': commands.deleterule,
#         'fix': commands.fix,
#         'gpl': commands.gpl,
#         'help': commands.help,
#         'purgecache': commands.purgecache,
#         'q': commands.quit,
#         'quit': commands.quit,
#         'r': commands.reset,
#         'reg': commands.register,
#         'register': commands.register,
#         'reset': commands.reset,
#         'rules': commands.rules,
#         's': commands.search,
#         'search': commands.search,
#         'sh': commands.show,
#         'show': commands.show,
#         'u': commands.update,
#         'unreg': commands.unregister,
#         'unregister': commands.unregister,
#         'update': commands.update,
#         'w': commands.watch,
#         'watch': commands.watch,
#     }
#     safe_exceptions = set([AIDParseError])
# 
#     def __init__(self, config: 'ConfigParser'):
#         self.last_cmd = []
#         self.state = s = State()
#         s.config = config
#         s.db = _connect(config['anime'].getpath('database'))
#         s.cache_manager = cachetable.make_manager(s.db)
#         s.cache_manager.setup()
#         s.titles = TitleSearcher(config['anime'].getpath('anidb_cache'))
#         s.results = AIDResultsManager({
#             'db': AIDResults([
#                 'Title', 'Type', 'Episodes', 'Complete', 'Available',
#             ]),
#             'anidb': AIDResults(['Title']),
#         })
# 
#     def cmdloop(self):
#         """Start CLI REPL."""
#         while True:
#             cmdline = input(self.prompt)
#             tokens = shlex.split(cmdline)
#             if not tokens:
#                 if self.last_cmd:
#                     tokens = self.last_cmd
#                 else:
#                     print('No previous command.')
#                     continue
#             if tokens[0] not in self.commands:
#                 print('Invalid command')
#                 continue
#             command = self.commands[tokens[0]]
#             self.last_cmd = tokens
#             try:
#                 if command(self.state, tokens):
#                     break
#             except CmdExit:
#                 continue
#             except Exception as e:
#                 if e not in self.safe_exceptions:
#                     logger.exception('Error!')
# 
# Path: animanager/config.py
# _CONVERTERS = {
#     'path': lambda x: Path(x).expanduser(),
#     'args': shlex.split,
# }
# _DEFAULTS = {
#     'general': {
#         'colors': 'false',
#     },
#     'anime': {
#         'database': '~/.animanager/database.db',
#         'anidb_cache': '~/.animanager/anidb',
#         'watchdir': '~/anime',
#         'player': 'mpv',
#     },
# }
# def load(path):
# 
# Path: animanager/migrations.py
# def migrate(path: str):
# def _migrate1(conn):
# def _migrate2(conn):
# def _migrate3(conn):
# def _parse_date(string: str) -> datetime.date:


import argparse
import logging
import logging.config
import os
import readline
import animanager
from animanager.animecmd import AnimeCmd
from animanager import config
from animanager import migrations
Copyright (C) 2015-2018  Allen Li

This program comes with ABSOLUTELY NO WARRANTY; for details type "gpl w".
This is free software, and you are welcome to redistribute it
under certain conditions; type "gpl c" for details.'''


def main():
    args = _parse_args()
    _setup_logging(args)
    cfg = config.load(args.config)
    db_path = cfg['anime'].getpath('database')
    migrations.migrate(str(db_path))
    cmd = AnimeCmd(cfg)
    print(_INTRO)
    cmd.cmdloop()


def _parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--config',
                        default=_DEFAULT_CONFIG,
                        help='Alternate configuration file to use.')
    parser.add_argument('--debug',
                        action='store_true',
                        help='Enable debug output.')
    return parser.parse_args()


def _setup_logging(args):

# Path: animanager/animecmd.py
# class AnimeCmd:
# 
#     prompt = 'A> '
#     commands = {
#         '?': commands.help,
#         'a': commands.add,
#         'add': commands.add,
#         'addrule': commands.addrule,
#         'as': commands.asearch,
#         'asearch': commands.asearch,
#         'deleterule': commands.deleterule,
#         'fix': commands.fix,
#         'gpl': commands.gpl,
#         'help': commands.help,
#         'purgecache': commands.purgecache,
#         'q': commands.quit,
#         'quit': commands.quit,
#         'r': commands.reset,
#         'reg': commands.register,
#         'register': commands.register,
#         'reset': commands.reset,
#         'rules': commands.rules,
#         's': commands.search,
#         'search': commands.search,
#         'sh': commands.show,
#         'show': commands.show,
#         'u': commands.update,
#         'unreg': commands.unregister,
#         'unregister': commands.unregister,
#         'update': commands.update,
#         'w': commands.watch,
#         'watch': commands.watch,
#     }
#     safe_exceptions = set([AIDParseError])
# 
#     def __init__(self, config: 'ConfigParser'):
#         self.last_cmd = []
#         self.state = s = State()
#         s.config = config
#         s.db = _connect(config['anime'].getpath('database'))
#         s.cache_manager = cachetable.make_manager(s.db)
#         s.cache_manager.setup()
#         s.titles = TitleSearcher(config['anime'].getpath('anidb_cache'))
#         s.results = AIDResultsManager({
#             'db': AIDResults([
#                 'Title', 'Type', 'Episodes', 'Complete', 'Available',
#             ]),
#             'anidb': AIDResults(['Title']),
#         })
# 
#     def cmdloop(self):
#         """Start CLI REPL."""
#         while True:
#             cmdline = input(self.prompt)
#             tokens = shlex.split(cmdline)
#             if not tokens:
#                 if self.last_cmd:
#                     tokens = self.last_cmd
#                 else:
#                     print('No previous command.')
#                     continue
#             if tokens[0] not in self.commands:
#                 print('Invalid command')
#                 continue
#             command = self.commands[tokens[0]]
#             self.last_cmd = tokens
#             try:
#                 if command(self.state, tokens):
#                     break
#             except CmdExit:
#                 continue
#             except Exception as e:
#                 if e not in self.safe_exceptions:
#                     logger.exception('Error!')
# 
# Path: animanager/config.py
# _CONVERTERS = {
#     'path': lambda x: Path(x).expanduser(),
#     'args': shlex.split,
# }
# _DEFAULTS = {
#     'general': {
#         'colors': 'false',
#     },
#     'anime': {
#         'database': '~/.animanager/database.db',
#         'anidb_cache': '~/.animanager/anidb',
#         'watchdir': '~/anime',
#         'player': 'mpv',
#     },
# }
# def load(path):
# 
# Path: animanager/migrations.py
# def migrate(path: str):
# def _migrate1(conn):
# def _migrate2(conn):
# def _migrate3(conn):
# def _parse_date(string: str) -> datetime.date:


# Path: animanager/__main__.py
import argparse
import logging
import logging.config
import os
import readline
import animanager
from animanager.animecmd import AnimeCmd
from animanager import config
from animanager import migrations

Copyright (C) 2015-2018  Allen Li

This program comes with ABSOLUTELY NO WARRANTY; for details type "gpl w".
This is free software, and you are welcome to redistribute it
under certain conditions; type "gpl c" for details.'''


def main():
    args = _parse_args()
    _setup_logging(args)
    cfg = config.load(args.config)
    db_path = cfg['anime'].getpath('database')
    migrations.migrate(str(db_path))
    cmd = AnimeCmd(cfg)
    print(_INTRO)
    cmd.cmdloop()


def _parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--config',
                        default=_DEFAULT_CONFIG,
                        help='Alternate configuration file to use.')
    parser.add_argument('--debug',
                        action='store_true',
                        help='Enable debug output.')
    return parser.parse_args()


def _setup_logging(args):

    logging.config.dictConfig({