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 collections import defaultdict 11from typing import Dict, List, Tuple 12 13from .matcher import Matcher 14 15# TODO: s/probes/probe 16from .probes import Probe 17from .utils import _get, prepend_properties 18 19 20class Config(object): 21 22 match_key = "match" 23 24 def __init__(self, *args, **kwargs): 25 self.name = args[0] 26 if "matchers" in kwargs: 27 self.matchers = kwargs["matchers"] 28 else: 29 self._set_matchers(args[1]) 30 31 def _set_matchers(self, config: dict) -> Dict[Tuple[str], Matcher]: 32 """ 33 Transform the nested config into a single dictionary 34 """ 35 keys = queue.SimpleQueue() 36 matchers = {} 37 38 for key, v in config.items(): 39 if isinstance(v, dict): 40 keys.put((key,)) 41 42 while not keys.empty(): 43 key = keys.get() 44 elem = _get(config, key) 45 46 if self.match_key in elem: 47 matchers[key] = Matcher(elem[self.match_key]) 48 else: 49 for k, v in elem.items(): 50 if isinstance(v, dict): 51 keys.put(key + (k,)) 52 53 self.matchers = matchers 54 55 def _get_splits(self) -> Dict[str, Dict[Tuple[str], Matcher]]: 56 """ 57 Find the splits that we need to make. Each 58 self.table_group_key is it's own split. 59 """ 60 splits = defaultdict(dict) 61 for key, matcher in self.matchers.items(): 62 splits[matcher.get_table_group()][key] = matcher 63 64 return splits 65 66 def get_match_keys(self) -> List[Tuple[str]]: 67 return [prepend_properties(key) for key in self.matchers.keys()] 68 69 def split(self) -> List[Config]: 70 """ 71 Split this config into multiple configs. 72 """ 73 splits = self._get_splits() 74 return [Config(name, matchers=matchers) for name, matchers in splits.items()] 75 76 def get_schema_elements(self, probes: List[Probe]) -> List[Tuple[tuple, Probe]]: 77 """ 78 Given a schema and set of probes, get a list of probe and 79 the location in the schema where those probes should be 80 inputted. 81 """ 82 schema_elements = [] 83 84 for key, matcher in self.matchers.items(): 85 # Get the element we are filling in from the schema 86 schema_key = prepend_properties(key) 87 88 # Get the probes for the fill-in 89 schema_elements += [(schema_key, p) for p in probes if matcher.matches(p)] 90 91 return schema_elements
class
Config:
21class Config(object): 22 23 match_key = "match" 24 25 def __init__(self, *args, **kwargs): 26 self.name = args[0] 27 if "matchers" in kwargs: 28 self.matchers = kwargs["matchers"] 29 else: 30 self._set_matchers(args[1]) 31 32 def _set_matchers(self, config: dict) -> Dict[Tuple[str], Matcher]: 33 """ 34 Transform the nested config into a single dictionary 35 """ 36 keys = queue.SimpleQueue() 37 matchers = {} 38 39 for key, v in config.items(): 40 if isinstance(v, dict): 41 keys.put((key,)) 42 43 while not keys.empty(): 44 key = keys.get() 45 elem = _get(config, key) 46 47 if self.match_key in elem: 48 matchers[key] = Matcher(elem[self.match_key]) 49 else: 50 for k, v in elem.items(): 51 if isinstance(v, dict): 52 keys.put(key + (k,)) 53 54 self.matchers = matchers 55 56 def _get_splits(self) -> Dict[str, Dict[Tuple[str], Matcher]]: 57 """ 58 Find the splits that we need to make. Each 59 self.table_group_key is it's own split. 60 """ 61 splits = defaultdict(dict) 62 for key, matcher in self.matchers.items(): 63 splits[matcher.get_table_group()][key] = matcher 64 65 return splits 66 67 def get_match_keys(self) -> List[Tuple[str]]: 68 return [prepend_properties(key) for key in self.matchers.keys()] 69 70 def split(self) -> List[Config]: 71 """ 72 Split this config into multiple configs. 73 """ 74 splits = self._get_splits() 75 return [Config(name, matchers=matchers) for name, matchers in splits.items()] 76 77 def get_schema_elements(self, probes: List[Probe]) -> List[Tuple[tuple, Probe]]: 78 """ 79 Given a schema and set of probes, get a list of probe and 80 the location in the schema where those probes should be 81 inputted. 82 """ 83 schema_elements = [] 84 85 for key, matcher in self.matchers.items(): 86 # Get the element we are filling in from the schema 87 schema_key = prepend_properties(key) 88 89 # Get the probes for the fill-in 90 schema_elements += [(schema_key, p) for p in probes if matcher.matches(p)] 91 92 return schema_elements
70 def split(self) -> List[Config]: 71 """ 72 Split this config into multiple configs. 73 """ 74 splits = self._get_splits() 75 return [Config(name, matchers=matchers) for name, matchers in splits.items()]
Split this config into multiple configs.
def
get_schema_elements( self, probes: List[mozilla_schema_generator.probes.Probe]) -> List[Tuple[tuple, mozilla_schema_generator.probes.Probe]]:
77 def get_schema_elements(self, probes: List[Probe]) -> List[Tuple[tuple, Probe]]: 78 """ 79 Given a schema and set of probes, get a list of probe and 80 the location in the schema where those probes should be 81 inputted. 82 """ 83 schema_elements = [] 84 85 for key, matcher in self.matchers.items(): 86 # Get the element we are filling in from the schema 87 schema_key = prepend_properties(key) 88 89 # Get the probes for the fill-in 90 schema_elements += [(schema_key, p) for p in probes if matcher.matches(p)] 91 92 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.