forked from Parsl/parsl
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'benc-usage-protocol' into update-usage-tracking
Merge changes from Ben related to usage tracking update Parsl#3229
- Loading branch information
Showing
4 changed files
with
111 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from parsl.utils import RepresentationMixin | ||
|
||
from abc import abstractmethod | ||
from functools import singledispatch | ||
from typing import Any, List, Sequence | ||
|
||
|
||
# Traverse the configuration heirarchy, returning a JSON component | ||
# for each one. Configuration components which implement | ||
# RepresentationMixin will be in the right form for inspecting | ||
# object attributes. Configuration components which are lists or tuples | ||
# are traversed in sequence. Other types default to reporting no | ||
# usage information. | ||
|
||
@singledispatch | ||
def get_parsl_usage(obj) -> List[Any]: | ||
return [] | ||
|
||
|
||
@get_parsl_usage.register | ||
def get_parsl_usage_representation_mixin(obj: RepresentationMixin): | ||
t = type(obj) | ||
qualified_name = t.__module__ + "." + t.__name__ | ||
|
||
# me can contain anything that can be rendered as JSON | ||
me: List[Any] = [] | ||
|
||
if isinstance(obj, UsageInformation): | ||
# report rich usage information for this component | ||
attrs = {'c': qualified_name} | ||
attrs.update(obj.get_usage_information()) | ||
me = [attrs] | ||
else: | ||
# report the class name of this component | ||
me = [qualified_name] | ||
|
||
# unwrap typeguard-style unwrapping | ||
init: Any = type(obj).__init__ | ||
if hasattr(init, '__wrapped__'): | ||
init = init.__wrapped__ | ||
|
||
import inspect | ||
|
||
argspec = inspect.getfullargspec(init) | ||
|
||
for arg in argspec.args[1:]: # skip first arg, self | ||
arg_value = getattr(obj, arg) | ||
d = get_parsl_usage(arg_value) | ||
me += d | ||
|
||
return me | ||
|
||
|
||
@get_parsl_usage.register(list) | ||
@get_parsl_usage.register(tuple) | ||
def get_parsl_usage_sequence(obj: Sequence): | ||
result = [] | ||
for v in obj: | ||
result += get_parsl_usage(v) | ||
return result | ||
|
||
|
||
class UsageInformation: | ||
@abstractmethod | ||
def get_usage_information(self) -> dict: | ||
pass |
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