Skip to content

Commit 9016947

Browse files
committed
Extract and simplify mask_command
* make it public for re-use possibilities
1 parent 231e6f9 commit 9016947

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

exec_helpers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"ExecHelperTimeoutError",
2525
"ExecHelper",
2626
"SSHClient",
27+
"mask_command",
2728
"SSHAuth",
2829
"SSHConfig",
2930
"HostsSSHConfigs",
@@ -38,6 +39,7 @@
3839
from ._ssh_helpers import HostsSSHConfigs
3940
from ._ssh_helpers import SSHConfig
4041
from .api import ExecHelper
42+
from .api import mask_command
4143
from .exceptions import CalledProcessError
4244
from .exceptions import ExecCalledProcessError
4345
from .exceptions import ExecHelperError

exec_helpers/api.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
.. versionchanged:: 1.3.5 make API public to use as interface
2020
"""
2121

22-
__all__ = ("ExecHelper", "ExecuteAsyncResult")
22+
__all__ = ("ExecHelper", "ExecuteAsyncResult", "mask_command")
2323

2424
# Standard Library
2525
import abc
@@ -86,6 +86,28 @@ def __exit__(self, exc_type: typing.Any, exc_val: typing.Any, exc_tb: typing.Any
8686
self._conn.__exit__(exc_type=exc_type, exc_val=exc_val, exc_tb=exc_tb) # type: ignore
8787

8888

89+
def mask_command(text: str, rules: str) -> str:
90+
"""Mask part of text using rules.
91+
92+
:param text: source text
93+
:param rules: regex rules to mask.
94+
:return: source with all MATCHED groups replaced by '<*masked*>'
95+
"""
96+
masked: typing.List[str] = []
97+
98+
# places to exclude
99+
prev = 0
100+
for match in re.finditer(rules, text):
101+
for idx, _ in enumerate(match.groups(), start=1):
102+
start, end = match.span(idx)
103+
masked.append(text[prev:start])
104+
masked.append("<*masked*>")
105+
prev = end
106+
masked.append(text[prev:])
107+
108+
return "".join(masked)
109+
110+
89111
class ExecHelper(metaclass=abc.ABCMeta):
90112
"""ExecHelper global API."""
91113

@@ -191,38 +213,12 @@ def _mask_command(self, cmd: str, log_mask_re: typing.Optional[str] = None) -> s
191213
.. versionadded:: 1.2.0
192214
"""
193215

194-
def mask(text: str, rules: str) -> str:
195-
"""Mask part of text using rules.
196-
197-
:param text: source text
198-
:param rules: regex rules to mask.
199-
:return: source with all MATCHED groups replaced by '<*masked*>'
200-
"""
201-
indexes: typing.List[int] = [0] # Start of the line
202-
masked: typing.List[str] = []
203-
204-
# places to exclude
205-
for match in re.finditer(rules, text):
206-
for idx, _ in enumerate(match.groups()):
207-
indexes.extend(match.span(idx + 1))
208-
indexes.append(len(text)) # End
209-
210-
# Replace inserts
211-
for idx in range(0, len(indexes) - 2, 2):
212-
start: int = indexes[idx]
213-
end: int = indexes[idx + 1]
214-
masked.append(text[start:end] + "<*masked*>")
215-
216-
# noinspection PyPep8
217-
masked.append(text[indexes[-2] : indexes[-1]]) # final part
218-
return "".join(masked)
219-
220216
result: str = cmd.rstrip()
221217

222218
if self.log_mask_re is not None:
223-
result = mask(result, self.log_mask_re)
219+
result = mask_command(result, self.log_mask_re)
224220
if log_mask_re is not None:
225-
result = mask(result, log_mask_re)
221+
result = mask_command(result, log_mask_re)
226222

227223
return result
228224

0 commit comments

Comments
 (0)