Skip to content

Helpful lints by Sourcery #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions gitsearchreplace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
DEFAULT_SEPARATOR = '///'

def run_subprocess(cmd):
pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output = pipe.communicate()[0]
return output
return subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]

def error(s):
print("git-search-replace: error: " + s)
print(f"git-search-replace: error: {s}")
sys.exit(-1)

class Expression(object):
Expand Down Expand Up @@ -91,8 +89,7 @@ def m(i):

def compile_expressions(self):
if not self.expressions_str:
error("no FROM-TO expressions specified")
return
return error("no FROM-TO expressions specified")

expressions = []
if self.separator is None:
Expand Down Expand Up @@ -149,8 +146,8 @@ def search_replace_in_files(self):
new_filename = self.sub(expr, new_filename, 'filename')
if new_filename != filename:
print()
print("rename-src-file: %s" % (filename, ))
print("rename-dst-file: %s" % (new_filename, ))
print(f"rename-src-file: {filename}")
print(f"rename-dst-file: {new_filename}")
if self.fix:
dirname = os.path.dirname(new_filename)
if dirname and not os.path.exists(dirname):
Expand All @@ -167,9 +164,8 @@ def show_file(self, filename, filedata):

def show_lines_grep_like(self, filename, filedata):
new_filedata = filedata
expr_id = 0
shown_lines = []
for expr in self.expressions:
for expr_id, expr in enumerate(self.expressions):
lines = []
line_pos = []
pos = 0
Expand All @@ -191,20 +187,17 @@ def show_lines_grep_like(self, filename, filedata):

def act_on_possible_modification(self, filename, new_filedata):
if self.diff:
print("diff -urN a/%s b/%s" % (filename, filename))
print(f"diff -urN a/{filename} b/{filename}")
self.show_diff(filename, new_filedata)
if self.fix:
fileobj = open(filename, "w")
fileobj.write(new_filedata)
fileobj.close()
with open(filename, "w") as fileobj:
fileobj.write(new_filedata)

def show_diff(self, filename, new_filedata):
fileobj = None
tempf = tempfile.mktemp()
try:
fileobj = open(tempf, "w")
fileobj.write(new_filedata)
fileobj.close()
with open(tempf, "w") as fileobj:
fileobj.write(new_filedata)
diff = str(run_subprocess(["diff", "-urN", filename, tempf]), 'utf8')
minus_matched = False
plus_matched = False
Expand All @@ -213,15 +206,13 @@ def show_diff(self, filename, new_filedata):
minus_matched = True
match = re.match("^--- ([^ ]+) (.*)$", line)
if match:
print("--- a/%s %s" % (filename,
match.groups(0)[1], ))
print(f"--- a/{filename} {match.groups(0)[1]}")
continue
if not plus_matched:
plus_matched = True
match = re.match("^[+][+][+] ([^ ]+) (.*)$", line)
if match:
print("+++ b/%s %s" % (filename,
match.groups(0)[1], ))
print(f"+++ b/{filename} {match.groups(0)[1]}")
continue
print(line)
finally:
Expand Down Expand Up @@ -284,9 +275,8 @@ def main():

(options, args) = parser.parse_args()
filters = getattr(options, 'filters', [])
if len(filters) >= 1:
if filters[0][0] == 'include':
filters = [('exclude', '**')] + filters
if filters and filters[0][0] == 'include':
filters = [('exclude', '**')] + filters

if options.pair_args:
assert options.separator is None
Expand Down