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 skipCodeBlock (self,s,i,kind): '''Skip the code block in a function or class definition.''' trace = False start = i if kind == 'class': i = self.skipInterface(s,i) else: i = self.skipBlock(s,i,delim1=None,delim2=None) if self.sigFailTokens: i = g.skip_ws(s,i) for z in self.sigFailTokens: if g.match(s,i,z): if trace: g.trace('failtoken',z) return start,False if i > start: i = self.skipNewline(s,i,kind) if trace: g.trace(g.callers()) g.trace('returns...\n',g.listToString(g.splitLines(s[start:i]))) return i,True
def replaceFileWithString(self, fn, s): '''Replace the file with s if s is different from theFile's contents. Return True if theFile was changed. ''' trace = False and not g.unitTesting verbose = False c = self.c x = self exists = g.os_path_exists(fn) if exists: # Read the file. Return if it is the same. s2, e = g.readFileIntoString(fn) if s2 is None: return False if s == s2: report = c.config.getBool('report_unchanged_files', default=True) if report and not g.unitTesting: g.es('unchanged:', fn) return False # Issue warning if directory does not exist. theDir = g.os_path_dirname(fn) if theDir and not g.os_path_exists(theDir): if not g.unitTesting: x.error('not written: %s directory not found' % fn) return False # Replace the file. try: f = open(fn, 'wb') # 2011/09/09: Use self.encoding. f.write(g.toEncodedString(s, encoding=self.encoding)) if trace: g.trace('encoding', self.encoding) if verbose: g.trace( 'fn', fn, '\nlines...\n%s' % (g.listToString(g.splitLines(s))), '\ncallers', g.callers(4)) f.close() if not g.unitTesting: # g.trace('created:',fn,g.callers()) if exists: g.es('wrote:', fn) else: g.es('created:', fn) return True except IOError: x.error('unexpected exception writing file: %s' % (fn)) g.es_exception() return False
def replaceFileWithString (self,fn,s): '''Replace the file with s if s is different from theFile's contents. Return True if theFile was changed. ''' trace = False and not g.unitTesting ; verbose = False x = self exists = g.os_path_exists(fn) if exists: # Read the file. Return if it is the same. s2,e = g.readFileIntoString(fn) if s2 is None: return False if s == s2: if not g.unitTesting: g.es('unchanged:',fn) return False # Issue warning if directory does not exist. theDir = g.os_path_dirname(fn) if theDir and not g.os_path_exists(theDir): if not g.unitTesting: x.error('not written: %s directory not found' % fn) return False # Replace the file. try: f = open(fn,'wb') # 2011/09/09: Use self.encoding. f.write(g.toEncodedString(s,encoding=self.encoding)) if trace: g.trace('encoding',self.encoding) if verbose: g.trace('fn',fn, '\nlines...\n%s' %(g.listToString(g.splitLines(s))), '\ncallers',g.callers(4)) f.close() if not g.unitTesting: # g.trace('created:',fn,g.callers()) if exists: g.es('wrote:',fn) else: g.es('created:',fn) return True except IOError: x.error('unexpected exception writing file: %s' % (fn)) g.es_exception() return False
def skipCodeBlock(self, s, i, kind): '''Skip the code block in a function or class definition.''' trace = False start = i if kind == 'class': i = self.skipInterface(s, i) else: i = self.skipBlock(s, i, delim1=None, delim2=None) if self.sigFailTokens: i = g.skip_ws(s, i) for z in self.sigFailTokens: if g.match(s, i, z): if trace: g.trace('failtoken', z) return start, False if i > start: i = self.skipNewline(s, i, kind) if trace: g.trace(g.callers()) g.trace('returns...\n', g.listToString(g.splitLines(s[start:i]))) return i, True
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 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