From 808ba603c54889b7e37840a150e069e07ed3e0e8 Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Wed, 27 Jan 2021 23:06:18 +0200 Subject: [PATCH] =?UTF-8?q?Fixed=20an=20issue=20when=20"pio=20device=20mon?= =?UTF-8?q?itor=20=E2=80=93eol"=20and=20=E2=80=9Csend=5Fon=5Fenter?= =?UTF-8?q?=E2=80=9D=20filter=20do=20not=20work=20properly=20//=20Resolve?= =?UTF-8?q?=20#3787?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HISTORY.rst | 1 + platformio/commands/device/command.py | 8 ++++---- platformio/commands/device/filters/base.py | 7 ++++--- platformio/commands/device/filters/send_on_enter.py | 11 +++++++++-- platformio/commands/device/helpers.py | 8 ++++---- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index f2a5bc61..2c24371a 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -45,6 +45,7 @@ PlatformIO Core 5 - Improved listing of `multicast DNS services `_ - Fixed a "UnicodeDecodeError: 'utf-8' codec can't decode byte" when using J-Link for firmware uploading on Linux (`issue #3804 `_) - Fixed an issue with a compiler driver for ".ccls" language server (`issue #3808 `_) + - Fixed an issue when `pio device monitor --eol `__ and "send_on_enter" filter do not work properly (`issue #3787 `_) 5.0.4 (2020-12-30) ~~~~~~~~~~~~~~~~~~ diff --git a/platformio/commands/device/command.py b/platformio/commands/device/command.py index 2e254742..fd385a46 100644 --- a/platformio/commands/device/command.py +++ b/platformio/commands/device/command.py @@ -179,7 +179,9 @@ def device_monitor(**kwargs): # pylint: disable=too-many-branches for name in os.listdir(filters_dir): if not name.endswith(".py"): continue - device_helpers.load_monitor_filter(os.path.join(filters_dir, name)) + device_helpers.load_monitor_filter( + os.path.join(filters_dir, name), options=kwargs + ) project_options = {} try: @@ -193,9 +195,7 @@ def device_monitor(**kwargs): # pylint: disable=too-many-branches if "platform" in project_options: with fs.cd(kwargs["project_dir"]): platform = PlatformFactory.new(project_options["platform"]) - device_helpers.register_platform_filters( - platform, kwargs["project_dir"], kwargs["environment"] - ) + device_helpers.register_platform_filters(platform, options=kwargs) if not kwargs["port"]: ports = util.get_serial_ports(filter_hwid=True) diff --git a/platformio/commands/device/filters/base.py b/platformio/commands/device/filters/base.py index 5c6d0400..6745a626 100644 --- a/platformio/commands/device/filters/base.py +++ b/platformio/commands/device/filters/base.py @@ -18,12 +18,13 @@ from platformio.project.config import ProjectConfig class DeviceMonitorFilter(miniterm.Transform): - def __init__(self, project_dir=None, environment=None): + def __init__(self, options=None): """ Called by PlatformIO to pass context """ miniterm.Transform.__init__(self) - self.project_dir = project_dir - self.environment = environment + self.options = options or {} + self.project_dir = self.options.get("project_dir") + self.environment = self.options.get("environment") self.config = ProjectConfig.get_instance() if not self.environment: diff --git a/platformio/commands/device/filters/send_on_enter.py b/platformio/commands/device/filters/send_on_enter.py index 10ca2103..8300c980 100644 --- a/platformio/commands/device/filters/send_on_enter.py +++ b/platformio/commands/device/filters/send_on_enter.py @@ -22,10 +22,17 @@ class SendOnEnter(DeviceMonitorFilter): super(SendOnEnter, self).__init__(*args, **kwargs) self._buffer = "" + if self.options.get("eol") == "CR": + self._eol = "\r" + elif self.options.get("eol") == "LF": + self._eol = "\n" + else: + self._eol = "\r\n" + def tx(self, text): self._buffer += text - if self._buffer.endswith("\r\n"): - text = self._buffer[:-2] + if self._buffer.endswith(self._eol): + text = self._buffer[: len(self._eol) * -1] self._buffer = "" return text return "" diff --git a/platformio/commands/device/helpers.py b/platformio/commands/device/helpers.py index 3bfe8fc6..a65b4895 100644 --- a/platformio/commands/device/helpers.py +++ b/platformio/commands/device/helpers.py @@ -76,7 +76,7 @@ def get_board_hwids(project_dir, platform, board): return platform.board_config(board).get("build.hwids", []) -def load_monitor_filter(path, project_dir=None, environment=None): +def load_monitor_filter(path, options=None): name = os.path.basename(path) name = name[: name.find(".")] module = load_python_module("platformio.commands.device.filters.%s" % name, path) @@ -87,12 +87,12 @@ def load_monitor_filter(path, project_dir=None, environment=None): or cls == DeviceMonitorFilter ): continue - obj = cls(project_dir, environment) + obj = cls(options) miniterm.TRANSFORMATIONS[obj.NAME] = obj return True -def register_platform_filters(platform, project_dir, environment): +def register_platform_filters(platform, options=None): monitor_dir = os.path.join(platform.get_dir(), "monitor") if not os.path.isdir(monitor_dir): return @@ -103,4 +103,4 @@ def register_platform_filters(platform, project_dir, environment): path = os.path.join(monitor_dir, name) if not os.path.isfile(path): continue - load_monitor_filter(path, project_dir, environment) + load_monitor_filter(path, options)