Skip to content

Commit 62ec16e

Browse files
committed
run black
1 parent 8d3e131 commit 62ec16e

File tree

2 files changed

+81
-94
lines changed

2 files changed

+81
-94
lines changed

tests/test_apply.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def setUp(self):
2626
self.lao = f.read().splitlines()
2727

2828
with open("tests/casefiles/tzu") as f:
29-
self.tzu = f.read().splitlines()
29+
self.tzu = f.read().splitlines()
3030

3131
def test_truth(self):
3232
self.assertEqual(type(self.lao), list)

whatthepatch/patch.py

Lines changed: 80 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,61 @@
66
from .snippets import split_by_regex, findall_regex
77
from . import exceptions
88

9-
header = namedtuple(
10-
'header',
11-
'index_path old_path old_version new_path new_version',
12-
)
9+
header = namedtuple("header", "index_path old_path old_version new_path new_version",)
1310

14-
diffobj = namedtuple('diff', 'header changes text')
15-
Change = namedtuple('Change', 'old new line hunk')
11+
diffobj = namedtuple("diff", "header changes text")
12+
Change = namedtuple("Change", "old new line hunk")
1613

17-
file_timestamp_str = '(.+?)(?:\t|:| +)(.*)'
14+
file_timestamp_str = "(.+?)(?:\t|:| +)(.*)"
1815
# .+? was previously [^:\t\n\r\f\v]+
1916

2017
# general diff regex
21-
diffcmd_header = re.compile('^diff.* (.+) (.+)$')
22-
unified_header_index = re.compile('^Index: (.+)$')
23-
unified_header_old_line = re.compile(r'^--- ' + file_timestamp_str + '$')
24-
unified_header_new_line = re.compile(r'^\+\+\+ ' + file_timestamp_str + '$')
25-
unified_hunk_start = re.compile(r'^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@(.*)$')
26-
unified_change = re.compile('^([-+ ])(.*)$')
27-
28-
context_header_old_line = re.compile(r'^\*\*\* ' + file_timestamp_str + '$')
29-
context_header_new_line = re.compile('^--- ' + file_timestamp_str + '$')
30-
context_hunk_start = re.compile(r'^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$')
31-
context_hunk_old = re.compile(r'^\*\*\* (\d+),?(\d*) \*\*\*\*$')
32-
context_hunk_new = re.compile(r'^--- (\d+),?(\d*) ----$')
33-
context_change = re.compile('^([-+ !]) (.*)$')
34-
35-
ed_hunk_start = re.compile(r'^(\d+),?(\d*)([acd])$')
36-
ed_hunk_end = re.compile('^.$')
18+
diffcmd_header = re.compile("^diff.* (.+) (.+)$")
19+
unified_header_index = re.compile("^Index: (.+)$")
20+
unified_header_old_line = re.compile(r"^--- " + file_timestamp_str + "$")
21+
unified_header_new_line = re.compile(r"^\+\+\+ " + file_timestamp_str + "$")
22+
unified_hunk_start = re.compile(r"^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@(.*)$")
23+
unified_change = re.compile("^([-+ ])(.*)$")
24+
25+
context_header_old_line = re.compile(r"^\*\*\* " + file_timestamp_str + "$")
26+
context_header_new_line = re.compile("^--- " + file_timestamp_str + "$")
27+
context_hunk_start = re.compile(r"^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$")
28+
context_hunk_old = re.compile(r"^\*\*\* (\d+),?(\d*) \*\*\*\*$")
29+
context_hunk_new = re.compile(r"^--- (\d+),?(\d*) ----$")
30+
context_change = re.compile("^([-+ !]) (.*)$")
31+
32+
ed_hunk_start = re.compile(r"^(\d+),?(\d*)([acd])$")
33+
ed_hunk_end = re.compile("^.$")
3734
# much like forward ed, but no 'c' type
38-
rcs_ed_hunk_start = re.compile(r'^([ad])(\d+) ?(\d*)$')
35+
rcs_ed_hunk_start = re.compile(r"^([ad])(\d+) ?(\d*)$")
3936

40-
default_hunk_start = re.compile(r'^(\d+),?(\d*)([acd])(\d+),?(\d*)$')
41-
default_hunk_mid = re.compile('^---$')
42-
default_change = re.compile('^([><]) (.*)$')
37+
default_hunk_start = re.compile(r"^(\d+),?(\d*)([acd])(\d+),?(\d*)$")
38+
default_hunk_mid = re.compile("^---$")
39+
default_change = re.compile("^([><]) (.*)$")
4340

