-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_list_of_blocks.py
127 lines (105 loc) · 5.54 KB
/
page_list_of_blocks.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Part of the 7 Days to Die Wiki Content Generator
Copyright (C) 2017 Liam Brandt <brandt.liam@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from xml.etree import ElementTree
from collections import OrderedDict
from wiki import WikiString, WikiList, WikiTable, get_path_settings
class Block(object):
def __init__(self):
pass
class BlockWikiTable(WikiTable):
def write_entry(self, key, value, table_file):
if not value.is_empty():
wiki_text = value.get_wiki_text()
table_file.write(wiki_text)
def write_header_entry(self, key, table_file):
table_file.write(key)
def create_table_blocks(path_settings):
table = OrderedDict()
recipe_root = ElementTree.parse(path_settings["xml_recipes"]).getroot()
recipe_names = []
for recipe in recipe_root:
recipe_names.append(recipe.attrib["name"])
root = ElementTree.parse(path_settings["xml_blocks"]).getroot()
developer_block_names = []
for block in root:
block_name = block.attrib["name"]
table[block_name] = OrderedDict([
("Block", WikiString(block_name, "blocks", is_link=True)),
("Id", WikiString(block.attrib["id"], None)),
("Upgrades To", WikiString("", None)),
("Downgrades To", WikiString("", None)),
("Extends", WikiString("", None)),
("Craftable", WikiString("No", None)),
("Group", WikiList()),
("Repair Items", WikiList())
])
if block_name in recipe_names:
table[block_name]["Craftable"] = WikiString("Yes", None)
for block_property in block:
if block_property.tag == "property":
#property has a name
if "name" in block_property.attrib:
property_name = block_property.attrib["name"]
property_value = block_property.attrib["value"]
if property_name == "Extends":
table[block_name]["Extends"] = WikiString(property_value, "blocks", is_link=True)
elif property_name == "Group":
for each_group in property_value.split(","):
table[block_name]["Group"].add_string(WikiString(each_group, None, is_link=True))
elif property_name == "IsDeveloper":
if property_value == "true":
developer_block_names.append(block_name)
elif property_name == "DowngradeBlock":
table[block_name]["Downgrades To"] = WikiString(property_value, "blocks", is_link=True)
elif property_name in table[block_name]:
table[block_name][property_name] = WikiString(property_value, None)
#property has a class with sub properties
elif "class" in block_property.attrib:
if block_property.attrib["class"] == "UpgradeBlock":
for upgrade_property in block_property:
property_name = upgrade_property.attrib["name"]
property_value = upgrade_property.attrib["value"]
if property_name == "ToBlock":
table[block_name]["Upgrades To"] = WikiString(property_value, "blocks", is_link=True)
elif block_property.attrib["class"] == "RepairItems":
for repair_property in block_property:
table[block_name]["Repair Items"].add_string(WikiString(repair_property.attrib["name"], "items", prefix=repair_property.attrib["value"]+" x ", is_link=True))
#go back an remove links to dev blocks
keys_to_remove_dev_links = ["Extends", "Upgrades To", "Downgrades To"]
for block_name, block in table.items():
for key in keys_to_remove_dev_links:
if not block[key].is_empty():
if block[key].original in developer_block_names:
block[key] = WikiString(block[key].original, "blocks")
#go back and apply extended properties
for block_name, block in table.items():
#if this block extends another block
if not block["Extends"].is_empty():
parent_name = block["Extends"].original
parent_block = table[parent_name]
for key, value in parent_block.items():
#if the value we are copying into is empty and the value we are copying is not empty
if block[key].is_empty() and not value.is_empty():
block[key] = value
#delete all developer blocks that may have already been used to extend non developer blocks
for dev_block_name in developer_block_names:
del table[dev_block_name]
return table
def main():
path_settings = get_path_settings()
table = create_table_blocks(path_settings)
blocks_wiki_table = BlockWikiTable(table)
blocks_wiki_table.write_table(path_settings["folder_wiki_pages"] + "page_list_of_blocks.txt")
main()