Skip to content

Commit

Permalink
chore: use get_nondefault_repr
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Oct 28, 2023
1 parent a68b3b7 commit 4d00ca3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 48 deletions.
5 changes: 1 addition & 4 deletions mknodes/extranodes/mksnippet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ def __init__(self, path: str | os.PathLike, **kwargs: Any):
super().__init__(**kwargs)
self.path = path

def __str__(self):
return self.to_markdown()

def __repr__(self):
return reprhelpers.get_repr(self, path=self.path)
return reprhelpers.get_nondefault_repr(self)

@classmethod
def create_example_page(cls, page):
Expand Down
8 changes: 1 addition & 7 deletions mknodes/templatenodes/mkcallable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,7 @@ def __call__(self) -> mk.MkNode:
return node

def __repr__(self):
return reprhelpers.get_repr(
self,
fn=self.fn,
args=self.args,
kw_args=self.kw_args,
_filter_empty=True,
)
return reprhelpers.get_nondefault_repr(self)

@property
def files(self):
Expand Down
26 changes: 7 additions & 19 deletions mknodes/templatenodes/mkmetadatabadges/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(
super().__init__(**kwargs)
self.block_separator = "\n"
self._package = package
self._typ = typ
self.typ = typ
self.font_size = font_size
self.font_name = font_name
self.num_padding_chars = num_padding_chars
Expand All @@ -63,19 +63,7 @@ def __init__(
self.use_gitlab_style = use_gitlab_style

def __repr__(self):
return reprhelpers.get_repr(
self,
typ=self._typ,
package=self._package,
font_size=self.font_size,
font_name=self.font_name,
badge_color=self.badge_color,
text_color=self.text_color,
num_padding_chars=self.num_padding_chars,
use_gitlab_style=self.use_gitlab_style,
_filter_empty=True,
_filter_false=True,
)
return reprhelpers.get_nondefault_repr(self)

@property
def badge_content(self) -> list[tuple]:
Expand All @@ -85,7 +73,7 @@ def badge_content(self) -> list[tuple]:
if self._package
else self.ctx.metadata
)
match self._typ:
match self.typ:
case "classifiers":
dct = ctx.classifier_map
for category, labels in dct.items():
Expand Down Expand Up @@ -120,10 +108,10 @@ def badge_content(self) -> list[tuple]:
(package.name, package.version, package.homepage) for package in pkgs
)
case str():
raise ValueError(self._typ)
case _ if self._typ in datatypes.CLASSIFIERS:
labels = ctx.classifier_map.get(self._typ, [])
items.extend([(i, self._typ, None) for i in labels])
raise ValueError(self.typ)
case _ if self.typ in datatypes.CLASSIFIERS:
labels = ctx.classifier_map.get(self.typ, [])
items.extend([(i, self.typ, None) for i in labels])
return items

@property
Expand Down
14 changes: 2 additions & 12 deletions mknodes/templatenodes/mkpipdeptree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class MkPipDepTree(mkdiagram.MkDiagram):
def __init__(
self,
package: types.ModuleType | str | None = None,
*,
direction: Literal["TD", "DT", "LR", "RL"] = "TD",
local_only: bool = False,
user_only: bool = False,
Expand All @@ -112,18 +113,7 @@ def __init__(
super().__init__(graph_type="flow", direction=direction, **kwargs)

def __repr__(self):
include_editables = self.include_editables if not self.include_editables else None
return reprhelpers.get_repr(
self,
package=self._package,
local_only=self.local_only,
user_only=self.user_only,
include_editables=include_editables,
editables_only=self.editables_only,
direction=self.direction,
_filter_empty=True,
_filter_false=True,
)
return reprhelpers.get_nondefault_repr(self)

@classmethod
def create_example_page(cls, page):
Expand Down
12 changes: 6 additions & 6 deletions mknodes/utils/reprhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ def __init__(self, some_value: int = 0):
spec.args.remove("self")
args = [getattr(instance, "items" if arg == "content" else arg) for arg in spec.args]
dct = spec.kwonlydefaults or {}
kwargs = {
kwargs = {}
for k, v in dct.items():
# check for hidden attribute first, then for attribute named like kwarg
k: getattr(instance, f"_{k}", getattr(instance, k))
for k, v in dct.items()
if (k in instance.__dict__ and v != getattr(instance, k))
or (f"_{k}" in instance.__dict__ and v != getattr(instance, f"_{k}"))
}
if f"_{k}" in instance.__dict__ and v != getattr(instance, f"_{k}"):
kwargs[k] = getattr(instance, f"_{k}")
if k in instance.__dict__ and v != getattr(instance, k):
kwargs[k] = getattr(instance, k)
return get_repr(instance, *args, **kwargs, _char_width=char_width, _shorten=shorten)


Expand Down

0 comments on commit 4d00ca3

Please sign in to comment.