|
| 1 | +#!/usr/bin/env python |
| 2 | +r""":mod:`pysassc` --- SassC compliant command line interface |
| 3 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | +This provides SassC_ compliant CLI executable named :program:`pysassc`: |
| 6 | +
|
| 7 | +.. sourcecode:: console |
| 8 | +
|
| 9 | + $ pysassc |
| 10 | + Usage: pysassc [options] SCSS_FILE [CSS_FILE] |
| 11 | +
|
| 12 | +There are options as well: |
| 13 | +
|
| 14 | +.. option:: -t <style>, --style <style> |
| 15 | +
|
| 16 | + Coding style of the compiled result. The same as :func:`sass.compile()` |
| 17 | + function's ``output_style`` keyword argument. Default is ``nested``. |
| 18 | +
|
| 19 | +.. option:: -s <style>, --output-style <style> |
| 20 | +
|
| 21 | + Alias for -t / --style. |
| 22 | +
|
| 23 | + .. deprecated:: 0.11.0 |
| 24 | +
|
| 25 | +.. option:: -I <dir>, --include-path <dir> |
| 26 | +
|
| 27 | + Optional directory path to find ``@import``\ ed (S)CSS files. |
| 28 | + Can be multiply used. |
| 29 | +
|
| 30 | +.. option:: -m, -g, --sourcemap |
| 31 | +
|
| 32 | + Emit source map. Requires the second argument (output CSS filename). |
| 33 | + The filename of source map will be the output CSS filename followed by |
| 34 | + :file:`.map`. |
| 35 | +
|
| 36 | + .. versionadded:: 0.4.0 |
| 37 | +
|
| 38 | +.. option:: -p, --precision |
| 39 | +
|
| 40 | + Set the precision for numbers. Default is 5. |
| 41 | +
|
| 42 | + .. versionadded:: 0.7.0 |
| 43 | +
|
| 44 | +.. option:: --source-comments |
| 45 | +
|
| 46 | + Include debug info in output. |
| 47 | +
|
| 48 | + .. versionadded:: 0.11.0 |
| 49 | +
|
| 50 | +.. option:: -v, --version |
| 51 | +
|
| 52 | + Prints the program version. |
| 53 | +
|
| 54 | +.. option:: -h, --help |
| 55 | +
|
| 56 | + Prints the help message. |
| 57 | +
|
| 58 | +.. _SassC: https://github.com/sass/sassc |
| 59 | +
|
| 60 | +""" |
| 61 | +from __future__ import print_function |
| 62 | + |
| 63 | +import functools |
| 64 | +import io |
| 65 | +import optparse |
| 66 | +import sys |
| 67 | + |
| 68 | +import sass |
| 69 | + |
| 70 | + |
| 71 | +def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): |
| 72 | + parser = optparse.OptionParser( |
| 73 | + usage='%prog [options] SCSS_FILE [OUT_CSS_FILE]', |
| 74 | + version='%prog {} (sass/libsass {})'.format( |
| 75 | + sass.__version__, sass.libsass_version, |
| 76 | + ), |
| 77 | + ) |
| 78 | + output_styles = list(sass.OUTPUT_STYLES) |
| 79 | + output_styles = ', '.join(output_styles[:-1]) + ', or ' + output_styles[-1] |
| 80 | + parser.add_option( |
| 81 | + '-t', '--style', '-s', '--output-style', metavar='STYLE', |
| 82 | + type='choice', choices=list(sass.OUTPUT_STYLES), default='nested', |
| 83 | + help=( |
| 84 | + 'Coding style of the compiled result. Choose one of ' + |
| 85 | + output_styles + '. [default: %default]' |
| 86 | + ), |
| 87 | + ) |
| 88 | + parser.add_option( |
| 89 | + '-m', '-g', '--sourcemap', dest='source_map', |
| 90 | + action='store_true', default=False, |
| 91 | + help='Emit source map. Requires the second argument ' |
| 92 | + '(output css filename).', |
| 93 | + ) |
| 94 | + parser.add_option( |
| 95 | + '-I', '--include-path', metavar='DIR', |
| 96 | + dest='include_paths', action='append', |
| 97 | + help='Path to find "@import"ed (S)CSS source files. ' |
| 98 | + 'Can be multiply used.', |
| 99 | + ) |
| 100 | + parser.add_option( |
| 101 | + '-p', '--precision', action='store', type='int', default=5, |
| 102 | + help='Set the precision for numbers. [default: %default]', |
| 103 | + ) |
| 104 | + parser.add_option( |
| 105 | + '--source-comments', action='store_true', default=False, |
| 106 | + help='Include debug info in output', |
| 107 | + ) |
| 108 | + parser.add_option( |
| 109 | + '--import-extensions', |
| 110 | + dest='custom_import_extensions', action='append', |
| 111 | + help='Extra extensions allowed for sass imports. ' |
| 112 | + 'Can be multiply used.', |
| 113 | + ) |
| 114 | + options, args = parser.parse_args(argv[1:]) |
| 115 | + error = functools.partial( |
| 116 | + print, |
| 117 | + parser.get_prog_name() + ': error:', |
| 118 | + file=stderr, |
| 119 | + ) |
| 120 | + if not args: |
| 121 | + parser.print_usage(stderr) |
| 122 | + error('too few arguments') |
| 123 | + return 2 |
| 124 | + elif len(args) > 2: |
| 125 | + parser.print_usage(stderr) |
| 126 | + error('too many arguments') |
| 127 | + return 2 |
| 128 | + filename = args[0] |
| 129 | + if options.source_map and len(args) < 2: |
| 130 | + parser.print_usage(stderr) |
| 131 | + error( |
| 132 | + '-m/-g/--sourcemap requires the second argument, the output ' |
| 133 | + 'css filename.', |
| 134 | + ) |
| 135 | + return 2 |
| 136 | + |
| 137 | + try: |
| 138 | + if options.source_map: |
| 139 | + source_map_filename = args[1] + '.map' # FIXME |
| 140 | + css, source_map = sass.compile( |
| 141 | + filename=filename, |
| 142 | + output_style=options.style, |
| 143 | + source_comments=options.source_comments, |
| 144 | + source_map_filename=source_map_filename, |
| 145 | + output_filename_hint=args[1], |
| 146 | + include_paths=options.include_paths, |
| 147 | + precision=options.precision, |
| 148 | + custom_import_extensions=options.custom_import_extensions, |
| 149 | + ) |
| 150 | + else: |
| 151 | + source_map_filename = None |
| 152 | + source_map = None |
| 153 | + css = sass.compile( |
| 154 | + filename=filename, |
| 155 | + output_style=options.style, |
| 156 | + source_comments=options.source_comments, |
| 157 | + include_paths=options.include_paths, |
| 158 | + precision=options.precision, |
| 159 | + custom_import_extensions=options.custom_import_extensions, |
| 160 | + ) |
| 161 | + except (IOError, OSError) as e: |
| 162 | + error(e) |
| 163 | + return 3 |
| 164 | + except sass.CompileError as e: |
| 165 | + error(e) |
| 166 | + return 1 |
| 167 | + else: |
| 168 | + if len(args) < 2: |
| 169 | + print(css, file=stdout) |
| 170 | + else: |
| 171 | + with io.open(args[1], 'w', encoding='utf-8', newline='') as f: |
| 172 | + f.write(css) |
| 173 | + if source_map_filename: |
| 174 | + with io.open( |
| 175 | + source_map_filename, 'w', encoding='utf-8', newline='', |
| 176 | + ) as f: |
| 177 | + f.write(source_map) |
| 178 | + return 0 |
| 179 | + |
| 180 | + |
| 181 | +if __name__ == '__main__': |
| 182 | + sys.exit(main()) |
0 commit comments