mozilla_schema_generator.config

 1# -*- coding: utf-8 -*-
 2
 3# This Source Code Form is subject to the terms of the Mozilla Public
 4# License, v. 2.0. If a copy of the MPL was not distributed with this
 5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6
 7from __future__ import annotations
 8
 9import queue
10from typing import Dict, List, Tuple
11
12from .matcher import Matcher
13
14# TODO: s/probes/probe
15from .probes import Probe
16from .utils import _get, prepend_properties
17
18
19class Config(object):
20    match_key = "match"
21
22    def __init__(self, *args, **kwargs):
23        self.name = args[0]
24        if "matchers" in kwargs:
25            self.matchers = kwargs["matchers"]
26        else:
27            self._set_matchers(args[1])
28
29    def _set_matchers(self, config: dict) -> Dict[Tuple[str], Matcher]:
30        """
31        Transform the nested config into a single dictionary
32        """
33        keys = queue.SimpleQueue()
34        matchers = {}
35
36        for key, v in config.items():
37            if isinstance(v, dict):
38                keys.put((key,))
39
40        while not keys.empty():
41            key = keys.get()
42            elem = _get(config, key)
43
44            if self.match_key in elem:
45                matchers[key] = Matcher(elem[self.match_key])
46            else:
47                for k, v in elem.items():
48                    if isinstance(v, dict):
49                        keys.put(key + (k,))
50
51        self.matchers = matchers
52
53    def get_match_keys(self) -> List[Tuple[str]]:
54        return [prepend_properties(key) for key in self.matchers.keys()]
55
56    def get_schema_elements(self, probes: List[Probe]) -> List[Tuple[tuple, Probe]]:
57        """
58        Given a schema and set of probes, get a list of probe and
59        the location in the schema where those probes should be
60        inputted.
61        """
62        schema_elements = []
63
64        for key, matcher in self.matchers.items():
65            # Get the element we are filling in from the schema
66            schema_key = prepend_properties(key)
67
68            # Get the probes for the fill-in
69            schema_elements += [(schema_key, p) for p in probes if matcher.matches(p)]
70
71        return schema_elements
class Config:
20class Config(object):
21    match_key = "match"
22
23    def __init__(self, *args, **kwargs):
24        self.name = args[0]
25        if "matchers" in kwargs:
26            self.matchers = kwargs["matchers"]
27        else:
28            self._set_matchers(args[1])
29
30    def _set_matchers(self, config: dict) -> Dict[Tuple[str], Matcher]:
31        """
32        Transform the nested config into a single dictionary
33        """
34        keys = queue.SimpleQueue()
35        matchers = {}
36
37        for key, v in config.items():
38            if isinstance(v, dict):
39                keys.put((key,))
40
41        while not keys.empty():
42            key = keys.get()
43            elem = _get(config, key)
44
45            if self.match_key in elem:
46                matchers[key] = Matcher(elem[self.match_key])
47            else:
48                for k, v in elem.items():
49                    if isinstance(v, dict):
50                        keys.put(key + (k,))
51
52        self.matchers = matchers
53
54    def get_match_keys(self) -> List[Tuple[str]]:
55        return [prepend_properties(key) for key in self.matchers.keys()]
56
57    def get_schema_elements(self, probes: List[Probe]) -> List[Tuple[tuple, Probe]]:
58        """
59        Given a schema and set of probes, get a list of probe and
60        the location in the schema where those probes should be
61        inputted.
62        """
63        schema_elements = []
64
65        for key, matcher in self.matchers.items():
66            # Get the element we are filling in from the schema
67            schema_key = prepend_properties(key)
68
69            # Get the probes for the fill-in
70            schema_elements += [(schema_key, p) for p in probes if matcher.matches(p)]
71
72        return schema_elements
Config(*args, **kwargs)
23    def __init__(self, *args, **kwargs):
24        self.name = args[0]
25        if "matchers" in kwargs:
26            self.matchers = kwargs["matchers"]
27        else:
28            self._set_matchers(args[1])
match_key = 'match'
name
def get_match_keys(self) -> List[Tuple[str]]:
54    def get_match_keys(self) -> List[Tuple[str]]:
55        return [prepend_properties(key) for key in self.matchers.keys()]
def get_schema_elements( self, probes: List[mozilla_schema_generator.probes.Probe]) -> List[Tuple[tuple, mozilla_schema_generator.probes.Probe]]:
57    def get_schema_elements(self, probes: List[Probe]) -> List[Tuple[tuple, Probe]]:
58        """
59        Given a schema and set of probes, get a list of probe and
60        the location in the schema where those probes should be
61        inputted.
62        """
63        schema_elements = []
64
65        for key, matcher in self.matchers.items():
66            # Get the element we are filling in from the schema
67            schema_key = prepend_properties(key)
68
69            # Get the probes for the fill-in
70            schema_elements += [(schema_key, p) for p in probes if matcher.matches(p)]
71
72        return schema_elements

Given a schema and set of probes, get a list of probe and the location in the schema where those probes should be inputted.