Skip to content

Commit

Permalink
🗓 Dec 21, 2024 9:18:22 AM
Browse files Browse the repository at this point in the history
🐙 update find_replace to use byte pattern
🤖 types added/updated
🧪 tests added/updated
  • Loading branch information
securisec committed Dec 21, 2024
1 parent 73fb105 commit 551dabf
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def __str__(self):
logging.exception(
"\n\nCannot print current state. Either chain with "
"another method, or use one of the output methods "
"Example: .o, .out, .state or .out\n\n"
"Example: .o, .out, or .state\n\n"
)
return ""

Expand Down
16 changes: 15 additions & 1 deletion chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def to_int(
if isinstance(self.state, bytes):
self.state = int.from_bytes(self.state, byteorder)
elif isinstance(self.state, str):
self.state = int(self.state, base)
self.state = int(self.state, int(base))
return self

@ChepyDecorators.call_stack
Expand Down Expand Up @@ -1494,6 +1494,20 @@ def substitute(self, x: str, y: str) -> DataFormatT:
self.state = s.translate(o)
return self

@ChepyDecorators.call_stack
def remove(self, pattern: Union[str, bytes] = b"") -> DataFormatT:
"""Remove is identifcal to find_replace, except it removes the identified
pattern with an empty value.
Args:
pattern (Union[str, bytes], optional): Replace non-printable characters with this. Defaults to ''.
Returns:
Chepy: The Chepy object.
"""
self.find_replace(pattern, b"")
return self

@ChepyDecorators.call_stack
def remove_nonprintable(self, replace_with: Union[str, bytes] = b"") -> DataFormatT:
"""Remove non-printable characters from string.
Expand Down
1 change: 1 addition & 0 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class DataFormat(ChepyCore):
def length(self: DataFormatT) -> DataFormatT: ...
def to_leetcode(self: DataFormatT, replace_space: str=...) -> DataFormatT: ...
def substitute(self: DataFormatT, x: str=..., y: str=...) -> DataFormatT: ...
def remove(self: DataFormatT, pattern: Union[str, bytes] = ...): ...
def remove_nonprintable(self: DataFormatT, replace_with: Union[str, bytes] = ...): ...
def to_base92(self: DataFormatT) -> DataFormatT: ...
def from_base92(self: DataFormatT) -> DataFormatT: ...
Expand Down
11 changes: 9 additions & 2 deletions chepy/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,9 @@ def strip_non_printable(self) -> UtilsT:
return self

@ChepyDecorators.call_stack
def find_replace(self, pattern: str, repl: str, ignore_case=True) -> UtilsT:
def find_replace(
self, pattern: Union[bytes, str], repl: Union[bytes, str], ignore_case=True
) -> UtilsT:
"""Replace matched pattern with repln
Args:
Expand All @@ -527,7 +529,12 @@ def find_replace(self, pattern: str, repl: str, ignore_case=True) -> UtilsT:
flags = 0
if ignore_case:
flags = re.IGNORECASE
self.state = re.sub(pattern, repl, self._convert_to_str(), flags=flags)
self.state = re.sub(
self._to_bytes(pattern),
self._to_bytes(repl),
self._convert_to_bytes(),
flags=flags,
)
return self

@ChepyDecorators.call_stack
Expand Down
2 changes: 1 addition & 1 deletion chepy/modules/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Utils(ChepyCore):
def strip_ansi(self: UtilsT) -> UtilsT: ...
def strip(self: UtilsT, pattern: str, ignore_case: bool=...) -> UtilsT: ...
def strip_non_printable(self: UtilsT) -> UtilsT: ...
def find_replace(self: UtilsT, pattern: str, repl: str, ignore_case: bool=...) -> UtilsT: ...
def find_replace(self: UtilsT, pattern: Union[bytes, str], repl: Union[bytes, str], ignore_case: bool=...) -> UtilsT: ...
def escape_string(self: UtilsT) -> UtilsT: ...
def unescape_string(self: UtilsT) -> UtilsT: ...
def color_hex_to_rgb(self: UtilsT) -> UtilsT: ...
Expand Down
24 changes: 15 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@

sys.path.insert(0, os.path.abspath("."))


def setup(app):
app.add_config_value('recommonmark_config', {
'enable_eval_rst': True,
}, True)
app.add_config_value(
"recommonmark_config",
{
"enable_eval_rst": True,
},
True,
)
app.add_transform(AutoStructify)


# -- Project information -----------------------------------------------------

project = "Chepy"
copyright = "2019, @securisec"
copyright = "2025, @securisec"
author = "@securisec"

master_doc = 'index'
master_doc = "index"


# -- General configuration ---------------------------------------------------
Expand Down Expand Up @@ -66,7 +71,8 @@ def setup(app):
autodoc_member_order = "groupwise"

source_suffix = {
'.rst': 'restructuredtext',
'.txt': 'markdown',
'.md': 'markdown',
}
".rst": "restructuredtext",
".txt": "markdown",
".md": "markdown",
}

1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def test_string_non_printable():

def test_find_replace():
assert Chepy("some some data").find_replace(r"some\s", "data").o == b"datadatadata"
assert Chepy("some some data").remove(r"some\s").o == b"data"


def test_escape_string():
Expand Down

0 comments on commit 551dabf

Please sign in to comment.