Python box.Box() Examples

The following are 30 code examples of box.Box(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module box , or try the search function .
Example #1
Source File: box_list.py    From Box with MIT License 6 votes vote down vote up
def from_json(cls, json_string: str = None, filename: str = None, encoding: str = 'utf-8', errors: str = 'strict',
                  multiline: bool = False, **kwargs):
        """
        Transform a json object string into a BoxList object. If the incoming
        json is a dict, you must use Box.from_json.

        :param json_string: string to pass to `json.loads`
        :param filename: filename to open and pass to `json.load`
        :param encoding: File encoding
        :param errors: How to handle encoding errors
        :param multiline: One object per line
        :param kwargs: parameters to pass to `Box()` or `json.loads`
        :return: BoxList object from json data
        """
        bx_args = {}
        for arg in list(kwargs.keys()):
            if arg in BOX_PARAMETERS:
                bx_args[arg] = kwargs.pop(arg)

        data = _from_json(json_string, filename=filename, encoding=encoding,
                          errors=errors, multiline=multiline, **kwargs)

        if not isinstance(data, list):
            raise BoxError(f'json data not returned as a list, but rather a {type(data).__name__}')
        return cls(data, **bx_args) 
Example #2
Source File: box_list.py    From Box with MIT License 6 votes vote down vote up
def from_toml(cls, toml_string: str = None, filename: str = None, key_name: str = 'toml',
                  encoding: str = 'utf-8', errors: str = 'strict', **kwargs):
        """
        Transforms a toml string or file into a BoxList object

        :param toml_string: string to pass to `toml.load`
        :param filename: filename to open and pass to `toml.load`
        :param key_name: Specify the name of the key to pull the list from
            (cannot directly convert from toml)
        :param encoding: File encoding
        :param errors: How to handle encoding errors
        :param kwargs: parameters to pass to `Box()`
        :return:
        """
        bx_args = {}
        for arg in list(kwargs.keys()):
            if arg in BOX_PARAMETERS:
                bx_args[arg] = kwargs.pop(arg)

        data = _from_toml(toml_string=toml_string, filename=filename, encoding=encoding, errors=errors)
        if key_name not in data:
            raise BoxError(f'{key_name} was not found.')
        return cls(data[key_name], **bx_args) 
Example #3
Source File: helpers.py    From tavern with MIT License 6 votes vote down vote up
def validate_regex(response, expression, header=None):
    """Make sure the response matches a regex expression

    Args:
        response (Response): requests.Response object
        expression (str): Regex expression to use
        header (str): Match against a particular header instead of the body

    Returns:
        dict: dictionary of regex: boxed name capture groups
    """
    if header:
        content = response.headers[header]
    else:
        content = response.text

    match = re.search(expression, content) or False
    assert match

    return {"regex": Box(match.groupdict())} 
Example #4
Source File: test_box_list.py    From Box with MIT License 6 votes vote down vote up
def test_box_list(self):
        new_list = BoxList({'item': x} for x in range(0, 10))
        new_list.extend([{'item': 22}])
        assert new_list[-1].item == 22
        new_list.append([{'bad_item': 33}])
        assert new_list[-1][0].bad_item == 33
        assert repr(new_list).startswith("<BoxList:")
        for x in new_list.to_list():
            assert not isinstance(x, (BoxList, Box))
        new_list.insert(0, {'test': 5})
        new_list.insert(1, ['a', 'b'])
        new_list.append('x')
        assert new_list[0].test == 5
        assert isinstance(str(new_list), str)
        assert isinstance(new_list[1], BoxList)
        assert not isinstance(new_list.to_list(), BoxList) 
Example #5
Source File: test_runner.py    From popper with MIT License 6 votes vote down vote up
def test_prepare_environment_with_git(self):
        repo = self.mk_repo()
        conf = ConfigLoader.load(workspace_dir=repo.working_dir)
        with StepRunner(conf) as r:
            step = Box(
                {"name": "a", "env": {"FOO": "BAR"}, "secrets": ["A"]}, default_box=True
            )
            os.environ["A"] = "BC"
            env = r._prepare_environment(step, {"other": "b"})
            expected = {
                "FOO": "BAR",
                "A": "BC",
                "other": "b",
                "GIT_COMMIT": conf.git_commit,
                "GIT_BRANCH": conf.git_branch,
                "GIT_SHA_SHORT": conf.git_sha_short,
                "GIT_REMOTE_ORIGIN_URL": conf.git_remote_origin_url,
            }
            self.assertDictEqual(expected, env)
            os.environ.pop("A") 
Example #6
Source File: test_config.py    From popper with MIT License 6 votes vote down vote up
def test_config_defaults(self):
        conf = ConfigLoader.load()
        expected = Box(
            {
                "skip_clone": False,
                "engine_name": "docker",
                "engine_opts": {},
                "resman_name": "host",
                "resman_opts": {},
                "skip_pull": False,
                "dry_run": False,
                "workspace_dir": os.getcwd(),
                "quiet": False,
                "reuse": False,
                "pty": False,
                "allow_undefined_secrets_in_ci": False,
            },
            default_box=True,
        )

        self.assertEqual(expected, TestPopperConfig.extract_dict(expected, conf)) 
Example #7
Source File: votemanager.py    From SML-Cogs with MIT License 6 votes vote down vote up
def __init__(self, title=None, description=None, role_ids=None, options=None, votes=None, timestamp=None):
        super().__init__()

        if role_ids is None:
            role_ids = BoxList()

        if options is None:
            options = BoxList()

        if votes is None:
            votes = Box()

        self.title = title
        self.description = description
        self.role_ids = role_ids
        self.options = options
        self.votes = votes
        self.timestamp = timestamp 
Example #8
Source File: core.py    From tavern with MIT License 5 votes vote down vote up
def run_stage(sessions, stage, tavern_box, test_block_config):
    """Run one stage from the test

    Args:
        sessions (dict): Dictionary of relevant 'session' objects used for this test
        stage (dict): specification of stage to be run
        tavern_box (box.Box): Box object containing format variables to be used
            in test
        test_block_config (dict): available variables for test
    """
    name = stage["name"]

    r = get_request_type(stage, test_block_config, sessions)

    tavern_box.update(request_vars=r.request_vars)

    expected = get_expected(stage, test_block_config, sessions)

    delay(stage, "before", test_block_config["variables"])

    logger.info("Running stage : %s", name)
    response = r.run()

    verifiers = get_verifiers(stage, test_block_config, sessions, expected)
    for v in verifiers:
        saved = v.verify(response)
        test_block_config["variables"].update(saved)

    tavern_box.pop("request_vars")
    delay(stage, "after", test_block_config["variables"]) 
Example #9
Source File: crapikey.py    From SML-Cogs with MIT License 5 votes vote down vote up
def config(self):
        if self._config is None:
            if os.path.exists(YAML):
                with open(YAML) as f:
                    self._config = Box(yaml.load(f), default_box=True)
        return self._config 
Example #10
Source File: bands.py    From SML-Cogs with MIT License 5 votes vote down vote up
def get_band(self, tag):
        """Get a clan."""
        data = None
        try:
            async with aiohttp.ClientSession(headers=self.headers) as session:
                async with session.get(self.api_url("bands", tag)) as resp:
                    if resp.status == 200:
                        data = await resp.json()
        except json.JSONDecodeError:
            return None

        return Box(data) 
Example #11
Source File: bands.py    From SML-Cogs with MIT License 5 votes vote down vote up
def bands_config(self):
        if os.path.exists(CONFIG_YAML):
            with open(CONFIG_YAML) as f:
                config = Box(yaml.load(f))
            return config
        return None 
Example #12
Source File: bands.py    From SML-Cogs with MIT License 5 votes vote down vote up
def get_band_config(self, tag):
        for band in self.bands_config.bands:
            if band.tag == tag:
                return Box(band, default_box=True)
        return None 
Example #13
Source File: test_runner.py    From popper with MIT License 5 votes vote down vote up
def test_prepare_environment_without_git(self):
        step = Box(
            {"name": "a", "env": {"FOO": "BAR"}, "secrets": ["A"]}, default_box=True
        )
        os.environ["A"] = "BC"

        with StepRunner(ConfigLoader.load(workspace_dir="/tmp/foo")) as r:
            env = r._prepare_environment(step, {"other": "b"})
            self.assertDictEqual({"FOO": "BAR", "A": "BC", "other": "b"}, env)
            os.environ.pop("A")

            # secret undefined should return an empty variable
            env = r._prepare_environment(step, {"other": "b"})
            self.assertDictEqual({"FOO": "BAR", "A": "", "other": "b"}, env) 
Example #14
Source File: test_runner_host.py    From popper with MIT License 5 votes vote down vote up
def test_create_container(self):
        config = ConfigLoader.load()
        step = Box(
            {
                "uses": "docker://alpine:3.9",
                "runs": ["echo hello"],
                "id": "kontainer_one",
            },
            default_box=True,
        )
        cid = pu.sanitized_name(step.id, config.wid)
        with DockerRunner(init_docker_client=True, config=config) as dr:
            c = dr._create_container(cid, step)
            self.assertEqual(c.status, "created")
            c.remove()
        step = Box(
            {
                "uses": "docker://alpine:3.9",
                "runs": ["echo", "hello_world"],
                "id": "KoNtAiNeR tWo",
            },
            default_box=True,
        )
        cid = pu.sanitized_name(step.id, config.wid)
        with DockerRunner(init_docker_client=True, config=config) as dr:
            c = dr._create_container(cid, step)
            self.assertEqual(c.status, "created")
            c.remove() 
Example #15
Source File: test_runner_host.py    From popper with MIT License 5 votes vote down vote up
def test_get_build_info(self):
        step = Box(
            {"uses": "popperized/bin/sh@master", "args": ["ls"], "id": "one",},
            default_box=True,
        )
        with DockerRunner(init_docker_client=False) as dr:
            build, img, tag, build_sources = dr._get_build_info(step)
            self.assertEqual(build, True)
            self.assertEqual(img, "popperized/bin")
            self.assertEqual(tag, "master")
            self.assertTrue(f"{os.environ['HOME']}/.cache/popper" in build_sources)
            self.assertTrue("github.com/popperized/bin/sh" in build_sources)

            step = Box(
                {
                    "uses": "docker://alpine:3.9",
                    "runs": ["sh", "-c", "echo $FOO > hello.txt ; pwd"],
                    "env": {"FOO": "bar"},
                    "id": "1",
                },
                default_box=True,
            )

        with DockerRunner(init_docker_client=False) as dr:
            build, img, tag, build_sources = dr._get_build_info(step)
            self.assertEqual(build, False)
            self.assertEqual(img, "alpine")
            self.assertEqual(tag, "3.9")
            self.assertEqual(build_sources, None) 
Example #16
Source File: test_runner_host.py    From popper with MIT License 5 votes vote down vote up
def test_create_container(self):
        config = ConfigLoader.load()
        step_one = Box(
            {
                "uses": "docker://alpine:3.9",
                "runs": ["echo hello"],
                "id": "kontainer_one",
            },
            default_box=True,
        )

        step_two = Box(
            {
                "uses": "popperized/bin/sh@master",
                "args": ["ls"],
                "id": "kontainer_two",
            },
            default_box=True,
        )

        cid_one = pu.sanitized_name(step_one.id, config.wid)
        cid_two = pu.sanitized_name(step_two.id, config.wid)

        with SingularityRunner(config=config) as sr:
            sr._setup_singularity_cache()
            sr._create_container(step_one, cid_one)
            self.assertEqual(
                os.path.exists(os.path.join(sr._singularity_cache, cid_one)), True
            )
            os.remove(os.path.join(sr._singularity_cache, cid_one))

        with SingularityRunner(config=config) as sr:
            sr._setup_singularity_cache()
            sr._create_container(step_one, cid_two)
            self.assertEqual(
                os.path.exists(os.path.join(sr._singularity_cache, cid_two)), True
            )
            os.remove(os.path.join(sr._singularity_cache, cid_two)) 
Example #17
Source File: test_runner_host.py    From popper with MIT License 5 votes vote down vote up
def test_get_build_info(self):
        step = Box(
            {"uses": "popperized/bin/sh@master", "args": ["ls"], "name": "one",},
            default_box=True,
        )
        with SingularityRunner() as sr:
            build, img, build_sources = sr._get_build_info(step)
            self.assertEqual(build, True)
            self.assertEqual(img, "popperized/bin")
            self.assertTrue(f"{os.environ['HOME']}/.cache/popper" in build_sources)
            self.assertTrue(f"github.com/popperized/bin/sh" in build_sources)

            step = Box(
                {
                    "uses": "docker://alpine:3.9",
                    "runs": ["sh", "-c", "echo $FOO > hello.txt ; pwd"],
                    "env": {"FOO": "bar"},
                    "name": "1",
                },
                default_box=True,
            )

        with SingularityRunner() as sr:
            build, img, build_sources = sr._get_build_info(step)
            self.assertEqual(build, False)
            self.assertEqual(img, "docker://alpine:3.9")
            self.assertEqual(build_sources, None) 
Example #18
Source File: racf.py    From SML-Cogs with MIT License 5 votes vote down vote up
def __init__(self, bot):
        """Constructor."""
        self.bot = bot
        with open(os.path.join("data", "racf", "config.yaml")) as f:
            self.config = Box(yaml.load(f, Loader=yaml.FullLoader))
        self.settings = dataIO.load_json(JSON) 
Example #19
Source File: request.py    From tavern with MIT License 5 votes vote down vote up
def request_vars(self):
        return Box(self._request_args) 
Example #20
Source File: request.py    From tavern with MIT License 5 votes vote down vote up
def request_vars(self):
        return Box(self._original_publish_args) 
Example #21
Source File: dict_util.py    From tavern with MIT License 5 votes vote down vote up
def format_keys(val, variables, no_double_format=True):
    """recursively format a dictionary with the given values

    Args:
        val (object): Input dictionary to format
        variables (dict): Dictionary of keys to format it with
        no_double_format (bool): Whether to use the 'inner formatted string' class to avoid double formatting
            This is required if passing something via pytest-xdist, such as markers:
            https://github.com/taverntesting/tavern/issues/431

    Returns:
        str,int,list,dict: recursively formatted values
    """
    formatted = val
    box_vars = Box(variables)

    if isinstance(val, dict):
        formatted = {}
        # formatted = {key: format_keys(val[key], box_vars) for key in val}
        for key in val:
            formatted[key] = format_keys(val[key], box_vars)
    elif isinstance(val, (list, tuple)):
        formatted = [format_keys(item, box_vars) for item in val]
    elif isinstance(formatted, _FormattedString):
        logger.debug("Already formatted %s, not double-formatting", formatted)
    elif isinstance(val, str):
        formatted = _check_and_format_values(val, box_vars)

        if no_double_format:
            formatted = _FormattedString(formatted)
    elif isinstance(val, TypeConvertToken):
        logger.debug("Got type convert token '%s'", val)
        if isinstance(val, ForceIncludeToken):
            formatted = _attempt_find_include(val.value, box_vars)
        else:
            value = format_keys(val.value, box_vars)
            formatted = val.constructor(value)
    else:
        logger.debug("Not formatting something of type '%s'", type(formatted))

    return formatted 
Example #22
Source File: util.py    From tavern with MIT License 5 votes vote down vote up
def load_global_cfg(pytest_config):
    """Load globally included config files from cmdline/cfg file arguments

    Args:
        pytest_config (pytest.Config): Pytest config object

    Returns:
        dict: variables/stages/etc from global config files

    Raises:
        exceptions.UnexpectedKeysError: Invalid settings in one or more config
            files detected
    """
    # Load ini first
    ini_global_cfg_paths = pytest_config.getini("tavern-global-cfg") or []
    # THEN load command line, to allow overwriting of values
    cmdline_global_cfg_paths = pytest_config.getoption("tavern_global_cfg") or []

    all_paths = ini_global_cfg_paths + cmdline_global_cfg_paths
    global_cfg = load_global_config(all_paths)

    try:
        loaded_variables = global_cfg["variables"]
    except KeyError:
        logger.debug("Nothing to format in global config files")
    else:
        tavern_box = Box({"tavern": {"env_vars": dict(os.environ)}})

        global_cfg["variables"] = format_keys(loaded_variables, tavern_box)

    # Can be overridden in tests
    global_cfg["strict"] = _load_global_strictness(pytest_config)
    global_cfg["follow_redirects"] = _load_global_follow_redirects(pytest_config)
    global_cfg["backends"] = _load_global_backends(pytest_config)
    global_cfg["merge_ext_values"] = _load_global_merge_ext(pytest_config)

    logger.debug("Global config: %s", global_cfg)

    return global_cfg 
Example #23
Source File: file.py    From tavern with MIT License 5 votes vote down vote up
def _get_test_fmt_vars(self, test_spec):
        """Get any format variables that can be inferred for the test at this point

        Args:
            test_spec (dict): Test specification, possibly with included config files

        Returns:
            dict: available format variables
        """
        # Get included variables so we can do things like:
        # skipif: {my_integer} > 2
        # skipif: 'https' in '{hostname}'
        # skipif: '{hostname}'.contains('ignoreme')
        fmt_vars = {}

        global_cfg = load_global_cfg(self.config)
        fmt_vars.update(**global_cfg.get("variables", {}))

        included = test_spec.get("includes", [])
        for i in included:
            fmt_vars.update(**i.get("variables", {}))

        # Needed if something in a config file uses tavern.env_vars
        tavern_box = Box({"tavern": {"env_vars": dict(os.environ)}})

        try:
            fmt_vars = _format_without_inner(fmt_vars, tavern_box)
        except exceptions.MissingFormatError as e:
            # eg, if we have {tavern.env_vars.DOESNT_EXIST}
            msg = "Tried to use tavern format variable that did not exist"
            raise exceptions.MissingFormatError(msg) from e

        return fmt_vars 
Example #24
Source File: loader.py    From PyFunceble with Apache License 2.0 5 votes vote down vote up
def empty_config(func):  # pylint: disable=no-self-argument
        """
        Decorator which will empty the configuration
        before calling the wrapped method.
        """

        @wraps(func)
        def wrapper(self, *args, **kwargs):
            self.config = Box({}, default_box=True, default_box_attr=None)

            return func(self, *args, **kwargs)  # pylint: disable=not-callable

        return wrapper 
Example #25
Source File: loader.py    From PyFunceble with Apache License 2.0 5 votes vote down vote up
def get_config(self) -> Box:
        """
        Provides the configuration to use.
        """

        return self.config 
Example #26
Source File: models.py    From brawlstats with MIT License 5 votes vote down vote up
def from_data(self, data):
        self.raw_data = data
        if isinstance(data, list):
            self._boxed_data = BoxList(
                data, camel_killer_box=True
            )
        else:
            self._boxed_data = Box(
                data, camel_killer_box=True
            )
        return self 
Example #27
Source File: votemanager.py    From SML-Cogs with MIT License 5 votes vote down vote up
def __init__(self, bot):
        """Init."""
        self.bot = bot
        self.settings = Box(dataIO.load_json(JSON), default_box=True) 
Example #28
Source File: models.py    From clashroyale with MIT License 5 votes vote down vote up
def from_data(self, data, cached, ts, response):
        self.cached = cached
        self.last_updated = ts
        self.raw_data = data
        self.response = response
        if isinstance(data, list):
            self._boxed_data = BoxList(
                data, camel_killer_box=not self.client.camel_case
            )
        else:
            self._boxed_data = Box(
                data, camel_killer_box=not self.client.camel_case
            )
        return self 
Example #29
Source File: test_k8s.py    From eks-rolling-update with Apache License 2.0 5 votes vote down vote up
def test_k8s_nodes_ready(self):
        with patch('eksrollup.lib.k8s.get_k8s_nodes') as get_k8s_nodes_mock:
            box = Box(self.k8s_response_mock, ordered_box=True)
            get_k8s_nodes_mock.return_value = box['items']
            self.assertTrue(k8s_nodes_ready(2, 1), True) 
Example #30
Source File: test_k8s.py    From eks-rolling-update with Apache License 2.0 5 votes vote down vote up
def test_k8s_nodes_ready_fail(self):
        with patch('eksrollup.lib.k8s.get_k8s_nodes') as get_k8s_nodes_mock:
            box = Box(self.k8s_response_mock_unhealthy, ordered_box=True)
            get_k8s_nodes_mock.return_value = box['items']
            self.assertFalse(k8s_nodes_ready(2, 1), False)