Skip to content

Commit 9ac33db

Browse files
Fix overwriting existing values in config.json in auto (#418)
`jq` merge direction in this case is right to left so if the user had set up custom paths it would replace them with the default ones. This PR switches the direction to use the defaults as fallback instead of overwriting user settings. ---- Didn't want to create an issue for the tiny change. Thanks for your work on the repo, it saved me a lot of time, 👍 --------- Co-authored-by: AbdBarho <ka70911@gmail.com>
1 parent a68734c commit 9ac33db

4 files changed

Lines changed: 84 additions & 12 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
/.devcontainer
22
/docker-compose.override.yml
3+
4+
# VSCode specific
5+
*.code-workspace
6+
/.vscode

services/AUTOMATIC1111/config.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

services/AUTOMATIC1111/config.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
3+
"""Checks and sets default values for config.json before starting the container."""
4+
5+
import json
6+
import re
7+
import os.path
8+
import sys
9+
10+
DEFAULT_FILEPATH = '/data/config/auto/config.json'
11+
12+
DEFAULT_OUTDIRS = {
13+
"outdir_samples": "",
14+
"outdir_txt2img_samples": "/output/txt2img",
15+
"outdir_img2img_samples": "/output/img2img",
16+
"outdir_extras_samples": "/output/extras",
17+
"outdir_grids": "",
18+
"outdir_txt2img_grids": "/output/txt2img-grids",
19+
"outdir_img2img_grids": "/output/img2img-grids",
20+
"outdir_save": "/output/saved",
21+
"outdir_init_images": "/output/init-images",
22+
}
23+
RE_VALID_OUTDIR = re.compile(r"(^/output(/\.?[\w\-\_]+)+/?$)|(^\s?$)")
24+
25+
DEFAULT_OTHER = {
26+
"font": "DejaVuSans.ttf",
27+
}
28+
29+
def dict_to_json_file(target_file: str, data: dict):
30+
"""Write dictionary to specified json file"""
31+
32+
with open(target_file, 'w') as f:
33+
json.dump(data, f)
34+
35+
def json_file_to_dict(config_file: str) -> dict|None:
36+
"""Load json file into a dictionary. Return None if file does not exist."""
37+
38+
if os.path.isfile(config_file):
39+
with open(config_file, 'r') as f:
40+
return json.load(f)
41+
else:
42+
return None
43+
44+
def replace_if_invalid(value: str, replacement: str, pattern: str|re.Pattern[str]) -> str:
45+
"""Returns original value if valid, fallback value if invalid"""
46+
47+
if re.match(pattern, value):
48+
return value
49+
else:
50+
return replacement
51+
52+
def check_and_replace_config(config_file: str, target_file: str = None):
53+
"""Checks given file for invalid values. Replaces those with fallback values (default: overwrites file)."""
54+
55+
# Get current user config, or empty if file does not exists
56+
data = json_file_to_dict(config_file) or {}
57+
58+
# Check and fix output directories
59+
for k, def_val in DEFAULT_OUTDIRS.items():
60+
if k not in data:
61+
data[k] = def_val
62+
else:
63+
data[k] = replace_if_invalid(value=data[k], replacement=def_val, pattern=RE_VALID_OUTDIR)
64+
65+
# Check and fix other default settings
66+
for k, def_val in DEFAULT_OTHER.items():
67+
if k not in data:
68+
data[k] = def_val
69+
70+
# Write results to file
71+
dict_to_json_file(target_file or config_file, data)
72+
73+
if __name__ == '__main__':
74+
if len(sys.argv) > 1:
75+
check_and_replace_config(*sys.argv[1:])
76+
else:
77+
check_and_replace_config(DEFAULT_FILEPATH)
78+

services/AUTOMATIC1111/entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ mkdir -p /data/config/auto/scripts/
88
find "${ROOT}/scripts/" -maxdepth 1 -type l -delete
99
cp -vrfTs /data/config/auto/scripts/ "${ROOT}/scripts/"
1010

11-
cp -n /docker/config.json /data/config/auto/config.json
12-
jq '. * input' /data/config/auto/config.json /docker/config.json | sponge /data/config/auto/config.json
11+
# Set up config file
12+
python /docker/config.py /data/config/auto/config.json
1313

1414
if [ ! -f /data/config/auto/ui-config.json ]; then
1515
echo '{}' >/data/config/auto/ui-config.json

0 commit comments

Comments
 (0)