Skip to content

Commit

Permalink
re.Pattern is also correct mask rule
Browse files Browse the repository at this point in the history
* drop asynctest test requirement: never supported python 3.8+, unittest from python3.8 can mock async
  • Loading branch information
penguinolog committed Dec 16, 2022
1 parent eaed0fb commit a0088ca
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 62 deletions.
1 change: 1 addition & 0 deletions CI_REQUIREMENTS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ PyYAML>=3.12
ruamel.yaml
defusedxml # PSF
logwrap>=7.0.0 # Apache 2
setuptools_scm
2 changes: 1 addition & 1 deletion doc/source/Subprocess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ API: Subprocess
ExecHelper global API.

:param log_mask_re: regex lookup rule to mask command for logger. all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None

.. versionchanged:: 1.2.0 log_mask_re regex rule for masking cmd
.. versionchanged:: 3.1.0 Not singleton anymore. Only lock is shared between all instances.
Expand Down
8 changes: 4 additions & 4 deletions exec_helpers/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def string_bytes_bytearray_as_bytes(src: str | bytes | bytearray) -> bytes:
raise TypeError(f"{src!r} has unexpected type: not conform to Union[str, bytes, bytearray]") # pragma: no cover


def _mask_command(text: str, rules: str) -> str:
def _mask_command(text: str, rules: str | re.Pattern[str]) -> str:
"""Mask part of text using rules.
:param text: source text
:type text: str
:param rules: regex rules to mask.
:type rules: str
:type rules: str | re.Pattern
:return: source with all MATCHED groups replaced by '<*masked*>'
:rtype: str
"""
Expand All @@ -55,13 +55,13 @@ def _mask_command(text: str, rules: str) -> str:
return "".join(masked)


def mask_command(text: str, *rules: str | None) -> str:
def mask_command(text: str, *rules: str | re.Pattern[str] | None) -> str:
"""Apply all rules to command.
:param text: source text
:type text: str
:param rules: regex rules to mask.
:type rules: str | None
:type rules: str | re.Pattern[str] | None
:return: source with all MATCHED groups replaced by '<*masked*>'
:rtype: str
"""
Expand Down
14 changes: 7 additions & 7 deletions exec_helpers/_ssh_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ def _exec_command( # type: ignore[override]
:type verbose: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param log_stdout: log STDOUT during read
Expand Down Expand Up @@ -1212,7 +1212,7 @@ def execute(
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -1287,7 +1287,7 @@ def __call__(
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -1368,7 +1368,7 @@ def check_call(
:type raise_on_err: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -1455,7 +1455,7 @@ def check_stderr(
:type expected: Iterable[int | proc_enums.ExitCodes]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -1644,7 +1644,7 @@ def execute_through_host(
:type log_stderr: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param get_pty: open PTY on target machine
:type get_pty: bool
:param width: PTY width
Expand Down Expand Up @@ -1731,7 +1731,7 @@ def execute_together(
:type verbose: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param exception_class: Exception to raise on error. Mandatory subclass of exceptions.ParallelCallProcessError
:type exception_class: type[exceptions.ParallelCallProcessError]
:param kwargs: additional parameters for execute_async call.
Expand Down
18 changes: 9 additions & 9 deletions exec_helpers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
)

CommandT = typing.Union[str, typing.Iterable[str]]
LogMaskReT = typing.Optional[str]
LogMaskReT = typing.Union[str, typing.Pattern[str], None]
ErrorInfoT = typing.Optional[str]
ChRootPathSetT = typing.Optional[typing.Union[str, pathlib.Path]]
ExpectedExitCodesT = typing.Iterable[ExitCodeT]
Expand Down Expand Up @@ -221,7 +221,7 @@ class ExecHelper(
:type logger: logging.Logger
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
.. versionchanged:: 1.2.0 log_mask_re regex rule for masking cmd
.. versionchanged:: 1.3.5 make API public to use as interface
Expand Down Expand Up @@ -327,7 +327,7 @@ def _mask_command(self, cmd: str, log_mask_re: LogMaskReT = None) -> str:
:type cmd: str
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:return: masked command
:rtype: str
Expand Down Expand Up @@ -418,7 +418,7 @@ def _exec_command(
:type verbose: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param log_stdout: log STDOUT during read
Expand All @@ -438,7 +438,7 @@ def _exec_command(
def _log_command_execute(
self,
command: str,
log_mask_re: str | None,
log_mask_re: LogMaskReT,
log_level: int,
chroot_path: str | None = None,
**_: typing.Any,
Expand Down Expand Up @@ -504,7 +504,7 @@ def execute(
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -584,7 +584,7 @@ def __call__( # pylint: disable=arguments-differ
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -653,7 +653,7 @@ def check_call(
:type raise_on_err: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -768,7 +768,7 @@ def check_stderr(
:type expected: Iterable[int | proc_enums.ExitCodes]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down
16 changes: 8 additions & 8 deletions exec_helpers/async_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class ExecHelper(
:type logger: logging.Logger
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
"""

__slots__ = ("__alock", "__logger", "log_mask_re", "__chroot_path")
Expand Down Expand Up @@ -301,7 +301,7 @@ def _mask_command(self, cmd: str, log_mask_re: LogMaskReT = None) -> str:
:type cmd: str
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:return: masked command
:rtype: str
Expand Down Expand Up @@ -386,7 +386,7 @@ async def _exec_command(
:type verbose: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param log_stdout: log STDOUT during read
Expand All @@ -404,7 +404,7 @@ async def _exec_command(
def _log_command_execute(
self,
command: str,
log_mask_re: str | None,
log_mask_re: LogMaskReT,
log_level: int,
chroot_path: str | None = None,
**_: typing.Any,
Expand Down Expand Up @@ -469,7 +469,7 @@ async def execute(
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -547,7 +547,7 @@ async def __call__( # pylint: disable=invalid-overridden-method,arguments-diffe
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -616,7 +616,7 @@ async def check_call(
:type raise_on_err: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -729,7 +729,7 @@ async def check_stderr(
:type expected: Iterable[int | proc_enums.ExitCodes]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down
12 changes: 6 additions & 6 deletions exec_helpers/async_api/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class Subprocess(api.ExecHelper):
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param logger: logger instance to use
:type logger: logging.Logger
Expand Down Expand Up @@ -310,7 +310,7 @@ async def _exec_command( # type: ignore[override]
:type verbose: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param log_stdout: log STDOUT during read
Expand Down Expand Up @@ -555,7 +555,7 @@ async def execute(
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -630,7 +630,7 @@ async def __call__(
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -712,7 +712,7 @@ async def check_call(
:type raise_on_err: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -799,7 +799,7 @@ async def check_stderr(
:type expected: Iterable[int | proc_enums.ExitCodes]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down
12 changes: 6 additions & 6 deletions exec_helpers/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class Subprocess(api.ExecHelper):
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
.. versionchanged:: 1.2.0 log_mask_re regex rule for masking cmd
.. versionchanged:: 3.1.0 Not singleton anymore. Only lock is shared between all instances.
Expand Down Expand Up @@ -304,7 +304,7 @@ def _exec_command( # type: ignore[override]
:type verbose: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param log_stdout: log STDOUT during read
Expand Down Expand Up @@ -571,7 +571,7 @@ def execute(
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -646,7 +646,7 @@ def __call__(
:type timeout: int | float | None
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -728,7 +728,7 @@ def check_call(
:type raise_on_err: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down Expand Up @@ -815,7 +815,7 @@ def check_stderr(
:type expected: Iterable[int | proc_enums.ExitCodes]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: str | None
:type log_mask_re: str | re.Pattern[str] | None
:param stdin: pass STDIN text to the process
:type stdin: bytes | str | bytearray | None
:param open_stdout: open STDOUT stream for read
Expand Down
1 change: 0 additions & 1 deletion pytest_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
asynctest
pytest >= 6.0
pytest-cov
pytest-mock
Expand Down
Loading

0 comments on commit a0088ca

Please sign in to comment.