Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../usr/lib64/python2..../lib2to3
File: refactor.py
if results:
[500] Fix | Delete
new = fixer.transform(node, results)
[501] Fix | Delete
if new is not None:
[502] Fix | Delete
node.replace(new)
[503] Fix | Delete
node = new
[504] Fix | Delete
[505] Fix | Delete
def processed_file(self, new_text, filename, old_text=None, write=False,
[506] Fix | Delete
encoding=None):
[507] Fix | Delete
"""
[508] Fix | Delete
Called when a file has been refactored and there may be changes.
[509] Fix | Delete
"""
[510] Fix | Delete
self.files.append(filename)
[511] Fix | Delete
if old_text is None:
[512] Fix | Delete
old_text = self._read_python_source(filename)[0]
[513] Fix | Delete
if old_text is None:
[514] Fix | Delete
return
[515] Fix | Delete
equal = old_text == new_text
[516] Fix | Delete
self.print_output(old_text, new_text, filename, equal)
[517] Fix | Delete
if equal:
[518] Fix | Delete
self.log_debug("No changes to %s", filename)
[519] Fix | Delete
if not self.write_unchanged_files:
[520] Fix | Delete
return
[521] Fix | Delete
if write:
[522] Fix | Delete
self.write_file(new_text, filename, old_text, encoding)
[523] Fix | Delete
else:
[524] Fix | Delete
self.log_debug("Not writing changes to %s", filename)
[525] Fix | Delete
[526] Fix | Delete
def write_file(self, new_text, filename, old_text, encoding=None):
[527] Fix | Delete
"""Writes a string to a file.
[528] Fix | Delete
[529] Fix | Delete
It first shows a unified diff between the old text and the new text, and
[530] Fix | Delete
then rewrites the file; the latter is only done if the write option is
[531] Fix | Delete
set.
[532] Fix | Delete
"""
[533] Fix | Delete
try:
[534] Fix | Delete
f = _open_with_encoding(filename, "w", encoding=encoding)
[535] Fix | Delete
except os.error as err:
[536] Fix | Delete
self.log_error("Can't create %s: %s", filename, err)
[537] Fix | Delete
return
[538] Fix | Delete
try:
[539] Fix | Delete
f.write(_to_system_newlines(new_text))
[540] Fix | Delete
except os.error as err:
[541] Fix | Delete
self.log_error("Can't write %s: %s", filename, err)
[542] Fix | Delete
finally:
[543] Fix | Delete
f.close()
[544] Fix | Delete
self.log_debug("Wrote changes to %s", filename)
[545] Fix | Delete
self.wrote = True
[546] Fix | Delete
[547] Fix | Delete
PS1 = ">>> "
[548] Fix | Delete
PS2 = "... "
[549] Fix | Delete
[550] Fix | Delete
def refactor_docstring(self, input, filename):
[551] Fix | Delete
"""Refactors a docstring, looking for doctests.
[552] Fix | Delete
[553] Fix | Delete
This returns a modified version of the input string. It looks
[554] Fix | Delete
for doctests, which start with a ">>>" prompt, and may be
[555] Fix | Delete
continued with "..." prompts, as long as the "..." is indented
[556] Fix | Delete
the same as the ">>>".
[557] Fix | Delete
[558] Fix | Delete
(Unfortunately we can't use the doctest module's parser,
[559] Fix | Delete
since, like most parsers, it is not geared towards preserving
[560] Fix | Delete
the original source.)
[561] Fix | Delete
"""
[562] Fix | Delete
result = []
[563] Fix | Delete
block = None
[564] Fix | Delete
block_lineno = None
[565] Fix | Delete
indent = None
[566] Fix | Delete
lineno = 0
[567] Fix | Delete
for line in input.splitlines(True):
[568] Fix | Delete
lineno += 1
[569] Fix | Delete
if line.lstrip().startswith(self.PS1):
[570] Fix | Delete
if block is not None:
[571] Fix | Delete
result.extend(self.refactor_doctest(block, block_lineno,
[572] Fix | Delete
indent, filename))
[573] Fix | Delete
block_lineno = lineno
[574] Fix | Delete
block = [line]
[575] Fix | Delete
i = line.find(self.PS1)
[576] Fix | Delete
indent = line[:i]
[577] Fix | Delete
elif (indent is not None and
[578] Fix | Delete
(line.startswith(indent + self.PS2) or
[579] Fix | Delete
line == indent + self.PS2.rstrip() + u"\n")):
[580] Fix | Delete
block.append(line)
[581] Fix | Delete
else:
[582] Fix | Delete
if block is not None:
[583] Fix | Delete
result.extend(self.refactor_doctest(block, block_lineno,
[584] Fix | Delete
indent, filename))
[585] Fix | Delete
block = None
[586] Fix | Delete
indent = None
[587] Fix | Delete
result.append(line)
[588] Fix | Delete
if block is not None:
[589] Fix | Delete
result.extend(self.refactor_doctest(block, block_lineno,
[590] Fix | Delete
indent, filename))
[591] Fix | Delete
return u"".join(result)
[592] Fix | Delete
[593] Fix | Delete
def refactor_doctest(self, block, lineno, indent, filename):
[594] Fix | Delete
"""Refactors one doctest.
[595] Fix | Delete
[596] Fix | Delete
A doctest is given as a block of lines, the first of which starts
[597] Fix | Delete
with ">>>" (possibly indented), while the remaining lines start
[598] Fix | Delete
with "..." (identically indented).
[599] Fix | Delete
[600] Fix | Delete
"""
[601] Fix | Delete
try:
[602] Fix | Delete
tree = self.parse_block(block, lineno, indent)
[603] Fix | Delete
except Exception as err:
[604] Fix | Delete
if self.logger.isEnabledFor(logging.DEBUG):
[605] Fix | Delete
for line in block:
[606] Fix | Delete
self.log_debug("Source: %s", line.rstrip(u"\n"))
[607] Fix | Delete
self.log_error("Can't parse docstring in %s line %s: %s: %s",
[608] Fix | Delete
filename, lineno, err.__class__.__name__, err)
[609] Fix | Delete
return block
[610] Fix | Delete
if self.refactor_tree(tree, filename):
[611] Fix | Delete
new = unicode(tree).splitlines(True)
[612] Fix | Delete
# Undo the adjustment of the line numbers in wrap_toks() below.
[613] Fix | Delete
clipped, new = new[:lineno-1], new[lineno-1:]
[614] Fix | Delete
assert clipped == [u"\n"] * (lineno-1), clipped
[615] Fix | Delete
if not new[-1].endswith(u"\n"):
[616] Fix | Delete
new[-1] += u"\n"
[617] Fix | Delete
block = [indent + self.PS1 + new.pop(0)]
[618] Fix | Delete
if new:
[619] Fix | Delete
block += [indent + self.PS2 + line for line in new]
[620] Fix | Delete
return block
[621] Fix | Delete
[622] Fix | Delete
def summarize(self):
[623] Fix | Delete
if self.wrote:
[624] Fix | Delete
were = "were"
[625] Fix | Delete
else:
[626] Fix | Delete
were = "need to be"
[627] Fix | Delete
if not self.files:
[628] Fix | Delete
self.log_message("No files %s modified.", were)
[629] Fix | Delete
else:
[630] Fix | Delete
self.log_message("Files that %s modified:", were)
[631] Fix | Delete
for file in self.files:
[632] Fix | Delete
self.log_message(file)
[633] Fix | Delete
if self.fixer_log:
[634] Fix | Delete
self.log_message("Warnings/messages while refactoring:")
[635] Fix | Delete
for message in self.fixer_log:
[636] Fix | Delete
self.log_message(message)
[637] Fix | Delete
if self.errors:
[638] Fix | Delete
if len(self.errors) == 1:
[639] Fix | Delete
self.log_message("There was 1 error:")
[640] Fix | Delete
else:
[641] Fix | Delete
self.log_message("There were %d errors:", len(self.errors))
[642] Fix | Delete
for msg, args, kwds in self.errors:
[643] Fix | Delete
self.log_message(msg, *args, **kwds)
[644] Fix | Delete
[645] Fix | Delete
def parse_block(self, block, lineno, indent):
[646] Fix | Delete
"""Parses a block into a tree.
[647] Fix | Delete
[648] Fix | Delete
This is necessary to get correct line number / offset information
[649] Fix | Delete
in the parser diagnostics and embedded into the parse tree.
[650] Fix | Delete
"""
[651] Fix | Delete
tree = self.driver.parse_tokens(self.wrap_toks(block, lineno, indent))
[652] Fix | Delete
tree.future_features = frozenset()
[653] Fix | Delete
return tree
[654] Fix | Delete
[655] Fix | Delete
def wrap_toks(self, block, lineno, indent):
[656] Fix | Delete
"""Wraps a tokenize stream to systematically modify start/end."""
[657] Fix | Delete
tokens = tokenize.generate_tokens(self.gen_lines(block, indent).next)
[658] Fix | Delete
for type, value, (line0, col0), (line1, col1), line_text in tokens:
[659] Fix | Delete
line0 += lineno - 1
[660] Fix | Delete
line1 += lineno - 1
[661] Fix | Delete
# Don't bother updating the columns; this is too complicated
[662] Fix | Delete
# since line_text would also have to be updated and it would
[663] Fix | Delete
# still break for tokens spanning lines. Let the user guess
[664] Fix | Delete
# that the column numbers for doctests are relative to the
[665] Fix | Delete
# end of the prompt string (PS1 or PS2).
[666] Fix | Delete
yield type, value, (line0, col0), (line1, col1), line_text
[667] Fix | Delete
[668] Fix | Delete
[669] Fix | Delete
def gen_lines(self, block, indent):
[670] Fix | Delete
"""Generates lines as expected by tokenize from a list of lines.
[671] Fix | Delete
[672] Fix | Delete
This strips the first len(indent + self.PS1) characters off each line.
[673] Fix | Delete
"""
[674] Fix | Delete
prefix1 = indent + self.PS1
[675] Fix | Delete
prefix2 = indent + self.PS2
[676] Fix | Delete
prefix = prefix1
[677] Fix | Delete
for line in block:
[678] Fix | Delete
if line.startswith(prefix):
[679] Fix | Delete
yield line[len(prefix):]
[680] Fix | Delete
elif line == prefix.rstrip() + u"\n":
[681] Fix | Delete
yield u"\n"
[682] Fix | Delete
else:
[683] Fix | Delete
raise AssertionError("line=%r, prefix=%r" % (line, prefix))
[684] Fix | Delete
prefix = prefix2
[685] Fix | Delete
while True:
[686] Fix | Delete
yield ""
[687] Fix | Delete
[688] Fix | Delete
[689] Fix | Delete
class MultiprocessingUnsupported(Exception):
[690] Fix | Delete
pass
[691] Fix | Delete
[692] Fix | Delete
[693] Fix | Delete
class MultiprocessRefactoringTool(RefactoringTool):
[694] Fix | Delete
[695] Fix | Delete
def __init__(self, *args, **kwargs):
[696] Fix | Delete
super(MultiprocessRefactoringTool, self).__init__(*args, **kwargs)
[697] Fix | Delete
self.queue = None
[698] Fix | Delete
self.output_lock = None
[699] Fix | Delete
[700] Fix | Delete
def refactor(self, items, write=False, doctests_only=False,
[701] Fix | Delete
num_processes=1):
[702] Fix | Delete
if num_processes == 1:
[703] Fix | Delete
return super(MultiprocessRefactoringTool, self).refactor(
[704] Fix | Delete
items, write, doctests_only)
[705] Fix | Delete
try:
[706] Fix | Delete
import multiprocessing
[707] Fix | Delete
except ImportError:
[708] Fix | Delete
raise MultiprocessingUnsupported
[709] Fix | Delete
if self.queue is not None:
[710] Fix | Delete
raise RuntimeError("already doing multiple processes")
[711] Fix | Delete
self.queue = multiprocessing.JoinableQueue()
[712] Fix | Delete
self.output_lock = multiprocessing.Lock()
[713] Fix | Delete
processes = [multiprocessing.Process(target=self._child)
[714] Fix | Delete
for i in xrange(num_processes)]
[715] Fix | Delete
try:
[716] Fix | Delete
for p in processes:
[717] Fix | Delete
p.start()
[718] Fix | Delete
super(MultiprocessRefactoringTool, self).refactor(items, write,
[719] Fix | Delete
doctests_only)
[720] Fix | Delete
finally:
[721] Fix | Delete
self.queue.join()
[722] Fix | Delete
for i in xrange(num_processes):
[723] Fix | Delete
self.queue.put(None)
[724] Fix | Delete
for p in processes:
[725] Fix | Delete
if p.is_alive():
[726] Fix | Delete
p.join()
[727] Fix | Delete
self.queue = None
[728] Fix | Delete
[729] Fix | Delete
def _child(self):
[730] Fix | Delete
task = self.queue.get()
[731] Fix | Delete
while task is not None:
[732] Fix | Delete
args, kwargs = task
[733] Fix | Delete
try:
[734] Fix | Delete
super(MultiprocessRefactoringTool, self).refactor_file(
[735] Fix | Delete
*args, **kwargs)
[736] Fix | Delete
finally:
[737] Fix | Delete
self.queue.task_done()
[738] Fix | Delete
task = self.queue.get()
[739] Fix | Delete
[740] Fix | Delete
def refactor_file(self, *args, **kwargs):
[741] Fix | Delete
if self.queue is not None:
[742] Fix | Delete
self.queue.put((args, kwargs))
[743] Fix | Delete
else:
[744] Fix | Delete
return super(MultiprocessRefactoringTool, self).refactor_file(
[745] Fix | Delete
*args, **kwargs)
[746] Fix | Delete
[747] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function