From bb0462fbc53cc31c5e449a606e2ada4c5d070984 Mon Sep 17 00:00:00 2001 From: Anthony DiGirolamo Date: Sat, 16 Jul 2022 00:18:29 +0000 Subject: [PATCH] pw_console: Remove dependence on __file__ This is a cleanup CL to switch all __file__ references to use the more modern importlib.resources API: https://docs.python.org/3/library/importlib.html#module-importlib.resources Change-Id: I2f993a9e5c23c12b4a5d0b37161c55f3e010882e Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/102380 Pigweed-Auto-Submit: Anthony DiGirolamo Reviewed-by: Wyatt Hepler Commit-Queue: Auto-Submit --- pw_console/py/BUILD.gn | 3 +++ pw_console/py/pw_console/console_app.py | 4 +++- pw_console/py/pw_console/docs/__init__.py | 0 pw_console/py/pw_console/embed.py | 2 +- pw_console/py/pw_console/help_window.py | 14 +++++++------- pw_console/py/pw_console/log_store.py | 2 +- pw_console/py/pw_console/templates/__init__.py | 0 7 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 pw_console/py/pw_console/docs/__init__.py create mode 100644 pw_console/py/pw_console/templates/__init__.py diff --git a/pw_console/py/BUILD.gn b/pw_console/py/BUILD.gn index 088d770214..fe1f7a8eda 100644 --- a/pw_console/py/BUILD.gn +++ b/pw_console/py/BUILD.gn @@ -28,6 +28,7 @@ pw_python_package("py") { "pw_console/command_runner.py", "pw_console/console_app.py", "pw_console/console_prefs.py", + "pw_console/docs/__init__.py", "pw_console/embed.py", "pw_console/filter_toolbar.py", "pw_console/get_pw_console_app.py", @@ -61,6 +62,7 @@ pw_python_package("py") { "pw_console/repl_pane.py", "pw_console/search_toolbar.py", "pw_console/style.py", + "pw_console/templates/__init__.py", "pw_console/text_formatting.py", "pw_console/widgets/__init__.py", "pw_console/widgets/border.py", @@ -92,6 +94,7 @@ pw_python_package("py") { "$dir_pw_log_tokenized/py", ] inputs = [ + "pw_console/docs/user_guide.rst", "pw_console/templates/keybind_list.jinja", "pw_console/templates/repl_output.jinja", ] diff --git a/pw_console/py/pw_console/console_app.py b/pw_console/py/pw_console/console_app.py index 80511487ca..862bb9d8de 100644 --- a/pw_console/py/pw_console/console_app.py +++ b/pw_console/py/pw_console/console_app.py @@ -16,6 +16,7 @@ import asyncio import builtins import functools +import importlib.resources import logging import os from pathlib import Path @@ -156,7 +157,8 @@ def __init__( # Setup the Jinja environment self.jinja_env = Environment( # Load templates automatically from pw_console/templates - loader=FileSystemLoader(Path(__file__).parent / 'templates'), + loader=FileSystemLoader( + importlib.resources.files('pw_console.templates')), # Raise errors if variables are undefined in templates undefined=make_logging_undefined( logger=logging.getLogger(__package__), ), diff --git a/pw_console/py/pw_console/docs/__init__.py b/pw_console/py/pw_console/docs/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/pw_console/py/pw_console/embed.py b/pw_console/py/pw_console/embed.py index 5ef83f065e..60ef80a4c1 100644 --- a/pw_console/py/pw_console/embed.py +++ b/pw_console/py/pw_console/embed.py @@ -65,7 +65,7 @@ def __init__(self, loggers={ 'Host Logs': [ logging.getLogger(__package__), - logging.getLogger(__file__), + logging.getLogger(__name__), ], 'Device Logs': [ logging.getLogger('usb_gadget'), diff --git a/pw_console/py/pw_console/help_window.py b/pw_console/py/pw_console/help_window.py index d8ca7ad550..8993928caf 100644 --- a/pw_console/py/pw_console/help_window.py +++ b/pw_console/py/pw_console/help_window.py @@ -14,9 +14,9 @@ """Help window container class.""" import functools +import importlib.resources import inspect import logging -from pathlib import Path from typing import Dict, TYPE_CHECKING from prompt_toolkit.document import Document @@ -239,14 +239,14 @@ def content_width(self) -> int: return min(desired_width, window_manager_width) def load_user_guide(self): - rstdoc = Path(__file__).parent / 'docs/user_guide.rst' + rstdoc_text = importlib.resources.read_text('pw_console.docs', + 'user_guide.rst') max_line_length = 0 rst_text = '' - with rstdoc.open() as rstfile: - for line in rstfile.readlines(): - if 'https://' not in line and len(line) > max_line_length: - max_line_length = len(line) - rst_text += line + for line in rstdoc_text.splitlines(): + if 'https://' not in line and len(line) > max_line_length: + max_line_length = len(line) + rst_text += line + '\n' self.max_line_length = max_line_length self.help_text_area = self._create_help_text_area( diff --git a/pw_console/py/pw_console/log_store.py b/pw_console/py/pw_console/log_store.py index 06096e87ab..33c5d68b2c 100644 --- a/pw_console/py/pw_console/log_store.py +++ b/pw_console/py/pw_console/log_store.py @@ -66,7 +66,7 @@ class LogStore(logging.Handler): loggers={ 'Host Logs': [ logging.getLogger(__package__), - logging.getLogger(__file__), + logging.getLogger(__name__), ], # Set the LogStore as the value of this logger window. 'Device Logs': device_log_store, diff --git a/pw_console/py/pw_console/templates/__init__.py b/pw_console/py/pw_console/templates/__init__.py new file mode 100644 index 0000000000..e69de29bb2