Skip to content

Commit b42f1cb

Browse files
committed
Replaced the use of unaccent.asciify(), which has similar functionality to unidecode.unidecode(). Changed the draft parser to work exclusively with unicode text, which both makes the removal of unaccent easier, and takes us closer to Py35 compatibility. Adjusted callers of the draft parser to send in unicode.
- Legacy-Id: 13673
1 parent ad57b10 commit b42f1cb

10 files changed

Lines changed: 32 additions & 169 deletions

File tree

ietf/doc/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,10 @@ def rebuild_reference_relations(doc,filename=None):
498498
filename=os.path.join(settings.INTERNET_DRAFT_PATH,doc.filename_with_rev())
499499

500500
try:
501-
refs = draft.Draft(draft._gettext(filename), filename).get_refs()
501+
with open(filename, 'rb') as file:
502+
refs = draft.Draft(file.read().decode('utf8'), filename).get_refs()
502503
except IOError as e:
503-
return { 'errors': ["%s :%s" % (e.strerror, filename)] }
504+
return { 'errors': ["%s :%s" % (e.strerror, filename)] }
504505

505506
doc.relateddocument_set.filter(relationship__slug__in=['refnorm','refinfo','refold','refunk']).delete()
506507

ietf/nomcom/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from ietf.person.models import Email, Person
2121
from ietf.mailtrigger.utils import gather_address_lists
2222
from ietf.utils.pipe import pipe
23-
from ietf.utils import unaccent
23+
from unidecode import unidecode
2424
from ietf.utils.mail import send_mail_text, send_mail
2525
from ietf.utils.log import log
2626

