Created
April 27, 2017 06:04
-
-
Save zeroping/fedad2a9bfd50c4d81655d3d06038f25 to your computer and use it in GitHub Desktop.
A parser for the part files of the MKS ksp mod.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| class Converter: | |
| def __init__(self, part, name, inputs, outputs): | |
| self.part = part | |
| self.name = name | |
| self.inputs = inputs #these are (resouce name, consumption) tuples | |
| self.outputs = outputs | |
| def __repr__(self): | |
| retstr = "[" | |
| retstr += self.part + ":" + self.name + " " | |
| retstr += " in:" | |
| for i in self.inputs: | |
| retstr += i[0] + ":" + str(i[1]) + "," | |
| retstr += " out:" | |
| for i in self.outputs: | |
| retstr += i[0] + ":" + str(i[1]) + "," | |
| retstr += "]" | |
| return retstr | |
| class Pnode: | |
| def __init__ (self, text): | |
| self.propertyMap = {} | |
| self.subnodes = [] | |
| self.ptype="" | |
| self.parseText(text) | |
| def parseText(self, text): | |
| #print("part: " + text[0]) | |
| self.ptype = text[0].strip() | |
| #eliminate blank trailing lines | |
| while(len(text[-1].strip())== 0): | |
| text = text[0:-1] | |
| #sanity check | |
| if (text[1].strip() != "{") or (text[-1].strip() != "}"): | |
| print("something wrong with formating, dump follows") | |
| for t in text: | |
| print ("D: " + t) | |
| print("a" + str(text[-1]) + "b") | |
| lastHeader = " - " | |
| lastSection = None | |
| parenDepth = 0 | |
| subsection = [] | |
| #print("parsing : " + str(text[2:-1])) | |
| for line in text[2:-1]: | |
| line = line.split("//")[0] | |
| if (parenDepth == 0) and (" = " in line): | |
| propval = line.split("=") | |
| #print ("property " + propval[0].strip()) | |
| self.propertyMap[propval[0].strip()] = propval[1].strip() | |
| if(parenDepth > 0): | |
| subsection.append(line) | |
| if line.strip() == "{": | |
| if(parenDepth == 0): | |
| subsection.append(line) | |
| parenDepth += 1 | |
| #if(parenDepth == 1) : | |
| #print ("section " + lastHeader) | |
| if line.strip() == "}": | |
| #print(" u") | |
| parenDepth -= 1 | |
| if (parenDepth == 0): | |
| #print ("finished section: " + str(subsection)) | |
| self.subnodes.append(Pnode(subsection)) | |
| if parenDepth == 0 and line.replace("_","").isupper(): | |
| lastHeader = line.strip() | |
| subsection = [line] | |
| def findConverters(self): | |
| converters = [] | |
| selfconv = self.checkIfConverter() | |
| if(selfconv): | |
| converters.append(selfconv) | |
| for sub in self.subnodes: | |
| converters = converters + sub.findConverters() | |
| return converters | |
| def checkIfConverter(self): | |
| if ("name" in self.propertyMap): | |
| if self.propertyMap["name"] == "ModuleResourceConverter_USI": | |
| #print("CONVET") | |
| inp = [] | |
| outp = [] | |
| for sub in self.subnodes: | |
| #print(sub.ptype) | |
| if sub.ptype == "INPUT_RESOURCE" : | |
| #print(sub.propertyMap) | |
| inp.append((sub.propertyMap["ResourceName"], float(sub.propertyMap["Ratio"]) )) | |
| if sub.ptype == "OUTPUT_RESOURCE" : | |
| #print(sub.propertyMap) | |
| outp.append((sub.propertyMap["ResourceName"], float(sub.propertyMap["Ratio"]) )) | |
| return Converter("a",self.propertyMap["ConverterName"], inp, outp) | |
| return None | |
| def __repr__(self): | |
| retstr = "" | |
| retstr += "type " + str(self.ptype) + "\n" | |
| retstr += "properties " + str(self.propertyMap) + "\n" | |
| retstr += "subnodes " + str([x.ptype for x in self.subnodes]) | |
| retstr += "\n\n" | |
| return retstr | |
| print("starting") | |
| partsdir = "/home/scott/games/ksp/KSP_linux_1.2.1/GameData/UmbraSpaceIndustries/MKS/Parts/" | |
| partfile = "/home/scott/games/ksp/KSP_linux_1.2.1/GameData/UmbraSpaceIndustries/MKS/Parts/Tundra_AssemblyPlant.cfg" | |
| outcsv = "/home/scott/projects/MKSparser/output.csv" | |
| import csv | |
| csvfile = open(outcsv, 'w') | |
| csvwriter = csv.writer(csvfile, quoting=csv.QUOTE_NONNUMERIC) | |
| #partfile = open(partfile, "r") | |
| #part = Pnode(partfile.readlines()) | |
| parts = [] | |
| for directory, subdirectories, files in os.walk(partsdir): | |
| for file in files: | |
| print(" parsing file " + file) | |
| partfile = open(os.path.join(directory, file)) | |
| partroot = Pnode(partfile.readlines()) | |
| partfile.close() | |
| parts.append(partroot) | |
| resources = ["ElectricCharge","Machinery","Recyclables"] | |
| partlist = [] | |
| for part in parts: | |
| convs = part.findConverters() | |
| if (len(part.findConverters())): | |
| print() | |
| print(part.propertyMap["title"]) | |
| print(" " + str(convs)) | |
| #for conv in convs: | |
| #for inp in conv.outputs: | |
| #if inp[0] not in resources: | |
| #resources.append(inp[0]) | |
| #for conv in convs: | |
| #for inp in conv.inputs: | |
| #if inp[0] not in resources: | |
| #resources.append(inp[0]) | |
| partlist.append( (part, convs) ) | |
| #make list of resouces | |
| for part,convs in partlist: | |
| for conv in convs: | |
| for inp in conv.outputs: | |
| if inp[0] not in resources: | |
| resources.append(inp[0]) | |
| for part,convs in partlist: | |
| for conv in convs: | |
| for inp in conv.inputs: | |
| if inp[0] not in resources: | |
| resources.append(inp[0]) | |
| print(resources) | |
| csvwriter.writerow( ["Part", "Converter"] + resources) | |
| for part,convs in partlist: | |
| csvwriter.writerow( [ part.propertyMap["title"]]) | |
| for conv in convs: | |
| consumptions = [] | |
| for r in resources: | |
| consumption = 0 | |
| for rinput in conv.inputs: | |
| if rinput[0] == r: | |
| consumption = 0.0-rinput[1] | |
| for routput in conv.outputs: | |
| if routput[0] == r: | |
| consumption += routput[1] | |
| if(consumption == 0): | |
| consumption = "" | |
| else: | |
| consumption = str(consumption) | |
| consumptions.append(consumption) | |
| csvwriter.writerow( ["", conv.name] + consumptions) | |
| csvfile.close() | |
| #part = parts[-37] | |
| #print(part) | |
| #print ("----") | |
| #print(part.subnodes[-8]) | |
| #print ("----") | |
| ##for sub in part.subnodes: | |
| ##if(sub.ptype == "MODULE"): | |
| ##print(sub.propertyMap["name"]) | |
| #print(part.propertyMap["title"]) | |
| #print(" " + str(part.findConverters())) | |
| #for line in partfile.readlines:: | |
| # print("l: " + line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment