Skip to content

Commit

Permalink
Parse single strings as collections of one item in structured wildcar…
Browse files Browse the repository at this point in the history
…d files (#113)

* Ignore falsy values in structured wildcards

* Parse single strings as collections of one item in structured wildcard file
  • Loading branch information
akx authored Dec 29, 2023
1 parent ff47441 commit b044bc9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/dynamicprompts/wildcards/collection/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,17 @@ def _parse_structured_file_dict(
for name, value in data.items():
if not isinstance(name, str):
continue

if not value:
continue

prefix_and_name = (*prefix, name)
name = "/".join(prefix_and_name)

if isinstance(value, str):
# Parse a single string as a list of one item.
value = [value]

if isinstance(value, list):
try:
entries = list(_parse_structured_file_list(value))
Expand Down
2 changes: 2 additions & 0 deletions tests/test_data/wildcards/pantry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ artists:
- Piet Mondrian
- Rembrandt van Rijn
- Vincent van Gogh
moonbase: john madden # interpreted as a single john madden
1234: 5678 # this is ignored
flurp: 12345 # this too
flem: "" # and this
10 changes: 9 additions & 1 deletion tests/wildcard/test_wildcardmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_hierarchy(wildcard_manager: WildcardManager):
"animals/reptiles/snakes",
"artists/dutch",
"artists/finnish",
"artists/moonbase",
"cars/ford/colors",
"cars/ford/name",
"cars/ford/types",
Expand Down Expand Up @@ -192,8 +193,9 @@ def test_hierarchy(wildcard_manager: WildcardManager):
"bitter", # from .json
}
assert set(root.child_nodes["artists"].collections) == {
"finnish", # from root pantry YAML's nested dict
"dutch", # from root pantry YAML's nested dict
"finnish", # from root pantry YAML's nested dict
"moonbase", # from root pantry YAML's nested dict
}


Expand Down Expand Up @@ -389,3 +391,9 @@ def test_weight_parsing(wildcard_manager: WildcardManager):
assert name_to_entry["cat"].weight == 3
assert name_to_entry["elephant"].weight == 50
assert name_to_entry["rhino"].weight == 20.5


def test_single_string_in_structured_parsed_as_list(wildcard_manager: WildcardManager):
assert wildcard_manager.get_values("artists/moonbase").string_values == [
"john madden",
]

0 comments on commit b044bc9

Please sign in to comment.