Skip to content

Commit 2af571d

Browse files
committed
Helpful lints by Sourcery + minor manuals lints
1 parent 48ebd20 commit 2af571d

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

gitsearchreplace/__init__.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414
DEFAULT_SEPARATOR = '///'
1515

1616
def run_subprocess(cmd):
17-
pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
18-
output = pipe.communicate()[0]
19-
return output
17+
return subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
2018

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

2523
class Expression(object):
@@ -91,8 +89,7 @@ def m(i):
9189

9290
def compile_expressions(self):
9391
if not self.expressions_str:
94-
error("no FROM-TO expressions specified")
95-
return
92+
return error("no FROM-TO expressions specified")
9693

9794
expressions = []
9895
if self.separator is None:
@@ -149,8 +146,8 @@ def search_replace_in_files(self):
149146
new_filename = self.sub(expr, new_filename, 'filename')
150147
if new_filename != filename:
151148
print()
152-
print("rename-src-file: %s" % (filename, ))
153-
print("rename-dst-file: %s" % (new_filename, ))
149+
print(f"rename-src-file: {filename}")
150+
print(f"rename-dst-file: {new_filename}")
154151
if self.fix:
155152
dirname = os.path.dirname(new_filename)
156153
if dirname and not os.path.exists(dirname):
@@ -167,9 +164,8 @@ def show_file(self, filename, filedata):
167164

168165
def show_lines_grep_like(self, filename, filedata):
169166
new_filedata = filedata
170-
expr_id = 0
171167
shown_lines = []
172-
for expr in self.expressions:
168+
for expr_id, expr in enumerate(self.expressions):
173169
lines = []
174170
line_pos = []
175171
pos = 0
@@ -191,20 +187,17 @@ def show_lines_grep_like(self, filename, filedata):
191187

192188
def act_on_possible_modification(self, filename, new_filedata):
193189
if self.diff:
194-
print("diff -urN a/%s b/%s" % (filename, filename))
190+
print(f"diff -urN a/{filename} b/{filename}")
195191
self.show_diff(filename, new_filedata)
196192
if self.fix:
197-
fileobj = open(filename, "w")
198-
fileobj.write(new_filedata)
199-
fileobj.close()
193+
with open(filename, "w") as fileobj:
194+
fileobj.write(new_filedata)
200195

201196
def show_diff(self, filename, new_filedata):
202-
fileobj = None
203197
tempf = tempfile.mktemp()
204198
try:
205-
fileobj = open(tempf, "w")
206-
fileobj.write(new_filedata)
207-
fileobj.close()
199+
with open(tempf, "w") as fileobj:
200+
fileobj.write(new_filedata)
208201
diff = str(run_subprocess(["diff", "-urN", filename, tempf]), 'utf8')
209202
minus_matched = False
210203
plus_matched = False
@@ -213,15 +206,13 @@ def show_diff(self, filename, new_filedata):
213206
minus_matched = True
214207
match = re.match("^--- ([^ ]+) (.*)$", line)
215208
if match:
216-
print("--- a/%s %s" % (filename,
217-
match.groups(0)[1], ))
209+
print(f"--- a/{filename} {match.groups(0)[1]}")
218210
continue
219211
if not plus_matched:
220212
plus_matched = True
221213
match = re.match("^[+][+][+] ([^ ]+) (.*)$", line)
222214
if match:
223-
print("+++ b/%s %s" % (filename,
224-
match.groups(0)[1], ))
215+
print(f"+++ b/{filename} {match.groups(0)[1]}")
225216
continue
226217
print(line)
227218
finally:
@@ -284,9 +275,8 @@ def main():
284275

285276
(options, args) = parser.parse_args()
286277
filters = getattr(options, 'filters', [])
287-
if len(filters) >= 1:
288-
if filters[0][0] == 'include':
289-
filters = [('exclude', '**')] + filters
278+
if filters and filters[0][0] == 'include':
279+
filters = [('exclude', '**')] + filters
290280

291281
if options.pair_args:
292282
assert options.separator is None

0 commit comments

Comments
 (0)