Skip to content

Commit

Permalink
Add _omit_parameters category attribute (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
30350n committed Jun 29, 2024
1 parent 717b076 commit b28047d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Additionally you can define the following meta attributes (starting with `_`):
- `_parameters` has to be a list of parameter names (for parameters defined in
[`parameters.yaml`](#parametersyaml)) this category uses<br>
**note: parameters get inherited by sub categories**
- `_omit_parameters` has to be a list of parameters names defined in parent categories that
get omitted from the category
- `_structural` can be set to `true` to make the category structural

Here's an example for a config with special attributes:
Expand Down
18 changes: 12 additions & 6 deletions inventree_part_import/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,10 @@ def add_alias(self, alias):
f"'{CATEGORIES_CONFIG}'"
)

CATEGORY_ATTRIBUTES = {"_parameters", "_description", "_ignore", "_structural", "_aliases"}
def parse_category_recursive(categories_dict, parameters=tuple(), path=tuple()):
CATEGORY_ATTRIBUTES = {
"_parameters", "_omit_parameters", "_description", "_ignore", "_structural", "_aliases"
}
def parse_category_recursive(categories_dict, parent_parameters=tuple(), path=tuple()):
if not categories_dict:
return {}

Expand All @@ -214,20 +216,24 @@ def parse_category_recursive(categories_dict, parameters=tuple(), path=tuple()):
if child.startswith("_") and child not in CATEGORY_ATTRIBUTES:
warning(f"ignoring unknown special attribute '{child}' in category '{name}'")

new_parameters = parameters + tuple(values.get("_parameters", []))
new_path = path + (name,)
omitted_parameters = values.get("_omit_parameters", [])
parameters = tuple(set(parent_parameters) - set(omitted_parameters))
parameters += tuple(values.get("_parameters", []))
for parameter in set(omitted_parameters) - set(parent_parameters):
warning(f"failed to omit parameter '{parameter}' in category '{name}'")

new_path = path + (name,)
categories[new_path] = Category(
name=name,
path=list(new_path),
description=values.get("_description", name),
ignore=values.get("_ignore", False),
structural=values.get("_structural", False),
aliases=values.get("_aliases", []),
parameters=new_parameters,
parameters=parameters,
)

categories.update(parse_category_recursive(values, new_parameters, new_path))
categories.update(parse_category_recursive(values, parameters, new_path))

return categories

Expand Down

0 comments on commit b28047d

Please sign in to comment.