-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhitelist_biomes.py
123 lines (85 loc) · 2.48 KB
/
whitelist_biomes.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
""" Takes a .json object of biome parameters and filters it
to only desired biomes """
# Get the full biome and parameter list
from file import all_biomes as RAW
# Get the desired biomes list
from file import allow_biomes as ALLOW
def main():
''' The main functionality '''
# Read the full biome list into a Python list
dirty_biomes = parse(RAW)
# Filter that list
clean_biomes = narrow(dirty_biomes, ALLOW)
# Assemble the filtered biomes back into a .json object
out = assemble(clean_biomes)
# Save to a file
f = open("out.json", "w")
f.write(out)
f.close()
# Done!
return 0
def parse(string):
''' Take a string and break it up into a list of brackets '''
# Hold each { .... } while we assemble it
tmp = ""
# The list to return
out = []
# How many brackets deep are we?
depth = 0
# Start at ~20 as we assume the all_biomes starts with
# { "biomes": [
# and new lines and junk. We just don't want that first bracket
# For each character in the string...
for c in string[20:]:
# Record it
tmp += c
# Each element in the list ends with a closing bracket.
# Is this the closing bracket that matches with the opening bracket?
if (c == "}"):
depth -= 1
# Each element in the list starts with an opening bracket.
elif (c == "{"):
depth += 1
# Have we fully found a { ... } ?
if (depth == 0):
# Store it as a list element
out.append(tmp)
# Get ready for a new list element
tmp = ""
# Return the list!
return out
def narrow(lst, filt):
''' Filter the list by a list of allowed elements '''
# What to return
out = []
# For each element in the list to be filtered...
for l in lst:
# For each element in the filter...
for f in filt:
# If the list element contains an allowed biome...
if (f in l):
# Record it!
out.append(l)
# Save time
break;
# Return the filtered list
return out
def assemble(lst):
''' Reassemble the string into something that looks like a .json '''
# Start the .json off with "biomes": [
out = "\"biomes\": ["
# For each { ... } element, except the last one
for l in lst[:-1]:
# Record it
out += l
# Add a comma
out += ",\n"
# Now add the last element, with no comma
out += lst[-1]
# Add a newline and close the list
out += "\n]"
# Return it!
return out
if __name__ == "__main__":
# If this file isn't a sub-module of something, automatically run
main()