Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import leo.core.leoBridge as leoBridge
  2. def outlineDFW(outline):
  3. """ Walk the specified outline
  4. returning the headlines in
  5. depth first order
  6. Arguments:
  7. outline: Tuples specifying the outline
  8. Returns:
  9. hdr: Next headline
  10. Raises:
  11. StopIteration
  12. When there is no next headline,
  13. StopIteration is raised.
  14. """
  15.  
  16. lifoIdx = [0]
  17. lifoLevelList = [outline]
  18. #import pdb; pdb.set_trace()
  19. while True:
  20. idx = lifoIdx[-1]
  21. lvlList = lifoLevelList[-1]
  22. if idx >= len(lvlList):
  23. if lifoIdx:
  24. lifoIdx.pop()
  25. lifoLevelList.pop()
  26. if not lifoIdx:
  27. break
  28. continue
  29. hdrOrTuple = lvlList[idx]
  30. idx += 1
  31. lifoIdx[-1] = idx
  32. if isinstance(hdrOrTuple, tuple):
  33. lifoLevelList.append(hdrOrTuple)
  34. lifoIdx.append(0)
  35. else:
  36. yield hdrOrTuple
  37. def checkOutline(cmdrT, outline):
  38. stopOn1stError = False
  39.  
  40. root = cmdrT.rootPosition()
  41. walkList = [posX.h for posX in root.self_and_subtree_iter()]
  42. checkList = [hdr for hdr in outlineDFW(outline)]
  43. if len(walkList) == len(checkList):
  44. if walkList != checkList:
  45. for act, exp in zip(walkList, checkList):
  46. if act != exp:
  47. print(f'{act} != {exp}')
  48. if stopOn1stError:
  49. import pdb; pdb.set_trace()
  50. exit(9)
  51. exit(50)
  52. else:
  53. print('Expected:')
  54. for hdr in checkList:
  55. print(hdr)
  56. print('Actual:')
  57. for hdr in walkList:
  58. print(hdr)
  59. if stopOn1stError:
  60. for line in open('test.leo', 'r'):
  61. print(line, end='')
  62. import pdb; pdb.set_trace()
  63. exit(9)
  64. exit(50)
  65. def createOutline(cmdrT, outline):
  66. lifoIdx = [0]
  67. lifoLevelList = [outline]
  68. curPos = cmdrT.rootPosition().copy()
  69. parPos = None
  70. lifoPos = [parPos]
  71. #import pdb; pdb.set_trace()
  72. while True:
  73. idx = lifoIdx[-1]
  74. lvlList = lifoLevelList[-1]
  75. if idx >= len(lvlList):
  76. if lifoIdx:
  77. lifoIdx.pop()
  78. lifoLevelList.pop()
  79. lifoPos.pop()
  80. if not lifoIdx:
  81. break
  82. continue
  83. parPos = lifoPos[-1]
  84. hdrOrTuple = lvlList[idx]
  85. idx += 1
  86. lifoIdx[-1] = idx
  87. if isinstance(hdrOrTuple, tuple):
  88. lifoLevelList.append(hdrOrTuple)
  89. lifoIdx.append(0)
  90. lifoPos.append(setPos.copy())
  91. continue
  92. if not curPos:
  93. curPos = parPos.insertAsLastChild()
  94. if isinstance(hdrOrTuple, str):
  95. curPos.h = hdrOrTuple
  96. setPos = curPos
  97. curPos = None
  98.  
  99. outline = (
  100. '0 Headline',
  101. (
  102. '0.0 Headline',
  103. (
  104. '0.0.0 Headline',
  105. ),
  106. '0.1 Headline',
  107. '0.2 Headline',
  108. (
  109. '0.2.0 Headline',
  110. )
  111. ),
  112. )
  113. def main():
  114. bridge = leoBridge.controller(gui='nullGui', silent=True,
  115. verbose=False, loadPlugins=False, readSettings=False)
  116. for i in range(1000):
  117. c = bridge.createFrame('')
  118. createOutline(c, outline)
  119. c.fileCommands.save('test.leo')
  120. c.close()
  121. c2 = bridge.openLeoFile('test.leo')
  122. checkOutline(c2, outline)
  123. c2.close()
  124. print('ok')
  125. if __name__ == '__main__':
  126. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement