-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from s-kostyuk/finalized_capabilities
Finalized capabilities (2/n) This pull request added `_capability_name` field for public Capabilities and adds a `capabilities` property for all Things
- Loading branch information
Showing
9 changed files
with
79 additions
and
4 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
This class contains a definition of the CapabilityFiller metaclass. | ||
""" | ||
import inspect | ||
from typing import Iterable, Generator | ||
|
||
|
||
class CapabilityFiller(type): | ||
""" | ||
CapabilityFiller is a metaclass which intercepts the class | ||
creation process, discovers all Capabilities (parent classes) | ||
of the class and fills the corresponding ``_capabilities`` | ||
private property of the class with the names of all Capabilities | ||
which was implemented by the class | ||
""" | ||
|
||
def __new__(mcs, name, bases, class_dict): | ||
cls = type.__new__(mcs, name, bases, class_dict) | ||
|
||
mros = inspect.getmro(cls) | ||
|
||
cls._capabilities = tuple( | ||
mcs._capability_list_generator(source=mros) | ||
) | ||
|
||
return cls | ||
|
||
@staticmethod | ||
def _capability_list_generator(source: Iterable) -> Generator[str, None, None]: | ||
""" | ||
A generator which extracts capability names from the | ||
specified list of Capabilities | ||
:param source: a list of parent classes to be parsed | ||
:return: a generator of Capability names | ||
""" | ||
attr = '_capability_name' | ||
|
||
for i in source: | ||
if attr not in i.__dict__: | ||
continue | ||
|
||
name = getattr(i, attr, None) | ||
|
||
assert name is not None | ||
|
||
yield name |
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