Skip to content

Commit

Permalink
Create AutoReport class
Browse files Browse the repository at this point in the history
  • Loading branch information
banesullivan committed Oct 20, 2023
1 parent 15ba95f commit 93098d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion scooby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
meets_version,
version_tuple,
)
from scooby.report import Report, get_version
from scooby.report import AutoReport, Report, get_version
from scooby.tracker import TrackedReport, track_imports, untrack_imports

doo = Report

__all__ = [
'AutoReport',
'Report',
'TrackedReport',
'doo',
Expand Down
35 changes: 35 additions & 0 deletions scooby/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,41 @@ def to_dict(self) -> Dict[str, str]:
return out


class AutoReport(Report):
"""Auto-generate a scooby.Report for a package.
This will check if the specified package has a ``Report`` class and use that or
fallback to generating a report based on the distribution requirements of the package.
"""

def __init__(self, module, additional=None, ncol=3, text_width=80, sort=False):
"""Initialize."""
if not isinstance(module, (str, ModuleType)):
raise TypeError("Cannot generate report for type " "({})".format(type(module)))

if isinstance(module, ModuleType):
module = module.__name__

try:
package = importlib.import_module(module)
if issubclass(package.Report, Report):
package.Report.__init__(
self, additional=additional, ncol=ncol, text_width=text_width, sort=sort
)
except (AttributeError, TypeError):
# Autogenerate from distribution requirements
core = [module, *get_distribution_dependencies(module)]
Report.__init__(
self,
additional=additional,
core=core,
optional=[],
ncol=ncol,
text_width=text_width,
sort=sort,
)


# This functionaliy might also be of interest on its own.
def get_version(module: Union[str, ModuleType]) -> Tuple[str, Optional[str]]:
"""Get the version of ``module`` by passing the package or it's name.
Expand Down

0 comments on commit 93098d3

Please sign in to comment.