Skip to content

Commit

Permalink
convert cli-style strings in selectors to normalized dictionaries
Browse files Browse the repository at this point in the history
[#2879]


automatic commit by git-black, original commits:
  bb83435
  • Loading branch information
gshank authored and iknox-fa committed Feb 8, 2022
1 parent 65602ed commit 3b94ad2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
5 changes: 3 additions & 2 deletions core/dbt/config/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,11 +393,12 @@ def create_project(self, rendered: RenderComponents) -> "Project":
packages = package_config_from_data(rendered.packages_dict)
selectors = selector_config_from_data(rendered.selectors_dict)
manifest_selectors: Dict[str, Any] = {}
if rendered.selectors_dict and rendered.selectors_dict['selectors']:
if rendered.selectors_dict and rendered.selectors_dict["selectors"]:
# this is a dict with a single key 'selectors' pointing to a list
# of dicts.
manifest_selectors = SelectorDict.parse_from_selectors_list(
rendered.selectors_dict['selectors'])
rendered.selectors_dict["selectors"]
)
project = Project(
project_name=name,
version=version,
Expand Down
19 changes: 9 additions & 10 deletions core/dbt/config/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ def validate_selector_default(selector_file: SelectorFile) -> None:
# be necessary to make changes here. Ideally it would be
# good to combine the two flows into one at some point.
class SelectorDict:

@classmethod
def parse_dict_definition(cls, definition):
key = list(definition)[0]
Expand All @@ -152,10 +151,10 @@ def parse_dict_definition(cls, definition):
new_value = cls.parse_from_definition(sel_def)
new_values.append(new_value)
value = new_values
if key == 'exclude':
if key == "exclude":
definition = {key: value}
elif len(definition) == 1:
definition = {'method': key, 'value': value}
definition = {"method": key, "value": value}
return definition

@classmethod
Expand All @@ -177,10 +176,10 @@ def parse_a_definition(cls, def_type, definition):
def parse_from_definition(cls, definition):
if isinstance(definition, str):
definition = SelectionCriteria.dict_from_single_spec(definition)
elif 'union' in definition:
definition = cls.parse_a_definition('union', definition)
elif 'intersection' in definition:
definition = cls.parse_a_definition('intersection', definition)
elif "union" in definition:
definition = cls.parse_a_definition("union", definition)
elif "intersection" in definition:
definition = cls.parse_a_definition("intersection", definition)
elif isinstance(definition, dict):
definition = cls.parse_dict_definition(definition)
return definition
Expand All @@ -191,8 +190,8 @@ def parse_from_definition(cls, definition):
def parse_from_selectors_list(cls, selectors):
selector_dict = {}
for selector in selectors:
sel_name = selector['name']
sel_name = selector["name"]
selector_dict[sel_name] = selector
definition = cls.parse_from_definition(selector['definition'])
selector_dict[sel_name]['definition'] = definition
definition = cls.parse_from_definition(selector["definition"])
selector_dict[sel_name]["definition"] = definition
return selector_dict
18 changes: 9 additions & 9 deletions core/dbt/graph/selector_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,20 @@ def selection_criteria_from_dict(
def dict_from_single_spec(cls, raw: str):
result = RAW_SELECTOR_PATTERN.match(raw)
if result is None:
return {'error': 'Invalid selector spec'}
return {"error": "Invalid selector spec"}
dct: Dict[str, Any] = result.groupdict()
method_name, method_arguments = cls.parse_method(dct)
meth_name = str(method_name)
if method_arguments:
meth_name += '.' + '.'.join(method_arguments)
dct['method'] = meth_name
dct = {k: v for k, v in dct.items() if (v is not None and v != '')}
if 'childrens_parents' in dct:
dct['childrens_parents'] = bool(dct.get('childrens_parents'))
if 'parents' in dct:
dct['parents'] = bool(dct.get('parents'))
if 'children' in dct:
dct['children'] = bool(dct.get('children'))
dct["method"] = meth_name
dct = {k: v for k, v in dct.items() if (v is not None and v != "")}
if "childrens_parents" in dct:
dct["childrens_parents"] = bool(dct.get("childrens_parents"))
if "parents" in dct:
dct["parents"] = bool(dct.get("parents"))
if "children" in dct:
dct["children"] = bool(dct.get("children"))
return dct

@classmethod
Expand Down

0 comments on commit 3b94ad2

Please sign in to comment.