-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlootupdater.py
149 lines (133 loc) · 4.5 KB
/
lootupdater.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#Copyright (c) 2020, catter1
#All rights reserved.
#
#This source code is licensed under the BSD-style license found in the
#LICENSE file in the root directory of this source tree.
import json, sys, os
#Declaring Variables
spaces = ""
singlefile = True
jsonfiles = []
### This Area is filtering through given arguments and checking file validity. ###
#Did they specify what they wanted modified?
try:
file = sys.argv[1]
except IndexError as err:
print("Please define a file to modify.")
sys.exit()
else:
jsonfiles.append(file)
#Function to get list of valid files in the directory if they seleced "all".
def filter_files():
for file in jsonfiles:
if ".json" not in file:
jsonfiles.remove(file)
#Makes sure it's a .json file, or they chose all files.
if ".json" not in file:
if file == "all":
jsonfiles = os.listdir(".")
filter_files()
print("Selecting all files in this directory ending in .json to edit.")
response = input("Are you sure you want to continue? (Y/N): ")
if response.lower() == "y":
print("Continuing...")
singlefile = False
else:
sys.exit()
else:
print("Error. Not a JSON file. This program only accepts files appended with .json.")
sys.exit()
#Function to check if the file is technically a valid json file. Will not be called if "all" files are selected.
def try_json():
try:
json.loads(data)
except TypeError as err:
print("Error: Please choose a valid JSON file. If you wish to bypass this, add \'bypass\' as an argument after the file.")
sys.exit()
### This area does a final JSON validity check, then starts searching for missing instances of TPYE. ###
for file in jsonfiles:
newlines = []
index = []
inside = False
with open(file, 'r') as data:
if singlefile:
try:
file = sys.argv[2]
except IndexError as err:
try_json()
else:
if sys.argv[2] != "bypass":
try_json()
else:
print("Bypassing any possible \'Invalid JSON\' errors...")
for line in data:
newlines.append(line)
if ('\"count\"' in line) & ('{' in line) & (':' in line):
inside = True
if inside:
if '\"type\"' in line:
inside = False
if '}' in line:
index.append(len(newlines) - 3)
inside = False
#This inserts the new "type": "minecraft:uniform" line into the list of lines, with correct indentation.
index.reverse()
for item in index:
count = 0
for letter in newlines[item]:
if letter == " ":
count += 1
spaces = " " * (count - 1)
newlines.insert(item, f'{spaces}\"type\": \"minecraft:uniform\",\n')
#Finally, writes the new lines into given json file, replacing the old text.
with open(file, 'w') as f_out:
f_out.writelines(newlines)
print("File viewed successfully!")
if len(index) != 0:
print(f"Added a TYPE for {len(index)} instances of COUNT in {file}.")
success = True
if not success:
print("No changes were to be made.")
###OLD CODE###
#count = []
#path = []
#def find_count(current):
# count.append(0)
# level = count[len(count)-1]
#
# if type(current) == dict:
# print(f'\n{current}\n')
# for k, v in current.items():
# if isinstance(k, str):
# if (k == "min") | (k == "max"):
# #current["type"] = "minecraft:uniform"
# print('')
# for item in current.items():
# find_count(item)#
#
# if type(current) == list:
# for item in current:
# find_count(item)
#
# if type(current) == tuple:
# count[level] = 0
# while count[level] <= len(current) - 1:
# if type(current[count[level]]) == str:
# count[level] += 1
# continue
# else:
# find_count(current[count[level]])
# count[level] += 1
#
# count.pop(len(count)-1)
#for item in data.items():
# if type(data) == dict:
# find_count(item)
# if type(data) == list:
# for item in data:
# find_count(item)
# if type(data) == tuple:
# if data[0] == "type":
# print("Found a type!\n\n")
# else:
# find_count(item)