Skip to content

Commit

Permalink
πŸ”… βœ… 🧨 ℹ️
Browse files Browse the repository at this point in the history
🧨 removed requests deps to extras.
πŸ”… version 2.4.0
ℹ️ updated travis and gha
πŸ”… added prettyprinter to deps
πŸ”… removed requests from deps
πŸ”… added TODO file
ℹ️ added extras in setup.py
βœ… pretty method added
βœ… set_plugin_path method added
🧨 sort_list method renamed
βœ… sort_dict_key method added
βœ… sort_dict_value method added
ℹ️ some refactors
πŸ”… new tests
  • Loading branch information
securisecctf committed Feb 28, 2020
1 parent 704ee9e commit 61366cd
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
git submodule update --init --recursive
pip install -r requirements.txt
python -m pip install --upgrade pip
pip install .
pip install .[extras]
- name: Install test requirements
run: |
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ before_install:
- python -V
- pip -V
- pip install pytest pytest-cov bandit sphinx recommonmark
- pip install -e .
- pip install -e .[extras]
script:
- pytest --disable-pytest-warnings --cov-report=xml --cov=chepy --cov-config=.coveragerc
tests/
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN pip install -r /chepy/requirements.txt \
&& pip install python-magic virtualenv \
&& virtualenv -p python3 /chepy/venv \
&& pip install pytest pytest-cov bandit \
&& pip install scapy markdown pefile pyelftools pydriller
&& pip install scapy markdown pefile pyelftools pydriller requests

COPY . /chepy/
RUN cd /chepy \
Expand Down Expand Up @@ -36,6 +36,8 @@ FROM python:3.8.0-slim
COPY --from=0 /chepy /chepy
RUN apt update \
&& apt install exiftool libmagic-dev -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& /chepy/venv/bin/chepy -v \
&& sed -i 's/enableplugins = false/enableplugins = true/' /root/.chepy/chepy.conf
WORKDIR /data
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Chepy can be installed in a few ways.
### Pypi
```bash
pip3 install chepy
# optionally with extra requirements
pip3 install chepy[extras]
```

### Git
Expand Down
4 changes: 4 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

Todo:
βœ” sort dict by key or value
βœ” Change sort list to be able to sort by any data type
2 changes: 1 addition & 1 deletion chepy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "2.3.2" # pragma: no cover
__version__ = "2.4.0" # pragma: no cover
__author__ = "Hapsida @securisec" # pragma: no cover
2 changes: 1 addition & 1 deletion chepy/chepy_plugins
Submodule chepy_plugins updated 1 files
+1 βˆ’1 requirements.txt
53 changes: 51 additions & 2 deletions chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import subprocess
from configparser import ConfigParser
from urllib.parse import urljoin
from prettyprinter import pformat
from typing import Any, Tuple, List, Union

import pyperclip
from requests import request
import ujson
import jsonpickle
import regex as re
Expand Down Expand Up @@ -104,7 +104,7 @@ def state(self, val):
def __str__(self):
try:
if isinstance(self.state, bytearray):
return re.sub(rb'[^\x00-\x7f]', b'.', self.state).decode()
return re.sub(rb"[^\x00-\x7f]", b".", self.state).decode()
else:
return self._convert_to_str()
except UnicodeDecodeError: # pragma: no cover
Expand Down Expand Up @@ -695,6 +695,12 @@ def json2str(obj): # pragma: no cover
else:
raise NotImplementedError

try:
from requests import request
except ImportError: # pragma: no cover
self._error_logger("Could not import requests. pip install requests")
return self

params = json2str(params)
headers = json2str(headers)
cookies = json2str(cookies)
Expand Down Expand Up @@ -756,6 +762,12 @@ def json2str(obj): # pragma: no cover
else:
raise NotImplementedError

try:
from requests import request
except ImportError: # pragma: no cover
self._error_logger("Could not import requests. pip install requests")
return self

params = json2str(params)
headers = json2str(headers)
cookies = json2str(cookies)
Expand Down Expand Up @@ -1060,6 +1072,19 @@ def shell_output(self): # pragma: no cover
self.state = subprocess.getoutput(self.state)
return self

@ChepyDecorators.call_stack
def pretty(self, indent: int = 2):
"""Prettify the state.
Args:
indent (int, optional): Indent level. Defaults to 2.
Returns:
Chepy: The Chepy object.
"""
self.state = pformat(self.state, indent=int(indent))
return self

