def __init__(self, plover_engine: IPlover.Engine) -> None: """ Main entry point for Spectra's Plover plugin. The Plover engine is our only argument. Command-line arguments are not used (sys.argv belongs to Plover), and translations are not read from files. We must create the main application object *and* start it before __init__ returns. The extension is added as attribute 'plover' solely to make it visible to the debug tools. """ spectra = Spectra(parse_args=False) spectra.translations_paths = () self._app = app = build_app(spectra) self._ext = app.plover = PloverExtension( EngineWrapper(plover_engine), stroke_limit=self.STROKE_LIMIT) app.async_run(self._start) app.start()
def main() -> int: """ Analyze translations files and create an examples index from them. Time the execution. """ opts = SpectraOptions("Batch script for creating an examples index.") opts.add( "size", Fcls.SIZE_DEFAULT, f"Relative size of generated index ({Fcls.SIZE_MINIMUM}-{Fcls.SIZE_MAXIMUM})." ) opts.add( "processes", 0, "Number of processes used for parallel execution (0 = one per CPU core)." ) spectra = Spectra(opts) log = spectra.logger.log log("Compiling examples index...") start_time = time() io = spectra.resource_io analyzer = spectra.analyzer files_in = spectra.translations_paths file_out = spectra.index_path translations = io.load_json_translations(*files_in) pairs = Fcls(opts.size).filter(translations.items()) examples = analyzer.compile_index(pairs, process_count=opts.processes) io.save_json_examples(file_out, examples) total_time = time() - start_time log(f"Index complete in {total_time:.1f} seconds.") return 0
def main() -> int: """ In standalone mode, we must create a QApplication and run a GUI event loop indefinitely. """ q_app = QApplication(sys.argv) opts = SpectraOptions("Run Spectra as a standalone GUI application.") spectra = Spectra(opts) app = build_app(spectra) app.start() return q_app.exec_()
def main() -> int: """ In standalone mode, we must create a QApplication and run a GUI event loop indefinitely. """ q_app = QApplication(sys.argv) opts = SpectraOptions("Run Spectra as a standalone GUI application.") spectra = Spectra(opts) try: app = build_app(spectra) app.start() except Exception as e: sys.excepthook(type(e), e, e.__traceback__) return q_app.exec_()
def main() -> int: """ Run the application as a Discord bot. """ opts = SpectraOptions("Run Spectra as a Discord bot.") opts.add("token", "", "Discord bot token (REQUIRED).") opts.add("command", "spectra", "!command string for Discord users.") spectra = Spectra(opts) log = spectra.logger.log log("Loading Discord bot...") app = build_app(spectra) if not opts.token: log("No token given. Opening test console...") return app.run_console() bot = DiscordBot(opts.token, log) bot.add_command(opts.command, app.query) log("Discord bot started.") return bot.run()
def main() -> int: """ Build the server, start it, and poll for connections indefinitely. """ opts = SpectraOptions("Run Spectra as an HTTP web server.") opts.add("http-addr", "", "IP address or hostname for server.") opts.add("http-port", 80, "TCP port to listen for connections.") opts.add("http-dir", HTTP_PUBLIC_DEFAULT, "Root directory for public HTTP file service.") spectra = Spectra(opts) log = spectra.logger.log log("Loading HTTP server...") app = build_app(spectra) dispatcher = build_dispatcher(app, opts.http_dir, log) server = ThreadedTCPServer(dispatcher) log("Server started.") try: server.start(opts.http_addr, opts.http_port) finally: server.shutdown() log("Server stopped.") return 0
def main() -> int: """ Run the application as a Discord bot. """ opts = SpectraOptions("Run Spectra as a Discord bot.") opts.add("token", "", "Discord bot token (REQUIRED).") opts.add("command", "spectra", "!command string for Discord users.") opts.add("cache_id", "", "Optional channel ID for image cache.") spectra = Spectra(opts) log.setHandler(spectra.logger.log) log.info("Loading Discord bot...") app = build_app(spectra, 400, 300, max_chars=100) token = opts.token.strip() if not token: log.info("No token given. Opening test console...") return introspect(app) http = HTTPClient(token) bot = DiscordBot(app, http, opts.cache_id) cmds = CommandDispatcher() cmds.add_command(opts.command, bot) client = Client(http, token) client.add_event_handler(cmds) client.add_event_handler(bot) log.info("Discord bot started.") client.start() return 0
""" Main feature tests for the Spectra steno lexer. Tests translation search, lexical analysis, and graphical rendering. """ import re import pytest from spectra_lexer import Spectra from . import TEST_TRANSLATIONS _spectra = Spectra() SEARCH_ENGINE = _spectra.search_engine ANALYZER = _spectra.analyzer BOARD_ENGINE = _spectra.board_engine GRAPH_ENGINE = _spectra.graph_engine del _spectra SEARCH_ENGINE.set_translations(TEST_TRANSLATIONS) TEST_TRANSLATION_PAIRS = list(TEST_TRANSLATIONS.items()) @pytest.mark.parametrize("keys, letters", TEST_TRANSLATION_PAIRS) def test_search(keys, letters) -> None: """ Go through each loaded test translation and check all search methods. """ search = SEARCH_ENGINE.search assert search(keys, count=2, mode_strokes=True) == {keys: (letters, )} assert search(letters, count=2) == {letters: (keys, )} assert keys in search(re.escape(keys), count=2, mode_strokes=True, mode_regex=True)
def _spectra(): from spectra_lexer import Spectra return Spectra()
def main() -> int: """ Load basic resources and run an interactive read-eval-print loop in a new console. """ opts = SpectraOptions("Run Spectra from scratch in an interactive Python console.") spectra = Spectra(opts) spectra.logger.log("Loading console...") return introspect(spectra, include_private=False)