Skip to content

Commit

Permalink
pw_console: Remove dependence on __file__
Browse files Browse the repository at this point in the history
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 <tonymd@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
  • Loading branch information
AnthonyDiGirolamo authored and CQ Bot Account committed Jul 16, 2022
1 parent b44a710 commit bb0462f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 10 deletions.
3 changes: 3 additions & 0 deletions pw_console/py/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
]
Expand Down
4 changes: 3 additions & 1 deletion pw_console/py/pw_console/console_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import asyncio
import builtins
import functools
import importlib.resources
import logging
import os
from pathlib import Path
Expand Down Expand Up @@ -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__), ),
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion pw_console/py/pw_console/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
14 changes: 7 additions & 7 deletions pw_console/py/pw_console/help_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion pw_console/py/pw_console/log_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Empty file.

0 comments on commit bb0462f

Please sign in to comment.