Better support for python2.x + python3.x
This commit is contained in:
@@ -98,7 +98,7 @@ class Color():
|
||||
h, s, l = args
|
||||
if type(l) == str: l = int(l.strip('%'))
|
||||
if type(s) == str: s = int(s.strip('%'))
|
||||
rgb = colorsys.hls_to_rgb(int(h) / 360, l / 100, s / 100)
|
||||
rgb = colorsys.hls_to_rgb(int(h) / 360.0, l / 100.0, s / 100.0)
|
||||
color = (round(c * 255) for c in rgb)
|
||||
return self._rgbatohex(color)
|
||||
raise ValueError('Illegal color values')
|
||||
@@ -114,8 +114,8 @@ class Color():
|
||||
h, s, l, a = args
|
||||
if type(l) == str: l = int(l.strip('%'))
|
||||
if type(s) == str: s = int(s.strip('%'))
|
||||
rgb = colorsys.hls_to_rgb(int(h) / 360, l / 100, s / 100)
|
||||
color = [round(c * 255) for c in rgb]
|
||||
rgb = colorsys.hls_to_rgb(int(h) / 360.0, l / 100.0, s / 100.0)
|
||||
color = [float(round(c * 255)) for c in rgb]
|
||||
color.append(round(float(a[:-1]) / 100.0, 2))
|
||||
return "rgba(%s,%s,%s,%s)" % tuple(color)
|
||||
raise ValueError('Illegal color values')
|
||||
@@ -352,7 +352,7 @@ class Color():
|
||||
def _ophsl(self, color, diff, idx, op):
|
||||
if type(diff) == str: diff = int(diff.strip('%'))
|
||||
hls = list(self._hextohls(color))
|
||||
hls[idx] = self._clamp(getattr(hls[idx], op)(diff / 100))
|
||||
hls[idx] = self._clamp(getattr(hls[idx], op)(diff / 100.0))
|
||||
rgb = colorsys.hls_to_rgb(*hls)
|
||||
color = (round(c * 255) for c in rgb)
|
||||
return self._rgbatohex(color)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
See LICENSE for details.
|
||||
.. moduleauthor:: Johann T. Mariusson <jtm@robot.is>
|
||||
"""
|
||||
import sys
|
||||
from .node import Node
|
||||
from lesscpy.lessc import utility
|
||||
from lesscpy.lessc import color
|
||||
@@ -116,7 +117,10 @@ class Expression(Node):
|
||||
'!=': '__ne__',
|
||||
'<>': '__ne__',
|
||||
}.get(oper)
|
||||
ret = getattr(vala, operation)(valb)
|
||||
if sys.version_info[0] < 3:
|
||||
ret = self.py2op(vala, operation, valb)
|
||||
else:
|
||||
ret = getattr(vala, operation)(valb)
|
||||
if ret is NotImplemented:
|
||||
ret = getattr(valb, operation)(vala)
|
||||
if oper in '+-*/':
|
||||
@@ -127,6 +131,25 @@ class Expression(Node):
|
||||
pass
|
||||
return ret
|
||||
|
||||
def py2op(self, vala, operation, valb):
|
||||
""" Python2 operators
|
||||
"""
|
||||
if operation == '__lt__':
|
||||
ret = (vala < valb)
|
||||
elif operation == '__gt__':
|
||||
ret = (vala > valb)
|
||||
elif operation == '__eq__':
|
||||
ret = (vala == valb)
|
||||
elif operation == '__ge__':
|
||||
ret = (vala >= valb)
|
||||
elif operation == '__le__':
|
||||
ret = (vala <= valb)
|
||||
elif operation == '__ne__':
|
||||
ret = (vala != valb)
|
||||
else:
|
||||
ret = getattr(vala, operation)(valb)
|
||||
return ret
|
||||
|
||||
def expression(self):
|
||||
"""Return str representation of expression
|
||||
returns:
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
See LICENSE for details.
|
||||
.. moduleauthor:: Johann T. Mariusson <jtm@robot.is>
|
||||
"""
|
||||
import copy, itertools
|
||||
import sys, copy, itertools
|
||||
from .node import Node
|
||||
from .block import Block
|
||||
from .expression import Expression
|
||||
@@ -53,13 +53,13 @@ class Mixin(Node):
|
||||
SyntaxError
|
||||
"""
|
||||
arguments = zip(args, [' '] * len(args)) if args and args[0] else None
|
||||
zl = itertools.zip_longest if sys.version_info[0] == 3 else itertools.izip_longest
|
||||
if self.args:
|
||||
parsed = [v if hasattr(v, 'parse') else v
|
||||
for v in copy.copy(self.args)]
|
||||
args = args if type(args) is list else [args]
|
||||
vars = [self._parse_arg(var, arg, scope)
|
||||
for arg, var in itertools.izip_longest([a for a in args],
|
||||
parsed)]
|
||||
for arg, var in zl([a for a in args], parsed)]
|
||||
for var in vars:
|
||||
if var: var.parse(scope)
|
||||
if not arguments:
|
||||
|
||||
@@ -67,11 +67,11 @@ class TestLessColor(unittest.TestCase):
|
||||
def test_hsla(self):
|
||||
test = self.color.hsla
|
||||
for h, s, l, a, v in [
|
||||
(31, '1%', '4%', '0%', 'rgba(10,10,10,0.0)'),
|
||||
(31, '30%', '4%', '1%', 'rgba(13,10,7,0.01)'),
|
||||
(31, '60%', '4%', '20%', 'rgba(16,10,4,0.2)'),
|
||||
(31, '90%', '4%', '60%', 'rgba(19,11,1,0.6)'),
|
||||
(31, '100%', '4%', '100%', 'rgba(20,11,0,1.0)'),
|
||||
(31, '1%', '4%', '0%', 'rgba(10.0,10.0,10.0,0.0)'),
|
||||
(31, '30%', '4%', '1%', 'rgba(13.0,10.0,7.0,0.01)'),
|
||||
(31, '60%', '4%', '20%', 'rgba(16.0,10.0,4.0,0.2)'),
|
||||
(31, '90%', '4%', '60%', 'rgba(19.0,11.0,1.0,0.6)'),
|
||||
(31, '100%', '4%', '100%', 'rgba(20.0,11.0,0.0,1.0)'),
|
||||
]:
|
||||
self.assertEqual(test(h, s, l, a), v)
|
||||
|
||||
|
||||
@@ -43,9 +43,15 @@ class TestExpression(unittest.TestCase):
|
||||
e = Expression(test[:3])
|
||||
self.assertEqual(test[3], e.parse(None), str(test))
|
||||
|
||||
def testeq(self):
|
||||
def testop(self):
|
||||
for test in [
|
||||
['0', '=', '0', True],
|
||||
['1', '>', '2', False],
|
||||
['1', '<', '2', True],
|
||||
['1', '>=', '2', False],
|
||||
['1', '<=', '2', True],
|
||||
['1', '!=', '2', True],
|
||||
['1', '<>', '2', True],
|
||||
]:
|
||||
e = Expression(test[:3])
|
||||
self.assertEqual(test[3], e.parse(None), test)
|
||||
|
||||
Reference in New Issue
Block a user