def propagate_changes(self, old_public_file, old_private_file): '''Propagate the changes from the public file (without_sentinels) to the private file (with_sentinels)''' trace = False and not g.unitTesting verbose = False import leo.core.leoAtFile as leoAtFile x = self at = self.c.atFileCommands at.errors = 0 if trace: g.trace('*** header scanned: encoding:', at.encoding) self.encoding = at.encoding s = at.readFileToUnicode(old_private_file) # Sets at.encoding and inits at.readLines. old_private_lines = g.splitLines(s) s = at.readFileToUnicode(old_public_file) if at.encoding != self.encoding: g.trace('can not happen: encoding mismatch: %s %s' % (at.encoding, self.encoding)) at.encoding = self.encoding old_public_lines = g.splitLines(s) if 0: g.trace('\nprivate lines...%s' % old_private_file) for s in old_private_lines: g.trace(type(s), g.isUnicode(s), repr(s)) g.trace('\npublic lines...%s' % old_public_file) for s in old_public_lines: g.trace(type(s), g.isUnicode(s), repr(s)) marker = x.markerFromFileLines(old_private_lines, old_private_file) if trace and verbose: g.trace( 'marker', marker, '\npublic_file', old_public_file, '\npublic lines...\n%s' % (g.listToString(old_public_lines, toRepr=True)), '\nprivate_file', old_private_file, '\nprivate lines...\n%s\n' % (g.listToString(old_private_lines, toRepr=True))) new_private_lines = x.propagate_changed_lines(old_public_lines, old_private_lines, marker) # Important bug fix: Never create the private file here! fn = old_private_file exists = g.os_path_exists(fn) different = new_private_lines != old_private_lines copy = exists and different if trace: g.trace('\nexists', exists, fn, 'different', different, 'errors', x.errors, at.errors) # 2010/01/07: check at.errors also. if copy and x.errors == 0 and at.errors == 0: s = ''.join(new_private_lines) ok = x.replaceFileWithString(fn, s) if trace: g.trace('ok', ok, 'writing private file', fn, g.callers()) return copy
def propagate_changes(self, old_public_file, old_private_file): ''' Propagate the changes from the public file (without_sentinels) to the private file (with_sentinels) ''' trace, verbose = False and not g.unitTesting, False import leo.core.leoAtFile as leoAtFile x = self ; at = self.c.atFileCommands at.errors = 0 if trace: g.trace('*** header scanned: encoding:',at.encoding) self.encoding = at.encoding s = at.readFileToUnicode(old_private_file) # Sets at.encoding and inits at.readLines. old_private_lines = g.splitLines(s) s = at.readFileToUnicode(old_public_file) if at.encoding != self.encoding: g.trace('can not happen: encoding mismatch: %s %s' % ( at.encoding,self.encoding)) at.encoding = self.encoding old_public_lines = g.splitLines(s) if 0: g.trace('\nprivate lines...%s' % old_private_file) for s in old_private_lines: g.trace(type(s),g.isUnicode(s),repr(s)) g.trace('\npublic lines...%s' % old_public_file) for s in old_public_lines: g.trace(type(s),g.isUnicode(s),repr(s)) marker = x.markerFromFileLines(old_private_lines,old_private_file) if trace and verbose: g.trace( 'marker',marker, '\npublic_file',old_public_file, '\npublic lines...\n%s' %( g.listToString(old_public_lines,toRepr=True)), '\nprivate_file',old_private_file, '\nprivate lines...\n%s\n' %( g.listToString(old_private_lines,toRepr=True))) new_private_lines = x.propagate_changed_lines( old_public_lines,old_private_lines,marker) # Important bug fix: Never create the private file here! fn = old_private_file exists = g.os_path_exists(fn) different = new_private_lines != old_private_lines copy = exists and different if trace: g.trace('\nexists',exists,fn,'different',different,'errors',x.errors,at.errors) # 2010/01/07: check at.errors also. if copy and x.errors == 0 and at.errors == 0: s = ''.join(new_private_lines) ok = x.replaceFileWithString(fn,s) if trace: g.trace('ok',ok,'writing private file',fn,g.callers()) return copy
def readFile (self,fileName,root): trace = False and not g.unitTesting verbose = True c = self.c if not g.enableDB: if trace: g.trace('g.enableDB is False') return '',False,None s,e = g.readFileIntoString(fileName,raw=True,silent=True) if s is None: if trace: g.trace('empty file contents',fileName) return s,False,None assert not g.isUnicode(s) if trace and verbose: for i,line in enumerate(g.splitLines(s)): print('%3d %s' % (i,repr(line))) # There will be a bug if s is not already an encoded string. key = self.fileKey(root.h,s,requireEncodedString=True) ok = self.db and key in self.db if trace: g.trace('in cache',ok,fileName,key) if ok: # Delete the previous tree, regardless of the @<file> type. while root.hasChildren(): root.firstChild().doDelete() # Recreate the file from the cache. aList = self.db.get(key) self.createOutlineFromCacheList(root.v,aList,fileName=fileName) return s,ok,key
def fastAddLastChild(self, parent_v, gnxString): ''' Create new VNode as last child of the receiver. If the gnx exists already, create a clone instead of new VNode. ''' trace = False and not g.unitTesting c = self.c indices = g.app.nodeIndices gnxString = g.toUnicode(gnxString) gnxDict = c.fileCommands.gnxDict if gnxString is None: v = None else: v = gnxDict.get(gnxString) is_clone = v is not None if trace: g.trace( 'clone', '%-5s' % (is_clone), 'parent_v', parent_v, 'gnx', gnxString, 'v', repr(v)) if is_clone: pass else: if gnxString: assert g.isUnicode(gnxString) v = leoNodes.VNode(context=c, gnx=gnxString) if g.trace_gnxDict: g.trace(c.shortFileName(), gnxString, v) else: v = leoNodes.VNode(context=c) # This is not an error: it can happen with @auto nodes. # g.trace('**** no gnx for',v,parent_v) child_v = v child_v._linkAsNthChild(parent_v, parent_v.numberOfChildren()) child_v.setVisited() # Supress warning/deletion of unvisited nodes. return is_clone, child_v
def readFile(self, fileName, root): ''' Read the file from the cache if possible. Return (s,ok,key) ''' # sfn = g.shortFileName(fileName) if not g.enableDB: return '', False, None s = g.readFileIntoEncodedString(fileName, silent=True) if s is None: return s, False, None assert not g.isUnicode(s) # There will be a bug if s is not already an encoded string. key = self.fileKey(fileName, s, requireEncodedString=True) # Fix bug #385: use the full fileName, not root.h. ok = self.db and key in self.db if ok: # Delete the previous tree, regardless of the @<file> type. while root.hasChildren(): root.firstChild().doDelete() # Recreate the file from the cache. aList = self.db.get(key) self.collectChangedNodes(root.v, aList, fileName) self.createOutlineFromCacheList2(root.v, aList) #self.createOutlineFromCacheList(root.v, aList, fileName=fileName) return s, ok, key
def export_outline(self, root, fn=None): ''' Entry point for export-jupyter-notebook Export the given .ipynb file. ''' self.root = root if not fn: fn = self.get_file_name() if not fn: return False try: nb = self.make_notebook() s = self.convert_notebook(nb) except Exception: g.es_exception() return False if not s: return False if g.isUnicode(s): s = g.toEncodedString(s, encoding='utf-8', reportErrors=True) try: f = open(fn, 'wb') f.write(s) f.close() g.es_print('wrote: %s' % fn) except IOError: g.es_print('can not open: %s' % fn) return True
def readFile(self, fileName, root): ''' Read the file from the cache if possible. Return (s,ok,key) ''' trace = (False or g.app.debug) and not g.unitTesting showHits, showLines, verbose = False, False, True sfn = g.shortFileName(fileName) if not g.enableDB: if trace and verbose: g.trace('g.enableDB is False', sfn) return '', False, None s = g.readFileIntoEncodedString(fileName, silent=True) if s is None: if trace: g.trace('empty file contents', sfn) return s, False, None assert not g.isUnicode(s) if trace and showLines: for i, line in enumerate(g.splitLines(s)): print('%3d %s' % (i, repr(line))) # There will be a bug if s is not already an encoded string. key = self.fileKey(root.h, s, requireEncodedString=True) ok = self.db and key in self.db if ok: if trace and showHits: g.trace('cache hit', key[-6:], sfn) # Delete the previous tree, regardless of the @<file> type. while root.hasChildren(): root.firstChild().doDelete() # Recreate the file from the cache. aList = self.db.get(key) self.createOutlineFromCacheList(root.v, aList, fileName=fileName) elif trace: g.trace('cache miss', key[-6:], sfn) return s, ok, key
def fastAddLastChild(self, parent_v, gnxString): ''' Create new VNode as last child of the receiver. If the gnx exists already, create a clone instead of new VNode. ''' trace = False and not g.unitTesting c = self.c gnxString = g.toUnicode(gnxString) gnxDict = c.fileCommands.gnxDict if gnxString is None: v = None else: v = gnxDict.get(gnxString) is_clone = v is not None if trace: g.trace( 'clone', '%-5s' % (is_clone), 'parent_v', parent_v, 'gnx', gnxString, 'v', repr(v)) if is_clone: pass else: if gnxString: assert g.isUnicode(gnxString) v = leoNodes.VNode(context=c, gnx=gnxString) if g.trace_gnxDict: g.trace(c.shortFileName(), gnxString, v) else: v = leoNodes.VNode(context=c) # This is not an error: it can happen with @auto nodes. # g.trace('**** no gnx for',v,parent_v) child_v = v child_v._linkAsNthChild(parent_v, parent_v.numberOfChildren()) child_v.setVisited() # Supress warning/deletion of unvisited nodes. return is_clone, child_v
def write(self, root): ''' Export_IPYNB: entry point for @auto writes. Signature must match signature of BaseWriter.write(). ''' at = self.c.atFileCommands if not root: g.trace('can not happen: no root') return False if not at and at.outputFile: g.trace('can not happen: no at.outputFile') return False # Write the text to at.outputFile. self.root = root try: nb = self.make_notebook() s = self.convert_notebook(nb) except Exception: g.es_exception() return False if not s: return False if g.isUnicode(s): s = g.toEncodedString(s, encoding='utf-8', reportErrors=True) at.outputFile.write(s) return True
def fileKey(self, s, content, requireEncodedString=False): ''' Compute the hash of s (usually a headline) and content. s may be unicode, content must be bytes (or plain string in Python 2.x) ''' trace = False and not g.unitTesting m = hashlib.md5() if g.isUnicode(s): s = g.toEncodedString(s) if g.isUnicode(content): if requireEncodedString: g.internalError('content arg must be str/bytes') content = g.toEncodedString(content) m.update(s) m.update(content) if trace: g.trace(m.hexdigest()) return "fcache/" + m.hexdigest()
def fileKey(self, s, content, requireEncodedString=False): ''' Compute the hash of s (usually a headline) and content. s may be unicode, content must be bytes (or plain string in Python 2.x) ''' ### Changed for reversion testing. ### This comment will be reverted in git to attempt to recreate the caching problem. m = hashlib.md5() if g.isUnicode(s): s = g.toEncodedString(s) if g.isUnicode(content): if requireEncodedString: g.internalError('content arg must be str/bytes') content = g.toEncodedString(content) m.update(s) m.update(content) return "fcache/" + m.hexdigest()
def fileKey(self, fileName, content, requireEncodedString=False): ''' Compute the hash of fileName and content. fileName may be unicode, content must be bytes (or plain string in Python 2.x). ''' m = hashlib.md5() if g.isUnicode(fileName): fileName = g.toEncodedString(fileName) if g.isUnicode(content): if requireEncodedString: g.internalError('content arg must be str/bytes') content = g.toEncodedString(content) # New in Leo 5.6: Use the git branch name in the key. branch = g.gitBranchName() branch = g.toEncodedString(branch) # Fix #475. m.update(branch) m.update(fileName) m.update(content) return "fcache/" + m.hexdigest()
def propagate_changes(self, old_public_file, old_private_file): """ Propagate the changes from the public file (without_sentinels) to the private file (with_sentinels) """ x, at = self, self.c.atFileCommands at.errors = 0 self.encoding = at.encoding s = at.readFileToUnicode(old_private_file) # Sets at.encoding and inits at.readLines. old_private_lines = g.splitLines(s or '') # #1466. s = at.readFileToUnicode(old_public_file) if at.encoding != self.encoding: g.trace( f"can not happen: encoding mismatch: {at.encoding} {self.encoding}" ) at.encoding = self.encoding old_public_lines = g.splitLines(s) if 0: g.trace(f"\nprivate lines...{old_private_file}") for s in old_private_lines: g.trace(type(s), g.isUnicode(s), repr(s)) g.trace(f"\npublic lines...{old_public_file}") for s in old_public_lines: g.trace(type(s), g.isUnicode(s), repr(s)) marker = x.markerFromFileLines(old_private_lines, old_private_file) new_private_lines = x.propagate_changed_lines(old_public_lines, old_private_lines, marker) # Important bug fix: Never create the private file here! fn = old_private_file exists = g.os_path_exists(fn) different = new_private_lines != old_private_lines copy = exists and different # 2010/01/07: check at.errors also. if copy and x.errors == 0 and at.errors == 0: s = ''.join(new_private_lines) x.replaceFileWithString(at.encoding, fn, s) return copy
def fileKey(self, fileName, content, requireEncodedString=False): ''' Compute the hash of fileName and content. fileName may be unicode, content must be bytes (or plain string in Python 2.x). ''' trace = False and not g.unitTesting m = hashlib.md5() if g.isUnicode(fileName): fileName = g.toEncodedString(fileName) if g.isUnicode(content): if requireEncodedString: g.internalError('content arg must be str/bytes') content = g.toEncodedString(content) # New in Leo 5.6: Use the git branch name in the key. branch = g.gitBranchName() # g.trace(type(branch), repr(branch)) branch = g.toEncodedString(branch) # Fix #475. m.update(branch) m.update(fileName) m.update(content) if trace: g.trace(m.hexdigest()) return "fcache/" + m.hexdigest()
def propagate_changes(self, old_public_file, old_private_file): ''' Propagate the changes from the public file (without_sentinels) to the private file (with_sentinels) ''' x, at = self, self.c.atFileCommands at.errors = 0 self.encoding = at.encoding s = at.readFileToUnicode(old_private_file) # Sets at.encoding and inits at.readLines. old_private_lines = g.splitLines(s) s = at.readFileToUnicode(old_public_file) if at.encoding != self.encoding: g.trace('can not happen: encoding mismatch: %s %s' % ( at.encoding, self.encoding)) at.encoding = self.encoding old_public_lines = g.splitLines(s) if 0: g.trace('\nprivate lines...%s' % old_private_file) for s in old_private_lines: g.trace(type(s), g.isUnicode(s), repr(s)) g.trace('\npublic lines...%s' % old_public_file) for s in old_public_lines: g.trace(type(s), g.isUnicode(s), repr(s)) marker = x.markerFromFileLines(old_private_lines, old_private_file) new_private_lines = x.propagate_changed_lines( old_public_lines, old_private_lines, marker) # Important bug fix: Never create the private file here! fn = old_private_file exists = g.os_path_exists(fn) different = new_private_lines != old_private_lines copy = exists and different # 2010/01/07: check at.errors also. if copy and x.errors == 0 and at.errors == 0: s = ''.join(new_private_lines) x.replaceFileWithString(at.encoding, fn, s) return copy
def write(self, root): ''' Export_IPYNB: entry point for @auto writes. Signature must match signature of BaseWriter.write(). ''' at = self.at # fn = root.atAutoNodeName() if not root: g.trace('can not happen: no root') return False if not at and at.outputFile: g.trace('can not happen: no at.outputFile') return False # Write the text to at.outputFile and return True. self.root = root self.put_outline() self.lines = self.clean_outline() s = '\n'.join(self.lines) if g.isUnicode(s): s = g.toEncodedString(s, encoding='utf-8', reportErrors=True) self.at.outputFile.write(s) return True
def fastAddLastChild(self, fileName, gnxString, parent_v): ''' Create new VNode as last child of the receiver. If the gnx exists already, create a clone instead of new VNode. ''' trace = 'gnx' in g.app.debug c = self.c gnxString = g.toUnicode(gnxString) gnxDict = c.fileCommands.gnxDict if gnxString is None: v = None else: v = gnxDict.get(gnxString) is_clone = v is not None if trace: g.trace('clone', '%-5s' % (is_clone), 'parent_v', parent_v, 'gnx', gnxString, 'v', repr(v)) if is_clone: # new-read: update tempRoots. if not hasattr(v, 'tempRoots'): v.tempRoots = set() v.tempRoots.add(fileName) else: if gnxString: assert g.isUnicode(gnxString) v = leoNodes.VNode(context=c, gnx=gnxString) if 'gnx' in g.app.debug: g.trace(c.shortFileName(), gnxString, v) else: v = leoNodes.VNode(context=c) # This is not an error: it can happen with @auto nodes. # g.trace('**** no gnx for',v,parent_v) # Indicate that this node came from an external file. v.tempRoots = set() v.tempRoots.add(fileName) child_v = v child_v._linkAsNthChild(parent_v, parent_v.numberOfChildren()) child_v.setVisited() # Supress warning/deletion of unvisited nodes. return is_clone, child_v
def export_outline(self, root, fn=None): ''' Entry point for export-jupyter-notebook Import the given .ipynb file. ''' self.root = root self.indent = 0 self.lines = [] if not fn: fn = self.get_file_name() if fn: self.put_outline() self.lines = self.clean_outline() s = '\n'.join(self.lines) if g.isUnicode(s): s = g.toEncodedString(s, encoding='utf-8', reportErrors=True) try: f = open(fn, 'wb') f.write(s) f.close() g.es_print('wrote: %s' % fn) except IOError: g.es_print('can not open: %s' % fn)
def cvt(s): return g.choose(g.isUnicode(s),s,g.toUnicode(s,self.encoding))
def propagate_changes(self, old_public_file, old_private_file): '''Propagate the changes from the public file (without_sentinels) to the private file (with_sentinels)''' trace = False and not g.unitTesting verbose = False x = self at = self.c.atFileCommands at.errors = 0 # A massive klude: read the file private file just to read the encoding. f = open(old_private_file, 'rb') # 2011/10/21: read in 'rb' mode. at.scanHeader(f, old_private_file) # sets at.encoding if trace and verbose: g.trace('*** header scanned: encoding:', at.encoding) f.close() self.encoding = at.encoding if trace and verbose: g.trace(self.encoding) if g.isPython3: try: old_public_lines = open(old_public_file, encoding=self.encoding).readlines() except UnicodeDecodeError: at.error('UnicodeDecodeError reading %s', old_public_file) return None try: old_private_lines = open(old_private_file, encoding=self.encoding).readlines() except UnicodeDecodeError: at.error('UnicodeDecodeError reading %s', old_private_file) return None else: try: old_public_lines = open(old_public_file).readlines() except UnicodeDecodeError: at.error('UnicodeDecodeError reading %s', old_public_file) return None try: old_private_lines = open(old_private_file).readlines() except UnicodeDecodeError: at.error('UnicodeDecodeError reading %s', old_private_file) return None # 2011/09/09: convert each line to unicode. def cvt(s): return g.choose(g.isUnicode(s), s, g.toUnicode(s, self.encoding)) old_public_lines = [cvt(s) for s in old_public_lines] old_private_lines = [cvt(s) for s in old_private_lines] if 0: g.trace('\nprivate lines...%s' % old_private_file) for s in old_private_lines: g.trace(type(s), g.isUnicode(s), repr(s)) g.trace('\npublic lines...%s' % old_public_file) for s in old_public_lines: g.trace(type(s), g.isUnicode(s), repr(s)) marker = x.markerFromFileLines(old_private_lines, old_private_file) if trace and verbose: g.trace( 'marker', marker, '\npublic_file', old_public_file, '\npublic lines...\n%s' % (g.listToString(old_public_lines, toRepr=True)), '\nprivate_file', old_private_file, '\nprivate lines...\n%s\n' % (g.listToString(old_private_lines, toRepr=True))) new_private_lines = x.propagate_changed_lines(old_public_lines, old_private_lines, marker) # Important bug fix: Never create the private file here! fn = old_private_file exists = g.os_path_exists(fn) different = new_private_lines != old_private_lines copy = exists and different if trace: g.trace('\nexists', exists, fn, 'different', different, 'errors', x.errors, at.errors) # 2010/01/07: check at.errors also. if copy and x.errors == 0 and at.errors == 0: s = ''.join(new_private_lines) ok = x.replaceFileWithString(fn, s) if trace: g.trace('ok', ok, 'writing private file', fn, g.callers()) return copy
def cvt(s): return g.choose(g.isUnicode(s), s, g.toUnicode(s, self.encoding))
def propagate_changes(self, old_public_file, old_private_file): '''Propagate the changes from the public file (without_sentinels) to the private file (with_sentinels)''' trace = False and not g.unitTesting verbose = False x = self ; at = self.c.atFileCommands at.errors = 0 # A massive klude: read the file private file just to read the encoding. f = open(old_private_file,'rb') # 2011/10/21: read in 'rb' mode. at.scanHeader(f,old_private_file) # sets at.encoding if trace and verbose: g.trace('*** header scanned: encoding:',at.encoding) f.close() self.encoding = at.encoding if trace and verbose: g.trace(self.encoding) if g.isPython3: try: old_public_lines = open(old_public_file,encoding=self.encoding).readlines() except UnicodeDecodeError: at.error('UnicodeDecodeError reading %s', old_public_file) return None try: old_private_lines = open(old_private_file,encoding=self.encoding).readlines() except UnicodeDecodeError: at.error('UnicodeDecodeError reading %s', old_private_file) return None else: try: old_public_lines = open(old_public_file).readlines() except UnicodeDecodeError: at.error('UnicodeDecodeError reading %s', old_public_file) return None try: old_private_lines = open(old_private_file).readlines() except UnicodeDecodeError: at.error('UnicodeDecodeError reading %s', old_private_file) return None # 2011/09/09: convert each line to unicode. def cvt(s): return g.choose(g.isUnicode(s),s,g.toUnicode(s,self.encoding)) old_public_lines = [cvt(s) for s in old_public_lines] old_private_lines = [cvt(s) for s in old_private_lines] if 0: g.trace('\nprivate lines...%s' % old_private_file) for s in old_private_lines: g.trace(type(s),g.isUnicode(s),repr(s)) g.trace('\npublic lines...%s' % old_public_file) for s in old_public_lines: g.trace(type(s),g.isUnicode(s),repr(s)) marker = x.markerFromFileLines(old_private_lines,old_private_file) if trace and verbose: g.trace( 'marker',marker, '\npublic_file',old_public_file, '\npublic lines...\n%s' %( g.listToString(old_public_lines,toRepr=True)), '\nprivate_file',old_private_file, '\nprivate lines...\n%s\n' %( g.listToString(old_private_lines,toRepr=True))) new_private_lines = x.propagate_changed_lines( old_public_lines,old_private_lines,marker) # Important bug fix: Never create the private file here! fn = old_private_file exists = g.os_path_exists(fn) different = new_private_lines != old_private_lines copy = exists and different if trace: g.trace('\nexists',exists,fn,'different',different,'errors',x.errors,at.errors) # 2010/01/07: check at.errors also. if copy and x.errors == 0 and at.errors == 0: s = ''.join(new_private_lines) ok = x.replaceFileWithString(fn,s) if trace: g.trace('ok',ok,'writing private file',fn,g.callers()) return copy