compatibility_csv_to_xml.py (3063B)
1 import csv 2 import sys 3 from xml.sax.saxutils import escape 4 5 SKIP_COLS = 1 6 SKIP_ROWS = 3 7 8 def compatibility_string_to_int(value): 9 value_lower = value.lower() 10 if (value_lower == "doesn't boot"): 11 return 1 12 elif (value_lower == "crashes in intro"): 13 return 2 14 elif (value_lower == "crashes in-game"): 15 return 3 16 elif (value_lower == "graphical/audio issues"): 17 return 4 18 elif (value_lower == "no issues"): 19 return 5 20 21 print("*** Unknown compatibility level string: '%s'" % value) 22 return 0 23 24 25 def compatibility_csv_to_xml(input_file, output_file): 26 fin = open(input_file, "r") 27 if (not input_file): 28 print("Failed to open %s" % input_file) 29 return False 30 31 fout = open(output_file, "w") 32 if (not output_file): 33 print("Failed to open %s" % output_file) 34 return False 35 36 fout.write("<?xml version=\"1.0\"?>\n") 37 fout.write("<compatibility-list>\n") 38 39 row_number = 0 40 for row in csv.reader(fin): 41 row_number += 1 42 if (row_number <= SKIP_ROWS): 43 continue 44 # Skip header rows 45 # TODO: Proper map for these if the column order changes 46 #if (row[SKIP_COLS + 0] == "Game Code" or row[SKIP_COLS + 1] == "Game Title" or row[SKIP_COLS + 2] == "Region" or 47 # row[SKIP_COLS + 3] == "Compatibility" or row[SKIP_COLS + 4] == "Upscaling Issues" or 48 # row[SKIP_COLS + 5] == "Version tested" or row[SKIP_COLS + 6] == "Comments"): 49 # continue 50 51 code = str(row[SKIP_COLS + 0]).strip() 52 title = str(row[SKIP_COLS + 1]).strip() 53 region = str(row[SKIP_COLS + 2]).strip() 54 compatibility = str(row[SKIP_COLS + 3]).strip() 55 upscaling_issues = str(row[SKIP_COLS + 4]).strip() 56 version_tested = str(row[SKIP_COLS + 5]).strip() 57 comments = str(row[SKIP_COLS + 6]).strip() 58 59 if (len(code) == 0): 60 print("** Code is missing for '%s' (%s), skipping" % (title, region)) 61 continue 62 63 # TODO: Quoting here 64 fout.write(" <entry code=\"%s\" title=\"%s\" region=\"%s\" compatibility=\"%d\">\n" % (escape(code), escape(title), escape(region), compatibility_string_to_int(compatibility))) 65 fout.write(" <compatibility>%s</compatibility>\n" % escape(compatibility)) 66 if (len(upscaling_issues) > 0): 67 fout.write(" <upscaling-issues>%s</upscaling-issues>\n" % escape(upscaling_issues)) 68 if (len(version_tested) > 0): 69 fout.write(" <version-tested>%s</version-tested>\n" % escape(version_tested)) 70 if (len(comments) > 0): 71 fout.write(" <comments>%s</comments>\n" % escape(comments)) 72 fout.write(" </entry>\n") 73 74 fout.write("</compatibility-list>\n") 75 fout.close() 76 fin.close() 77 return True 78 79 80 if (__name__ == "__main__"): 81 if (len(sys.argv) < 3): 82 print("Usage: %s <path to csv> <path to xml>" % sys.argv[0]) 83 sys.exit(1) 84 85 result = compatibility_csv_to_xml(sys.argv[1], sys.argv[2]) 86 sys.exit(0 if result else 1) 87