Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

146 rindas
4.5 KiB

  1. import sys
  2. import os
  3. import subprocess
  4. import argparse
  5. parser = argparse.ArgumentParser(description='Convert sgf go records into a kifu format.')
  6. parser.add_argument('sgfFile')
  7. parser.add_argument('-se', "--splitevery", dest="step", type=int, default=50)
  8. parser.add_argument('-c', "--compile", action="store_true", dest="c", default=False)
  9. parser.add_argument('-o', "--open", action="store_true", dest="o", default=False)
  10. args = parser.parse_args(sys.argv[1:])
  11. filePath = args.sgfFile
  12. splitBoardAt = [x for x in range(0, 400, args.step)]
  13. with open(filePath, 'r') as myfile:
  14. sgfData = myfile.read().replace("\n", "").split(";")[1:]
  15. sgfData[-1] = sgfData[-1][:-1]
  16. header = sgfData[0]
  17. moves = sgfData[1:]
  18. def get_tag_from_header(tag):
  19. eventIdxStart = header.lower().find(tag.lower() + "[")
  20. eventIdxEnd = -1
  21. if eventIdxStart != -1 :
  22. eventIdxEnd = header.find("]", eventIdxStart)
  23. return header[len(tag) + 1 + eventIdxStart:eventIdxEnd]
  24. return ""
  25. def extractCoordinatesFromMove(move):
  26. try:
  27. firstCoordinate = move[2]
  28. secondCoordinate = ord(move[3])-96
  29. if ord(move[2]) >= ord("i"):
  30. firstCoordinate = chr(ord(firstCoordinate) + 1)
  31. return firstCoordinate, secondCoordinate
  32. except IndexError:
  33. return -1,-1
  34. def generateTitle():
  35. out = ""
  36. if parsedHeader["event"] != "":
  37. out+=parsedHeader["event"] + "\\\\"
  38. out += parsedHeader["playerBlack"]
  39. if parsedHeader["rankBlack"] != "":
  40. out += "[" + parsedHeader["rankBlack"] + "]"
  41. out += " - " + parsedHeader["playerWhite"]
  42. if parsedHeader["rankWhite"] != "":
  43. out += "[" + parsedHeader["rankWhite"] + "]"
  44. return out
  45. parsedHeader = {
  46. "event" : get_tag_from_header("EV"),
  47. "gameName" : get_tag_from_header("GN"),
  48. "date" : get_tag_from_header("RD"),
  49. "boardSize" : int(get_tag_from_header("SZ")),
  50. "playerBlack" : get_tag_from_header("PB"),
  51. "playerWhite" : get_tag_from_header("PW"),
  52. "rankBlack" : get_tag_from_header("BR"),
  53. "rankWhite" : get_tag_from_header("WR"),
  54. "komi" : get_tag_from_header("KM"),
  55. "result" : get_tag_from_header("RE")
  56. }
  57. outText = """
  58. \\documentclass[a4paper]{article}
  59. \\usepackage{psgo}
  60. \\usepackage[ngerman]{babel}
  61. \\usepackage[margin=2cm,nohead]{geometry}
  62. \\setgounit{0.5cm}
  63. \\author{}
  64. \\title{%s}
  65. \\date{%s}
  66. \\begin{document}
  67. \\maketitle
  68. \\vspace{3.5cm}
  69. \\begin{center}
  70. """ % (generateTitle(), parsedHeader["date"])
  71. finished = False
  72. for i in range(len(splitBoardAt)-1):
  73. currentSplit = splitBoardAt[i]
  74. nextSplit = splitBoardAt[i+1]
  75. outText += "\n\\setcounter{gomove}{0}\n"
  76. outText += "\\begin{psgoboard}\n\t"
  77. # old moves
  78. for j in range(currentSplit):
  79. firstCoordinate, secondCoordinate = extractCoordinatesFromMove(moves[j])
  80. outText += "\\move*{%s}{%d} " % (firstCoordinate, parsedHeader["boardSize"] - secondCoordinate + 1)
  81. if j % 5 == 4:
  82. outText += "\n\t"
  83. elif parsedHeader["boardSize"] - secondCoordinate < 9: # nice spacing
  84. outText += " "
  85. # new moves
  86. for j in range(nextSplit-currentSplit):
  87. firstCoordinate, secondCoordinate = extractCoordinatesFromMove(moves[currentSplit+j])
  88. if not (firstCoordinate == -1 or secondCoordinate == -1):
  89. outText += "\\move{%s}{%d} " % (firstCoordinate, parsedHeader["boardSize"] - secondCoordinate + 1)
  90. if j % 5 == 4:
  91. outText += "\n\t"
  92. elif parsedHeader["boardSize"] - secondCoordinate < 9: # nice spacing
  93. outText += " "
  94. # was it the last move?
  95. if currentSplit+j == len(moves)-1:
  96. finished = True
  97. break
  98. outText += "\n\\end{psgoboard}\n"
  99. if finished:
  100. break
  101. outText += """
  102. \\textbf{%s}
  103. \\end{center}
  104. \\end{document}
  105. """ % parsedHeader["result"]
  106. outFileBaseName = ".".join(filePath.split(".")[:-1])
  107. with open(outFileBaseName+ ".tex", 'w') as outFile:
  108. outFile.write(outText)
  109. # should be compiled to pdf?
  110. if args.c:
  111. try:
  112. subprocess.check_call(['latex', outFileBaseName + ".tex"], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
  113. subprocess.check_call(['dvips', "-P", "pdf", outFileBaseName + ".dvi"], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
  114. subprocess.check_call(['ps2pdf', outFileBaseName + ".ps"], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
  115. except subprocess.CalledProcessError:
  116. print("error")
  117. else:
  118. if args.o:
  119. os.system('"%s.pdf"' % outFileBaseName)