def readFile(self, fileName): '''Read the opml file.''' dumpTree = False if not fileName: return g.trace('no fileName') c = self.c.new() # Create the new commander *now* # so that created vnodes will have the proper context. # Pass one: create the intermediate nodes. dummyRoot = self.parse_opml_file(fileName) if not dummyRoot: return if dumpTree: self.dumpTree(dummyRoot) # Pass two: create the outline from the sax nodes. children = self.createVnodes(c, dummyRoot) p = leoNodes.Position(v=children[0], childIndex=0, stack=None) # Check the outline. errors = c.checkOutline() if errors: c.dumpOutline() return g.trace('%s errors!' % errors) # if self.opml_read_derived_files: # at = c.atFileCommands # c.fileCommands.tnodesDict = self.createTnodesDict() # self.resolveTnodeLists(c) # if self.opml_read_derived_files: # c.atFileCommands.readAll(c.rootPosition()) c.selectPosition(p) c.redraw() return c # for testing.
def init_env(self): ''' Init c.abbrev_subst_env by executing the contents of the @data abbreviations-subst-env node. ''' c = self.c at = c.atFileCommands if c.abbrev_place_start and self.enabled: aList = self.subst_env script = [] for z in aList: # Compatibility with original design. if z.startswith('\\:'): script.append(z[2:]) else: script.append(z) script = ''.join(script) # Allow Leo directives in @data abbreviations-subst-env trees. import leo.core.leoNodes as leoNodes v = leoNodes.VNode(context=c) root = leoNodes.Position(v) # Similar to g.getScript. script = at.writeFromString( root=root, s=script, forcePythonSentinels=True, useSentinels=False) script = script.replace("\r\n", "\n") try: exec(script, c.abbrev_subst_env, c.abbrev_subst_env) except Exception: g.es('Error exec\'ing @data abbreviations-subst-env') g.es_exception() else: c.abbrev_subst_start = False
def create_nodes(self, parent, parent_d): '''Create the tree of nodes rooted in parent.''' import pprint trace = False and not g.unitTesting d = self.gnx_dict if trace: g.trace(parent.h, pprint.pprint(parent_d)) for child_gnx in parent_d.get('children'): d2 = d.get(child_gnx) if trace: g.trace('child', pprint.pprint(d2)) if child_gnx in self.vnodes_dict: # It's a clone. v = self.vnodes_dict.get(child_gnx) n = parent.numberOfChildren() child = leoNodes.Position(v) child._linkAsNthChild(parent, n) # Don't create children again. else: child = parent.insertAsLastChild() child.h = d2.get('h') or '<**no h**>' child.b = d2.get('b') or g.u('') if d2.get('gnx'): child.v.findIndex = gnx = d2.get('gnx') self.vnodes_dict[gnx] = child.v if d2.get('ua'): child.u = d2.get('ua') self.create_nodes(child, d2)
def readFile(self, fileName): """Read the opml file.""" dumpTree = False if not fileName: g.trace('no fileName') return None # Create the new commander *now*, so that created vnodes will have the proper context. c = self.c.new() # Pass one: create the intermediate nodes. dummyRoot = self.parse_opml_file(fileName) if not dummyRoot: return None if dumpTree: self.dumpTree(dummyRoot) # Pass two: create the outline from the sax nodes. children = self.createVnodes(c, dummyRoot) p = leoNodes.Position(v=children[0], childIndex=0, stack=None) # Check the outline. errors = c.checkOutline() if errors: c.dumpOutline() g.trace('%s errors!' % errors) return None c.selectPosition(p) c.redraw() return c # for testing.
def paste_outline(self, fn, s): ''' Paste an outline into the present outline from the s. Nodes do *not* retain their original identify. Similar to fc.getLeoOutlineFromClipboard ''' c, fc = self.c, self.c.fileCommands parent = self.file_node fc.initReadIvars() # Save... children = c.hiddenRootNode.children oldGnxDict = fc.gnxDict fc.gnxDict = {} try: fc.usingClipboard = True # This encoding must match the encoding used in putLeoOutline. s = g.toEncodedString(s, fc.leo_file_encoding, reportErrors=True) # readSaxFile modifies the hidden root. v = fc.readSaxFile( theFile=None, fileName=fn, silent=True, # don't tell about stylesheet elements. inClipboard=True, reassignIndices=True, s=s) if not v: g.es("invalid external file", color="blue") return None finally: fc.usingClipboard = False # Restore... c.hiddenRootNode.children = children # Unlink v from the hidden root. v.parents.remove(c.hiddenRootNode) p = leoNodes.Position(v) n = parent.numberOfChildren() p._linkAsNthChild(parent, n, adjust=False) # Do *not* adjust links when linking v. # The read code has already done that. # Reassign indices. fc.gnxDict = oldGnxDict ni = g.app.nodeIndices for p2 in p.self_and_subtree(): ni.getNewIndex(p2.v) # Clean up. fc.initReadIvars() c.validateOutline() c.setCurrentPosition(p) # c.selectPosition causes flash(!) return p
def init_env(self): """ Init c.abbrev_subst_env by executing the contents of the @data abbreviations-subst-env node. """ c = self.c at = c.atFileCommands if c.abbrev_place_start and self.enabled: aList = self.subst_env script = [] for z in aList: # Compatibility with original design. if z.startswith('\\:'): script.append(z[2:]) else: script.append(z) script = ''.join(script) # Allow Leo directives in @data abbreviations-subst-env trees. import leo.core.leoNodes as leoNodes # #1674: Avoid unnecessary entries in c.fileCommands.gnxDict. root = c.rootPosition() if root: v = root.v else: # Defensive programming. Probably will never happen. v = leoNodes.VNode(context=c) root = leoNodes.Position(v) # Similar to g.getScript. script = at.stringToString(root=root, s=script, forcePythonSentinels=True, sentinels=False) script = script.replace("\r\n", "\n") try: exec(script, c.abbrev_subst_env, c.abbrev_subst_env) except Exception: g.es('Error exec\'ing @data abbreviations-subst-env') g.es_exception() else: c.abbrev_subst_start = False
def create_nodes(self, parent, parent_d): '''Create the tree of nodes rooted in parent.''' d = self.gnx_dict for child_gnx in parent_d.get('children'): d2 = d.get(child_gnx) if child_gnx in self.vnodes_dict: # It's a clone. v = self.vnodes_dict.get(child_gnx) n = parent.numberOfChildren() child = leoNodes.Position(v) child._linkAsNthChild(parent, n) # Don't create children again. else: child = parent.insertAsLastChild() child.h = d2.get('h') or '<**no h**>' child.b = d2.get('b') or '' if d2.get('gnx'): child.v.findIndex = gnx = d2.get('gnx') self.vnodes_dict[gnx] = child.v if d2.get('ua'): child.u = d2.get('ua') self.create_nodes(child, d2)