-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblueprint_analyser
executable file
·91 lines (67 loc) · 2.45 KB
/
blueprint_analyser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
from factorio_blueprint_analyser import (
options,
blueprint_analyser
)
import json
import os
import yaml
# Default config YAML format:
#
# factorio:
# difficulty: normal
# inserter_capacity_bonus: 0
# data_file_path: "factorio_blueprint_analyser/assets/factorio_raw/factorio_raw_min.json"
# verbose_level: 3
# network:
# display: true
default_config_path = "config/config_default.yaml"
config = {}
def load_config(config_path):
global config
# Check if the default config file exists
if not os.path.exists(default_config_path):
raise FileNotFoundError(
f"The default config file '{default_config_path}' does not exist")
with open(default_config_path, "r") as ymlfile:
default_config = yaml.load(ymlfile, Loader=yaml.FullLoader)
if config_path is None:
return get_config_dict(default_config)
# Check if the default config file exists
if not os.path.exists(config_path):
raise FileNotFoundError(
f"The config file '{config_path}' does not exist")
with open(config_path, "r") as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
# Merge the default config with the user config
for key in cfg:
if key in default_config:
default_config[key] = cfg[key]
else:
print(f"Warning: Unknown config key: {key}")
return get_config_dict(default_config)
def get_config_dict(ymlfile):
return {
"inserterCapacityBonus": get_config_value(ymlfile, "factorio", "inserter_capacity_bonus"),
"dataFilePath": get_config_value(ymlfile, "factorio", "data_file_path"),
"displayNetwork": get_config_value(ymlfile, "network", "display"),
"verboseLevel": get_config_value(ymlfile, "verbose_level")
}
def get_config_value(ymlfile, *args):
conf = ymlfile
for arg in args:
if arg not in conf:
raise KeyError(
f"Couldn't find the config key: {args.join('.')}")
conf = conf[arg]
return conf
if __name__ == "__main__":
# Read and check the user parameters
options.read_options()
config = load_config(options.config_path)
blueprint_analyser.init(config)
analysed_blueprint = blueprint_analyser.analyse_blueprint_from_path(
options.input)
# Export analysed blueprint in a json file
with open(options.output, "w") as f:
f.write(json.dumps(analysed_blueprint, indent=4))