@@ -365,7 +365,7 @@ def make_nomineeposition_for_newperson(nomcom, candidate_name, candidate_email,
365365
# This is expected to fail if called with an existing email address
366366
email = Email.objects.create(address=candidate_email)
367367
person = Person.objects.create(name=candidate_name,
368-
ascii=unaccent.asciify(candidate_name),
368+
ascii=unidecode(candidate_name),
369369
address=candidate_email)
370370
email.person = person
371371
email.save()

ietf/secr/drafts/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def process_files(request,draft):
108108
file_type_list.append(extension)
109109
if extension == '.txt':
110110
txt_size = file.size
111-
wrapper = Draft(file.read(),file.name)
111+
wrapper = Draft(file.read().decode('utf8'),file.name)
112112
handle_uploaded_file(file)
113113

114114
# create Submission record, leaved unsaved

ietf/stats/backfill_data.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import os.path
88
import argparse
9+
import six
910
import time
1011

1112
basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
@@ -59,13 +60,14 @@ def unicode(text):
5960
for encoding in ['ascii', 'utf8', 'latin1', ]:
6061
try:
6162
utext = text.decode(encoding)
62-
if encoding == 'latin1':
63-
say("Warning: falling back to latin1 decoding for %s" % utext)
63+
# if encoding == 'latin1':
64+
# say("Warning: falling back to latin1 decoding for %s ..." % utext[:216]])
6465
return utext
6566
except UnicodeDecodeError:
6667
pass
6768

6869
start = time.time()
70+
say("Running query for documents to process ...")
6971
for doc in docs_qs.prefetch_related("docalias_set", "formal_languages", "documentauthor_set", "documentauthor_set__person", "documentauthor_set__person__alias_set"):
7072
canonical_name = doc.name
7173
for n in doc.docalias_set.all():
@@ -81,10 +83,10 @@ def unicode(text):
8183
say("Skipping %s, no txt file found at %s" % (doc.name, path))
8284
continue
8385

84-
with open(path, 'r') as f:
86+
with open(path, 'rb') as f:
8587
say("\nProcessing %s" % doc.name)
8688
sys.stdout.flush()
87-
d = Draft(f.read(), path)
89+
d = Draft(unicode(f.read()), path)
8890

8991
updated = False
9092

@@ -126,7 +128,11 @@ def unicode(text):
126128
# it's an extra author - skip those extra authors
127129
seen = set()
128130
for full, _, _, _, _, email, country, company in d.get_author_list():
129-
full, email, country, company = [ unicode(s) for s in [full, email, country, company, ] ]
131+
assert full is None or isinstance(full, six.text_type)
132+
assert email is None or isinstance(email, six.text_type)
133+
assert country is None or isinstance(country, six.text_type)
134+
assert company is None or isinstance(company, six.text_type)
135+
#full, email, country, company = [ unicode(s) for s in [full, email, country, company, ] ]
130136
if email in seen:
131137
continue
132138
seen.add(email)

ietf/submit/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def clean(self):
203203
# try to parse it
204204
txt_file = self.cleaned_data['txt']
205205
txt_file.seek(0)
206-
self.parsed_draft = Draft(txt_file.read(), txt_file.name)
206+
self.parsed_draft = Draft(txt_file.read().decode('utf8'), txt_file.name)
207207
self.filename = self.parsed_draft.filename
208208
self.revision = self.parsed_draft.revision
209209
self.title = self.parsed_draft.get_title()

ietf/submit/tests.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
3+
24
import datetime
35
import os
46
import shutil
@@ -302,11 +304,11 @@ def submit_existing(self, formats, change_authors=True, group_type='wg', stream_
302304
draft.save_with_history([DocEvent.objects.create(doc=draft, rev=draft.rev, type="added_comment", by=Person.objects.get(user__username="secretary"), desc="Test")])
303305
if not change_authors:
304306
draft.documentauthor_set.all().delete()
305-
author_person, author_email = ensure_person_email_info_exists('Author Name','author@example.com')
307+
author_person, author_email = ensure_person_email_info_exists(u'Author Name',u'author@example.com')
306308
draft.documentauthor_set.create(person=author_person, email=author_email)
307309
else:
308310
# Make it such that one of the previous authors has an invalid email address
309-
bogus_person, bogus_email = ensure_person_email_info_exists('Bogus Person',None)
311+
bogus_person, bogus_email = ensure_person_email_info_exists(u'Bogus Person',None)
310312
DocumentAuthor.objects.create(document=draft, person=bogus_person, email=bogus_email, order=draft.documentauthor_set.latest('order').order+1)
311313

312314
prev_author = draft.documentauthor_set.all()[0]

ietf/submit/utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import os
44
import datetime
5-
import six
5+
import six # pyflakes:ignore
66
from unidecode import unidecode
77

88
from django.conf import settings
@@ -24,7 +24,6 @@
2424
from ietf.submit.mail import announce_to_lists, announce_new_version, announce_to_authors
2525
from ietf.submit.models import Submission, SubmissionEvent, Preapproval, DraftSubmissionStateName
2626
from ietf.utils import log
27-
from ietf.utils import unaccent
2827
from ietf.utils.mail import is_valid_email
2928

3029

@@ -401,10 +400,8 @@ def ensure_person_email_info_exists(name, email):
401400
if not person:
402401
person = Person()
403402
person.name = name
404-
if isinstance(person.name, six.text_type):
405-
person.ascii = unidecode(person.name).decode('ascii')
406-
else:
407-
person.ascii = unaccent.asciify(person.name).decode('ascii')
403+
log.assertion('isinstance(person.name, six.text_type)')
404+
person.ascii = unidecode(person.name).decode('ascii')
408405
person.save()
409406

410407
# make sure we have an email address

ietf/submit/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def upload_submission(request):
7171
# be retrieved from the generated text file. Provide a
7272
# parsed draft object to get at that kind of information.
7373
with open(file_name['txt']) as txt_file:
74-
form.parsed_draft = Draft(txt_file.read(), txt_file.name)
74+
form.parsed_draft = Draft(txt_file.read().decode('utf8'), txt_file.name)
7575

7676
else:
7777
file_size = form.cleaned_data['txt'].size

ietf/utils/draft.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python
22
# -*- python -*-
3-
3+
from __future__ import unicode_literals
44
"""
55
NAME
66
%(program)s - Extract meta-information from an IETF draft.
@@ -37,9 +37,11 @@
3737
import os.path
3838
import re
3939
import stat
40+
import six
4041
import sys
4142
import time
4243

44+
4345
version = "0.35"
4446
program = os.path.basename(sys.argv[0])
4547
progdir = os.path.dirname(sys.argv[0])
@@ -124,6 +126,7 @@ def acronym_match(s, l):
124126
class Draft():
125127

126128
def __init__(self, text, source):
129+
assert isinstance(text, six.text_type)
127130
self.source = source
128131
self.rawtext = text
129132

@@ -1168,8 +1171,8 @@ def getmeta(fn):
11681171
return
11691172

11701173
timestamp = time.strftime("%Y-%m-%dT%H:%M:%S+00:00", time.gmtime(os.stat(filename)[stat.ST_MTIME]))
1171-
text = _gettext(filename)
1172-
draft = Draft(text, filename)
1174+
with open(filename, 'rb') as file:
1175+
draft = Draft(file.read().decode('utf8'), filename)
11731176
#_debug("\n".join(draft.lines))
11741177

11751178
fields["eventdate"] = timestamp

ietf/utils/unaccent.py

Lines changed: 0 additions & 146 deletions
This file was deleted.

0 commit comments

Comments
 (0)