Skip to content

Commit

Permalink
release 0.2.0: mixin & util/exists_module
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyElaina committed Jun 24, 2022
1 parent 39d8174 commit 5ad10ed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
46 changes: 39 additions & 7 deletions creart/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import annotations

import functools
import importlib.util
import os
import sys
import types
from importlib.metadata import entry_points
from typing import Any, TypeVar

Expand All @@ -23,9 +27,7 @@ def _env_scan():


def add_creator(creator: type[AbstractCreator]):
intersection = {f"{i.module}:{i.identify}" for i in creator.targets}.intersection(
_mapping.keys()
)
intersection = {f"{i.module}:{i.identify}" for i in creator.targets}.intersection(_mapping.keys())
if intersection:
raise ValueError(f"conclict target for {', '.join(intersection)}")
_creators.append(creator)
Expand All @@ -42,9 +44,7 @@ def supported(target: type):

def _assert_supported(target: type):
if not supported(target):
raise TypeError(
f"current environment does not contain support for {_signature(target)}"
)
raise TypeError(f"current environment does not contain support for {_signature(target)}")


def _get_creator(target: type) -> type[AbstractCreator]:
Expand All @@ -64,7 +64,39 @@ def create(target_type: type[T], *, cache: bool = True) -> T:
return result


def exists_module(package: str) -> bool:
return package in sys.modules or importlib.util.find_spec(package) is not None


def mixin(*creators: AbstractCreator):
def wrapper(target: Any):
if isinstance(target, staticmethod):
target = target.__func__
if isinstance(target, types.FunctionType):
if target.__name__ == "available":

@functools.wraps(target)
def inner():
return all([target(), *[i.available() for i in creators]])

return inner
else:
raise ValueError(f"no supported mixin for {target.__name__}")
elif isinstance(target, type) and issubclass(target, AbstractCreator):
origin = target.available

@functools.wraps(origin)
def inner():
return all([origin(), *[i.available() for i in creators]])

return inner
else:
raise TypeError(f"no supported mixin for {type(target)}")

return wrapper


it = create

if os.getenv("CREART_AUTO_SCAN") not in {"false", "False", "0", "no", "off"}:
_env_scan()
_env_scan()
4 changes: 3 additions & 1 deletion creart/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
class CreateTargetInfo:
module: str
identify: str

# info for cli
humanized_name: str | None = None
description: str | None = None
author: list[str] | None = None


T = TypeVar("T")


class AbstractCreator(metaclass=ABCMeta):
targets: ClassVar[tuple[CreateTargetInfo, ...]]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "creart"
version = "0.1.1"
version = "0.2.0"
description = "a universal, extensible class instantiation helper"
authors = [
{name = "GreyElaina", email = "[email protected]"},
Expand Down

0 comments on commit 5ad10ed

Please sign in to comment.