Module: cli

The CLI is basically documented everywhere else. It takes 5 subcommands: * fetch: Connect to a bunch of servers and fetch toots * analyse: Read the files we stored from fetch and analyse them * post: Post the results on Mastodon * graph: Draw a histogram diagram * wordcloud: Draw a word cloud diagram

Debugging

The main thing that the CLI module does is handle command line arguments and set the debug level. I intended debug to be a config option in the config module. That way you could turn up or turn down the verbosity on different phases. I didn't implement it that way. That's on the TODO list. Instead you have to give the --debug (or -d) option on the command line and it will apply everywhere.

Code Reference

cli.py: Command line module for calling mastoscore to do its work

main()

Parse command line arguments, figure out what we're doing. Do it.

Source code in mastoscore/cli.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
79
80
81
82
83
84
85
86
87
def main():
    """ 
    Parse command line arguments, figure out what we're doing. Do it.
    """
    args = None
    logger = None
    valid_actions = ['fetch', 'analyse', 'post', 'graph', 'wordcloud', 'postgraphs']
    parser = argparse.ArgumentParser(
        description='Fetch recent toots on a hashtag, score them. Optionally post the analysis')
    parser.add_argument('-d', '--debug', type=str, required=False,
                        default='WARN',  help='Enable debugging messages. Logger levels like DEBUG, INFO, WARN, ERROR. Default: WARN')
    parser.add_argument('config', type=argparse.FileType(mode='r'), nargs='?',
                        help='INI config file with config plus credentials.')
    parser.add_argument('action', type=str, nargs='?', default=None,
                        help='Action to take: fetch, analyse, post, graph, wordcloud, postgraphs.')
    args = parser.parse_args()

    logger = logging.getLogger(__name__)
    logging.basicConfig(format='%(levelname)s\t%(message)s')

    args.debug = args.debug.upper()
    # Don't emit logger activation messages on levels less than INFO
    if (args.debug == 'DEBUG'):
        logger.setLevel(logging.DEBUG)
        logger.debug("Debug logging activated")
    elif (args.debug == 'INFO'):
        logger.setLevel(logging.INFO)
        logger.info("Info logging activated")
    elif (args.debug == 'ERROR'):
        logger.setLevel(logging.ERROR)
    elif (args.debug == 'WARN'):
        logger.setLevel(logging.WARN)
    elif (args.debug == 'CRITICAL'):
        logger.setLevel(logging.CRITICAL)
    else:
        logger.error(f"'{args.debug}' is not a valid log level.")
        exit(1)

    config = read_config(args.config)
    if config is None:
        exit(2)
    config.set(config.default_section, 'debug', str(logger.level))
    match args.action:
        case "fetch":
            fetch(config)
        case "analyse":
            from pprint import pp
            analysis = analyse(config)

            pp(analysis)
        case "post":
            results = analyse(config)
            post(config, results)
        case "graph":
            graph(config)
        case "wordcloud":
            write_wordcloud(config)
        case "postgraphs":
            post_graphs(config)
        case _:
            parser.print_help()
            logger.error(
                f"'{args.action}' is not a valid action. Must be one of: {valid_actions}")
            exit(1)
    exit(0)