Browse Source

Fix unicode litterals

pull/35/head
Florian Mounier 12 years ago
parent
commit
64b6ec94d4
  1. 6
      pygal/_compat.py
  2. 15
      pygal/svg.py
  3. 23
      pygal/test/test_util.py
  4. 6
      pygal/util.py

6
pygal/_compat.py

@ -36,6 +36,12 @@ def to_str(string):
return string return string
def u(s):
if sys.version_info[0] == 2:
return unicode(s, "unicode_escape")
return s
def total_seconds(td): def total_seconds(td):
if sys.version_info[:2] == (2, 6): if sys.version_info[:2] == (2, 6):
return ( return (

15
pygal/svg.py

@ -22,7 +22,7 @@ Svg helper
""" """
from __future__ import division from __future__ import division
from pygal._compat import urlparse, to_str from pygal._compat import urlparse, to_str, u
import io import io
import os import os
import json import json
@ -41,7 +41,7 @@ class Svg(object):
def __init__(self, graph): def __init__(self, graph):
self.graph = graph self.graph = graph
self.processing_instructions = [ self.processing_instructions = [
etree.PI(u'xml', u"version='1.0' encoding='utf-8'")] etree.PI(u('xml'), u("version='1.0' encoding='utf-8'"))]
self.root = etree.Element( self.root = etree.Element(
"{%s}svg" % self.ns, "{%s}svg" % self.ns,
nsmap={ nsmap={
@ -49,10 +49,11 @@ class Svg(object):
'xlink': 'http://www.w3.org/1999/xlink', 'xlink': 'http://www.w3.org/1999/xlink',
}) })
self.root.append( self.root.append(
etree.Comment(u'Generated with pygal %s ©Kozea 2011-2013 on %s' % ( etree.Comment(u(
__version__, date.today().isoformat()))) 'Generated with pygal %s ©Kozea 2011-2013 on %s' % (
self.root.append(etree.Comment(u'http://pygal.org')) __version__, date.today().isoformat()))))
self.root.append(etree.Comment(u'http://github.com/Kozea/pygal')) self.root.append(etree.Comment(u('http://pygal.org')))
self.root.append(etree.Comment(u('http://github.com/Kozea/pygal')))
self.defs = self.node(tag='defs') self.defs = self.node(tag='defs')
self.title = self.node(tag='title') self.title = self.node(tag='title')
self.title.text = graph.title or 'Pygal' self.title.text = graph.title or 'Pygal'
@ -63,7 +64,7 @@ class Svg(object):
if urlparse(css).scheme: if urlparse(css).scheme:
self.processing_instructions.append( self.processing_instructions.append(
etree.PI( etree.PI(
u'xml-stylesheet', u'href="%s"' % css)) u('xml-stylesheet'), u('href="%s"' % css)))
else: else:
if not os.path.exists(css): if not os.path.exists(css):
css = os.path.join( css = os.path.join(

23
pygal/test/test_util.py

@ -16,6 +16,7 @@
# #
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>. # along with pygal. If not, see <http://www.gnu.org/licenses/>.
from pystil._compat import u
from pygal.util import ( from pygal.util import (
round_to_int, round_to_float, _swap_curly, template, humanize, round_to_int, round_to_float, _swap_curly, template, humanize,
is_major, truncate, minify_css) is_major, truncate, minify_css)
@ -46,9 +47,9 @@ def test_round_to_float():
def test_swap_curly(): def test_swap_curly():
for str in ( for str in (
'foo', 'foo',
u'foo foo foo bar', u('foo foo foo bar'),
'foo béè b¡ð/ijə˘©þß®~¯æ', 'foo béè b¡ð/ijə˘©þß®~¯æ',
u'foo béè b¡ð/ijə˘©þß®~¯æ'): u('foo béè b¡ð/ijə˘©þß®~¯æ')):
assert _swap_curly(str) == str assert _swap_curly(str) == str
assert _swap_curly('foo{bar}baz') == 'foo{{bar}}baz' assert _swap_curly('foo{bar}baz') == 'foo{{bar}}baz'
assert _swap_curly('foo{{bar}}baz') == 'foo{bar}baz' assert _swap_curly('foo{{bar}}baz') == 'foo{bar}baz'
@ -94,12 +95,12 @@ def test_humanize():
assert humanize(0.001) == '1m' assert humanize(0.001) == '1m'
assert humanize(0.002) == '2m' assert humanize(0.002) == '2m'
assert humanize(0.0025) == '2.5m' assert humanize(0.0025) == '2.5m'
assert humanize(0.0001) == u'100µ' assert humanize(0.0001) == u('100µ')
assert humanize(0.000123) == u'123µ' assert humanize(0.000123) == u('123µ')
assert humanize(0.00001) == u'10µ' assert humanize(0.00001) == u('10µ')
assert humanize(0.000001) == u'' assert humanize(0.000001) == u('')
assert humanize(0.0000001) == u'100n' assert humanize(0.0000001) == u('100n')
assert humanize(0.0000000001) == u'100p' assert humanize(0.0000000001) == u('100p')
assert humanize(0) == '0' assert humanize(0) == '0'
assert humanize(0.) == '0' assert humanize(0.) == '0'
@ -116,9 +117,9 @@ def test_is_major():
def test_truncate(): def test_truncate():
assert truncate('1234567890', 50) == '1234567890' assert truncate('1234567890', 50) == '1234567890'
assert truncate('1234567890', 5) == u'1234…' assert truncate('1234567890', 5) == u('1234…')
assert truncate('1234567890', 1) == u'' assert truncate('1234567890', 1) == u('')
assert truncate('1234567890', 9) == u'12345678…' assert truncate('1234567890', 9) == u('12345678…')
assert truncate('1234567890', 10) == '1234567890' assert truncate('1234567890', 10) == '1234567890'
assert truncate('1234567890', 0) == '1234567890' assert truncate('1234567890', 0) == '1234567890'
assert truncate('1234567890', -1) == '1234567890' assert truncate('1234567890', -1) == '1234567890'

6
pygal/util.py

@ -21,14 +21,14 @@ Various utils
""" """
from __future__ import division from __future__ import division
from pygal._compat import to_str from pygal._compat import to_str, u
import re import re
from decimal import Decimal from decimal import Decimal
from math import floor, pi, log, log10, ceil from math import floor, pi, log, log10, ceil
from itertools import cycle from itertools import cycle
from functools import reduce from functools import reduce
from pygal.adapters import not_zero, positive from pygal.adapters import not_zero, positive
ORDERS = u"yzafpnµm kMGTPEZY" ORDERS = u("yzafpnµm kMGTPEZY")
def float_format(number): def float_format(number):
@ -231,7 +231,7 @@ def cycle_fill(short_list, max_len):
def truncate(string, index): def truncate(string, index):
"""Truncate a string at index and add ...""" """Truncate a string at index and add ..."""
if len(string) > index and index > 0: if len(string) > index and index > 0:
string = string[:index - 1] + u'' string = string[:index - 1] + u('')
return string return string

Loading…
Cancel
Save