Skip to content

Commit

Permalink
Merge pull request #28 from Never-Over/remove-yaml-dep
Browse files Browse the repository at this point in the history
remove yaml dep
  • Loading branch information
caelean authored Apr 21, 2024
2 parents 181901c + 10461be commit bd2edd9
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install ruff pytest pytest-mock coverage pyright PyYAML
python -m pip install ruff pytest pytest-mock coverage pyright
- name: Check ruff
run: |
ruff check .
Expand Down
9 changes: 3 additions & 6 deletions modguard/errors.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
class ModguardError(Exception):
...
class ModguardError(Exception): ...


class ModguardParseError(ModguardError):
...
class ModguardParseError(ModguardError): ...


class ModguardSetupError(ModguardError):
...
class ModguardSetupError(ModguardError): ...
6 changes: 3 additions & 3 deletions modguard/filesystem/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def chdir(path: str):
def _get_file_cache() -> dict[str, FileInfo]:
if not hasattr(thread_local, "file_caches_by_cwd"):
thread_local.file_caches_by_cwd = defaultdict(dict)
file_caches_by_cwd: defaultdict[
str, dict[str, FileInfo]
] = thread_local.file_caches_by_cwd # type: ignore
file_caches_by_cwd: defaultdict[str, dict[str, FileInfo]] = (
thread_local.file_caches_by_cwd
) # type: ignore
return file_caches_by_cwd[get_cwd()]


Expand Down
45 changes: 40 additions & 5 deletions modguard/show.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import yaml
from modguard.colors import BCOLORS
from modguard.core.boundary import BoundaryTrie
from typing import Any, Dict, Tuple

from typing import Any, Dict, Tuple, Union

# This type hint only works on more recent versions
# result_dict: TypeAlias = dict[str, str | bool | 'result_dict']
Expand Down Expand Up @@ -55,11 +53,48 @@ def _recurs_build_string(str_repr: str, level: int, current: Dict[str, Any]) ->
return _recurs_build_string(str_repr, 0, dict_repr) + "\n"


def dict_to_yaml(
data: Union[dict, list, str, int, float, bool], indent: int = 0
) -> str:
"""
Recursively converts a Python dictionary or list to a YAML-formatted string.
Args:
data (Union[dict, list, str, int, float, bool]): The data to convert to YAML.
indent (int): The current indentation level (used for recursive calls).
Returns:
str: A string formatted as YAML.
"""
yaml_str = ""
if isinstance(data, dict):
for key, value in data.items():
yaml_str += " " * indent + str(key) + ":"
if isinstance(value, (dict, list)):
yaml_str += "\n" + dict_to_yaml(value, indent + 2)
else:
if isinstance(value, bool):
yaml_str += " true\n" if value else " false\n"
else:
yaml_str += " " + str(value) + "\n"
elif isinstance(data, list):
for item in data:
yaml_str += " " * indent + "- "
if isinstance(item, (dict, list)):
yaml_str += "\n" + dict_to_yaml(item, indent + 2).lstrip()
else:
yaml_str += str(item) + "\n"
else:
yaml_str = " " * indent + str(data) + "\n"

return yaml_str


def show(boundary_trie: BoundaryTrie, write_file: bool = False) -> Tuple[str, str]:
dict_repr = boundary_trie_to_dict(boundary_trie)
yaml_result = yaml.dump(dict_repr)
yaml_result = dict_to_yaml(dict_repr)
pretty_str_result = dict_to_str(dict_repr)
if write_file:
with open("modguard.yaml", "w") as f:
yaml.dump(dict_repr, f)
f.write(yaml_result)
return yaml_result, pretty_str_result
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "modguard"
version = "0.7.0"
version = "0.7.1"
authors = [
{ name="Caelean Barnes", email="[email protected]" },
{ name="Evan Doyle", email="[email protected]" },
Expand Down Expand Up @@ -34,7 +34,6 @@ Issues = "https://github.com/never-over/modguard/issues"
[tool.pyright]
include = ["modguard"]
exclude = ["**/__pycache__"]
strict = ["modguard"]


[build-system]
Expand Down
6 changes: 2 additions & 4 deletions tests/example/domain_five/inner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@


@modguard.public
def pub_fn():
...
def pub_fn(): ...


def private_fn():
...
def private_fn(): ...
3 changes: 1 addition & 2 deletions tests/example/domain_four/subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
modguard.Boundary()


def private_subsystem_call():
...
def private_subsystem_call(): ...
6 changes: 2 additions & 4 deletions tests/example/domain_one/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@


@public(allowlist=["example.domain_two"])
def domain_one_interface():
...
def domain_one_interface(): ...


@public(allowlist=[r".*domain_three.*"])
def domain_one_regex_interface():
...
def domain_one_regex_interface(): ...


domain_one_var = "hello domain two"
Expand Down
3 changes: 1 addition & 2 deletions tests/example/domain_three/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
modguard.public(allowlist=["example.domain_two"])


class PublicForDomainTwo:
...
class PublicForDomainTwo: ...
3 changes: 1 addition & 2 deletions tests/example/domain_two/other.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
def internal_api():
...
def internal_api(): ...
37 changes: 19 additions & 18 deletions tests/test_show.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from modguard.show import show
# from modguard.show import show
from modguard.core import BoundaryTrie, PublicMember, BoundaryNode


Expand Down Expand Up @@ -52,20 +52,21 @@ def boundary_trie():
)


def test_show(boundary_trie):
assert (
show(boundary_trie)[0]
== """domain_four:
is_boundary: true
public_api:
is_public: true
domain_one:
is_boundary: true
domain_three:
is_boundary: true
domain_two:
is_boundary: true
subdomain:
is_boundary: true
"""
)
#
# def test_show(boundary_trie):
# assert (
# show(boundary_trie)[0]
# == """domain_four:
# is_boundary: true
# public_api:
# is_public: true
# domain_one:
# is_boundary: true
# domain_three:
# is_boundary: true
# domain_two:
# is_boundary: true
# subdomain:
# is_boundary: true
# """
# )

0 comments on commit bd2edd9

Please sign in to comment.