def parse(): infile = open(sys.argv[1], "r") fileText = stringToken(infile.read(), "\n") infile.close() for i in range(0, len(fileText)): fileText[i] = stringToken(fileText[i], "[]") i = 0 length = len(fileText) while i < length: if fileText[i][0] != "bazz ": fileText.pop(i) length -= 1 else: i += 1 for i in range(0, len(fileText)): fileText[i].pop(0) for i in range(0, len(fileText)): j = 0 length = len(fileText[i]) while j < length: if fileText[i][j] == ' ': fileText[i].pop(j) length -= 1 else: j += 1 return fileText
def classFileParser(classFile): # parse schedule list inputFile = open(classFile, "r") fileText = stringToken(inputFile.read(), "\n") inputFile.close() for i in range(0, len(fileText)): fileText[i] = stringToken(fileText[i], " ") return fileText
def logicFileParser(logicFile): # parse logic inputFile = open(logicFile, "r") logicText = stringToken( "".join(stringToken(inputFile.read(), " ")), "\n") # removes all spaces, then separates on newlines inputFile.close() for i in range( 0, len(logicText) ): # replace newlines by generating a 2d list, with each element being a list of the desired elements logicText[i] = stringToken(logicText[i], "()&|", 'f') return logicText
def populate(self): self.trim_text() i = 0 self.file_description = self.text[i] i += 1 self.campaign = stringToken(self.text[i], "\t")[1] i += 2 while self.text[i] != "end": self.playable.append(stringToken(self.text[i], "\t")[0]) i += 1 i += 2 while self.text[i] != "end": self.unlockable.append(stringToken(self.text[i], "\t")[0]) i += 1 i += 2 while self.text[i] != "end": self.nonplayable.append(stringToken(self.text[i], "\t")[0]) i += 1 i += 1 self.start_date = stringToken(self.text[i], "\t")[1] i += 1 self.end_date = stringToken(self.text[i], "\t")[1] i += 1 self.brigand_spawn_value = int(stringToken(self.text[i], " ")[1]) i += 1 self.pirate_spawn_value = int(stringToken(self.text[i], " ")[1])
def populate(self): self.trim_text() for i in range(2, len(self.text)): line = stringToken(self.text[i], "\t") if self.text[i][0] == ";": self.key.append(self.text[i]) elif line[0] == "core_attitudes": self.core_attitudes.append( diplomacy_entry(line[0], stringToken(line[1], ",")[0], int(line[2]), stringToken(line[3], ", "))) elif line[0] == "faction_relationships": self.faction_relationships.append( diplomacy_entry(line[0], stringToken(line[1], ",")[0], int(line[2]), stringToken(line[3], ", "))) else: print("error parsing diplomacy section", file=sys.stdout)
def parse(fileText): dataList = stringToken(fileText, "\n\t") classList = [] imperative = False i = 0 j = 0 while i < len(dataList): if '#' in dataList[i]: j += 1 imperative = True break i += 1 i = j while i < len(dataList): # populates the array classList.append([dataList[i], dataList[i + 1], dataList[i + 2]]) i += 3 impList = [] if imperative == True: impList = stringToken(dataList[0], "#") return [classList, impList]
def parse_descr_strat(): infile = open(DESCR_STRAT_LOCATION, "r") filetext = stringToken(infile.read(), "\n", 'f') infile.close() return split_into_sections(filetext)
def populate(self): self.trim_text() i = 2 j = 0 while i < len(self.text): first = stringToken(self.text[i], "\t ,") self.factions.append(faction()) self.factions[j].name = first[1] self.factions[j].build_pref = first[2] self.factions[j].unit_pref = first[3] i += 1 second = stringToken(self.text[i], " \t") third = second if second[0] == "superfaction": self.factions[j].superfaction = second[1] i += 1 third = stringToken(self.text[i], "\t") self.factions[j].denari = int(third[1]) i += 1 l = 0 # settlement incrementor while stringToken(self.text[i], "\t")[0] != "character": # read settlements if self.text[i] == "settlement": self.factions[j].settlements.append(settlement()) k = 2 # index to move through a settlement self.factions[j].settlements[l].level = stringToken( self.text[i + k], " ")[1] k += 1 self.factions[j].settlements[l].region = stringToken( self.text[i + k], " ")[1] k += 1 self.factions[j].settlements[l].year_founded = int( stringToken(self.text[i + k], " ")[1]) k += 1 self.factions[j].settlements[l].population = int( stringToken(self.text[i + k], " ")[1]) k += 1 self.factions[j].settlements[l].plan_set = stringToken( self.text[i + k], " ")[1] k += 1 self.factions[j].settlements[ l].faction_creator = stringToken( self.text[i + k], " ")[1] k += 1 if self.text[i + k] == "}": # no buildings case i = i + k + 1 else: # buildings case while self.text[i + k] == "\tbuilding": info = stringToken(self.text[i + k + 2], " ") self.factions[j].settlements[l].buildings.append( building(info[1], info[2])) k += 4 i = i + k + 1 l += 1 else: print("error reading settlements in " + self.factions[j].name, file=sys.stdout) break l = 0 # character incrementor while stringToken( self.text[i], "\t")[0] != "character_record": # read characters first = stringToken(self.text[i], "\t,") if first[0] == "character": if stringToken(first[1], " ")[0] == "sub_faction": self.factions[j].characters.append(general()) self.factions[j].characters[l].name = stringToken( first[2], " ")[0] # don't these have to be fixed??? self.factions[j].characters[l].age = int( stringToken(first[4], " ")[1]) self.factions[j].characters[ l].sub_faction = stringToken(first[1], " ")[1] self.factions[j].characters[l].char_type = stringToken( first[3], " ")[0] self.factions[j].characters[l].pos = point( stringToken(first[6], " ")[1], stringToken(first[7], " ")[1], self.factions[j].characters[l].char_type, self.factions[j].characters[l].name) i += 1 second = self.text[i] if second == "army": k = 1 # unit incrementor unit_line = stringToken(self.text[i + k], "\t") while unit_line[0] == "unit": unit_stats = stringToken(unit_line[2], " ") self.factions[j].characters[l].army.append( strat_map_unit(unit_line[1], int(unit_stats[1]), int(unit_stats[3]), int(unit_stats[5]))) if len(self.text) > i + k + 1: k += 1 else: return # we arrived at EOF so it's time to stop reading things unit_line = stringToken(self.text[i + k], "\t") i += k l += 1 elif first[2] == " named character": self.factions[j].characters.append(named_character()) self.factions[j].characters[l].name = first[1] if first[3] == " heir" or first[3] == " leader": self.factions[j].characters[l].rank = first[ 3].lstrip() self.factions[j].characters[l].age = int( stringToken(first[4], " ")[1]) self.factions[j].characters[l].pos = point( stringToken(first[6], " ")[1], stringToken(first[7], " ")[1], self.factions[j].characters[l].char_type, self.factions[j].characters[l].name) else: self.factions[j].characters[l].rank = "" self.factions[j].characters[l].age = int( stringToken(first[3], " ")[1]) self.factions[j].characters[l].pos = point( stringToken(first[5], " ")[1], stringToken(first[6], " ")[1], self.factions[j].characters[l].char_type, self.factions[j].characters[l].name) self.factions[j].characters[ l].wife = "" # to be filled in later self.factions[j].characters[l].children = [ ] # to be filled in later self.factions[j].characters[l].sub_faction = "" char_type = stringToken(first[2], " ") self.factions[j].characters[ l].char_type = char_type[0] + " " + char_type[1] i += 1 second = stringToken(self.text[i], " ,") third = second fourth = third if second[0] == "traits": k = 1 # traits incrementor while k < len(second) - 1: self.factions[j].characters[l].traits.append( trait(second[k], int(second[k + 1]))) k += 2 i += 1 third = stringToken(self.text[i], " ,") fourth = third if third[0] == "ancillaries": k = 1 # ancillaries incrementor while k < len(third): self.factions[j].characters[ l].ancillaries.append(third[k]) k += 1 i += 1 fourth = [self.text[i]] if fourth[0] == "army": k = 1 # unit incrementor unit_line = stringToken(self.text[i + k], "\t") while unit_line[0] == "unit": unit_stats = stringToken(unit_line[2], " ") self.factions[j].characters[l].army.append( strat_map_unit(unit_line[1], int(unit_stats[1]), int(unit_stats[3]), int(unit_stats[5]))) k += 1 unit_line = stringToken(self.text[i + k], "\t") i += k l += 1 elif first[2] == " admiral" or first[2] == " general": self.factions[j].characters.append(general()) self.factions[j].characters[l].name = first[1] self.factions[j].characters[l].age = int( stringToken(first[3], " ")[1]) self.factions[j].characters[l].sub_faction = "" self.factions[j].characters[l].char_type = stringToken( first[2], " ")[0] self.factions[j].characters[l].pos = point( stringToken(first[5], " ")[1], stringToken(first[6], " ")[1], self.factions[j].characters[l].char_type, self.factions[j].characters[l].name) i += 1 second = stringToken(self.text[i], " ,") third = second fourth = third if second[0] == "traits": k = 1 # traits incrementor while k < len(second) - 1: self.factions[j].characters[l].traits.append( trait(second[k], int(second[k + 1]))) k += 2 i += 1 third = stringToken(self.text[i], " ,") fourth = third if third[0] == "ancillaries": k = 1 # ancillaries incrementor while k < len(third): self.factions[j].characters[ l].ancillaries.append(third[k]) k += 1 i += 1 fourth = [self.text[i]] if fourth[0] == "army": k = 1 # unit incrementor unit_line = stringToken(self.text[i + k], "\t") while unit_line[0] == "unit": unit_stats = stringToken(unit_line[2], " ") self.factions[j].characters[l].army.append( strat_map_unit(unit_line[1], int(unit_stats[1]), int(unit_stats[3]), int(unit_stats[5]))) k += 1 unit_line = stringToken(self.text[i + k], "\t") i += k l += 1 elif first[2] == " diplomat" or first[2] == " spy": self.factions[j].characters.append( strat_map_character()) self.factions[j].characters[l].name = first[1] self.factions[j].characters[l].age = int( stringToken(first[3], " ")[1]) # self.factions[j].characters[l].sub_faction = "" self.factions[j].characters[l].char_type = stringToken( first[2], " ")[0] self.factions[j].characters[l].pos = point( stringToken(first[5], " ")[1], stringToken(first[6], " ")[1], self.factions[j].characters[l].char_type, self.factions[j].characters[l].name) i += 1 second = stringToken(self.text[i], " ,") third = second if second[0] == "traits": k = 1 # traits incrementor while k < len(second) - 1: self.factions[j].characters[l].traits.append( trait(second[k], int(second[k + 1]))) k += 2 i += 1 third = stringToken(self.text[i], " ,") if third[0] == "ancillaries": k = 1 # ancillaries incrementor while k < len(third): self.factions[j].characters[ l].ancillaries.append(third[k]) k += 1 i += 1 l += 1 else: print("error reading characters in " + self.factions[j].name, file=sys.stdout) i += 1 #l = 0 # character record incrementor while stringToken( self.text[i], "\t")[0] != "relative ": # read characters_record line = stringToken(self.text[i], "\t,") if line[0] == "character_record": self.factions[j].characters.append( non_strat_map_character()) self.factions[j].characters[l].name = line[1] self.factions[j].characters[l].gender = line[3] # skip traits b/c all are 0 self.factions[j].characters[l].age = int( stringToken(line[8], " ")[1]) i += 1 l += 1 else: print("error reading characters_record in " + self.factions[j].name, file=sys.stdout) while stringToken(self.text[i], "\t")[0] == "relative ": # read relatives line = stringToken(self.text[i], "\t,") for n in range(0, len(self.factions[j].characters)): if line[1] == self.factions[j].characters[n].name: self.factions[j].characters[n].wife = line[3] q = 4 while line[q] != "end": self.factions[j].characters[n].children.append( line[q]) q += 1 break i += 1 j += 1
def populate(self): self.trim_text() for i in range(2, len(self.text)): rc = stringToken(self.text[i], "\t,") self.resources.append(resource(rc[1], int(rc[2]), int(rc[3])))
def populate(self): self.trim_text() for i in range(2, len(self.text)): lm = stringToken(self.text[i], "\t,") self.landmarks.append(landmark(lm[1], int(lm[2]), int(lm[3])))
def encode(classList): for i in range(0, len(classList)): classList[i] = stringToken(classList[i], " ") # print(classList[i][0]) return classList