Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Made compile_filename file output optional #70

Merged
merged 3 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,38 +148,26 @@ Arguments:
Returns:
- Qt compliant CSS string

### `compile_filename(input_file, dest_file, **kwargs)`:
### `compile_filename(input_file, output_file=None, **kwargs)`:

Compile and save QtSASS file as Qt compliant CSS.
Compile and return a QtSASS file as Qt compliant CSS. Optionally save to a file.

Examples:

```bash
>>> import qtsass
>>> qtsass.compile_filename('dummy.scss', 'dummy.css')
```

Arguments:
- input_file: Path to QtSass file.
- dest_file: Path to destination Qt compliant CSS file.
- kwargs: Keyword arguments to pass to sass.compile

### `compile_filename(input_file, output_file, **kwargs)`:

Compile and save QtSASS file as Qt compliant CSS.

Examples:

```bash
>>> import qtsass
>>> qtsass.compile_filename('dummy.scss', 'dummy.css')
>>> qtsass.compile_filename("dummy.scss", "dummy.css")
>>> css = qtsass.compile_filename("dummy.scss")
```

Arguments:
- input_file: Path to QtSass file.
- output_file: Path to write Qt compliant CSS.
- kwargs: Keyword arguments to pass to sass.compile

Returns:
- Qt compliant CSS string

### `compile_dirname(input_dir, output_dir, **kwargs)`:

Compiles QtSASS files in a directory including subdirectories.
Expand Down
22 changes: 13 additions & 9 deletions qtsass/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ def compile(string, **kwargs):
raise


def compile_filename(input_file, output_file, **kwargs):
"""Compile and save QtSASS file as CSS.
def compile_filename(input_file, output_file=None, **kwargs):
"""Compile and return a QtSASS file as Qt compliant CSS.
Optionally save to a file.

.. code-block:: python

>>> import qtsass
>>> qtsass.compile_filename("dummy.scss", "dummy.css")
>>> css = qtsass.compile_filename("dummy.scss")

:param input_file: Path to QtSass file.
:param output_file: Path to write Qt compliant CSS.
:param output_file: Optional path to write Qt compliant CSS.
:param kwargs: Keyword arguments to pass to sass.compile
:returns: CSS string
"""
Expand All @@ -128,13 +130,15 @@ def compile_filename(input_file, output_file, **kwargs):
_log.info('Compiling {}...'.format(os.path.normpath(input_file)))
css = compile(string, **kwargs)

output_root = os.path.abspath(os.path.dirname(output_file))
if not os.path.isdir(output_root):
os.makedirs(output_root)
if output_file is not None:
output_root = os.path.abspath(os.path.dirname(output_file))
if not os.path.isdir(output_root):
os.makedirs(output_root)

with open(output_file, 'w') as css_file:
css_file.write(css)
_log.info('Created CSS file {}'.format(os.path.normpath(output_file)))
with open(output_file, 'w') as css_file:
css_file.write(css)
_log.info('Created CSS file {}'.format(
os.path.normpath(output_file)))

return css

Expand Down
14 changes: 14 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ def test_compile_filename(tmpdir):
assert exists(output.strpath)


def test_compile_filename_no_save():
"""compile_filename simple."""

qss = qtsass.compile_filename(example('dummy.scss'))
assert isinstance(qss, str)


def test_compile_filename_imports(tmpdir):
"""compile_filename with imports."""

Expand All @@ -146,6 +153,13 @@ def test_compile_filename_imports(tmpdir):
assert exists(output.strpath)


def test_compile_filename_imports_no_save():
"""compile_filename with imports."""

qss = qtsass.compile_filename(example('complex', 'dark.scss'))
assert isinstance(qss, str)


def test_compile_dirname(tmpdir):
"""compile_dirname complex."""

Expand Down