Module: cli

The CLI provides command-line access to Mastoscore functionality. For a web-based interface, see Backstage. This is largely obviated by the backstage graphical interface. The reason I broke it into modules was to carefully, step-by-step go through the process. The web GUI does that now.

Subcommands

Usage

mastoscore [--debug LEVEL] CONFIG_FILE ACTION

Arguments:

Options:

Debugging

The main thing that the CLI module does is handle the --debug (or -d) option 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
 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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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=str,
        help="INI config file with config plus credentials.",
    )
    parser.add_argument(
        "action",
        type=str,
        default=None,
        help="Action to take: fetch, analyse, post, graph, wordcloud, postgraphs.",
    )
    args = parser.parse_args()

    logger = logging.getLogger(__name__)
    logging.basicConfig(
        format="%(asctime)s %(levelname)-8s %(message)s",
        level=logging.ERROR,
        datefmt="%H:%M:%S",
    )

    args.debug = args.debug.upper()
    log_level = logging.getLevelName(args.debug)
    # If level_name is a string recognized by logging, you get an int
    # else, you get a string
    if type(log_level).__name__ == "int":
        logger.setLevel(log_level)
        logger.debug("Logging activated")
    else:
        logger.setLevel(logging.ERROR)
        logger.error(f"'{args.debug}' is not a valid log level. Logging set to ERROR")

    try:
        config = read_config(args.config)
        if config is None:
            exit(2)
    except FileNotFoundError:
        logger.error(f"Config file not found: {args.config}")
        exit(2)
    except PermissionError:
        logger.error(f"Permission denied reading config file: {args.config}")
        exit(2)
    except Exception as e:
        logger.error(f"Error reading config file: {e}")
        exit(2)
    config.set(config.default_section, "debug", str(logger.level))
    match args.action:
        case "blog":
            blog(config)
        case "fetch":
            fetch(config)
        case "analyse":
            analyse(config)
        case "post":
            post(config)
        case "graph":
            graph(config)
        case "graphs":
            graph(config)
            write_wordcloud(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)