Skip to content

Instantly share code, notes, and snippets.

@komeda-shinji
Created January 30, 2015 09:46
Show Gist options
  • Save komeda-shinji/2640ac754b59c5717f24 to your computer and use it in GitHub Desktop.
Save komeda-shinji/2640ac754b59c5717f24 to your computer and use it in GitHub Desktop.
twillでの文字エンコーディングを改善する
--- commands.py 2007-12-28 15:45:16.000000000 +0900
+++ commands.py 2013-02-05 22:22:42.000000000 +0900
@@ -231,8 +231,8 @@
For explanations of these, please see the Python re module
documentation.
"""
- regexp = re.compile(what, _parseFindFlags(flags))
- page = browser.get_html()
+ regexp = re.compile(unicode(what), _parseFindFlags(flags))
+ page = browser.get_html().decode(browser._browser.encoding(), 'ignore')
m = regexp.search(page)
if not m:
@@ -252,8 +252,8 @@
Fail if the regular expression is on the page.
"""
- regexp = re.compile(what, _parseFindFlags(flags))
- page = browser.get_html()
+ regexp = re.compile(unicode(what), _parseFindFlags(flags))
+ page = browser.get_html().decode(browser._browser.encoding(), 'ignore')
if regexp.search(page):
raise TwillAssertionError("match to '%s'" % (what,))
@@ -273,7 +273,7 @@
Show the HTML for the current page.
"""
- html = browser.get_html()
+ html = browser.get_html().decode(browser._browser.encoding(), 'ignore')
print>>OUT, html
return html
@@ -447,7 +447,7 @@
if isinstance(control, ClientForm.FileControl):
raise TwillException('form field is for file upload; use "formfile" instead')
- set_form_control_value(control, value)
+ set_form_control_value(control, value.encode(browser._browser.encoding()))
fv = formvalue
@@ -707,8 +707,8 @@
Succeed if the regular expression is in the page title.
"""
- regexp = re.compile(what)
- title = browser.get_title()
+ regexp = re.compile(unicode(what))
+ title = browser.get_title().decode(browser._browser.encoding(), 'ignore')
print>>OUT, "title is '%s'." % (title,)
@@ -883,7 +883,7 @@
else:
print ''
if check_html:
- title = browser.get_title()
+ title = browser.get_title().decode(browser._browser.encoding(), 'ignore')
print >>OUT, '\tPage title:', title
forms = browser.get_all_forms()
--- browser.py 2007-12-28 15:45:16.000000000 +0900
+++ browser.py 2013-02-05 22:42:01.000000000 +0900
@@ -225,7 +225,7 @@
forms = self.get_all_forms()
for n, f in enumerate(forms):
- print_form(n, f, OUT)
+ print_form(n, f, OUT, encoding=self._browser.encoding())
def showlinks(self):
"""
@@ -233,7 +233,11 @@
"""
print>>OUT, 'Links:\n'
for n, link in enumerate(self._browser.links()):
- print>>OUT, "%d. %s ==> %s" % (n, link.text, link.url,)
+ if link.text:
+ link_text = link.text.decode(self._browser.encoding(), 'ignore')
+ else:
+ link_text = ""
+ print>>OUT, "%d. %s ==> %s" % (n, link_text, link.url,)
print>>OUT, ''
def showhistory(self):
--- utils.py 2007-12-28 15:45:16.000000000 +0900
+++ utils.py 2013-02-05 23:15:05.000000000 +0900
@@ -52,7 +52,7 @@
return s
-def print_form(n, f, OUT):
+def print_form(n, f, OUT, encoding=None):
"""
Pretty-print the given form, assigned # n.
"""
@@ -76,11 +76,20 @@
nonclickies = [c for c in f.controls if c not in clickies]
for n, field in enumerate(f.controls):
+ if encoding is not None:
+ if isinstance(field.value, list):
+ value = "['%s']" % "', '".join(field.value).decode(encoding)
+ else:
+ value = field.value.decode(encoding)
+ else:
+ value = field.value
if hasattr(field, 'items'):
items = [ i.name for i in field.items ]
- value_displayed = "%s of %s" % (field.value, items)
+ if encoding is not None:
+ items = "['%s']" % "', '".join(items).decode(encoding)
+ value_displayed = "%s of %s" % (value, items)
else:
- value_displayed = "%s" % (field.value,)
+ value_displayed = "%s" % (value,)
if field.is_of_kind('clickable'):
submit_index = "%-2s" % (submit_indices[field],)
@@ -245,7 +254,7 @@
# stuff to run 'tidy'...
#
-_tidy_cmd = ["tidy", "-q", "-ashtml"]
+_tidy_cmd = ["tidy", "-q", "-ashtml", "-raw"]
_tidy_exists = True
def run_tidy(html):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment