Skip to content

Commit 5dd4ef6

Browse files
committed
make plain parser pep8 compliant. See ietf-tools#585.
- Legacy-Id: 2827
1 parent 8635333 commit 5dd4ef6

1 file changed

Lines changed: 36 additions & 37 deletions

File tree

ietf/submit/parsers/plain_parser.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self, fd):
1515
super(PlainParser, self).__init__(fd)
1616
self.lines = fd.file.readlines()
1717
fd.file.seek(0)
18-
self.full_text= self.normalize_text(''.join(self.lines))
18+
self.full_text = self.normalize_text(''.join(self.lines))
1919

2020
def normalize_text(self, text):
2121
text = re.sub(".\x08", "", text) # Get rid of inkribbon backspace-emphasis
@@ -111,29 +111,29 @@ def parse_normal_001_title(self):
111111
self.parsed_info.metadraft.title = title
112112

113113
def parse_normal_002_num_pages(self):
114-
pagecount = len(re.findall("\[[Pp]age [0-9ixldv]+\]", self.full_text)) or len(self.lines)/58
114+
pagecount = len(re.findall("\[[Pp]age [0-9ixldv]+\]", self.full_text)) or len(self.lines) / 58
115115
self.parsed_info.metadraft.pagecount = pagecount
116116

117117
def parse_normal_003_creation_date(self):
118-
month_names = [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ]
118+
month_names = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
119119
date_regexes = [
120120
r'\s{3,}(?P<month>\w+)\s+(?P<day>\d{1,2}),?\s+(?P<year>\d{4})',
121121
r'\s{3,}(?P<day>\d{1,2}),?\s+(?P<month>\w+)\s+(?P<year>\d{4})',
122122
r'\s{3,}(?P<day>\d{1,2})-(?P<month>\w+)-(?P<year>\d{4})',
123123
# 'October 2008' - default day to today's.
124124
r'\s{3,}(?P<month>\w+)\s+(?P<year>\d{4})',
125-
]
125+
]
126126

127127
first = self.parsed_info.metadraft.first_two_pages or self.full_text
128128
for regex in date_regexes:
129129
match = re.search(regex, first)
130130
if match:
131131
md = match.groupdict()
132132
mon = md['month'][0:3].lower()
133-
day = int( md.get( 'day', datetime.date.today().day ) )
134-
year = int( md['year'] )
133+
day = int(md.get('day', datetime.date.today().day))
134+
year = int(md['year'])
135135
try:
136-
month = month_names.index( mon ) + 1
136+
month = month_names.index(mon) + 1
137137
self.parsed_info.metadraft.creation_date = datetime.date(year, month, day)
138138
return
139139
except ValueError:
@@ -142,12 +142,11 @@ def parse_normal_003_creation_date(self):
142142
continue
143143
self.parsed_info.add_warning('creation_date', 'Creation Date field is empty or the creation date is not in a proper format.')
144144

145-
146145
def parse_normal_004_authors(self):
147146
"""
148147
comes from http://svn.tools.ietf.org/svn/tools/ietfdb/branch/idsubmit/ietf/utils/draft.py
149148
"""
150-
149+
151150
def _stripheaders(rawlines):
152151
stripped = []
153152
pages = []
@@ -160,15 +159,16 @@ def _stripheaders(rawlines):
160159

161160
def endpage(pages, page, line):
162161
if line:
163-
page += [ line ]
162+
page += [line]
164163
return begpage(pages, page)
164+
165165
def begpage(pages, page, line=None):
166166
if page and len(page) > 5:
167-
pages += [ "\n".join(page) ]
167+
pages += ["\n".join(page)]
168168
page = []
169169
newpage = True
170170
if line:
171-
page += [ line ]
171+
page += [line]
172172
return pages, page
173173

