Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse single strings as collections of one item in structured wildcard files #113

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
]