-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_for_broken_BPs.py
More file actions
114 lines (93 loc) · 3.12 KB
/
search_for_broken_BPs.py
File metadata and controls
114 lines (93 loc) · 3.12 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import argparse
from pathlib import Path
from bp_from_json import blueprint
import json
import collections
import re
assembling_machine = (
"assembling-machine-1",
"assembling-machine-2",
"assembling-machine-3",
"centrifuge",
"chemical-plant",
"oil-refinery",
)
# ====================================
def get_script_dir():
return Path(__file__).parent.resolve()
# ====================================
def get_current_working_directory():
return Path().resolve()
# ====================================
def get_index(filename):
result = re.search(r"(\d+)-index (.*)", filename.name)
if result:
return int(result[1]), result[2]
else:
return None, None
# ====================================
def search_for_broken_bps_from_folder_parse(folder, f):
# using recursion, we look through all directories
for file in folder.iterdir():
if file.is_file() and (file.suffix == ".bin" or file.suffix == ".txt"):
check_the_BP(file, f)
elif file.is_dir():
search_for_broken_bps_from_folder_parse(folder / file, f)
# ====================================
def check_the_BP(filename, f):
if filename.suffix == ".bin":
bp = blueprint.from_file(filename)
else:
bp = blueprint.from_json_file(filename)
# print(filename)
if "entities" in bp.obj:
for entity in bp.obj["entities"]:
if entity["name"] in assembling_machine:
if "recipe" not in entity:
print(filename)
print(filename, file=f, flush=True)
break
# ====================================
def search_for_broken_bps_from_folder(folder, f):
# there should be one folder or one file in the root directory
if len(tuple(folder.iterdir())) == 1:
for file in folder.iterdir():
if file.is_file() and file.suffix == ".bin":
check_the_BP(file, f)
elif file.is_dir():
search_for_broken_bps_from_folder_parse(folder / file, f)
else:
raise RuntimeError(
"ERROR!!! the number of files and directories in root is greater than 1"
)
return None
# ====================================
def init_parser():
parser = argparse.ArgumentParser(
description=(
'Script "bps_from_folders.py" from the directory "bps_folder" creates a "bp_out.txt" '
'For example: bps_from_folders.py -d="in_folder" -b="out-bp"'
)
)
parser.add_argument(
"-d",
"--directory",
type=str,
default="",
help=(
'(IN) Directory with blueprints. Default = "working_directory/bps_folder"'
),
)
return parser
######################################
#
# main
if __name__ == "__main__":
args = init_parser().parse_args()
if args.directory:
bps_folder = Path(args.directory)
else:
bps_folder = get_current_working_directory().joinpath("bps_folder")
output_file = "broken_BPs.txt.ignore"
with open(output_file, mode="w", encoding="utf8") as f:
search_for_broken_bps_from_folder(bps_folder, f)