174174
for line in rawlines:
@@ -213,13 +213,13 @@ def begpage(pages, page, line=None):
213213
sentence = True
214214
if re.search("^[ \t]*$", line):
215215
haveblank = True
216-
page += [ line ]
216+
page += [line]
217217
continue
218-
page += [ line ]
219-
stripped += [ line ]
218+
page += [line]
219+
stripped += [line]
220220
pages, page = begpage(pages, page)
221221
return stripped, pages
222-
222+
223223
self.fd.file.seek(0)
224224
raw_lines = self.fd.file.read().split("\n")
225225
draft_lines, draft_pages = _stripheaders(raw_lines)
@@ -233,15 +233,15 @@ def begpage(pages, page, line=None):
233233
"Jerry": "Gerald",
234234
"Liz": "Elizabeth",
235235
"Lynn": "Carolyn",
236-
"Ned": "Edward" ,
237-
"Ted":"Edward",
236+
"Ned": "Edward",
237+
"Ted": "Edward",
238238
}
239239
aux = {
240-
"honor" : r"(?:Dr\.?|Prof(?:\.?|essor)|Sir|Lady|Dame)",
240+
"honor": r"(?:Dr\.?|Prof(?:\.?|essor)|Sir|Lady|Dame)",
241241
"prefix": r"([Dd]e|Hadi|van|van de|van der|Ver|von)",
242242
"suffix": r"(jr|II|2nd|III|3rd|IV|4th)",
243-
"first" : r"([A-Z][-A-Za-z]*)((\.?[- ]{1,2}[A-Za-z]+)*)",
244-
"last" : r"([-A-Za-z']{2,})",
243+
"first": r"([A-Z][-A-Za-z]*)((\.?[- ]{1,2}[A-Za-z]+)*)",
244+
"last": r"([-A-Za-z']{2,})",
245245
}
246246
authformats = [
247247
r" {6}(%(first)s[ \.]{1,3}((%(prefix)s )?%(last)s)( %(suffix)s)?)([, ]?(.+\.?|\(.+\.?|\)))?$" % aux,
@@ -272,7 +272,7 @@ def begpage(pages, page, line=None):
272272
match = re.search(authformat, line)
273273
if match:
274274
author = match.group(1)
275-
authors += [ author ]
275+
authors += [author]
276276
if line.strip() == "":
277277
if prev_blankline:
278278
break
@@ -291,7 +291,7 @@ def begpage(pages, page, line=None):
291291
if author == None:
292292
continue
293293
if "," in author:
294-
last, first = author.split(",",1)
294+
last, first = author.split(",", 1)
295295
author = "%s %s" % (first.strip(), last.strip())
296296
if not " " in author:
297297
if "." in author:
@@ -303,7 +303,7 @@ def begpage(pages, page, line=None):
303303
else:
304304
first, last = author.rsplit(" ", 1)
305305

306-
for author in [ "%s %s"%(first,last), "%s %s"%(last,first), ]:
306+
for author in ["%s %s" % (first, last), "%s %s" % (last, first)]:
307307
# Pattern for full author information search, based on first page author name:
308308
authpat = author
309309
# Permit expansion of first name
@@ -315,32 +315,32 @@ def begpage(pages, page, line=None):
315315
authpat = re.sub("-", ".*?-", authpat)
316316
# Some chinese names are shown with double-letter(latin) abbreviated given names, rather than
317317
# a single-letter(latin) abbreviation:
318-
authpat = re.sub("^([A-Z])[A-Z]+\.\*", r"\1[-\w]+", authpat)
318+
authpat = re.sub("^([A-Z])[A-Z]+\.\*", r"\1[-\w]+", authpat)
319319
authpat = "^(?:%s ?)?(%s)( *\(.*\)|,( [A-Z][-A-Za-z0-9]*)?)?" % (aux["honor"], authpat)
320320
start = 0
321321
col = None
322322

323323
# Find start of author info for this author (if any).
324324
# Scan from the end of the file, looking for a match to authpath
325325
try:
326-
for j in range(len(draft_lines)-1, 15, -1):
326+
for j in range(len(draft_lines) - 1, 15, -1):
327327
line = draft_lines[j].strip()
328-
forms = [ line ] + [ line.replace(short, longform[short]) for short in longform if short in line ]
328+
forms = [line] + [line.replace(short, longform[short]) for short in longform if short in line]
329329
for line in forms:
330330
if re.search(authpat, line):
331331
start = j
332332
columns = re.split("( +)", line)
333333
# Find which column:
334-
cols = [ c for c in range(len(columns)) if re.search(authpat+r"$", columns[c].strip()) ]
334+
cols = [c for c in range(len(columns)) if re.search(authpat + r"$", columns[c].strip())]
335335
if cols:
336336
col = cols[0]
337337
if not (start, col) in found_pos:
338-
found_pos += [ (start, col) ]
338+
found_pos += [(start, col)]
339339
beg = len("".join(columns[:col]))
340-
if col == len(columns) or col == len(columns)-1:
340+
if col == len(columns) or col == len(columns) - 1:
341341
end = None
342342
else:
343-
end = beg + len("".join(columns[col:col+2]))
343+
end = beg + len("".join(columns[col:col + 2]))
344344
author = re.search(authpat, columns[col].strip()).group(1)
345345
if author in companies:
346346
authors[i] = None
@@ -360,7 +360,7 @@ def begpage(pages, page, line=None):
360360
count = 0
361361
keyword = False
362362
blanklines = 0
363-
for line in draft_lines[start+1:]:
363+
for line in draft_lines[start + 1:]:
364364
# Break on the second blank line
365365
if not line:
366366
blanklines += 1
@@ -370,14 +370,14 @@ def begpage(pages, page, line=None):
370370
continue
371371
else:
372372
count += 1
373-
authmatch = [ a for a in authors[i+1:] if a and not a in companies and re.search((r"(^|\W)"+re.sub("\.? ", ".* ", a)+"(\W|$)"), line.strip()) ]
373+
authmatch = [a for a in authors[i + 1:] if a and not a in companies and re.search((r"(^|\W)" + re.sub("\.? ", ".* ", a) + "(\W|$)"), line.strip())]
374374
if authmatch:
375375
if count == 1 or (count == 2 and not blanklines):
376376
# First line after an author -- this is a company
377377
companies += authmatch
378-
companies += [ line.strip() ] # XXX fix this for columnized author list
378+
companies += [line.strip()] # XXX fix this for columnized author list
379379
companies = list(set(companies))
380-
for k in range(i+1, len(authors)):
380+
for k in range(i + 1, len(authors)):
381381
if authors[k] in companies:
382382
authors[k] = None
383383
elif not "@" in line:
@@ -399,7 +399,7 @@ def begpage(pages, page, line=None):
399399
else:
400400
authors[i] = None
401401

402-
authors = [ re.sub(r" +"," ", a) for a in authors if a != None ]
402+
authors = [re.sub(r" +", " ", a) for a in authors if a != None]
403403
if authors:
404404
authors.sort()
405405
self.parsed_info.metadraft.authors = authors
@@ -408,6 +408,5 @@ def begpage(pages, page, line=None):
408408

409409
return authors
410410

411-
412411
def parse_normal_005_abstract(self):
413412
pass

0 commit comments

Comments
 (0)