Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import leo.core.leoBridge as leoBridge
- def outlineDFW(outline):
- """ Walk the specified outline
- returning the headlines in
- depth first order
- Arguments:
- outline: Tuples specifying the outline
- Returns:
- hdr: Next headline
- Raises:
- StopIteration
- When there is no next headline,
- StopIteration is raised.
- """
- lifoIdx = [0]
- lifoLevelList = [outline]
- #import pdb; pdb.set_trace()
- while True:
- idx = lifoIdx[-1]
- lvlList = lifoLevelList[-1]
- if idx >= len(lvlList):
- if lifoIdx:
- lifoIdx.pop()
- lifoLevelList.pop()
- if not lifoIdx:
- break
- continue
- hdrOrTuple = lvlList[idx]
- idx += 1
- lifoIdx[-1] = idx
- if isinstance(hdrOrTuple, tuple):
- lifoLevelList.append(hdrOrTuple)
- lifoIdx.append(0)
- else:
- yield hdrOrTuple
- def checkOutline(cmdrT, outline):
- stopOn1stError = False
- root = cmdrT.rootPosition()
- walkList = [posX.h for posX in root.self_and_subtree_iter()]
- checkList = [hdr for hdr in outlineDFW(outline)]
- if len(walkList) == len(checkList):
- if walkList != checkList:
- for act, exp in zip(walkList, checkList):
- if act != exp:
- print(f'{act} != {exp}')
- if stopOn1stError:
- import pdb; pdb.set_trace()
- exit(9)
- exit(50)
- else:
- print('Expected:')
- for hdr in checkList:
- print(hdr)
- print('Actual:')
- for hdr in walkList:
- print(hdr)
- if stopOn1stError:
- for line in open('test.leo', 'r'):
- print(line, end='')
- import pdb; pdb.set_trace()
- exit(9)
- exit(50)
- def createOutline(cmdrT, outline):
- lifoIdx = [0]
- lifoLevelList = [outline]
- curPos = cmdrT.rootPosition().copy()
- parPos = None
- lifoPos = [parPos]
- #import pdb; pdb.set_trace()
- while True:
- idx = lifoIdx[-1]
- lvlList = lifoLevelList[-1]
- if idx >= len(lvlList):
- if lifoIdx:
- lifoIdx.pop()
- lifoLevelList.pop()
- lifoPos.pop()
- if not lifoIdx:
- break
- continue
- parPos = lifoPos[-1]
- hdrOrTuple = lvlList[idx]
- idx += 1
- lifoIdx[-1] = idx
- if isinstance(hdrOrTuple, tuple):
- lifoLevelList.append(hdrOrTuple)
- lifoIdx.append(0)
- lifoPos.append(setPos.copy())
- continue
- if not curPos:
- curPos = parPos.insertAsLastChild()
- if isinstance(hdrOrTuple, str):
- curPos.h = hdrOrTuple
- setPos = curPos
- curPos = None
- outline = (
- '0 Headline',
- (
- '0.0 Headline',
- (
- '0.0.0 Headline',
- ),
- '0.1 Headline',
- '0.2 Headline',
- (
- '0.2.0 Headline',
- )
- ),
- )
- def main():
- bridge = leoBridge.controller(gui='nullGui', silent=True,
- verbose=False, loadPlugins=False, readSettings=False)
- for i in range(1000):
- c = bridge.createFrame('')
- createOutline(c, outline)
- c.fileCommands.save('test.leo')
- c.close()
- c2 = bridge.openLeoFile('test.leo')
- checkOutline(c2, outline)
- c2.close()
- print('ok')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement