GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#23)
by
unknown
01:25
created

qtsass.api.watch()   B

Complexity

Conditions 2

Size

Total Lines 26
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
nop 4
1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Copyright (c) 2015 Yann Lanthony
4
# Copyright (c) 2017-2018 Spyder Project Contributors
5
#
6
# Licensed under the terms of the MIT License
7
# (See LICENSE.txt for details)
8
# -----------------------------------------------------------------------------
9
"""qtsass - Compile SCSS files to valid Qt stylesheets."""
10
11
# Standard library imports
12
from __future__ import absolute_import, print_function
13
import logging
14
import os
15
16
# Third party imports
17
import sass
18
from watchdog.observers import Observer
19
20
# Local imports
21
from qtsass.conformers import scss_conform, qt_conform
22
from qtsass.functions import qlineargradient, rgba
23
from qtsass.importers import qss_importer
24
from qtsass.events import SourceEventHandler
25
26
27
logging.basicConfig(level=logging.DEBUG)
28
_log = logging.getLogger(__name__)
0 ignored issues
show
Coding Style Naming introduced by
The name _log does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
29
30
31
def compile(input_file):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in compile.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
32
    """Compile QtSASS to CSS."""
33
34
    _log.debug('Compiling {}...'.format(input_file))
0 ignored issues
show
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
35
36
    with open(input_file, 'r') as f:
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
37
        input_str = f.read()
38
39
    try:
40
        importer_root = os.path.dirname(os.path.abspath(input_file))
41
        return qt_conform(
42
            sass.compile(
43
                string=scss_conform(input_str),
44
                source_comments=False,
45
                custom_functions={
46
                    'qlineargradient': qlineargradient,
47
                    'rgba': rgba
48
                },
49
                importers=[(0, qss_importer(importer_root))]
50
            )
51
        )
52
    except sass.CompileError as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
53
        _log.error('Failed to compile {}:\n{}'.format(input_file, e))
0 ignored issues
show
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
54
    return ""
55
56
57
def compile_filename(input_file, dest_file):
58
    """Compile QtSASS to CSS and save."""
59
60
    css = compile(input_file)
61
    with open(dest_file, 'w') as css_file:
62
        css_file.write(css)
63
        _log.info('Created CSS file {}'.format(dest_file))
0 ignored issues
show
introduced by
Use formatting in logging functions and pass the parameters as arguments
Loading history...
64
65
66
def compile_dirname(input_dir, output_dir):
67
    """Compiles QtSASS files in a directory including subdirectories."""
68
69
    def is_valid(file):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
70
        return not file.startswith('_') and file.endswith('.scss')
71
72
    for root, subdirs, files in os.walk(input_dir):
0 ignored issues
show
Unused Code introduced by
The variable subdirs seems to be unused.
Loading history...
73
        relative_root = os.path.relpath(root, input_dir)
74
        output_root = os.path.join(output_dir, relative_root)
75
76
        for file in [f for f in files if is_valid(f)]:
77
            scss_path = os.path.join(root, file)
78
            css_file = os.path.splitext(file)[0] + '.css'
79
            css_path = os.path.join(output_root, css_file)
80
            if not os.path.isdir(output_root):
81
                os.makedirs(output_root)
82
            compile_filename(scss_path, css_path)
83
84
85
def watch(source, destination, compiler=None, recursive=True):
86
    """
87
    Watches a source file or directory, compiling QtSass files when modified.
88
89
    The compiler function defaults to compile_filename when source is a file
90
    and compile_dirname when source is a directory.
91
92
    :param source: Path to source QtSass file or directory.
93
    :param destination: Path to output css file or directory.
94
    :param compiler: Compile function (optional)
95
    :param recursive: If True, watch subdirectories (default: True).
96
    :returns: watchdog.Observer
97
    """
98
99
    if os.path.isfile(source):
100
        watch_dir = os.path.dirname(source)
101
        compiler = compiler or compile_filename
102
    else:
103
        watch_dir = source
104
        compiler = compiler or compile_dirname
105
106
    event_handler = SourceEventHandler(source, destination, compiler)
107
108
    observer = Observer()
109
    observer.schedule(event_handler, watch_dir, recursive=recursive)
110
    return observer
111