Skip to content

Commit 37bf546

Browse files
committed
Options.output_style field
1 parent ac7df37 commit 37bf546

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

sass.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,44 @@ typedef struct {
66
struct sass_options options;
77
} sass_OptionsObject;
88

9+
static struct {
10+
char *label;
11+
int value;
12+
} sass_Options_output_style_enum[] = {
13+
{"nested", SASS_STYLE_NESTED},
14+
{"expanded", SASS_STYLE_EXPANDED},
15+
{"compact", SASS_STYLE_COMPACT},
16+
{"compressed", SASS_STYLE_COMPRESSED},
17+
{NULL}
18+
};
19+
920
static int
1021
sass_Options_init(sass_OptionsObject *self, PyObject *args, PyObject *kwds)
1122
{
12-
static char *sig[] = {"include_paths", "image_path", NULL};
13-
PyObject *include_paths, *image_path, *item;
23+
static char *sig[] = {"output_style", "include_paths", "image_path", NULL};
24+
PyObject *output_style, *include_paths, *image_path, *item;
1425
char *include_paths_cstr, *item_buffer, *image_path_cstr;
1526
size_t include_paths_len, include_paths_size, i, offset, item_size,
1627
image_path_size;
17-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OS", sig,
18-
&include_paths, &image_path)) {
28+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOS", sig,
29+
&output_style, &include_paths,
30+
&image_path)) {
1931
return -1;
2032
}
2133

22-
self->options.output_style = SASS_STYLE_NESTED;
34+
for (i = 0; sass_Options_output_style_enum[i].label; ++i) {
35+
if (0 == strncmp(PyString_AsString(output_style),
36+
sass_Options_output_style_enum[i].label,
37+
PyString_Size(output_style))) {
38+
self->options.output_style =
39+
sass_Options_output_style_enum[i].value;
40+
break;
41+
}
42+
}
43+
if (sass_Options_output_style_enum[i].label == NULL) {
44+
PyErr_SetString(PyExc_ValueError, "invalid output_style option");
45+
return -1;
46+
}
2347

2448
if (include_paths == Py_None) {
2549
PyErr_SetString(PyExc_TypeError, "include_paths must not be None");
@@ -104,6 +128,27 @@ sass_Options_dealloc(sass_OptionsObject *self)
104128
self->ob_type->tp_free((PyObject *) self);
105129
}
106130

131+
static PyObject *
132+
sass_Options_get_output_style(sass_OptionsObject *self, void *closure)
133+
{
134+
int value;
135+
PyObject *label;
136+
size_t i;
137+
138+
value = self->options.output_style;
139+
for (i = 0; sass_Options_output_style_enum[i].label; ++i) {
140+
if (value == sass_Options_output_style_enum[i].value) {
141+
label = PyString_FromString(
142+
sass_Options_output_style_enum[i].label);
143+
Py_INCREF(label);
144+
return label;
145+
}
146+
}
147+
148+
PyErr_Format(PyExc_ValueError, "output_style is invalid (%d)", value);
149+
return NULL;
150+
}
151+
107152
static PyObject *
108153
sass_Options_get_include_paths(sass_OptionsObject *self, void *closure)
109154
{
@@ -145,6 +190,8 @@ sass_Options_get_image_path(sass_OptionsObject *self, void *closure)
145190
}
146191

147192
static PyGetSetDef sass_Options_getset[] = {
193+
{"output_style", (getter) sass_Options_get_output_style, NULL,
194+
"The string value of output style option."},
148195
{"include_paths", (getter) sass_Options_get_include_paths, NULL,
149196
"The list of paths to include."},
150197
{"image_path", (getter) sass_Options_get_image_path, NULL,

sasstests.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,42 @@
99
suite = Tests()
1010

1111

12+
@suite.test
13+
def options_output_style():
14+
for style in 'nested', 'expanded', 'compact', 'compressed':
15+
o = Options(output_style=style, include_paths='a:b', image_path='')
16+
assert o.output_style == style
17+
with raises(TypeError):
18+
Options(output_style=None, include_paths='a:b', image_path='')
19+
with raises(TypeError):
20+
Options(output_style=123, include_paths='a:b', image_path='')
21+
with raises(TypeError):
22+
Options(output_style=['abc'], include_paths='a:b', image_path='')
23+
with raises(ValueError):
24+
Options(output_style='abc', include_paths='a:b', image_path='')
25+
26+
1227
@suite.test
1328
def options_include_paths():
14-
o = Options(include_paths='ab/cd:de/fg', image_path='')
29+
o = Options('nested', include_paths='ab/cd:de/fg', image_path='')
1530
assert o.include_paths == ['ab/cd', 'de/fg']
16-
o = Options(include_paths=['li/st', 'te/st'], image_path='')
31+
o = Options('nested', include_paths=['li/st', 'te/st'], image_path='')
1732
assert o.include_paths == ['li/st', 'te/st']
18-
o = Options(include_paths=('tup/le', 'te/st'), image_path='')
33+
o = Options('nested', include_paths=('tup/le', 'te/st'), image_path='')
1934
assert o.include_paths == ['tup/le', 'te/st']
2035
with raises(TypeError):
21-
Options(include_paths=None, image_path='a/b')
36+
Options('nested', include_paths=None, image_path='a/b')
2237
with raises(TypeError):
23-
Options(include_paths=123, image_path='a/b')
38+
Options('nested', include_paths=123, image_path='a/b')
2439

2540

2641
@suite.test
2742
def options_image_path():
28-
o = Options(include_paths='a:b', image_path='image/path')
43+
o = Options('nested', include_paths='a:b', image_path='image/path')
2944
assert o.image_path == 'image/path'
3045
with raises(TypeError):
31-
Options(include_paths='a:b', image_path=None)
46+
Options('nested', include_paths='a:b', image_path=None)
3247
with raises(TypeError):
33-
Options(include_paths='a:b', image_path=123)
48+
Options('nested', include_paths='a:b', image_path=123)
3449
with raises(TypeError):
35-
Options(include_paths='a:b', image_path=['a/b', 'c/d'])
50+
Options('nested', include_paths='a:b', image_path=['a/b', 'c/d'])

0 commit comments

Comments
 (0)