-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for custom name generation (#2961)
* Allow for custom name generation * add type annotation * Simpler flow
Showing
5 changed files
with
32 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,44 @@ | ||
from typing import Optional | ||
from typing import Optional, Protocol, Union | ||
|
||
from sanic.base.meta import SanicMeta | ||
|
||
|
||
class NameProtocol(Protocol): | ||
name: str | ||
|
||
|
||
class DunderNameProtocol(Protocol): | ||
__name__: str | ||
|
||
|
||
class BaseMixin(metaclass=SanicMeta): | ||
"""Base class for some other mixins.""" | ||
"""Base class for various mixins.""" | ||
|
||
name: str | ||
strict_slashes: Optional[bool] | ||
|
||
def _generate_name(self, *objects) -> str: | ||
name = None | ||
|
||
def _generate_name( | ||
self, *objects: Union[NameProtocol, DunderNameProtocol, str] | ||
) -> str: | ||
print(objects) | ||
name: Optional[str] = None | ||
for obj in objects: | ||
if obj: | ||
if isinstance(obj, str): | ||
name = obj | ||
break | ||
|
||
try: | ||
name = obj.name | ||
except AttributeError: | ||
try: | ||
name = obj.__name__ | ||
except AttributeError: | ||
continue | ||
else: | ||
break | ||
|
||
if not name: # noqa | ||
if not obj: | ||
continue | ||
if isinstance(obj, str): | ||
name = obj | ||
else: | ||
name = getattr(obj, "name", getattr(obj, "__name__", None)) | ||
|
||
if name: | ||
break | ||
if not name or not isinstance(name, str): | ||
raise ValueError("Could not generate a name for handler") | ||
|
||
if not name.startswith(f"{self.name}."): | ||
name = f"{self.name}.{name}" | ||
|
||
return name | ||
|
||
def generate_name(self, *objects) -> str: | ||
return self._generate_name(*objects) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters