openrvdas.server.lmcmd

A command line tool for interacting with the LoggerManager and database. If called with no arguments other than the optional database specification, it starts an iterative command line interface. If arguments are present, it treats them as a an OpenRVDAS command line command, executes it, them, then exits.

Within the iterative command line interface, you can type 'help' to see available commands, and use arrow keys to navigate command history.

Args: --database Either 'django' or 'sqlite'; which database to connect to for OpenRVDAS state. If not specified, defaults to 'django'. -v If specified, increase verbosity of diagnostics from 'warning' to 'info'. If repeated, increase to 'debug'.

Usage: server/lmcmd.py Start a command line reader that reads/writes the default Django database used by OpenRVDAS.

server/lmcmd.py help
    Print available interface commands and exit.

server/lcmd.py get_active_mode
    Connect to default (django) database and run the 'get_active_mode'
    command.
 1#! /usr/bin/env python3
 2"""
 3A command line tool for interacting with the LoggerManager and database. If called
 4with no arguments other than the optional database specification, it starts an
 5iterative command line interface. If arguments are present, it treats them as a
 6an OpenRVDAS command line command, executes it, them, then exits.
 7
 8Within the iterative command line interface, you can type 'help' to see
 9available commands, and use arrow keys to navigate command history.
10
11Args:
12    --database
13        Either 'django' or 'sqlite'; which database to connect to for OpenRVDAS
14        state. If not specified, defaults to 'django'.
15    -v
16        If specified, increase verbosity of diagnostics from 'warning'
17        to 'info'. If repeated, increase to 'debug'.
18
19Usage:
20    server/lmcmd.py
21        Start a command line reader that reads/writes the default Django
22        database used by OpenRVDAS.
23
24    server/lmcmd.py help
25        Print available interface commands and exit.
26
27    server/lcmd.py get_active_mode
28        Connect to default (django) database and run the 'get_active_mode'
29        command.
30"""
31import argparse
32import atexit
33import logging
34import os
35import readline
36from logger.utils.stderr_logging import DEFAULT_LOGGING_FORMAT   # noqa: E402
37from server.server_api_command_line import ServerAPICommandLine  # noqa: E402
38
39################################################################################
40if __name__ == '__main__':  # noqa: C901
41    parser = argparse.ArgumentParser()
42    parser.add_argument('--database', dest='database', action='store',
43                        choices=['django', 'sqlite'],
44                        default='django', help='Which backing store database '
45                        'to connect to')
46
47    parser.add_argument('-v', '--verbosity', dest='verbosity',
48                        default=0, action='count',
49                        help='Increase output verbosity')
50
51    # Everything else on the command line
52    parser.add_argument('remainder', nargs='*')
53    args = parser.parse_args()
54
55    ############################
56    # Set up logging first
57    LOG_LEVELS = {0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}
58    log_level = LOG_LEVELS[min(args.verbosity, max(LOG_LEVELS))]
59    logging.basicConfig(format=DEFAULT_LOGGING_FORMAT, level=log_level)
60    parser = argparse.ArgumentParser()
61
62    ############################
63    # Instantiate API - which database is our backing store?
64    if args.database == 'django':
65        from django_gui.django_server_api import DjangoServerAPI
66        api = DjangoServerAPI()
67    elif args.database == 'sqlite':
68        from server.sqlite_server_api import SQLiteServerAPI
69        api = SQLiteServerAPI()
70    else:
71        raise ValueError('Illegal arg for --database: "%s"' % args.database)
72
73    ############################
74    # Set up command line interface to get commands. Start by
75    # reading history file, if one exists, to get previous commands.
76    hist_filename = '.openrvdas_logger_manager_history'
77    hist_path = os.path.join(os.path.expanduser('~'), hist_filename)
78    try:
79        readline.read_history_file(hist_path)
80        # default history len is -1 (infinite), which may grow unruly
81        readline.set_history_length(1000)
82    except (FileNotFoundError, PermissionError):
83        pass
84    atexit.register(readline.write_history_file, hist_path)
85
86    command_line_reader = ServerAPICommandLine(api=api)
87    command = ' '.join(args.remainder)
88
89    ############################
90    # Did they give us any additional args on command line? If so
91    # execute them and exit. Otherwise fire up iterative reading
92    # of commands.
93    if command:
94        command_line_reader.process_command(command)
95        readline.add_history(command)
96    else:
97        command_line_reader.run()