def test_from_path_constructor_with_existing_file(self): tf = tempfile.NamedTemporaryFile(delete=False) self.addCleanup(tf.close) tf.write('stuff'.encode('utf8')) tf.flush() s = Source.from_path(tf.name) self.assertIsInstance(s, Source) self.assertEqual(s.path, tf.name) self.assertEqual(s.contents, 'stuff')
def _write_to_file(path, new_contents): source = Source.from_path(path) # strip callouts new_contents = re.sub(r' +#$', '', new_contents, flags=re.MULTILINE) new_contents = re.sub(r' +//$', '', new_contents, flags=re.MULTILINE) if not os.path.exists(path): dir = os.path.dirname(path) if not os.path.exists(dir): os.makedirs(dir) else: old_lines = source.lines new_lines = new_contents.strip('\n').split('\n') if "[..." not in new_contents: new_contents = _replace_lines_in(old_lines, new_lines) else: if new_contents.count("[...") == 1: split_line = [l for l in new_lines if "[..." in l][0] split_line_pos = new_lines.index(split_line) if split_line_pos == 0: new_contents = _replace_lines_in(old_lines, new_lines[1:]) elif split_line == new_lines[-1]: new_contents = _replace_lines_in(old_lines, new_lines[:-1]) elif split_line_pos == 1: if 'import' in new_lines[0]: new_contents = add_import_and_new_lines( new_lines, old_lines) elif 'class' in new_lines[0]: new_contents = add_to_class(new_lines, old_lines) else: lines_before = new_lines[:split_line_pos] last_line_before = lines_before[-1] lines_after = new_lines[split_line_pos + 1:] last_old_line = [ l for l in old_lines if l.strip() == last_line_before.strip() ][0] last_old_line_pos = old_lines.index(last_old_line) old_lines_after = old_lines[last_old_line_pos + 1:] # special-case: stray browser.quit in chap. 2 if 'rest of comments' in split_line: assert 'browser.quit()' in [ l.strip() for l in old_lines_after ] assert old_lines_after[-2] == 'browser.quit()' old_lines_after = old_lines_after[:-2] new_contents = '\n'.join( lines_before + [get_indent(split_line) + l for l in old_lines_after] + lines_after) elif new_contents.strip().startswith( "[...]") and new_contents.endswith("[...]"): new_contents = _replace_lines_in(old_lines, new_lines[1:-1]) else: raise Exception("I don't know how to deal with this") # strip trailing whitespace new_contents = re.sub(r'^ +$', '', new_contents, flags=re.MULTILINE) source.update(new_contents) source.write()
def test_from_path_constructor_with_nonexistent_file(self): s = Source.from_path('no.such.path') self.assertIsInstance(s, Source) self.assertEqual(s.path, 'no.such.path') self.assertEqual(s.contents, '')
def _write_to_file(path, new_contents): source = Source.from_path(path) # strip callouts new_contents = re.sub(r' +#$', '', new_contents, flags=re.MULTILINE) new_contents = re.sub(r' +//$', '', new_contents, flags=re.MULTILINE) if not os.path.exists(path): dir = os.path.dirname(path) if not os.path.exists(dir): os.makedirs(dir) else: old_lines = source.lines new_lines = new_contents.strip('\n').split('\n') if "[..." not in new_contents: new_contents = _replace_lines_in(old_lines, new_lines) else: if new_contents.count("[...") == 1: split_line = [l for l in new_lines if "[..." in l][0] split_line_pos = new_lines.index(split_line) if split_line_pos == 0: new_contents = _replace_lines_in(old_lines, new_lines[1:]) elif split_line == new_lines[-1]: new_contents = _replace_lines_in(old_lines, new_lines[:-1]) elif split_line_pos == 1: if 'import' in new_lines[0]: new_contents = add_import_and_new_lines(new_lines, old_lines) elif 'class' in new_lines[0]: new_contents = add_to_class(new_lines, old_lines) else: lines_before = new_lines[:split_line_pos] last_line_before = lines_before[-1] lines_after = new_lines[split_line_pos + 1:] last_old_line = [ l for l in old_lines if l.strip() == last_line_before.strip() ][0] last_old_line_pos = old_lines.index(last_old_line) old_lines_after = old_lines[last_old_line_pos + 1:] # special-case: stray browser.quit in chap. 2 if 'rest of comments' in split_line: assert 'browser.quit()' in [l.strip() for l in old_lines_after] assert old_lines_after[-2] == 'browser.quit()' old_lines_after = old_lines_after[:-2] new_contents = '\n'.join( lines_before + [get_indent(split_line) + l for l in old_lines_after] + lines_after ) elif new_contents.strip().startswith("[...]") and new_contents.endswith("[...]"): new_contents = _replace_lines_in(old_lines, new_lines[1:-1]) else: raise Exception("I don't know how to deal with this") # strip trailing whitespace new_contents = re.sub(r'^ +$', '', new_contents, flags=re.MULTILINE) source.update(new_contents) source.write()