How to use the dffml.df.base.OperationImplementation function in dffml

To help you get started, we’ve selected a few dffml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github intel / dffml / dffml / df / base.py View on Github external
uses_config = None
        if config_cls is not None:
            for name, param in sig.parameters.items():
                if param.annotation is config_cls:
                    uses_config = name

        # Create the test method which creates the contexts and runs
        async def test(**kwargs):
            async with func.imp(BaseConfig()) as obj:
                async with obj(None, None) as ctx:
                    return await ctx.run(kwargs)

        func.test = test

        class Implementation(
            context_stacker(OperationImplementation, imp_enter)
        ):
            def __init__(self, config):
                if config_cls is not None and isinstance(config, dict):
                    if getattr(config_cls, "_fromdict", None) is not None:
                        # Use _fromdict method if it exists
                        config = config_cls._fromdict(**config)
                    else:
                        # Otherwise expand if existing config is a dict
                        config = config_cls(**config)
                super().__init__(config)

        if config_cls is not None:
            Implementation.CONFIG = config_cls

        if inspect.isclass(func) and issubclass(
            func, OperationImplementationContext
github intel / dffml / feature / auth / dffml_feature_auth / feature / operations.py View on Github external
hashed_password = hashed_password.hex()
        salt = salt.hex()

        return hashed_password, salt

    async def run(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
        # TODO raise error if longer than 1024 (validation should be done before
        # we submit to the thread pool. Weird behavior can happen if we raise in
        # there.
        hashed_password, salt = await self.parent.loop.run_in_executor(
            self.parent.pool, self.hash_password, inputs["password"]
        )
        return {"password": {"hashed": hashed_password, "salt": salt}}


class Scrypt(OperationImplementation):

    op = scrypt
    CONTEXT = ScryptContext

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.loop = None
        self.pool = None
        self.__pool = None

    async def __aenter__(self) -> "OperationImplementationContext":
        self.loop = asyncio.get_event_loop()
        # ProcessPoolExecutor is slightly faster but deprecated and will be
        # removed in 3.9
        self.pool = concurrent.futures.ThreadPoolExecutor()
        self.__pool = self.pool.__enter__()
github intel / dffml / dffml / df / memory.py View on Github external
# Take the lock
                self.logger.debug("Acquiring: %s(%r)", item.uid, item.value)
                await stack.enter_async_context(lock)
                self.logger.debug("Acquired: %s(%r)", item.uid, item.value)
            # All locks for these parameters have been acquired
            yield


@entry_point("memory")
class MemoryLockNetwork(BaseLockNetwork, BaseMemoryDataFlowObject):

    CONTEXT = MemoryLockNetworkContext


class MemoryOperationImplementationNetworkConfig(NamedTuple):
    operations: Dict[str, OperationImplementation]


class MemoryOperationImplementationNetworkContext(
    BaseOperationImplementationNetworkContext
):
    def __init__(
        self,
        config: BaseConfig,
        parent: "MemoryOperationImplementationNetwork",
    ) -> None:
        super().__init__(config, parent)
        self.opimps = self.parent.config.operations
        self.operations = {}
        self.completed_event = asyncio.Event()

    async def __aenter__(
github intel / dffml / dffml / df / base.py View on Github external
def isopimp(item):
    """
    Similar to inspect.isclass and that family of functions. Returns true if
    item is a subclass of OperationImpelmentation.

    >>> # Get all operation implementations imported in a file
    >>> list(map(lambda item: item[1],
    >>>          inspect.getmembers(sys.modules[__name__],
    >>>                             predicate=isopimp)))
    """
    return bool(
        (
            inspect.isclass(item)
            and issubclass(item, OperationImplementation)
            and item is not OperationImplementation
        )
        or (
            inspect.ismethod(item)
            and issubclass(item.__self__, OperationImplementationContext)
            and item.__name__ == "imp"
        )
github intel / dffml / dffml_operations_binsec / operations.py View on Github external
self.__class__.__qualname__,
            self.URL,
            self.body[:10],
        )


class URLToURLBytesContext(OperationImplementationContext):
    async def run(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
        self.logger.debug("Start resp: %s", inputs["URL"])
        async with self.parent.session.get(inputs["URL"]) as resp:
            return {
                "download": URLBytesObject(URL=inputs["URL"], body=await resp.read())
            }


class URLToURLBytes(OperationImplementation):

    op = url_to_urlbytes
    CONTEXT = URLToURLBytesContext

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.client = None
        self.session = None

    async def __aenter__(self) -> "OperationImplementationContext":
        self.client = aiohttp.ClientSession(trust_env=True)
        self.session = await self.client.__aenter__()
        return self

    async def __aexit__(self, exc_type, exc_value, traceback):
        await self.client.__aexit__(exc_type, exc_value, traceback)
github intel / dffml / dffml / df / base.py View on Github external
def isopimp(item):
    """
    Similar to inspect.isclass and that family of functions. Returns true if
    item is a subclass of OperationImpelmentation.

    >>> # Get all operation implementations imported in a file
    >>> list(map(lambda item: item[1],
    >>>          inspect.getmembers(sys.modules[__name__],
    >>>                             predicate=isopimp)))
    """
    return bool(
        (
            inspect.isclass(item)
            and issubclass(item, OperationImplementation)
            and item is not OperationImplementation
        )
        or (
            inspect.ismethod(item)
            and issubclass(item.__self__, OperationImplementationContext)
            and item.__name__ == "imp"
        )