diff options
| author | Xavier Morel <xmo@odoo.com> | 2017-05-11 11:55:22 +0200 |
|---|---|---|
| committer | Xavier Morel <xmo@odoo.com> | 2017-05-15 12:26:30 +0200 |
| commit | 01e35141479b8dfc4a390518f89fc52e7cec5396 (patch) | |
| tree | 24ac6661613e8088140eb607e604d44b2ff5ce42 /addons/mail/models/html2text.py | |
| parent | 54df4b36d7c826de3d07f81297aa17f74d8271ea (diff) | |
[FIX] P3: urllib, urllib2 and urlparse
In Python 3, all of these were "consolidated" under urllib(.request,
.parse, .errors) which is inconvenient.
Since we already have hard dependencies on requests and
werkzeug(.urls, which is a backport of Python 3's unicode-aware
urllib.parse) migrate *everything* to that.
A sticking point is urllib2.URLError, those were (mostly) replaced by
the slightly more general IOError which URLError extends.
Diffstat (limited to 'addons/mail/models/html2text.py')
| -rwxr-xr-x | addons/mail/models/html2text.py | 36 |
1 files changed, 4 insertions, 32 deletions
diff --git a/addons/mail/models/html2text.py b/addons/mail/models/html2text.py index 5e2fb25150f..8830cb0266d 100755 --- a/addons/mail/models/html2text.py +++ b/addons/mail/models/html2text.py @@ -1,5 +1,7 @@ #!/usr/bin/env python """html2text: Turn HTML into equivalent Markdown-structured text.""" +from werkzeug import urls + from odoo.tools import pycompat __version__ = "2.36" @@ -10,9 +12,8 @@ __contributors__ = ["Martin 'Joey' Schulze", "Ricardo Reyes", "Kevin Jay North"] # TODO: # Support decoded entities with unifiable. -import re, sys, urllib, htmlentitydefs, codecs +import re, sys, htmlentitydefs, codecs import sgmllib -import urlparse sgmllib.charref = re.compile('&#([xX]?[0-9a-fA-F]+)[^0-9a-fA-F]') try: from textwrap import wrap @@ -390,7 +391,7 @@ class _html2text(sgmllib.SGMLParser): newa = [] for link in self.a: if self.outcount > link['outcount']: - self.out(" ["+repr(link['count'])+"]: " + urlparse.urljoin(self.baseurl, link['href'])) + self.out(" ["+repr(link['count'])+"]: " + urls.url_join(self.baseurl, link['href'])) if 'title' in link: self.out(" ("+link['title']+")") self.out("\n") else: @@ -426,32 +427,3 @@ def html2text_file(html, out=wrapwrite, baseurl=''): def html2text(html, baseurl=''): return optwrap(html2text_file(html, None, baseurl)) - -if __name__ == "__main__": - baseurl = '' - if sys.argv[1:]: - arg = sys.argv[1] - if arg.startswith('http://'): - baseurl = arg - j = urllib.urlopen(baseurl) - try: - from feedparser import _getCharacterEncoding as enc - except ImportError: - enc = lambda x, y: ('utf-8', 1) - text = j.read() - encoding = enc(j.headers, text)[0] - if encoding == 'us-ascii': encoding = 'utf-8' - data = text.decode(encoding) - - else: - encoding = 'utf8' - if len(sys.argv) > 2: - encoding = sys.argv[2] - f = open(arg, 'r') - try: - data = f.read().decode(encoding) - finally: - f.close() - else: - data = sys.stdin.read().decode('utf8') - wrapwrite(html2text(data, baseurl)) |