def plugins(self, enable: str) -> None: # pragma: no cover
"""Use this method to enable or disable Chepy plugins.
Expand Down Expand Up @@ -1093,3 +1118,27 @@ def plugins(self, enable: str) -> None: # pragma: no cover
)
sys.exit()
return None

def set_plugin_path(self, path: str) -> None: # pragma: no cover
"""Use this method to set the path for Chepy plugins.
Args:
path (str): Path to plugins directory
Returns:
None
"""
expand_path = self._abs_path(path)
if expand_path.exists():
conf_path = pathlib.Path().home() / ".chepy" / "chepy.conf"
c = ConfigParser()
c.read(conf_path)
c.set("Plugins", "pluginpath", str(expand_path))
with open(conf_path, "w") as f:
c.write(f)
self._info_logger(green("Plugin path has been set. Restart for changes."))
sys.exit()
return None
else:
raise AttributeError("The path does not exist")

71 changes: 56 additions & 15 deletions chepy/modules/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import difflib
import regex as re
from collections import OrderedDict
from typing import Any, Union
import pydash

Expand Down Expand Up @@ -205,39 +206,79 @@ def unique(self):
"""Get an array of unique list items
Raises:
TypeError: If state is not a list
StateNotList: If state is not a list
Returns:
Chepy: The Chepy object.
"""
if isinstance(self.state, list):
self.state = pydash.uniq(self.state)
return self
else: # pragma: no cover
raise TypeError("State is not a list")
assert isinstance(self.state, list), StateNotList()
self.state = pydash.uniq(self.state)
return self

@ChepyDecorators.call_stack
def sorted(self, reverse: bool = False):
def sort_list(self, reverse: bool = False):
"""Sort a list
Args:
reverse (bool, optional): In reverse order. Defaults to False.
Raises:
TypeError: If state is not list
StateNotList: If state is not list
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy(["a", "b", "1", "2"]).sorted().o
>>> Chepy(["a", "b", "1", "2"]).sort_list().o
["1", "2", "a", "b"]
"""
if isinstance(self.state, (list)):
self.state = sorted(self.state)
return self
else: # pragma: no cover
raise TypeError("State is not a list")
assert isinstance(self.state, list), StateNotList()
self.state = sorted(
self.state, key=lambda v: (isinstance(v, str), v), reverse=reverse
)
return self

@ChepyDecorators.call_stack
def sort_dict_key(self, reverse: bool = False):
"""Sort a dictionary by key
Args:
reverse (bool, optional): Reverse sort order. Defaults to False.
Returns:
Chepy: The Chepy object.
Examples:
>>> c = Chepy({'z': 'string', 'a': True, 'zz': 1, 'aaa': {'bb': 'data'}, 'ccc': [1,'a']})
>>> c.sort_dict_key(reverse=True)
{'zz': 1, 'z': 'string', 'ccc': [1, 'a'], 'aaa': {'bb': 'data'}, 'a': True}
"""
assert isinstance(self.state, dict), StateNotDict()
self.state = dict(OrderedDict(sorted(self.state.items(), reverse=reverse)))
return self

@ChepyDecorators.call_stack
def sort_dict_value(self, reverse=False):
"""Sort dictionary by value
Args:
reverse (bool, optional): Reverse sort order. Defaults to False.
Returns:
Chepy: The Chepy object.
Examples:
>>> c = Chepy({'z': 'string', 'a': 'True', 'zz': '1', 'aaa': {'bb': 'data'}, 'ccc': [1,'a']})
>>> c.sort_dict_value()
{'zz': '1', 'a': 'True', 'ccc': [1, 'a'], 'z': 'string', 'aaa': {'bb': 'data'}}
"""
assert isinstance(self.state, dict), StateNotDict()
self.state = dict(
OrderedDict(
sorted(self.state.items(), reverse=reverse, key=lambda x: str(x[1]))
)
)
return self

@ChepyDecorators.call_stack
def filter_list(self, by: Union[str, dict], regex: bool = True):
Expand All @@ -248,7 +289,7 @@ def filter_list(self, by: Union[str, dict], regex: bool = True):
regex (bool, optional): If pattern is a regex. Defaults to True
Raises:
TypeError: If state is not a list
StateNotList: If state is not a list
Returns:
Chepy: The Chepy object.
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jsonpickle
parsel
phpserialize
Pillow
prettyprinter
prompt_toolkit>=2.0.8
pycipher
pycryptodome
Expand All @@ -23,6 +24,4 @@ pyperclip
PyExifTool
PyYAML
regex
requests
requests-toolbelt
ujson
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def read_requirements():
"Documentation": "https://chepy.readthedocs.io/en/latest/",
"Source Code": "https://github.com/securisec/chepy",
},
extras_require={"extras": ["requests"]},
packages=find_packages(exclude=(["tests", "docs"])),
install_requires=requirements,
classifiers=[
Expand Down
12 changes: 12 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,15 @@ def test_load_from_url():
)
== bytes
)


def test_pretty():
assert (
Chepy({"a": 1, "b": 2, "c": [1, 2, 3]}).pretty().o
== """{
'a': 1,
'b': 2,
'c': [1, 2, 3]
}"""
)

38 changes: 36 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,42 @@ def test_unique():
assert len(Chepy('["a", "a", 1]').str_list_to_list().unique().o) == 2


def test_sorted():
assert Chepy(["a", "b", "1", "2"]).sorted().o == ["1", "2", "a", "b"]
def test_sorted_list():
assert Chepy(["a", "b", "1", "2"]).sort_list().o == ["1", "2", "a", "b"]
assert Chepy(["a", "b", "1", "2"]).sort_list(reverse=True).o == ["b", "a", "2", "1"]


def test_sort_dict_key():
assert Chepy(
{"z": "string", "a": True, "zz": 1, "aaa": {"bb": "data"}, "ccc": [1, "a"]}
).sort_dict_key().o == {
"a": True,
"aaa": {"bb": "data"},
"ccc": [1, "a"],
"z": "string",
"zz": 1,
}
assert Chepy(
{"z": "string", "a": True, "zz": 1, "aaa": {"bb": "data"}, "ccc": [1, "a"]}
).sort_dict_key(reverse=True).o == {
"zz": 1,
"z": "string",
"ccc": [1, "a"],
"aaa": {"bb": "data"},
"a": True,
}


def test_sort_dict_value():
assert Chepy(
{"z": "string", "a": "True", "zz": "1", "aaa": {"bb": "data"}, "ccc": [1, "a"]}
).sort_dict_value().o == {
"zz": "1",
"a": "True",
"ccc": [1, "a"],
"z": "string",
"aaa": {"bb": "data"},
}


def test_filter_list():
Expand Down

0 comments on commit 61366cd

Please sign in to comment.