4441
# Headers
4542

4643
# git has a special index header and no end part
47-
git_diffcmd_header = re.compile('^diff --git a/(.+) b/(.+)$')
48-
git_header_index = re.compile(r'^index ([a-f0-9]+)..([a-f0-9]+) ?(\d*)$')
49-
git_header_old_line = re.compile('^--- (.+)$')
50-
git_header_new_line = re.compile(r'^\+\+\+ (.+)$')
51-
git_header_file_mode = re.compile(r'^(new|deleted) file mode \d{6}$')
52-
git_header_binary_file = re.compile('^Binary files (.+) and (.+) differ')
44+
git_diffcmd_header = re.compile("^diff --git a/(.+) b/(.+)$")
45+
git_header_index = re.compile(r"^index ([a-f0-9]+)..([a-f0-9]+) ?(\d*)$")
46+
git_header_old_line = re.compile("^--- (.+)$")
47+
git_header_new_line = re.compile(r"^\+\+\+ (.+)$")
48+
git_header_file_mode = re.compile(r"^(new|deleted) file mode \d{6}$")
49+
git_header_binary_file = re.compile("^Binary files (.+) and (.+) differ")
5350

5451
bzr_header_index = re.compile("=== (.+)")
5552
bzr_header_old_line = unified_header_old_line
5653
bzr_header_new_line = unified_header_new_line
5754

5855
svn_header_index = unified_header_index
59-
svn_header_timestamp_version = re.compile(
60-
r'\((?:working copy|revision (\d+))\)'
61-
)
62-
svn_header_timestamp = re.compile(r'.*(\(.*\))$')
56+
svn_header_timestamp_version = re.compile(r"\((?:working copy|revision (\d+))\)")
57+
svn_header_timestamp = re.compile(r".*(\(.*\))$")
6358

6459
cvs_header_index = unified_header_index
65-
cvs_header_rcs = re.compile(r'^RCS file: (.+)(?:,\w{1}$|$)')
66-
cvs_header_timestamp = re.compile(r'(.+)\t([\d.]+)')
67-
cvs_header_timestamp_colon = re.compile(r':([\d.]+)\t(.+)')
68-
old_cvs_diffcmd_header = re.compile('^diff.* (.+):(.*) (.+):(.*)$')
60+
cvs_header_rcs = re.compile(r"^RCS file: (.+)(?:,\w{1}$|$)")
61+
cvs_header_timestamp = re.compile(r"(.+)\t([\d.]+)")
62+
cvs_header_timestamp_colon = re.compile(r":([\d.]+)\t(.+)")
63+
old_cvs_diffcmd_header = re.compile("^diff.* (.+):(.*) (.+):(.*)$")
6964

7065

7166
def parse_patch(text):
@@ -93,7 +88,7 @@ def parse_patch(text):
9388
break
9489

9590
for diff in diffs:
96-
difftext = '\n'.join(diff) + '\n'
91+
difftext = "\n".join(diff) + "\n"
9792
h = parse_header(diff)
9893
d = parse_diff(diff)
9994
if h or d:
@@ -114,11 +109,11 @@ def parse_scm_header(text):
114109
lines = text
115110

116111
check = [
117-
(git_header_index, parse_git_header),
118-
(old_cvs_diffcmd_header, parse_cvs_header),
119-
(cvs_header_rcs, parse_cvs_header),
120-
(svn_header_index, parse_svn_header),
121-
]
112+
(git_header_index, parse_git_header),
113+
(old_cvs_diffcmd_header, parse_cvs_header),
114+
(cvs_header_rcs, parse_cvs_header),
115+
(svn_header_index, parse_svn_header),
116+
]
122117

123118
for regex, parser in check:
124119
diffs = findall_regex(lines, regex)
@@ -129,18 +124,18 @@ def parse_scm_header(text):
129124
if res:
130125
old_path = res.old_path
131126
new_path = res.new_path
132-
if old_path.startswith('a/'):
127+
if old_path.startswith("a/"):
133128
old_path = old_path[2:]
134129

135-
if new_path.startswith('b/'):
130+
if new_path.startswith("b/"):
136131
new_path = new_path[2:]
137132

138133
return header(
139134
index_path=res.index_path,
140135
old_path=old_path,
141136
old_version=res.old_version,
142137
new_path=new_path,
143-
new_version=res.new_version
138+
new_version=res.new_version,
144139
)
145140
else:
146141
res = parser(lines)
@@ -234,37 +229,37 @@ def parse_git_header(text):
234229
new_path = binary.group(2)
235230

