Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
pyckles / contexts / __init__.py
Size: Mime:
# -*- coding: utf-8 -*-
import abc
import importlib
import inspect
import os
from collections import Sequence

import six


@six.add_metaclass(abc.ABCMeta)
class PycklesContext(object):
    def __init__(self, pycklets_package_names=None, config=None, debug=None):

        if pycklets_package_names is None:
            pycklets_package_names = ["pycklets"]
        elif isinstance(pycklets_package_names, six.string_types):
            pycklets_package_names = [pycklets_package_names]

        self._base_packages = {}
        self._context_repos = []

        if debug is None:
            debug = False

        self._debug = debug

        for base_package_name in pycklets_package_names:
            try:
                bp = importlib.import_module(base_package_name)
                base_module_path = inspect.getfile(bp)
                # base_resource_path = base_module_path
                # for _ in base_package_name.split("."):
                #     base_resource_path = os.path.join(base_resource_path, "..")
                # _path_tokens = [
                #     base_resource_path,
                #     "..",
                #     "resources",
                # ] + base_package_name.split(".")
                base_resource_path = os.path.realpath(
                    os.path.join(os.path.dirname(base_module_path), "resources")
                )

                resource_types = []
                for child in os.listdir(base_resource_path):

                    f = os.path.join(base_resource_path, child)
                    if not os.path.isdir(f):
                        continue
                    self._context_repos.append("{}::{}".format(child, f))
                    resource_types.append(child)

                self._base_packages[base_package_name] = {
                    "module_path": base_module_path,
                    "resources": base_resource_path,
                    "resource_types": resource_types,
                }

            except (Exception) as e:
                raise Exception(
                    "Can't load pycklets from module path '{}': {}".format(
                        base_package_name, e
                    )
                )

        if config is None:
            config = {}

        self._context_config = config

        if "callback" not in self._context_config.keys():
            self._context_config["callback"] = [{"log": {"profile": "DEBUG"}}]

        pass

    @property
    def debug(self):

        return self._debug

    @property
    def context_config(self):

        return self._context_config

    @property
    def context_repos(self):

        return self._context_repos

    def run(self, pycklets, inventory=None, run_config=None):
        """Run one or several pycklets.

        Args:
            pycklets: one or several Pycklets
            inventory: an inventory to draw variable values from (in case the there are any)
            run_config: either a FrecklesRunConfig object, a dictionary with config values, or a target string (e.g. admin@example.com)
        Returns:
            dict: the registered result values of the run
        """

        if not isinstance(pycklets, Sequence):
            pycklets = [pycklets]

        frecklet_list = []
        for p in pycklets:
            frecklet_list.extend(p.frecklets())

        return self._run(
            frecklet_list=frecklet_list, inventory=inventory, run_config=run_config
        )

    @abc.abstractmethod
    def _run(self, frecklet_list, inventory=None, run_config=None):

        pass


try:
    from pyckles.contexts.python import PythonContext as DEFAULT_CONTEXT_CLASS
except (Exception):
    from pyckles.contexts.freck import FreckContext as DEFAULT_CONTEXT_CLASS

default_context = DEFAULT_CONTEXT_CLASS(pycklets_package_names=["pycklets"])
debug_context = DEFAULT_CONTEXT_CLASS(
    pycklets_package_names=["pycklets"],
    config={"callback": [{"default": {"profile": "full"}}], "keep_run_folder": True},
)