Skip to content

Commit b926178

Browse files
authored
fix: quicker calculation of status from draft text (ietf-tools#8111)
* fix: quicker calculation of status from draft text * chore: remove unused import * fix: only read a small prefix of draft text when needed
1 parent 8a4d020 commit b926178

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

ietf/doc/models.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,22 +530,29 @@ def replaces(self):
530530
def replaced_by(self):
531531
return set([ r.document for r in self.related_that("replaces") ])
532532

533-
def text(self):
533+
def text(self, size = -1):
534534
path = self.get_file_name()
535535
root, ext = os.path.splitext(path)
536536
txtpath = root+'.txt'
537537
if ext != '.txt' and os.path.exists(txtpath):
538538
path = txtpath
539539
try:
540540
with io.open(path, 'rb') as file:
541-
raw = file.read()
541+
raw = file.read(size)
542542
except IOError:
543543
return None
544+
text = None
544545
try:
545546
text = raw.decode('utf-8')
546547
except UnicodeDecodeError:
547-
text = raw.decode('latin-1')
548-
#
548+
for back in range(1,4):
549+
try:
550+
text = raw[:-back].decode('utf-8')
551+
break
552+
except UnicodeDecodeError:
553+
pass
554+
if text is None:
555+
text = raw.decode('latin-1')
549556
return text
550557

551558
def text_or_error(self):

ietf/doc/views_doc.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
from ietf.review.utils import can_request_review_of_doc, review_assignments_to_list_for_docs, review_requests_to_list_for_docs
8585
from ietf.review.utils import no_review_from_teams_on_doc
8686
from ietf.utils import markup_txt, log, markdown
87-
from ietf.utils.draft import PlaintextDraft
87+
from ietf.utils.draft import get_status_from_draft_text
8888
from ietf.utils.meetecho import MeetechoAPIError, SlidesManager
8989
from ietf.utils.response import permission_denied
9090
from ietf.utils.text import maybe_split
@@ -2261,12 +2261,11 @@ def idnits2_state(request, name, rev=None):
22612261
elif doc.intended_std_level:
22622262
doc.deststatus = doc.intended_std_level.name
22632263
else:
2264-
text = doc.text()
2264+
# 10000 is a conservative prefix on number of utf-8 encoded bytes to
2265+
# cover at least the first 10 lines of characters
2266+
text = doc.text(size=10000)
22652267
if text:
2266-
parsed_draft = PlaintextDraft(
2267-
text=doc.text(), source=name, name_from_source=False
2268-
)
2269-
doc.deststatus = parsed_draft.get_status()
2268+
doc.deststatus = get_status_from_draft_text(text)
22702269
else:
22712270
doc.deststatus = "Unknown"
22722271
return render(

ietf/utils/draft.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ def acronym_match(s, l):
131131
#_debug(" s:%s; l:%s => %s; %s" % (s, l, acronym, s==acronym))
132132
return s == acronym
133133

134+
def get_status_from_draft_text(text):
135+
136+
# Take prefix to shortcut work over very large drafts
137+
# 5000 is conservatively much more than a full page of characters and we
138+
# only want the first 10 lines.
139+
text = text.strip()[:5000] # Take prefix to shortcut work over very large drafts
140+
text = re.sub(".\x08", "", text) # Get rid of inkribbon backspace-emphasis
141+
text = text.replace("\r\n", "\n") # Convert DOS to unix
142+
text = text.replace("\r", "\n") # Convert MAC to unix
143+
lines = text.split("\n")[:10]
144+
status = None
145+
for line in lines:
146+
status_match = re.search(r"^\s*Intended [Ss]tatus:\s*(.*?) ", line)
147+
if status_match:
148+
status = status_match.group(1)
149+
break
150+
return status
151+
134152
class Draft:
135153
"""Base class for drafts
136154

0 commit comments

Comments
 (0)