236231
if old_path and new_path:
237-
if old_path.startswith('a/'):
232+
if old_path.startswith("a/"):
238233
old_path = old_path[2:]
239234

240-
if new_path.startswith('b/'):
235+
if new_path.startswith("b/"):
241236
new_path = new_path[2:]
242237
return header(
243238
index_path=None,
244239
old_path=old_path,
245240
old_version=old_version,
246241
new_path=new_path,
247-
new_version=new_version
242+
new_version=new_version,
248243
)
249244

250245
# if we go through all of the text without finding our normal info,
251246
# use the cmd if available
252247
if cmd_old_path and cmd_new_path and old_version and new_version:
253248
print("returning from dumb path")
254-
if cmd_old_path.startswith('a/'):
249+
if cmd_old_path.startswith("a/"):
255250
cmd_old_path = cmd_old_path[2:]
256251

257-
if cmd_new_path.startswith('b/'):
252+
if cmd_new_path.startswith("b/"):
258253
cmd_new_path = cmd_new_path[2:]
259254

260255
return header(
261256
index_path=None,
262257
# wow, I kind of hate this:
263258
# assume /dev/null if the versions are zeroed out
264-
old_path='/dev/null' if old_version == '0000000' else cmd_old_path,
259+
old_path="/dev/null" if old_version == "0000000" else cmd_old_path,
265260
old_version=old_version,
266-
new_path='/dev/null' if new_version == '0000000' else cmd_new_path,
267-
new_version=new_version
261+
new_path="/dev/null" if new_version == "0000000" else cmd_new_path,
262+
new_version=new_version,
268263
)
269264

270265
return None
@@ -305,7 +300,7 @@ def parse_svn_header(text):
305300
elif opath:
306301
ts = svn_header_timestamp.match(opath)
307302
if ts:
308-
opath = opath[:-len(ts.group(1))]
303+
opath = opath[: -len(ts.group(1))]
309304
oend = svn_header_timestamp_version.match(ts.group(1))
310305
if oend and oend.group(1):
311306
over = int(oend.group(1))
@@ -319,7 +314,7 @@ def parse_svn_header(text):
319314
elif npath:
320315
ts = svn_header_timestamp.match(npath)
321316
if ts:
322-
npath = npath[:-len(ts.group(1))]
317+
npath = npath[: -len(ts.group(1))]
323318
nend = svn_header_timestamp_version.match(ts.group(1))
324319
if nend and nend.group(1):
325320
nver = int(nend.group(1))
@@ -564,10 +559,10 @@ def parse_default_diff(text):
564559
kind = c.group(1)
565560
line = c.group(2)
566561

567-
if kind == '<' and (r != old_len or r == 0):
562+
if kind == "<" and (r != old_len or r == 0):
568563
changes.append(Change(old + r, None, line, hunk_n))
569564
r += 1
570-
elif kind == '>' and (i != new_len or i == 0):
565+
elif kind == ">" and (i != new_len or i == 0):
571566
changes.append(Change(None, new + i, line, hunk_n))
572567
i += 1
573568

@@ -621,13 +616,13 @@ def parse_unified_diff(text):
621616
line = c.group(2)
622617
c = None
623618

624-
if kind == '-' and (r != old_len or r == 0):
619+
if kind == "-" and (r != old_len or r == 0):
625620
changes.append(Change(old + r, None, line, hunk_n))
626621
r += 1
627-
elif kind == '+' and (i != new_len or i == 0):
622+
elif kind == "+" and (i != new_len or i == 0):
628623
changes.append(Change(None, new + i, line, hunk_n))
629624
i += 1
630-
elif kind == ' ' and r != old_len and i != new_len:
625+
elif kind == " " and r != old_len and i != new_len:
631626
changes.append(Change(old + r, new + i, line, hunk_n))
632627
r += 1
633628
i += 1
@@ -702,15 +697,16 @@ def parse_context_diff(text):
702697
kind = c.group(1)
703698
line = c.group(2)
704699

705-
if kind == '-' and (j != old_len or j == 0):
700+
if kind == "-" and (j != old_len or j == 0):
706701
changes.append(Change(old + j, None, line, hunk_n))
707702
j += 1
708-
elif kind == ' ' and ((j != old_len and k != new_len)
709-
or (j == 0 or k == 0)):
703+
elif kind == " " and (
704+
(j != old_len and k != new_len) or (j == 0 or k == 0)
705+
):
710706
changes.append(Change(old + j, new + k, line, hunk_n))
711707
j += 1
712708
k += 1
713-
elif kind == '+' or kind == '!':
709+
elif kind == "+" or kind == "!":
714710
raise exceptions.ParseException(msg + kind, hunk_n)
715711

