Example #1
0
    def import_and_conform_file(import_file):

        real_import_file = find_file(import_file)
        with open(real_import_file, 'r') as f:
            import_str = f.read()

        return [(import_file, scss_conform(import_str))]
Example #2
0
    def import_and_conform_file(import_file):
        """Return base file and conformed scss file."""
        real_import_file = find_file(import_file)
        with open(real_import_file, 'r') as f:
            import_str = f.read()

        return [(import_file, scss_conform(import_str))]
Example #3
0
def compile(string, **kwargs):
    """
    Conform and Compile QtSASS source code to CSS.

    This function conforms QtSASS to valid SCSS before passing it to
    sass.compile. Any keyword arguments you provide will be combined with
    qtsass's default keyword arguments and passed to sass.compile.

    .. code-block:: python

        >>> import qtsass
        >>> qtsass.compile("QWidget {background: rgb(0, 0, 0);}")
        QWidget {background:black;}

    :param string: QtSASS source code to conform and compile.
    :param kwargs: Keyword arguments to pass to sass.compile
    :returns: CSS string
    """

    kwargs.setdefault('source_comments', DEFAULT_SOURCE_COMMENTS)
    kwargs.setdefault('custom_functions', [])
    kwargs.setdefault('importers', [])
    kwargs.setdefault('include_paths', [])

    # Add QtSass importers
    if isinstance(kwargs['importers'], Sequence):
        kwargs['importers'] = (list(kwargs['importers']) +
                               [(0, qss_importer(*kwargs['include_paths']))])
    else:
        raise ValueError('Expected Sequence for importers '
                         'got {}'.format(type(kwargs['importers'])))

    # Add QtSass custom_functions
    if isinstance(kwargs['custom_functions'], Sequence):
        kwargs['custom_functions'] = dict(
            DEFAULT_CUSTOM_FUNCTIONS,
            **{fn.__name__: fn
               for fn in kwargs['custom_functions']})
    elif isinstance(kwargs['custom_functions'], Mapping):
        kwargs['custom_functions'].update(DEFAULT_CUSTOM_FUNCTIONS)
    else:
        raise ValueError('Expected Sequence or Mapping for custom_functions '
                         'got {}'.format(type(kwargs['custom_functions'])))

    # Conform QtSass source code
    try:
        kwargs['string'] = scss_conform(string)
    except Exception:
        _log.error('Failed to conform source code')
        raise

    # Compile QtSass source code
    try:
        return qt_conform(sass.compile(**kwargs))
    except sass.CompileError:
        _log.error('Failed to compile source code')
        raise
Example #4
0
def compile(input_file):
    """Compile QtSASS to CSS."""

    _log.debug('Compiling {}...'.format(input_file))

    with open(input_file, 'r') as f:
        input_str = f.read()

    try:
        importer_root = os.path.dirname(os.path.abspath(input_file))
        return qt_conform(
            sass.compile(string=scss_conform(input_str),
                         source_comments=False,
                         custom_functions={
                             'qlineargradient': qlineargradient,
                             'rgba': rgba
                         },
                         importers=[(0, qss_importer(importer_root))]))
    except sass.CompileError as e:
        _log.error('Failed to compile {}:\n{}'.format(input_file, e))
    return ""