def setup_collector(self, args_dict, logging_handler, extra_files): """Create and initialize a collector""" config_name = None if args_dict["photons_app"]["config"] is not sb.NotSpecified: config_name = args_dict["photons_app"]["config"].name collector = Collector() collector.prepare(config_name, args_dict, extra_files=extra_files) if "term_colors" in collector.configuration: self.setup_logging_theme(logging_handler, colors=collector.configuration["term_colors"]) return collector
def collector(self): with OverrideLoop(new_loop=False): superman = mock.Mock( name="resolvedsuperman", instantiated_name="superman", spec=["instantiated_name"], ) batman = mock.Mock( name="resolvedbatman", instantiated_name="batman", spec=["instantiated_name"] ) vegemite = mock.Mock( name="resolvedvegemite", instantiated_name="vegemite", spec=["instantiated_name"], ) road = mock.Mock( name="resolvedroad", instantiated_name="road", spec=["instantiated_name"] ) collector = Collector() collector.prepare(None, {}) reg = collector.configuration["target_register"] HeroTarget = mock.Mock(name="HeroTarget") herotarget = Target.FieldSpec().empty_normalise(type="hero") reg.register_type("hero", HeroTarget) VillianTarget = mock.Mock(name="VillianTarget") villiantarget = Target.FieldSpec().empty_normalise(type="villian") reg.register_type("villian", VillianTarget) InfraTarget = mock.Mock(name="InfraTarget") infratarget = Target.FieldSpec().empty_normalise(type="infrastructure") reg.register_type("infrastructure", InfraTarget) supermancreator = mock.Mock(name="supermancreator", return_value=superman) reg.add_target("superman", herotarget, supermancreator) batmancreator = mock.Mock(name="batmancreator", return_value=batman) reg.add_target("batman", herotarget, batmancreator) vegemitecreator = mock.Mock(name="vegemitecreator", return_value=vegemite) reg.add_target("vegemite", villiantarget, vegemitecreator) roadcreator = mock.Mock(name="roadcreator", return_value=road) reg.add_target("road", infratarget, roadcreator) yield collector
def library_setup(config_filename="lifx.yml", photons_modules=True, extra_files=None, photons_app_options=None): """ Get us a setup photons Collector instance. config_filename Optionally provide the filename of configuration to read in and parse photons_modules If this is set to True then all photons modules in your environment will be activated. If it is an array, then only the modules in that array will be activated. If you only want modules from photons-core then say photons_modules=["core"] extra_files Optionally provide extra configuration files to load in photons_app_options Options to instantiate the photons_app object with. This function returns :ref:`photons_app_collector`. """ photons_app = photons_app_options if photons_app_options is not None else {} def init_addons(adns): if "addons" not in photons_app: photons_app["addons"] = {} if "lifx.photons" not in photons_app["addons"]: photons_app["addons"]["lifx.photons"] = adns if photons_modules is True: init_addons(["__all__"]) elif type(photons_modules) is str: init_addons([photons_modules]) elif type(photons_modules) is list: init_addons(photons_modules) collector = Collector() collector.prepare(config_filename, {"photons_app": photons_app}, extra_files=extra_files) collector.configuration["target_register"].add_targets( collector.configuration["targets"]) return collector
# coding: spec from photons_socket.target import SocketTarget from photons_app.test_helpers import TestCase from photons_app.collector import Collector from photons_app import helpers as hp from photons_messages import DiscoveryMessages from textwrap import dedent describe TestCase, "Addon": it "works": collector = Collector() with hp.a_temp_file() as fle: fle.write(dedent(""" --- photons_app: addons: lifx.photons: - socket targets: lan: type: lan """).encode()) fle.flush() collector.prepare(fle.name, {})
from input_algorithms import spec_base as sb from option_merge_addons import Register from option_merge import MergedOptions from input_algorithms.meta import Meta from contextlib import contextmanager from option_merge.path import Path from textwrap import dedent import pkg_resources import asyncio import uuid import mock import os describe TestCase, "Collector": it "can be cloned": collector = Collector() prepare = mock.Mock(name="prepare") with hp.a_temp_file() as fle: fle.write(dedent(""" --- photons_app: artifact: "blah" ignored: "an option" """).encode()) fle.close() collector.prepare(fle.name, {"one": 2}) with mock.patch.object(Collector, "prepare", prepare):
def collector(): with OverrideLoop(new_loop=False): collector = Collector() collector.prepare(None, {}) yield collector
from delfick_project.option_merge import MergedOptions from delfick_project.option_merge.path import Path from delfick_project.addons import Register from contextlib import contextmanager from textwrap import dedent from unittest import mock import asyncio import pytest import uuid import os describe "Collector": it "has a shortcut to the photons_app": collector = Collector() collector.prepare(None, {}) assert collector.photons_app is collector.configuration["photons_app"] it "has a shortcut to resolve a target": collector = Collector() collector.prepare(None, {}, extra_files=[{"targets": {"lan": {"type": "lan"}}}]) class Target(dictobj.Spec): pass collector.configuration["target_register"].register_type("lan", Target.FieldSpec()) collector.configuration["target_register"].add_targets(collector.configuration["targets"]) lan = collector.configuration["target_register"].resolve("lan") assert collector.resolve_target("lan") is lan
def collector(s): collector = Collector() collector.prepare(None, {}) return collector
fle.close() with open(fle.name) as realfile: args_dict = { "photons_app": { "config": realfile } } app = App() logging_handler = mock.Mock(name="logging_handler") collector = app.setup_collector(args_dict, logging_handler, None) return fle.name, args_dict, logging_handler, collector it "prepares the collector with the photons_app.config": collector = Collector() FakeCollector = mock.Mock(name="Collector", return_value=collector) original_prepare = Collector.prepare def prepare(*args, **kwargs): return original_prepare(collector, *args, **kwargs) prepare = mock.Mock(name="prepare", side_effect=prepare) with mock.patch("photons_app.executor.Collector", FakeCollector): with mock.patch.object(Collector, "prepare", prepare): location, args_dict, logging_handler, collector = self.from_config("") prepare.assert_called_once_with(location, args_dict, extra_files=None) it "gets us the target from chosen_task if we haven't already got a target":