716712
continue
@@ -728,15 +724,16 @@ def parse_context_diff(text):
728724
kind = c.group(1)
729725
line = c.group(2)
730726

731-
if kind == '+' and (k != new_len or k == 0):
727+
if kind == "+" and (k != new_len or k == 0):
732728
changes.append(Change(None, new + k, line, hunk_n))
733729
k += 1
734-
elif kind == ' ' and ((j != old_len and k != new_len)
735-
or (j == 0 or k == 0)):
730+
elif kind == " " and (
731+
(j != old_len and k != new_len) or (j == 0 or k == 0)
732+
):
736733
changes.append(Change(old + j, new + k, line, hunk_n))
737734
j += 1
738735
k += 1
739-
elif kind == '-' or kind == '!':
736+
elif kind == "-" or kind == "!":
740737
raise exceptions.ParseException(msg + kind, hunk_n)
741738
continue
742739

@@ -758,17 +755,17 @@ def parse_context_diff(text):
758755
if not (oc or nc):
759756
del old_hunk[0]
760757
del new_hunk[0]
761-
elif okind == ' ' and nkind == ' ' and oline == nline:
758+
elif okind == " " and nkind == " " and oline == nline:
762759
changes.append(Change(old + j, new + k, oline, hunk_n))
763760
j += 1
764761
k += 1
765762
del old_hunk[0]
766763
del new_hunk[0]
767-
elif okind == '-' or okind == '!' and (j != old_len or j == 0):
764+
elif okind == "-" or okind == "!" and (j != old_len or j == 0):
768765
changes.append(Change(old + j, None, oline, hunk_n))
769766
j += 1
770767
del old_hunk[0]
771-
elif nkind == '+' or nkind == '!' and (k != new_len or k == 0):
768+
elif nkind == "+" or nkind == "!" and (k != new_len or k == 0):
772769
changes.append(Change(None, new + k, nline, hunk_n))
773770
k += 1
774771
del new_hunk[0]
@@ -814,7 +811,7 @@ def parse_ed_diff(text):
814811
old_end = int(o.group(2)) if len(o.group(2)) else old
815812

816813
hunk_kind = o.group(3)
817-
if hunk_kind == 'd':
814+
if hunk_kind == "d":
818815
k = 0
819816
while old_end >= old:
820817
changes.append(Change(old + k, None, None, hunk_n))
@@ -825,7 +822,7 @@ def parse_ed_diff(text):
825822

826823
while len(hunk) > 0:
827824
e = ed_hunk_end.match(hunk[0])
828-
if not e and hunk_kind == 'c':
825+
if not e and hunk_kind == "c":
829826
k = 0
830827
while old_end >= old:
831828
changes.append(Change(old + k, None, None, hunk_n))
@@ -835,21 +832,11 @@ def parse_ed_diff(text):
835832

836833
# I basically have no idea why this works
837834
# for these tests.
838-
changes.append(Change(
839-
None,
840-
old - r + i + k + j,
841-
hunk[0],
842-
hunk_n,
843-
))
835+
changes.append(Change(None, old - r + i + k + j, hunk[0], hunk_n,))
844836
i += 1
845837
j += 1
846-
if not e and hunk_kind == 'a':
847-
changes.append(Change(
848-
None,
849-
old - r + i + 1,
850-
hunk[0],
851-
hunk_n,
852-
))
838+
if not e and hunk_kind == "a":
839+
changes.append(Change(None, old - r + i + 1, hunk[0], hunk_n,))
853840
i += 1
854841

855842
del hunk[0]
@@ -889,7 +876,7 @@ def parse_rcs_ed_diff(text):
889876
old = int(o.group(2))
890877
size = int(o.group(3))
891878

892-
if hunk_kind == 'a':
879+
if hunk_kind == "a":
893880
old += total_change_size + 1
894881
total_change_size += size
895882
while size > 0 and len(hunk) > 0:
@@ -899,7 +886,7 @@ def parse_rcs_ed_diff(text):
899886

900887
del hunk[0]
901888

902-
elif hunk_kind == 'd':
889+
elif hunk_kind == "d":
903890
total_change_size -= size
904891
while size > 0:
905892
changes.append(Change(old + j, None, None, hunk_n))

0 commit comments

Comments
 (0)