-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomNatmod.py
More file actions
197 lines (184 loc) · 5.66 KB
/
Copy pathDomNatmod.py
File metadata and controls
197 lines (184 loc) · 5.66 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import SpecialWords
class DomNationMod:
def __init__(self):
self.modinfo = {} #Dictionary of --Command:Value-- pairs
#All others are list of Dictionaries of --Command:Value-- pairs (Even if it doesn't make sense to have a list...)
#Such as nation, which will be a list of a single dictionary. Fix later to remove list layer?
self.weapons = []
self.armors = []
self.units = []
self.names = []
self.nation = []
self.spells = []
self.magicitems = []
self.general = []
self.poptypes = []
self.mercenaries = []
self.sites = []
class FileManipulator:
def __init__(self, filepath):
self.filepath = filepath
self.thefile = self.getfile()
def getfile(self):
filestream = open(self.filepath)
filelist = []
for line in filestream:
if line.strip() == "":
pass
elif line[0:2] == "--":
pass
else:
filelist.append(line.strip())
filestream.close()
return(filelist)
#finds and returns the command present at the start of the line
def get_line_command(self, theline):
command = ""
for letter in theline:
if letter != " ":
command += letter
else:
break
return(command)
def get_line_string(self, line):
returnvalue = '"'
startindex = line.index('"')
line = line[startindex+1:]
for letter in line:
if letter == '"':
returnvalue += letter
return(returnvalue)
else:
returnvalue += letter
def get_line_value(self, line):
returnvalue = ""
for letter in line:
if letter is " ":
return(returnvalue)
else:
returnvalue += letter
return(returnvalue)
#Gets the value present in a line. Optional tostrip argument should be a line command (ex. #attk).
def get_line_values(self, line, tostrip = ""): ## !! MUST BE UPDATED TO 3 MAX VALUES!!
value1 = ""
value2 = ""
value = ""
if tostrip != "":
value = line.strip(tostrip)
if "--" in value:
indexremove = value.index("--")
value = value[:indexremove]
value = value.strip()
if '"' in value:
value1 = self.get_line_string(value)
else:
value1 = self.get_line_value(value)
index = value.index(value1)
value2 = value[index + len(value1):]
value1 = value1.strip()
value1 = value1.strip('"')
value2 = value2.strip()
value2 = value2.strip('"')
if value2 != "":
return(value1,value2)
else:
return(value1)
#returns a dictionary of modinfo commands and their respective values
def get_mod_info(self):
end = False
value = ""
command = ""
modinfodictionary = {}
while end == False:
for line in self.thefile:
if "#newweapon" in line or "#selectweapon" in line:
end = True
break
if line[0] == "#":
command = self.get_line_command(line)
if command in SpecialWords.modinfocommands:
value = self.get_line_values(line, tostrip = command)
modinfodictionary[command] = value
return(modinfodictionary)
#get all the indexes for a string, which should be a #command that starts a new item type
def get_item_indexes(self, toindex):
itemindexes = []
for i, theline in enumerate(self.thefile):
if toindex in theline:
itemindexes.append(i)
return(itemindexes)
#gets info from an index up to #end command, returns dictionary of --Command:Value-- Pairs
def get_item_info(self, index):
itemdict = {}
#these values may have more than one entry per unit, so their values are taken as a list of all values found
commandswithlist = ["#weapon","#armor","#custommagic","#magicskill"]
newfile = self.thefile[index:]
for line in newfile:
command = self.get_line_command(line)
if "#end" in line:
break
elif "#" not in line:
pass
elif command in commandswithlist:
value = self.get_line_values(line, command)
if command not in itemdict:
itemlist = []
itemlist.append(value)
itemdict[command] = itemlist
else:
itemlist = itemdict[command]
itemlist.append(value)
itemdict[command] = itemlist
#Summary has multiline descriptions so needs different value parsing method to gather desc. from each line
elif command == "#summary":
start = False
summaryvalue = []
for line in newfile:
if start == True and "#summary" not in line and "#" in line:
itemdict[command] = summaryvalue
break
elif "#summary" in line:
start = True
line =line.strip("#summary")
line = line.strip()
line = line.strip('"')
summaryvalue.append(line)
elif start == True and "#":
line = line.strip()
line = line.strip('"')
summaryvalue.append(line)
else:
value = self.get_line_values(line,command)
itemdict[command] = value
return(itemdict)
#Gets all mod information of input type
def get_all(self, toget):
listofitems = []
startindexes = []
indexstartkeys = []
if toget == "weapons":
indexstartkeys = ["#newweapon", "#selectweapon"]
elif toget == "armors":
indexstartkeys = ["#newarmor", "#selectarmor"]
elif toget == "units":
indexstartkeys = ["#newmonster", "#selectmonster"]
elif toget == "spells":
indexstartkeys = ["#clearallspells","#selectspell","#newspell"]
elif toget == "items":
indexstartkeys = ["#clearallitems", "#selectitem", "#newitem"]
elif toget == "names":
indexstartkeys = ["#selectnametype"]
elif toget == "mercs":
indexstartkeys = ["#clearmerc", "#newmerc"]
elif toget == "sites":
indexstartkeys = ["#selectsite", "#newsite"]
elif toget == "nation":
indexstartkeys = ["#indepflag", "#selectnation", "#newnation"]
elif toget == "pops":
indexstartkeys = ["#selectpoptype"]
for startkey in indexstartkeys:
startindexes += self.get_item_indexes(startkey)
if startindexes != []:
for index in startindexes:
listofitems.append(self.get_item_info(index))
return(listofitems)