Skip to content

Commit 66f99cd

Browse files
committed
Move sass types into sass module.
1 parent 349c211 commit 66f99cd

File tree

4 files changed

+110
-116
lines changed

4 files changed

+110
-116
lines changed

pysass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static struct PySass_Pair PySass_output_style_enum[] = {
3737

3838
static PyObject* _to_py_value(const union Sass_Value* value) {
3939
PyObject* retv = NULL;
40-
PyObject* types_mod = PyImport_ImportModule("sassutils.sass_types");
40+
PyObject* types_mod = PyImport_ImportModule("sass");
4141
PyObject* sass_comma = PyObject_GetAttrString(types_mod, "SASS_SEPARATOR_COMMA");
4242
PyObject* sass_space = PyObject_GetAttrString(types_mod, "SASS_SEPARATOR_SPACE");
4343

@@ -131,7 +131,7 @@ static PyObject* _to_py_value(const union Sass_Value* value) {
131131

132132
static union Sass_Value* _to_sass_value(PyObject* value) {
133133
union Sass_Value* retv = NULL;
134-
PyObject* types_mod = PyImport_ImportModule("sassutils.sass_types");
134+
PyObject* types_mod = PyImport_ImportModule("sass");
135135
PyObject* sass_number_t = PyObject_GetAttrString(types_mod, "SassNumber");
136136
PyObject* sass_color_t = PyObject_GetAttrString(types_mod, "SassColor");
137137
PyObject* sass_list_t = PyObject_GetAttrString(types_mod, "SassList");

sass.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'a b {\n color: blue; }\n'
1111
1212
"""
13+
from __future__ import absolute_import
1314
import collections
1415
import inspect
1516
import os
@@ -103,6 +104,7 @@ def compile_dirname(
103104
return False, v
104105
return True, None
105106

107+
106108
def compile(**kwargs):
107109
"""There are three modes of parameters :func:`compile()` can take:
108110
``string``, ``filename``, and ``dirname``.
@@ -394,3 +396,86 @@ def and_join(strings):
394396
return ''
395397
iterator = enumerate(strings)
396398
return ', '.join('and ' + s if i == last else s for i, s in iterator)
399+
400+
"""
401+
This module provides datatypes to be used in custom sass functions.
402+
403+
The following mappings from sass types to python types are used:
404+
405+
SASS_NULL: ``None``
406+
SASS_BOOLEAN: ``True`` or ``False``
407+
SASS_STRING: class:`str`
408+
SASS_NUMBER: class:`SassNumber`
409+
SASS_COLOR: class:`SassColor`
410+
SASS_LIST: class:`SassList`
411+
SASS_MAP: class:`dict` or class:`SassMap`
412+
SASS_ERROR: class:`SassError`
413+
SASS_WARNING: class:`SassWarning`
414+
"""
415+
416+
417+
class SassNumber(collections.namedtuple('SassNumber', ('value', 'unit'))):
418+
def __new__(cls, value, unit):
419+
value = float(value)
420+
if not isinstance(unit, text_type):
421+
unit = unit.decode('UTF-8')
422+
return super(SassNumber, cls).__new__(cls, value, unit)
423+
424+
425+
class SassColor(collections.namedtuple('SassColor', ('r', 'g', 'b', 'a'))):
426+
def __new__(cls, r, g, b, a):
427+
r = float(r)
428+
g = float(g)
429+
b = float(b)
430+
a = float(a)
431+
return super(SassColor, cls).__new__(cls, r, g, b, a)
432+
433+
434+
SASS_SEPARATOR_COMMA = collections.namedtuple('SASS_SEPARATOR_COMMA', ())()
435+
SASS_SEPARATOR_SPACE = collections.namedtuple('SASS_SEPARATOR_SPACE', ())()
436+
SEPARATORS = frozenset((SASS_SEPARATOR_COMMA, SASS_SEPARATOR_SPACE))
437+
438+
439+
class SassList(collections.namedtuple('SassList', ('items', 'separator'))):
440+
def __new__(cls, items, separator):
441+
items = tuple(items)
442+
assert separator in SEPARATORS
443+
return super(SassList, cls).__new__(cls, items, separator)
444+
445+
446+
class SassError(collections.namedtuple('SassError', ('msg',))):
447+
def __new__(cls, msg):
448+
if not isinstance(msg, text_type):
449+
msg = msg.decode('UTF-8')
450+
return super(SassError, cls).__new__(cls, msg)
451+
452+
453+
class SassWarning(collections.namedtuple('SassError', ('msg',))):
454+
def __new__(cls, msg):
455+
if not isinstance(msg, text_type):
456+
msg = msg.decode('UTF-8')
457+
return super(SassWarning, cls).__new__(cls, msg)
458+
459+
460+
class SassMap(dict):
461+
"""Because sass maps can have mapping types as keys, we need an immutable
462+
hashable mapping type.
463+
"""
464+
__slots__ = ('_hash',)
465+
466+
def __new__(cls, *args, **kwargs):
467+
value = super(SassMap, cls).__new__(cls, *args, **kwargs)
468+
# An assertion that all things are hashable
469+
value._hash = hash(frozenset(value.items()))
470+
return value
471+
472+
def __repr__(self):
473+
return '{0}({1})'.format(type(self).__name__, frozenset(self.items()))
474+
475+
def __hash__(self):
476+
return self._hash
477+
478+
def _immutable(self, *_):
479+
raise AssertionError('SassMaps are immutable')
480+
481+
__setitem__ = __delitem__ = _immutable

sasstests.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import sass
2323
import sassc
24-
from sassutils import sass_types
2524
from sassutils.builder import Manifest, build_directory
2625
from sassutils.wsgi import SassMiddleware
2726

@@ -788,57 +787,57 @@ def test_raises_typeerror_star_kwargs(self):
788787

789788
class SassTypesTest(unittest.TestCase):
790789
def test_number_no_conversion(self):
791-
num = sass_types.SassNumber(123., u'px')
790+
num = sass.SassNumber(123., u'px')
792791
assert type(num.value) is float, type(num.value)
793792
assert type(num.unit) is text_type, type(num.unit)
794793

795794
def test_number_conversion(self):
796-
num = sass_types.SassNumber(123, b'px')
795+
num = sass.SassNumber(123, b'px')
797796
assert type(num.value) is float, type(num.value)
798797
assert type(num.unit) is text_type, type(num.unit)
799798

800799
def test_color_no_conversion(self):
801-
color = sass_types.SassColor(1., 2., 3., .5)
800+
color = sass.SassColor(1., 2., 3., .5)
802801
assert type(color.r) is float, type(color.r)
803802
assert type(color.g) is float, type(color.g)
804803
assert type(color.b) is float, type(color.b)
805804
assert type(color.a) is float, type(color.a)
806805

807806
def test_color_conversion(self):
808-
color = sass_types.SassColor(1, 2, 3, 1)
807+
color = sass.SassColor(1, 2, 3, 1)
809808
assert type(color.r) is float, type(color.r)
810809
assert type(color.g) is float, type(color.g)
811810
assert type(color.b) is float, type(color.b)
812811
assert type(color.a) is float, type(color.a)
813812

814813
def test_sass_list_no_conversion(self):
815-
lst = sass_types.SassList(
816-
('foo', 'bar'), sass_types.SASS_SEPARATOR_COMMA,
814+
lst = sass.SassList(
815+
('foo', 'bar'), sass.SASS_SEPARATOR_COMMA,
817816
)
818817
assert type(lst.items) is tuple, type(lst.items)
819-
assert lst.separator is sass_types.SASS_SEPARATOR_COMMA, lst.separator
818+
assert lst.separator is sass.SASS_SEPARATOR_COMMA, lst.separator
820819

821820
def test_sass_list_conversion(self):
822-
lst = sass_types.SassList(
823-
['foo', 'bar'], sass_types.SASS_SEPARATOR_SPACE,
821+
lst = sass.SassList(
822+
['foo', 'bar'], sass.SASS_SEPARATOR_SPACE,
824823
)
825824
assert type(lst.items) is tuple, type(lst.items)
826-
assert lst.separator is sass_types.SASS_SEPARATOR_SPACE, lst.separator
825+
assert lst.separator is sass.SASS_SEPARATOR_SPACE, lst.separator
827826

828827
def test_sass_warning_no_conversion(self):
829-
warn = sass_types.SassWarning(u'error msg')
828+
warn = sass.SassWarning(u'error msg')
830829
assert type(warn.msg) is text_type, type(warn.msg)
831830

832831
def test_sass_warning_no_conversion(self):
833-
warn = sass_types.SassWarning(b'error msg')
832+
warn = sass.SassWarning(b'error msg')
834833
assert type(warn.msg) is text_type, type(warn.msg)
835834

836835
def test_sass_error_no_conversion(self):
837-
err = sass_types.SassError(u'error msg')
836+
err = sass.SassError(u'error msg')
838837
assert type(err.msg) is text_type, type(err.msg)
839838

840839
def test_sass_error_conversion(self):
841-
err = sass_types.SassError(b'error msg')
840+
err = sass.SassError(b'error msg')
842841
assert type(err.msg) is text_type, type(err.msg)
843842

844843

@@ -855,25 +854,25 @@ def identity(x):
855854

856855
custom_functions = {
857856
'raises': lambda: raise_exc(AssertionError('foo')),
858-
'returns_warning': lambda: sass_types.SassWarning('This is a warning'),
859-
'returns_error': lambda: sass_types.SassError('This is an error'),
857+
'returns_warning': lambda: sass.SassWarning('This is a warning'),
858+
'returns_error': lambda: sass.SassError('This is an error'),
860859
# Tuples are a not-supported type.
861860
'returns_unknown': lambda: (1, 2, 3),
862861
'returns_true': lambda: True,
863862
'returns_false': lambda: False,
864863
'returns_none': lambda: None,
865864
'returns_unicode': lambda: u'☃',
866865
'returns_bytes': lambda: u'☃'.encode('UTF-8'),
867-
'returns_number': lambda: sass_types.SassNumber(5, 'px'),
868-
'returns_color': lambda: sass_types.SassColor(1, 2, 3, .5),
869-
'returns_comma_list': lambda: sass_types.SassList(
870-
('Arial', 'sans-serif'), sass_types.SASS_SEPARATOR_COMMA,
866+
'returns_number': lambda: sass.SassNumber(5, 'px'),
867+
'returns_color': lambda: sass.SassColor(1, 2, 3, .5),
868+
'returns_comma_list': lambda: sass.SassList(
869+
('Arial', 'sans-serif'), sass.SASS_SEPARATOR_COMMA,
871870
),
872-
'returns_space_list': lambda: sass_types.SassList(
873-
('medium', 'none'), sass_types.SASS_SEPARATOR_SPACE,
871+
'returns_space_list': lambda: sass.SassList(
872+
('medium', 'none'), sass.SASS_SEPARATOR_SPACE,
874873
),
875874
'returns_py_dict': lambda: {'foo': 'bar'},
876-
'returns_map': lambda: sass_types.SassMap((('foo', 'bar'),)),
875+
'returns_map': lambda: sass.SassMap((('foo', 'bar'),)),
877876
# TODO: returns SassMap
878877
'identity': identity,
879878
}

sassutils/sass_types.py

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

0 commit comments

Comments
 (0)