1
0
forked from VimPlug/jedi

Start using pathlib.Path instead of all the os.path functions

This commit is contained in:
Dave Halter
2020-07-10 17:30:36 +02:00
parent 92af043906
commit db0e90763b
12 changed files with 97 additions and 104 deletions

View File

@@ -1,6 +1,3 @@
from os.path import dirname, basename, join, relpath
import os
import re
import difflib
from parso import split_lines
@@ -43,11 +40,11 @@ class ChangedFile(object):
if self._from_path is None:
from_p = ''
else:
from_p = relpath(self._from_path, project_path)
from_p = self._from_path.relative_to(project_path)
if self._to_path is None:
to_p = ''
else:
to_p = relpath(self._to_path, project_path)
to_p = self._to_path.relative_to(project_path)
diff = difflib.unified_diff(
old_lines, new_lines,
fromfile=from_p,
@@ -115,7 +112,7 @@ class Refactoring(object):
project_path = self._inference_state.project.path
for from_, to in self.get_renames():
text += 'rename from %s\nrename to %s\n' \
% (relpath(from_, project_path), relpath(to, project_path))
% (from_.relative_to(project_path), to.relative_to(project_path))
return text + ''.join(f.get_diff() for f in self.get_changed_files().values())
@@ -127,17 +124,14 @@ class Refactoring(object):
f.apply()
for old, new in self.get_renames():
os.rename(old, new)
old.rename(new)
def _calculate_rename(path, new_name):
name = basename(path)
dir_ = dirname(path)
if name in ('__init__.py', '__init__.pyi'):
parent_dir = dirname(dir_)
return dir_, join(parent_dir, new_name)
ending = re.search(r'\.pyi?$', name).group(0)
return path, join(dir_, new_name + ending)
dir_ = path.parent
if path.name in ('__init__.py', '__init__.pyi'):
return dir_, dir_.parent.joinpath(new_name)
return path, dir_.joinpath(new_name + path.suffix)
def rename(inference_state, definitions, new_name):