KSP_PartParser: Added commandline option for csv, restored default
KSP_PartParser: Added commandline option for csv, restored default
functionality to human-readable printout.

--- a/KSP_PartParser.py
+++ b/KSP_PartParser.py
@@ -10,6 +10,43 @@
 from _tools import warn

 from collections import OrderedDict

 

+def printCSV(parts):

+	csv_columns = OrderedDict({o.__name__: [o.HumanName] for o in Component.__subclasses__() if o is not MultiValueComponent})

+	

+	for part in parts:

+		for mvc in MultiValueComponent.__subclasses__():

+			if mvc.__name__ in part._components:

+				if mvc.__name__ not in csv_columns:

+					csv_columns[mvc.__name__] = set()

+				for key in getattr(part, "get{0}Node".format(mvc.__name__))().getValue().keys():

+					csv_columns[mvc.__name__].add(key)

+	

+	csv_columns.move_to_end('Title', last=False)

+	header1 = ["{0} {1}".format(o, k) for o in csv_columns for k in csv_columns[o]]

+	

+	print(','.join(header1))

+	

+	for part in parts:

+		row = []

+		for classname in csv_columns:

+			if classname in part._components:

+				node = getattr(part, "get{0}Node".format(classname))()

+			else:

+				node = None

+			classfields = []

+			for key in csv_columns[classname]:

+				if isinstance(node, MultiValueComponent):

+					if key in node.getValue():

+						classfields += [str(node.getValue()[key])]

+					else:

+						classfields += ['']

+				elif node is None:

+					classfields += ['']

+				else:

+					classfields += [str(node.getValue())]

+			row += classfields

+		print(','.join(row))

+

 def main():

 	import argparse, os.path

 	

@@ -17,6 +54,7 @@
 	parser.add_argument('files', metavar='FILE', type=str, nargs='+', help='The file or files to be parsed.')

 	parser.add_argument('-k', '--ksp-dir', metavar='DIR', type=str, nargs=1, help='Path to the KSP main directory.')

 	parser.add_argument('-r', '--resource-file', metavar='FILE', type=str, nargs=1, help='The ResourcesGeneric.cfg file to be used.')

+	parser.add_argument('-c', '--csv', action='store_true', help='Creates CSV output for all parsed part files.')

 	

 	args = parser.parse_args()

 	

@@ -47,7 +85,6 @@
 	global resources

 	resources = Resource.createFromFile(resourceFile)

 	

-	csv_columns = OrderedDict({o.__name__: [o.HumanName] for o in Component.__subclasses__() if o is not MultiValueComponent})

 	parts = []

 	for filename in files:

 		_, ext = os.path.splitext(filename)

@@ -57,40 +94,15 @@
 			continue

 		

 		part = Part.createFromFile(filename)

-		for mvc in MultiValueComponent.__subclasses__():

-			if mvc.__name__ in part._components:

-				if mvc.__name__ not in csv_columns:

-					csv_columns[mvc.__name__] = set()

-				for key in getattr(part, "get{0}Node".format(mvc.__name__))().getValue().keys():

-					csv_columns[mvc.__name__].add(key)

+		

 		#print(part)

 		parts.append(part)

 	

-	csv_columns.move_to_end('Title', last=False)

-	header1 = ["{0} {1}".format(o, k) for o in csv_columns for k in csv_columns[o]]

-	

-	print(','.join(header1))

-	

-	for part in parts:

-		row = []

-		for classname in csv_columns:

-			if classname in part._components:

-				node = getattr(part, "get{0}Node".format(classname))()

-			else:

-				node = None

-			classfields = []

-			for key in csv_columns[classname]:

-				if isinstance(node, MultiValueComponent):

-					if key in node.getValue():

-						classfields += [str(node.getValue()[key])]

-					else:

-						classfields += ['']

-				elif node is None:

-					classfields += ['']

-				else:

-					classfields += [str(node.getValue())]

-			row += classfields

-		print(','.join(row))

+	if args.csv:

+		printCSV(parts)

+	else:

+		for part in parts:

+			print(part)

 

 if __name__ == "__main__":

 	main()