Skip to content

Commit

Permalink
Fix: refactor the missing keyword argument logic
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Dec 18, 2020
1 parent 257d4c1 commit 229f09b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
32 changes: 9 additions & 23 deletions calcipy/doit_helpers/doit_globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,38 @@
"""DoIt task type for annotations."""


def _member_filter(member: Any, instance_type: Any, prefix: Optional[str],
debug: bool = False) -> bool:
def _member_filter(member: Any, instance_type: Any) -> bool:
"""Return True if the member matches the filters.
Args:
cls: class
instance_type: optional instance type
prefix: optional string prefix to check starts with
debug: if True, will log debug information
Returns:
List[Tuple[str, Callable]]: filtered members from the class
"""
match_instance = (instance_type is None or isinstance(member, instance_type))
match_prefix = (prefix is None or (hasattr(member, '__name__') # noqa: P002
and member.__name__.startswith(prefix)))
if debug:
name = member.__name__ if hasattr(member, '__name__') else '__no_name__' # noqa: P002
logger.debug('{match_instance} and {match_prefix} ({name}={member})', match_instance=match_instance,
match_prefix=match_prefix, member=member, name=name)
return match_instance and match_prefix
return (instance_type is None or isinstance(member, instance_type))


def _get_members(cls: object, **kwargs: Any) -> List[Tuple[str, Callable]]:
def _get_members(cls: object, prefix: Optional[str], **kwargs: Any) -> List[Tuple[str, Callable]]:
"""Return the members that match the parameters.
Example to return all methods that start with `do_`: `_get_members(cls, instance_type=Callable, prefix='do_')`
Args:
cls: class
prefix: optional string prefix to check starts with
**kwargs: keyword arguments passed to `_member_filter`
Returns:
List[Tuple[str, Callable]]: filtered members from the class
"""
return inspect.getmembers(cls, predicate=partial(_member_filter, **kwargs))
members = inspect.getmembers(cls, predicate=partial(_member_filter, **kwargs))
if prefix:
members = [(name, member) for (name, member) in members if name.startswith(prefix)]
return members # noqa: R504


def _verify_initialized_paths(cls: object) -> None:
Expand All @@ -72,15 +66,7 @@ def _verify_initialized_paths(cls: object) -> None:
"""
logger.info(f'Class: {cls}')

missing = []
for name, path_raw in _get_members(cls, instance_type=(type(Path()), type(None)), prefix=None):
if name.startswith('path_') and path_raw is None:
# ^ PLANNED: filter for startswith in `_get_members` instead of `_member_filter`
missing.append(name)
else:
logger.warning(f'{name}, {path_raw}, {type(path_raw)}')

missing = [name for name, _m in _get_members(cls, instance_type=type(None), prefix='path_')]
if missing:
kwargs = ', '.join(missing)
raise RuntimeError(f'Missing keyword arguments for: {kwargs}')
Expand Down
4 changes: 2 additions & 2 deletions calcipy/log_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import time
from inspect import signature
from typing import Any, Callable, Dict
from typing import Any, Callable, Dict, Type

from decorator import contextmanager, decorator
from loguru import logger
Expand Down Expand Up @@ -56,7 +56,7 @@ def serializable_compact(record: Dict[str, Any]) -> str:


@contextmanager
def log_action(message: str, level: str = 'INFO', _logger: type(logger) = logger, **kwargs: Any) -> None:
def log_action(message: str, level: str = 'INFO', _logger: Type(logger) = logger, **kwargs: Any) -> None:
"""Log the beggining and end of an action.
Args:
Expand Down

0 comments on commit 229f09b

Please sign in to comment.