pbn2xml.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import re
  2. from datetime import date
  3. # from pprint import pprint
  4. from xml.etree.ElementTree import Element, SubElement, tostring, ElementTree
  5. # from xml.etree.ElementTree import dump
  6. import xml.dom.minidom
  7. def process_pbn(xml_root):
  8. order_of_suits = ["Spades", "Hearts", "Diamonds", "Clubs"]
  9. dict_of_hands = {"N": "North", "E": "East", "S": "South", "W": "West"}
  10. dealer = ''
  11. vulnerable = ''
  12. with open('hands.pbn', 'r') as f_pbn:
  13. for line in f_pbn:
  14. if not line.strip():
  15. dealer = ''
  16. vulnerable = ''
  17. if '[Dealer' in line:
  18. dealer = line[9]
  19. if '[Vulnerable' in line:
  20. reg = re.search('NS|EW|All|None', line)
  21. vulnerable = reg.group(0)
  22. if '[Deal ' in line:
  23. # print('{}'.format(line))
  24. # print('{}'.format(line[7]))
  25. # print('{}'.format(line[9:-3]))
  26. first_hand = line[7]
  27. assert first_hand in ['N', 'E', 'S', 'W'], \
  28. "Cannot determine first hand. Got {} but expected 'N', 'E', 'S' or 'W'".format(first_hand)
  29. order_of_hands = []
  30. if first_hand == 'N':
  31. order_of_hands = ["North", "East", "South", "West"]
  32. hands = line[9:-3].split(' ')
  33. assert len(hands) == 4, "There are no 4 hands in PBN string {}".format(line[9:-3])
  34. xml_deal = SubElement(xml_root, "Deal")
  35. assert dealer in ["N", "E", "S", "W", ''], "Unknown Dealer {}".format(dealer)
  36. if dealer:
  37. xml_deal.attrib["Dealer"] = dict_of_hands[dealer]
  38. assert vulnerable in ["NS", "EW", "All", "None", ''], "Unknown vulnerable {}".format(vulnerable)
  39. if vulnerable:
  40. xml_deal.attrib['Vulnerable'] = vulnerable
  41. i = 0
  42. for hand in hands:
  43. if '.' in hand:
  44. xml_hand = SubElement(xml_deal, "Hand")
  45. xml_hand.attrib["Position"] = order_of_hands[i]
  46. suits = hand.split('.')
  47. assert len(suits) == 4, "There are no 4 suits in PBN hand {}".format(suits)
  48. j = 0
  49. for suit in suits:
  50. xml_hand.attrib[order_of_suits[j]] = suit
  51. j += 1
  52. # dump(xml_hand)
  53. # dump(xml_deal)
  54. i += 1
  55. if __name__ == '__main__':
  56. xml_dealsets = Element("DealSets")
  57. xml_dealset = SubElement(xml_dealsets, "DealSet")
  58. xml_dealset.attrib["ComplexityLevel"] = "1"
  59. xml_dealset.attrib["IBIS-ID"] = "007-Foppe-Hemminga-{}".format(date.today().strftime('%Y%m%d'))
  60. xml_dealset.attrib["VulnerabilityDefault"] = "None"
  61. SubElement(xml_dealset, "Parameters")
  62. process_pbn(xml_dealset)
  63. # dump(xml_dealsets)
  64. xml_tree = ElementTree(xml_dealsets)
  65. dom = xml.dom.minidom.parseString(tostring(xml_dealsets, encoding='utf-8', method='xml'))
  66. # print(dom.toprettyxml().replace('<?xml version="1.0" ?>', '<?xml version=\'1.0\' encoding=\'utf-8\'?>'))
  67. with open("output.xml", 'w') as f_xml:
  68. f_xml.write(dom.toprettyxml().replace('<?xml version="1.0" ?>', '<?xml version=\'1.0\' encoding=\'utf-8\'?>'))
  69. # xml_tree.write("output.xml", encoding="utf-8", xml_declaration=True)