main.py 2.19 KB
Newer Older
Dos Santos David's avatar
Dos Santos David committed
1 2
import argparse

Dos Santos David's avatar
Dos Santos David committed
3 4
from gogole import commands
from gogole.config import COLLECTIONS
Dos Santos David's avatar
Dos Santos David committed
5

Dos Santos David's avatar
Dos Santos David committed
6 7
from gogole.tokenizer.simple_tokenizer import SimpleTokenizer
from gogole.indexer import BSBIIndexer
Dos Santos David's avatar
Dos Santos David committed
8

Dos Santos David's avatar
Dos Santos David committed
9

Dos Santos David's avatar
Dos Santos David committed
10 11
def build_cli_analyze_parser(root_parser):
    args_parser_analyze = root_parser.add_parser(
Dos Santos David's avatar
Dos Santos David committed
12 13 14 15
        'analyze',
        description="Run an analyze like tokens count or find heap's law parameters",
        help="additional help for analyze",
    )
Dos Santos David's avatar
Dos Santos David committed
16 17

    args_parser_analyze.add_argument("analyze_command",
Dos Santos David's avatar
Dos Santos David committed
18
        nargs='*',
Dos Santos David's avatar
Dos Santos David committed
19 20 21
        choices=commands.analyze_command.COMMANDS,
        metavar="command",
        help="can be any of {}".format(", ".join(commands.analyze_command.COMMANDS))
Dos Santos David's avatar
Dos Santos David committed
22
    )
Dos Santos David's avatar
Dos Santos David committed
23 24


Dos Santos David's avatar
Dos Santos David committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
def build_cli_index_parser(root_parser):

    index_parser = root_parser.add_parser('index', description="run commands on the index", aliases=['i'])
    index_subparser = index_parser.add_subparsers(dest="index_command")
    lookup_parser = index_subparser.add_parser('lookup', help="find the documents where a token is")

    lookup_parser.add_argument('token', nargs=1)

    build_parser = index_subparser.add_parser('build', help="build the index")


def build_cli_parser():
     # cli parser
    cli_parser = argparse.ArgumentParser(prog="", add_help=False)
    cli_subparser = cli_parser.add_subparsers(dest="main_command")
Dos Santos David's avatar
Dos Santos David committed
40

Dos Santos David's avatar
Dos Santos David committed
41 42
    build_cli_analyze_parser(cli_subparser)
    build_cli_index_parser(cli_subparser)
Dos Santos David's avatar
Dos Santos David committed
43

Dos Santos David's avatar
Dos Santos David committed
44 45 46 47
    return cli_parser

def main():
    print('not supported yet')
Dos Santos David's avatar
Dos Santos David committed
48 49 50


if __name__ == "__main__":
Dos Santos David's avatar
Dos Santos David committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    # top-level parser
    # mainly collection information and stop-words
    main_parser = argparse.ArgumentParser(prog="gogole")

    main_parser.add_argument(
        "-c", "--collection",
        help="collection to use")

    main_args = main_parser.parse_args()
    parser_cls = COLLECTIONS[main_args.collection]

    print("Welcome to Gogole !")
    cli_parser = build_cli_parser()


    parser = parser_cls()
    tokenizer = SimpleTokenizer()
    indexer = BSBIIndexer()

    while True:
        raw_input = input("google > ")
        try:
            args = cli_parser.parse_args(raw_input.split(' '))

            commands.MAIN_COMMANDS_MAP[args.main_command](parser, tokenizer, indexer, args)

        except SystemExit:
            pass