pbn2xml.py 3.4 KB

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