Skip to content

Commit

Permalink
pw_console: Unify widget imports
Browse files Browse the repository at this point in the history
- Re-tool Pygments code theme loading to not require entry points.
- Set constants for pw_console module paths for importlib

Change-Id: Ie1c098e00e824a4532fbcd9b7023105a9fb51b45
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/122332
Reviewed-by: Keir Mierle <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
  • Loading branch information
AnthonyDiGirolamo authored and CQ Bot Account committed Dec 3, 2022
1 parent 7818d3b commit b754605
Show file tree
Hide file tree
Showing 30 changed files with 393 additions and 332 deletions.
9 changes: 4 additions & 5 deletions pw_cli/py/pw_cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
from pathlib import Path
from typing import NamedTuple, Optional, Union, Iterator

import pw_cli.color
import pw_cli.env
import pw_cli.plugins
from pw_cli.color import colors as pw_cli_colors
from pw_cli.env import pigweed_environment

# Log level used for captured output of a subprocess run through pw.
LOGLEVEL_STDOUT = 21
Expand Down Expand Up @@ -126,9 +125,9 @@ def install(
if not logger:
logger = logging.getLogger()

colors = pw_cli.color.colors(use_color)
colors = pw_cli_colors(use_color)

env = pw_cli.env.pigweed_environment()
env = pigweed_environment()
if env.PW_SUBPROCESS or hide_timestamp:
# If the logger is being run in the context of a pw subprocess, the
# time and date are omitted (since pw_cli.process will provide them).
Expand Down
22 changes: 21 additions & 1 deletion pw_console/py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_python//python:defs.bzl", "py_library", "py_test")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")

package(default_visibility = ["//visibility:public"])

Expand Down Expand Up @@ -85,6 +85,15 @@ py_library(
"pw_console/window_list.py",
"pw_console/window_manager.py",
],
data = [
"pw_console/docs/user_guide.rst",
"pw_console/html/index.html",
"pw_console/html/main.js",
"pw_console/html/style.css",
"pw_console/py.typed",
"pw_console/templates/keybind_list.jinja",
"pw_console/templates/repl_output.jinja",
],
imports = ["."],
deps = [
":event_count_history",
Expand All @@ -94,6 +103,17 @@ py_library(
],
)

py_binary(
name = "pw_console_test_mode",
srcs = [
"pw_console/__main__.py",
],
main = "pw_console/__main__.py",
deps = [
":pw_console",
],
)

py_test(
name = "command_runner_test",
size = "small",
Expand Down
25 changes: 24 additions & 1 deletion pw_console/py/command_runner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
flatten_formatted_text_tuples,
join_adjacent_style_tuples,
)
from window_manager_test import target_list_and_pane, window_pane_titles


def _create_console_app(log_pane_count=2):
Expand All @@ -56,6 +55,30 @@ def _create_console_app(log_pane_count=2):
return console_app


def window_pane_titles(window_manager):
return [
[
pane.pane_title() + ' - ' + pane.pane_subtitle()
for pane in window_list.active_panes
]
for window_list in window_manager.window_lists
]


def target_list_and_pane(window_manager, list_index, pane_index):
# pylint: disable=protected-access
# Bypass prompt_toolkit has_focus()
pane = window_manager.window_lists[list_index].active_panes[pane_index]
# If the pane is in focus it will be visible.
pane.show_pane = True
window_manager._get_active_window_list_and_pane = MagicMock( # type: ignore
return_value=(
window_manager.window_lists[list_index],
window_manager.window_lists[list_index].active_panes[pane_index],
)
)


class TestCommandRunner(unittest.TestCase):
"""Tests for CommandRunner."""

Expand Down
5 changes: 4 additions & 1 deletion pw_console/py/help_window_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@

from pw_console.help_window import HelpWindow

_PW_CONSOLE_MODULE = 'pw_console'


_jinja_env = Environment(
loader=PackageLoader('pw_console'),
loader=PackageLoader(_PW_CONSOLE_MODULE),
undefined=make_logging_undefined(logger=logging.getLogger('pw_console')),
trim_blocks=True,
lstrip_blocks=True,
Expand Down
29 changes: 11 additions & 18 deletions pw_console/py/pw_console/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,21 @@
import sys
from typing import Optional, Dict

import pw_cli.log
import pw_cli.argument_types
from pw_cli import log as pw_cli_log
from pw_cli import argument_types

import pw_console
import pw_console.python_logging
import pw_console.test_mode
from pw_console import PwConsoleEmbed
from pw_console.python_logging import create_temp_log_file
from pw_console.log_store import LogStore
from pw_console.plugins.calc_pane import CalcPane
from pw_console.plugins.clock_pane import ClockPane
from pw_console.plugins.twenty48_pane import Twenty48Pane
from pw_console.test_mode import FAKE_DEVICE_LOGGER_NAME

_LOG = logging.getLogger(__package__)
_ROOT_LOG = logging.getLogger('')


# TODO(tonymd): Remove this when no downstream projects are using it.
def create_temp_log_file():
return pw_console.python_logging.create_temp_log_file()


def _build_argument_parser() -> argparse.ArgumentParser:
"""Setup argparse."""
parser = argparse.ArgumentParser(
Expand All @@ -49,7 +44,7 @@ def _build_argument_parser() -> argparse.ArgumentParser:
parser.add_argument(
'-l',
'--loglevel',
type=pw_cli.argument_types.log_level,
type=argument_types.log_level,
default=logging.DEBUG,
help='Set the log level' '(debug, info, warning, error, critical)',
)
Expand Down Expand Up @@ -83,17 +78,17 @@ def main() -> int:
if not args.logfile:
# Create a temp logfile to prevent logs from appearing over stdout. This
# would corrupt the prompt toolkit UI.
args.logfile = pw_console.python_logging.create_temp_log_file()
args.logfile = create_temp_log_file()

pw_cli.log.install(
pw_cli_log.install(
level=args.loglevel,
use_color=True,
hide_timestamp=False,
log_file=args.logfile,
)

if args.console_debug_log_file:
pw_cli.log.install(
pw_cli_log.install(
level=logging.DEBUG,
use_color=True,
hide_timestamp=False,
Expand All @@ -108,9 +103,7 @@ def main() -> int:
_ROOT_LOG.addHandler(root_log_store)
_ROOT_LOG.debug('pw_console test-mode starting...')

fake_logger = logging.getLogger(
pw_console.test_mode.FAKE_DEVICE_LOGGER_NAME
)
fake_logger = logging.getLogger(FAKE_DEVICE_LOGGER_NAME)
default_loggers = {
# Don't include pw_console package logs (_LOG) in the log pane UI.
# Add the fake logger for test_mode.
Expand All @@ -137,7 +130,7 @@ def main() -> int:
"""
)

console = pw_console.PwConsoleEmbed(
console = PwConsoleEmbed(
global_vars=global_vars,
loggers=default_loggers,
test_mode=args.test_mode,
Expand Down
26 changes: 12 additions & 14 deletions pw_console/py/pw_console/command_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@
from prompt_toolkit.widgets import MenuItem
from prompt_toolkit.widgets import TextArea

import pw_console.widgets.border
import pw_console.widgets.checkbox
import pw_console.widgets.mouse_handlers
from pw_console.widgets import (
create_border,
mouse_handlers,
to_keybind_indicator,
)

if TYPE_CHECKING:
from pw_console.console_app import ConsoleApp
Expand Down Expand Up @@ -162,7 +164,7 @@ def __init__(
'class:command-runner-setting',
'> ',
functools.partial(
pw_console.widgets.mouse_handlers.on_click,
mouse_handlers.on_click,
self.focus_self,
),
)
Expand Down Expand Up @@ -237,7 +239,7 @@ def __init__(
def _create_bordered_content(self) -> None:
"""Wrap self.command_runner_content in a border."""
# This should be called whenever the window_title changes.
self.bordered_content = pw_console.widgets.border.create_border(
self.bordered_content = create_border(
self.command_runner_content,
title=self.window_title,
border_style='class:command-runner-border',
Expand Down Expand Up @@ -420,14 +422,10 @@ def _previous_item(self) -> None:

def _get_input_field_button_fragments(self) -> StyleAndTextTuples:
# Mouse handlers
focus = functools.partial(
pw_console.widgets.mouse_handlers.on_click, self.focus_self
)
cancel = functools.partial(
pw_console.widgets.mouse_handlers.on_click, self.close_dialog
)
focus = functools.partial(mouse_handlers.on_click, self.focus_self)
cancel = functools.partial(mouse_handlers.on_click, self.close_dialog)
select_item = functools.partial(
pw_console.widgets.mouse_handlers.on_click, self._run_selected_item
mouse_handlers.on_click, self._run_selected_item
)

separator_text = ('', ' ', focus)
Expand All @@ -439,7 +437,7 @@ def _get_input_field_button_fragments(self) -> StyleAndTextTuples:

# Cancel button
fragments.extend(
pw_console.widgets.checkbox.to_keybind_indicator(
to_keybind_indicator(
key='Ctrl-c',
description='Cancel',
mouse_handler=cancel,
Expand All @@ -450,7 +448,7 @@ def _get_input_field_button_fragments(self) -> StyleAndTextTuples:

# Run button
fragments.extend(
pw_console.widgets.checkbox.to_keybind_indicator(
to_keybind_indicator(
'Enter', 'Run', select_item, base_style=button_style
)
)
Expand Down
Loading

0 comments on commit b754605

Please sign in to comment.