Source code for uppaal2jetracer.cli

"""Command line interface

This file contains all functionality required to run uppaal2jetracer in the command line.

The CLI accepts the following commands:

    * help:     Get list of available commands.
    * parse:    Parse an uppaal system to an executable model.
    * prj:      Access projects which contain versions of parsed uppaal systems.
    * quit:     Quit uppaal2jetracer.
    * run:      Run a parsed uppaal system.
    * ver:      Access saved versions of parsed uppaal systems.

This file can be imported as a module and contains the following classes:

    * Uppaal2JetRacer:  Starts uppaal2jetracer.
    * CLI:              Handles CLI input.
"""

import logging
import os
import readline
from typing import List

from uppaal2jetracer.command_system import CommandHandler, CommandResultType
from uppaal2jetracer.versioncontrol.database import DatabaseConnection
from uppaal2jetracer.versioncontrol.versioncontrol import VersionManager, ProjectManager


[docs] class Uppaal2JetRacer: """ The entry class for uppaal2jetracer. """
[docs] @staticmethod def main(): """ Start uppaal2jetracer. """ db: DatabaseConnection = DatabaseConnection("data" + os.sep + "database") cli: CLI = CLI(VersionManager(db), ProjectManager(db)) logging.getLogger("user_command").info("Initialized CLI handler.") cli.handle_input()
[docs] class CLI(CommandHandler): """ A CommandHandler that handles CLI commands. """ _HELP_MESSAGE = """uppaal2jetracer has the following commands: * parse: Parse an uppaal system to an executable model. * pnr: Parse and run an uppaal system. * prj: Access projects which contain versions of parsed uppaal systems. * quit: Quit uppaal2jetracer. * run: Run a parsed uppaal system. * ver: Access saved versions of parsed uppaal systems.""" _INPUT_PROMPT = "u2j >>> " _HELP_COMMAND = "help" _HELP_OPTION = "-h" _HELP_OPTION_ALT = "--help" _DEBUG_OPTION = "-d" _DEBUG_OPTION_ALT = "--debug" _ERROR_PREFIX = "Error: " _ERROR_NOT_FOUND = "Could not find command '{}'."
[docs] def handle_input(self): """ Handles command line input from a user. """ try: while self._running: self._execute_command(input(self._INPUT_PROMPT)) except KeyboardInterrupt: self.quit()
def _execute_command(self, command: str): split_command = command.split(" ") command: str = split_command[0] args: List[str] = split_command[1:] if command == self._HELP_COMMAND: print(self._HELP_MESSAGE) elif command not in self._commands: print(self._ERROR_PREFIX + self._ERROR_NOT_FOUND.format(command)) elif self._HELP_OPTION in args or self._HELP_OPTION_ALT in args: print(self._commands[command].help_message) else: if self._DEBUG_OPTION in args or self._DEBUG_OPTION_ALT in args: while self._DEBUG_OPTION in args: args.remove(self._DEBUG_OPTION) while self._DEBUG_OPTION_ALT in args: args.remove(self._DEBUG_OPTION_ALT) CommandHandler.activate_debug(self._commands[command].loggers) result = self._commands[command].execute(args) match result.result_type: case CommandResultType.SUCCESS: print(result.message) case CommandResultType.FAILURE: print(self._ERROR_PREFIX + result.message) self.deactivate_debug()
if __name__ == "__main__": Uppaal2JetRacer.main()