From 10ee082c1409a2790ebc53a662b9f8c46cbc4789 Mon Sep 17 00:00:00 2001 From: Andrea Stagi Date: Mon, 5 Jan 2015 22:02:10 +0100 Subject: [PATCH 1/3] Add precision parameter --- pysass.cpp | 21 ++++++++++++--------- sass.py | 7 ++++--- sassc.py | 8 ++++++-- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pysass.cpp b/pysass.cpp index 9ba74bdd..30f0e6e2 100644 --- a/pysass.cpp +++ b/pysass.cpp @@ -39,13 +39,13 @@ static PyObject * PySass_compile_string(PyObject *self, PyObject *args) { struct sass_context *context; char *string, *include_paths, *image_path; - int output_style, source_comments; + int output_style, source_comments, precision; PyObject *result; if (!PyArg_ParseTuple(args, - PySass_IF_PY3("yiiyy", "siiss"), + PySass_IF_PY3("yiiyyi", "siissi"), &string, &output_style, &source_comments, - &include_paths, &image_path)) { + &include_paths, &image_path, &precision)) { return NULL; } @@ -55,6 +55,7 @@ PySass_compile_string(PyObject *self, PyObject *args) { context->options.source_comments = source_comments; context->options.include_paths = include_paths; context->options.image_path = image_path; + context->options.precision = precision; sass_compile(context); @@ -71,13 +72,13 @@ static PyObject * PySass_compile_filename(PyObject *self, PyObject *args) { struct sass_file_context *context; char *filename, *include_paths, *image_path; - int output_style, source_comments, error_status; + int output_style, source_comments, error_status, precision; PyObject *source_map_filename, *result; if (!PyArg_ParseTuple(args, - PySass_IF_PY3("yiiyyO", "siissO"), + PySass_IF_PY3("yiiyyiO", "siissiO"), &filename, &output_style, &source_comments, - &include_paths, &image_path, &source_map_filename)) { + &include_paths, &image_path, &precision, &source_map_filename)) { return NULL; } @@ -99,6 +100,7 @@ PySass_compile_filename(PyObject *self, PyObject *args) { context->options.source_comments = source_comments; context->options.include_paths = include_paths; context->options.image_path = image_path; + context->options.precision = precision; sass_compile_file(context); @@ -119,14 +121,14 @@ static PyObject * PySass_compile_dirname(PyObject *self, PyObject *args) { struct sass_folder_context *context; char *search_path, *output_path, *include_paths, *image_path; - int output_style, source_comments; + int output_style, source_comments, precision; PyObject *result; if (!PyArg_ParseTuple(args, - PySass_IF_PY3("yyiyy", "ssiss"), + PySass_IF_PY3("yyiiyyi", "ssiissi"), &search_path, &output_path, &output_style, &source_comments, - &include_paths, &image_path)) { + &include_paths, &image_path, precision)) { return NULL; } @@ -137,6 +139,7 @@ PySass_compile_dirname(PyObject *self, PyObject *args) { context->options.source_comments = source_comments; context->options.include_paths = include_paths; context->options.image_path = image_path; + context->options.precision = precision; sass_compile_folder(context); diff --git a/sass.py b/sass.py index 8908dc31..e021b295 100644 --- a/sass.py +++ b/sass.py @@ -158,6 +158,7 @@ def compile(**kwargs): elif len(modes) > 1: raise TypeError(and_join(modes) + ' are exclusive each other; ' 'cannot be used at a time') + precision = kwargs.pop('precision', 5) output_style = kwargs.pop('output_style', 'nested') if not isinstance(output_style, string_types): raise TypeError('output_style must be a string, not ' + @@ -235,7 +236,7 @@ def compile(**kwargs): string = string.encode('utf-8') s, v = compile_string(string, output_style, source_comments, - include_paths, image_path) + include_paths, image_path, precision) if s: return v.decode('utf-8') elif 'filename' in modes: @@ -249,7 +250,7 @@ def compile(**kwargs): s, v, source_map = compile_filename( filename, output_style, source_comments, - include_paths, image_path, source_map_filename + include_paths, image_path, precision, source_map_filename ) if s: v = v.decode('utf-8') @@ -299,7 +300,7 @@ def compile(**kwargs): output_path = output_path.encode(fs_encoding) s, v = compile_dirname(search_path, output_path, output_style, source_comments, - include_paths, image_path) + include_paths, image_path, precision) if s: return else: diff --git a/sassc.py b/sassc.py index 9856e7ee..a60dfb98 100755 --- a/sassc.py +++ b/sassc.py @@ -87,6 +87,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): parser.add_option('-w', '--watch', action='store_true', help='Watch file for changes. Requires the second ' 'argument (output css filename).') + parser.add_option('-p', '--precision', action='store', type="int", default=5, + help='Set number of decimal places. [default: %default]') options, args = parser.parse_args(argv[1:]) error = functools.partial(print, parser.get_prog_name() + ': error:', @@ -122,7 +124,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): output_style=options.output_style, source_map_filename=source_map_filename, include_paths=options.include_paths, - image_path=options.image_path + image_path=options.image_path, + precision=options.precision ) else: source_map_filename = None @@ -131,7 +134,8 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): filename=filename, output_style=options.output_style, include_paths=options.include_paths, - image_path=options.image_path + image_path=options.image_path, + precision=options.precision ) except (IOError, OSError) as e: error(e) From f4d14945a8c492170a774ed4aa9b8e1f3b3e5b58 Mon Sep 17 00:00:00 2001 From: Andrea Stagi Date: Mon, 5 Jan 2015 22:02:57 +0100 Subject: [PATCH 2/3] Add test for precision parameter --- sasstests.py | 31 +++++++++++++++++++++++++++++-- test/g.scss | 9 +++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 test/g.scss diff --git a/sasstests.py b/sasstests.py index e86f6f9c..b3f11aff 100644 --- a/sasstests.py +++ b/sasstests.py @@ -103,6 +103,20 @@ def normalize_path(path): color: red; } ''' +G_EXPECTED_CSS = '''\ +body { + font: 100% Helvetica, sans-serif; + color: #333; + height: 1.42857; } +''' + +G_EXPECTED_CSS_WITH_PRECISION_8 = '''\ +body { + font: 100% Helvetica, sans-serif; + color: #333; + height: 1.42857143; } +''' + SUBDIR_RECUR_EXPECTED_CSS = '''\ body p { color: blue; } @@ -304,6 +318,12 @@ def test_compile_source_map_deprecated_source_comments_map(self): self.assertEqual(expected, actual) self.assert_source_map_equal(expected_map, actual_map) + def test_compile_with_precision(self): + actual = sass.compile(filename='test/g.scss') + assert actual == G_EXPECTED_CSS + actual = sass.compile(filename='test/g.scss', precision=8) + assert actual == G_EXPECTED_CSS_WITH_PRECISION_8 + def test_regression_issue_2(self): actual = sass.compile(string=''' @media (min-width: 980px) { @@ -340,7 +360,7 @@ def tearDown(self): def test_builder_build_directory(self): css_path = self.css_path result_files = build_directory(self.sass_path, css_path) - self.assertEqual(6, len(result_files)) + self.assertEqual(7, len(result_files)) self.assertEqual('a.scss.css', result_files['a.scss']) with open(os.path.join(css_path, 'a.scss.css'), **utf8_if_py3) as f: css = f.read() @@ -365,6 +385,13 @@ def test_builder_build_directory(self): os.path.join('subdir', 'recur.scss.css'), result_files[os.path.join('subdir', 'recur.scss')] ) + with open(os.path.join(css_path, 'g.scss.css'), **utf8_if_py3) as f: + css = f.read() + self.assertEqual(G_EXPECTED_CSS, css) + self.assertEqual( + os.path.join('subdir', 'recur.scss.css'), + result_files[os.path.join('subdir', 'recur.scss')] + ) with open(os.path.join(css_path, 'subdir', 'recur.scss.css'), **utf8_if_py3) as f: css = f.read() @@ -374,7 +401,7 @@ def test_output_style(self): css_path = self.css_path result_files = build_directory(self.sass_path, css_path, output_style='compressed') - self.assertEqual(6, len(result_files)) + self.assertEqual(7, len(result_files)) self.assertEqual('a.scss.css', result_files['a.scss']) with open(os.path.join(css_path, 'a.scss.css'), **utf8_if_py3) as f: css = f.read() diff --git a/test/g.scss b/test/g.scss new file mode 100644 index 00000000..cc2df97f --- /dev/null +++ b/test/g.scss @@ -0,0 +1,9 @@ +$font-stack: Helvetica, sans-serif; +$primary-color: #333; +$variabile: 5 / 3 * 6 / 7; + +body { + font: 100% $font-stack; + color: $primary-color; + height: $variabile; +} From 29998e12cd794a272f762301423d798a016cfad3 Mon Sep 17 00:00:00 2001 From: Andrea Stagi Date: Tue, 6 Jan 2015 22:26:20 +0100 Subject: [PATCH 3/3] Add documentation for "precision" parameter --- sass.py | 6 ++++++ sassc.py | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/sass.py b/sass.py index e021b295..67262881 100644 --- a/sass.py +++ b/sass.py @@ -73,6 +73,8 @@ def compile(**kwargs): :type include_paths: :class:`collections.Sequence`, :class:`str` :param image_path: an optional path to find images :type image_path: :class:`str` + :param precision: optional precision for numbers. :const:`5` by default. + :type precision: :class:`int` :returns: the compiled CSS string :rtype: :class:`str` :raises sass.CompileError: when it fails for any reason @@ -102,6 +104,8 @@ def compile(**kwargs): :type include_paths: :class:`collections.Sequence`, :class:`str` :param image_path: an optional path to find images :type image_path: :class:`str` + :param precision: optional precision for numbers. :const:`5` by default. + :type precision: :class:`int` :returns: the compiled CSS string, or a pair of the compiled CSS string and the source map string if ``source_comments='map'`` :rtype: :class:`str`, :class:`tuple` @@ -134,6 +138,8 @@ def compile(**kwargs): :type include_paths: :class:`collections.Sequence`, :class:`str` :param image_path: an optional path to find images :type image_path: :class:`str` + :param precision: optional precision for numbers. :const:`5` by default. + :type precision: :class:`int` :raises sass.CompileError: when it fails for any reason (for example the given SASS has broken syntax) diff --git a/sassc.py b/sassc.py index a60dfb98..c3996451 100755 --- a/sassc.py +++ b/sassc.py @@ -40,6 +40,12 @@ .. versionadded:: 0.4.0 +.. option:: -p, --precision + + Set the precision for numbers. Default is 5. + + .. versionadded:: 0.6.3 + .. option:: -v, --version Prints the program version. @@ -88,7 +94,7 @@ def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): help='Watch file for changes. Requires the second ' 'argument (output css filename).') parser.add_option('-p', '--precision', action='store', type="int", default=5, - help='Set number of decimal places. [default: %default]') + help='Set the precision for numbers. [default: %default]') options, args = parser.parse_args(argv[1:]) error = functools.partial(print, parser.get_prog_name() + ': error:',