Skip to content

Commit c2537f0

Browse files
committed
Sync slug_attr and dict key
also update docs and add a few missed types. do not set anything on item, if it's not requested to.
1 parent bce7cfb commit c2537f0

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ import typing
8383

8484

8585
class Animal(abc.ABC):
86-
is_wild: typing.Optional[bool] = None
86+
slug: str
87+
is_wild: bool
8788

8889
def walk(self):
8990
pass
@@ -93,6 +94,7 @@ class Animal(abc.ABC):
9394
animal_registry = registerer.Registerer(
9495
parent_class=Animal,
9596
max_size=5, # only 5 items can register
97+
slug_attr="slug", # set the slug of item as attribute on it
9698
validators=[
9799
registerer.RegistryValidator(
98100
lambda item: item.is_wild is False, # check passed if returns True
@@ -134,12 +136,13 @@ assert animal_registry._registry_dict == {"Sheep": Sheep, "kitty": Cat}
134136
assert animal_registry["Sheep"]().walk() == "sheep walks"
135137
assert animal_registry["kitty"]().walk() == "cat walks"
136138
```
137-
The `register` method will also set an attribute on the registered item as `registry_slug`.
139+
The `register` method will also set an attribute on the registered item as `registry_slug`.
140+
You can change the attribute name when creating the Registerer object.
138141
So, in last example we have:
139142

140143
```python
141-
assert Cat.registry_slug == "kitty"
142-
assert animal_registry["kitty"].registry_slug == "kitty"
144+
assert Cat.slug == "kitty"
145+
assert animal_registry["kitty"].slug == "kitty"
143146

144147
```
145148
if you need to add attributes on the registered item on registration (it's optional), you can pass kwargs to the `register` method.
@@ -236,7 +239,7 @@ A utility that can be used to create a registry object to register class or func
236239
```python
237240
__init__(
238241
parent_class: Optional[Type[~T]] = None,
239-
slug_attr='registry_slug',
242+
slug_attr: Optional[str] = None,
240243
max_size: Optional[int] = None,
241244
validators: Optional[List[registerer.validators.RegistryValidator]] = None
242245
)
@@ -247,6 +250,7 @@ __init__(
247250
**Args:**
248251

249252
- <b>`parent_class`</b>: The class of parent. If you set this, the registered class should be subclass of the this, If it's not the register method going to raise RegistrationError. Also by setting this you'll be benefit from type hints in your IDE.
253+
- <b>`slug_attr`</b>: Pass the attribute name of registered item that you want to set registry slug to or read from registered item.
250254
- <b>`max_size`</b>: allowed size of registered items. Defaults to None which means there is no limit.
251255
- <b>`validators`</b>: custom validation for on registering items.
252256

@@ -267,7 +271,7 @@ get actual registered items as list (classes or functions)
267271

268272
---
269273

270-
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L58"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
274+
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L60"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
271275

272276
#### <kbd>method</kbd> `Registerer.is_registered`
273277

@@ -279,7 +283,7 @@ is the slug registered?
279283

280284
---
281285

282-
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L94"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
286+
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L96"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
283287

284288
#### <kbd>method</kbd> `Registerer.register`
285289

@@ -335,7 +339,7 @@ assert postgresql_connection.env == "prod"
335339

336340
---
337341

338-
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L73"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
342+
<a href="https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L75"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
339343

340344
#### <kbd>method</kbd> `Registerer.validate`
341345

docgen/README.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def command_handler(command, args):
8383

8484

8585
class Animal(abc.ABC):
86-
is_wild: typing.Optional[bool] = None
86+
slug: str
87+
is_wild: bool
8788

8889
def walk(self):
8990
pass
@@ -93,6 +94,7 @@ def walk(self):
9394
animal_registry = registerer.Registerer(
9495
parent_class=Animal,
9596
max_size=5, # only 5 items can register
97+
slug_attr="slug", # set the slug of item as attribute on it
9698
validators=[
9799
registerer.RegistryValidator(
98100
lambda item: item.is_wild is False, # check passed if returns True
@@ -134,12 +136,13 @@ def walk(self):
134136
assert animal_registry["Sheep"]().walk() == "sheep walks"
135137
assert animal_registry["kitty"]().walk() == "cat walks"
136138
"""
137-
The `register` method will also set an attribute on the registered item as `registry_slug`.
139+
The `register` method will also set an attribute on the registered item as `registry_slug`.
140+
You can change the attribute name when creating the Registerer object.
138141
So, in last example we have:
139142
140143
"""
141-
assert Cat.registry_slug == "kitty"
142-
assert animal_registry["kitty"].registry_slug == "kitty"
144+
assert Cat.slug == "kitty"
145+
assert animal_registry["kitty"].slug == "kitty"
143146

144147
"""
145148
if you need to add attributes on the registered item on registration (it's optional), you can pass kwargs to the `register` method.

registerer/registry.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(
1818
self,
1919
parent_class: typing.Optional[typing.Type[T]] = None,
2020
*,
21-
slug_attr="registry_slug",
21+
slug_attr: typing.Optional[str] = None,
2222
max_size: typing.Optional[int] = None,
2323
validators: typing.Optional[typing.List[RegistryValidator]] = None,
2424
):
@@ -28,6 +28,8 @@ def __init__(
2828
If you set this, the registered class should be subclass of the this,
2929
If it's not the register method going to raise RegistrationError.
3030
Also by setting this you'll be benefit from type hints in your IDE.
31+
slug_attr: Pass the attribute name of registered item that you want to
32+
set registry slug to or read from registered item.
3133
max_size: allowed size of registered items.
3234
Defaults to None which means there is no limit.
3335
validators: custom validation for on registering items.
@@ -38,7 +40,7 @@ def __init__(
3840
self._registry_dict: typing.Dict[str, typing.Type[T]] = {}
3941
self.parent_class: typing.Optional[typing.Type[T]] = parent_class
4042
self.max_size: typing.Optional[int] = max_size
41-
self.slug_attr: str = slug_attr
43+
self.slug_attr: typing.Optional[str] = slug_attr
4244
self.validators: typing.List = validators if validators else []
4345

4446
if self.max_size is not None and (not isinstance(self.max_size, int) or self.max_size <= 0):
@@ -132,12 +134,14 @@ def postgresql_connection:
132134
"""
133135

134136
def _wrapper_function(item):
135-
registry_slug = custom_slug if custom_slug else item.__name__
137+
registry_slug = custom_slug or getattr(item, self.slug_attr or "", "") or item.__name__
136138

137139
if self.is_registered(registry_slug):
138140
raise RegistrationError(f"There is another item already registered with slug='{registry_slug}'.")
139141

140-
setattr(item, self.slug_attr, registry_slug)
142+
if self.slug_attr:
143+
setattr(item, self.slug_attr, registry_slug)
144+
141145
for key, value in kwargs.items():
142146
setattr(item, key, value)
143147

0 commit comments

Comments
 (0)