Skip to content

Commit c8f293a

Browse files
authored
Merge pull request #11 from domdfcoding/1.8.0-dev
Add new features for v1.9.0
2 parents 16d2393 + 01c4df8 commit c8f293a

File tree

9 files changed

+149
-38
lines changed

9 files changed

+149
-38
lines changed

.github/workflows/mypy.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# This file is managed by 'repo_helper'. Don't edit it directly.
2+
---
3+
name: mypy
4+
5+
on:
6+
push:
7+
pull_request:
8+
branches: ["master"]
9+
10+
jobs:
11+
Run:
12+
name: "mypy"
13+
runs-on: ${{ matrix.os }}
14+
15+
strategy:
16+
matrix:
17+
os: [ubuntu-latest, windows-latest, macos-latest]
18+
19+
steps:
20+
- name: Checkout 🛎️
21+
uses: "actions/checkout@v2"
22+
23+
- name: Setup Python 🐍
24+
uses: "actions/setup-python@v2"
25+
with:
26+
python-version: "3.8"
27+
28+
- name: Install dependencies 🔧
29+
run: |
30+
python -VV
31+
python -m site
32+
python -m pip install --upgrade pip setuptools wheel
33+
python -m pip install tox
34+
35+
- name: "Run mypy"
36+
run: "python -m tox -e mypy"

.github/workflows/python_ci.yml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ on:
99

1010
jobs:
1111
tests:
12-
name: "Python ${{ matrix.python-version }}"
12+
name: "Python ${{ matrix.config.python-version }}"
1313
runs-on: "windows-2019"
1414
env:
1515
USING_COVERAGE: '3.6,3.7,3.8,3.9'
1616

1717
strategy:
1818
fail-fast: False
1919
matrix:
20-
python-version: ["3.6","3.7","3.8","3.9"]
20+
config:
21+
- {python-version: "3.6", testenvs: "py36,build"}
22+
- {python-version: "3.7", testenvs: "py37,build"}
23+
- {python-version: "3.8", testenvs: "py38,build"}
24+
- {python-version: "3.9", testenvs: "py39,build"}
2125

2226
steps:
2327
- name: Checkout 🛎️
@@ -26,14 +30,14 @@ jobs:
2630
- name: Setup Python 🐍
2731
uses: "actions/setup-python@v2"
2832
with:
29-
python-version: "${{ matrix.python-version }}"
33+
python-version: "${{ matrix.config.python-version }}"
3034

3135
- name: Install dependencies 🔧
3236
run: |
3337
python -VV
3438
python -m site
3539
python -m pip install --upgrade pip setuptools wheel
36-
python -m pip install --upgrade tox tox-gh-actions virtualenv
40+
python -m pip install --upgrade tox virtualenv
3741
38-
- name: "Run Tests for Python ${{ matrix.python-version }}"
39-
run: "python -m tox"
42+
- name: "Run Tests for Python ${{ matrix.config.python-version }}"
43+
run: "python -m tox -e ${{ matrix.config.testenvs }}"

.github/workflows/python_ci_linux.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ on:
99

1010
jobs:
1111
tests:
12-
name: "Python ${{ matrix.python-version }}"
12+
name: "Python ${{ matrix.config.python-version }}"
1313
runs-on: "ubuntu-18.04"
1414
env:
1515
USING_COVERAGE: '3.6,3.7,3.8,pypy3,3.9'
1616

1717
strategy:
1818
fail-fast: False
1919
matrix:
20-
python-version: ["3.6","3.7","3.8","pypy3","3.9"]
20+
config:
21+
- {python-version: "3.6", testenvs: "py36,build"}
22+
- {python-version: "3.7", testenvs: "py37,build"}
23+
- {python-version: "3.8", testenvs: "py38,build"}
24+
- {python-version: "pypy3", testenvs: "pypy3,build"}
25+
- {python-version: "3.9", testenvs: "py39,build"}
2126

2227
steps:
2328
- name: Checkout 🛎️
@@ -26,24 +31,24 @@ jobs:
2631
- name: Setup Python 🐍
2732
uses: "actions/setup-python@v2"
2833
with:
29-
python-version: "${{ matrix.python-version }}"
34+
python-version: "${{ matrix.config.python-version }}"
3035

3136
- name: Install dependencies 🔧
3237
run: |
3338
python -VV
3439
python -m site
3540
python -m pip install --upgrade pip setuptools wheel
36-
python -m pip install --upgrade tox tox-gh-actions virtualenv
41+
python -m pip install --upgrade tox virtualenv
3742
python -m pip install --upgrade coverage_pyver_pragma
3843
39-
- name: "Run Tests for Python ${{ matrix.python-version }}"
40-
run: "python -m tox"
44+
- name: "Run Tests for Python ${{ matrix.config.python-version }}"
45+
run: "python -m tox -e ${{ matrix.config.testenvs }}"
4146

4247

4348
- name: "Upload Coverage"
4449
uses: actions/upload-artifact@v2
4550
with:
46-
name: "coverage-${{ matrix.python-version }}"
51+
name: "coverage-${{ matrix.config.python-version }}"
4752
path: .coverage
4853

4954

.github/workflows/python_ci_macos.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ on:
99

1010
jobs:
1111
tests:
12-
name: "Python ${{ matrix.python-version }}"
12+
name: "Python ${{ matrix.config.python-version }}"
1313
runs-on: "macos-latest"
1414
env:
1515
USING_COVERAGE: '3.6,3.7,3.8,pypy3,3.9'
1616

1717
strategy:
1818
fail-fast: False
1919
matrix:
20-
python-version: ["3.6","3.7","3.8","pypy3","3.9"]
20+
config:
21+
- {python-version: "3.6", testenvs: "py36,build"}
22+
- {python-version: "3.7", testenvs: "py37,build"}
23+
- {python-version: "3.8", testenvs: "py38,build"}
24+
- {python-version: "pypy3", testenvs: "pypy3,build"}
25+
- {python-version: "3.9", testenvs: "py39,build"}
2126

2227
steps:
2328
- name: Checkout 🛎️
@@ -26,14 +31,14 @@ jobs:
2631
- name: Setup Python 🐍
2732
uses: "actions/setup-python@v2"
2833
with:
29-
python-version: "${{ matrix.python-version }}"
34+
python-version: "${{ matrix.config.python-version }}"
3035

3136
- name: Install dependencies 🔧
3237
run: |
3338
python -VV
3439
python -m site
3540
python -m pip install --upgrade pip setuptools wheel
36-
python -m pip install --upgrade tox tox-gh-actions virtualenv
41+
python -m pip install --upgrade tox virtualenv
3742
38-
- name: "Run Tests for Python ${{ matrix.python-version }}"
39-
run: "python -m tox"
43+
- name: "Run Tests for Python ${{ matrix.config.python-version }}"
44+
run: "python -m tox -e ${{ matrix.config.testenvs }}"

domdf_python_tools/paths.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
# stdlib
4343
import contextlib
44+
import gzip
4445
import json
4546
import os
4647
import pathlib
@@ -543,17 +544,20 @@ def dump_json(
543544
encoding: Optional[str] = "UTF-8",
544545
errors: Optional[str] = None,
545546
json_library: JsonLibrary = json, # type: ignore
547+
*,
548+
compress: bool = False,
546549
**kwargs,
547550
) -> None:
548-
"""
551+
r"""
549552
Dump ``data`` to the file as JSON.
550553
551554
:param data: The object to serialise to JSON.
552555
:param encoding: The encoding to write to the file in.
553556
:param errors:
554557
:param json_library: The JSON serialisation library to use.
555558
:default json_library: :mod:`json`
556-
:param kwargs: Keyword arguments to pass to the JSON serialisation function.
559+
:param compress: Whether to compress the JSON file using gzip.
560+
:param \*\*kwargs: Keyword arguments to pass to the JSON serialisation function.
557561
558562
.. versionadded:: 0.5.0
559563
@@ -562,37 +566,60 @@ def dump_json(
562566
Now uses :meth:`PathPlus.write_clean <domdf_python_tools.paths.PathPlus.write_clean>`
563567
rather than :meth:`PathPlus.write_text <domdf_python_tools.paths.PathPlus.write_text>`,
564568
and returns :py:obj:`None` rather than :class:`int`.
569+
570+
.. versionchanged:: 1.9.0
571+
572+
Added the ``compress`` keyword-only argument.
565573
"""
566574

567-
return self.write_clean(
568-
json_library.dumps(data, **kwargs),
569-
encoding=encoding,
570-
errors=errors,
571-
)
575+
if compress:
576+
with gzip.open(self, mode="wt", encoding=encoding, errors=errors) as fp:
577+
fp.write(json_library.dumps(data, **kwargs))
578+
579+
else:
580+
self.write_clean(
581+
json_library.dumps(data, **kwargs),
582+
encoding=encoding,
583+
errors=errors,
584+
)
572585

573586
def load_json(
574587
self,
575588
encoding: Optional[str] = "UTF-8",
576589
errors: Optional[str] = None,
577590
json_library: JsonLibrary = json, # type: ignore
591+
*,
592+
decompress: bool = False,
578593
**kwargs,
579594
) -> Any:
580-
"""
595+
r"""
581596
Load JSON data from the file.
582597
583598
:param encoding: The encoding to write to the file in.
584599
:param errors:
585600
:param json_library: The JSON serialisation library to use.
586601
:default json_library: :mod:`json`
587-
:param kwargs: Keyword arguments to pass to the JSON deserialisation function.
602+
:param decompress: Whether to decompress the JSON file using gzip.
603+
Will raise an exception if the file is not compressed.
604+
:param \*\*kwargs: Keyword arguments to pass to the JSON deserialisation function.
588605
589606
:return: The deserialised JSON data.
590607
591608
.. versionadded:: 0.5.0
609+
610+
.. versionchanged:: 1.9.0
611+
612+
Added the ``compress`` keyword-only argument.
592613
"""
593614

615+
if decompress:
616+
with gzip.open(self, mode="rt", encoding=encoding, errors=errors) as fp:
617+
content = fp.read()
618+
else:
619+
content = self.read_text(encoding=encoding, errors=errors)
620+
594621
return json_library.loads(
595-
self.read_text(encoding=encoding, errors=errors),
622+
content,
596623
**kwargs,
597624
)
598625

domdf_python_tools/terminal_colours.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@
6666

6767
# stdlib
6868
import re
69+
import warnings
6970
from abc import ABC
7071
from typing import List, Pattern
7172

7273
# 3rd party
73-
from colorama import init # type: ignore
74+
import colorama # type: ignore
7475
from typing_extensions import Final
7576

7677
__all__ = [
@@ -94,7 +95,14 @@
9495
"strip_ansi",
9596
]
9697

97-
init()
98+
warnings.warn(
99+
"domdf_python_tools.terminal_colours is deprecated and will be removed in v2. "
100+
"Use consolekit.terminal_colours instead.",
101+
DeprecationWarning,
102+
stacklevel=2,
103+
)
104+
105+
colorama.init()
98106

99107
CSI: Final[str] = "\u001b["
100108
OSC: Final[str] = "\u001b]"

domdf_python_tools/utils.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
:func:`~domdf_python_tools.iterative.Len`, and
2323
:func:`~domdf_python_tools.iterative.double_chain`
2424
moved to :func:`domdf_python_tools.iterative`.
25-
2625
They can still be imported from here until version 2.0.0, but that use is deprecated.
2726
"""
2827
#
@@ -93,13 +92,15 @@
9392
# this package
9493
import domdf_python_tools.words
9594
from domdf_python_tools import __version__, iterative
96-
from domdf_python_tools.terminal_colours import Colour, Fore
9795
from domdf_python_tools.typing import HasHead, String
9896

9997
if typing.TYPE_CHECKING or domdf_python_tools.__docs: # pragma: no cover
10098
# 3rd party
10199
from pandas import DataFrame, Series # type: ignore
102100

101+
# this package
102+
from domdf_python_tools.terminal_colours import Colour, Fore
103+
103104
Series.__module__ = "pandas"
104105
DataFrame.__module__ = "pandas"
105106

@@ -256,6 +257,8 @@ def posargs2kwargs(
256257
"""
257258
Convert the positional args in ``args`` to kwargs, based on the relative order of ``args`` and ``posarg_names``.
258259
260+
.. important:: Python 3.8's Positional-Only Parameters (:pep:`570`) are not supported.
261+
259262
:param args: List of positional arguments provided to a function.
260263
:param posarg_names: Either a list of positional argument names for the function, or the function object.
261264
:param kwargs: Optional mapping of keyword argument names to values.
@@ -275,6 +278,8 @@ def posargs2kwargs(
275278

276279
kwargs.update(zip(posarg_names, args))
277280

281+
# TODO: positional only arguments
282+
278283
return kwargs
279284

280285

@@ -623,8 +628,8 @@ def coloured_diff(
623628
tofiledate: str = '',
624629
n: int = 3,
625630
lineterm: str = '\n',
626-
removed_colour: Colour = Fore.RED,
627-
added_colour: Colour = Fore.GREEN,
631+
removed_colour: Optional["Colour"] = None,
632+
added_colour: Optional["Colour"] = None,
628633
) -> str:
629634
r"""
630635
Compare two sequences of lines; generate the delta as a unified diff.
@@ -679,6 +684,15 @@ def coloured_diff(
679684
# this package
680685
from domdf_python_tools.stringlist import StringList
681686

687+
with warnings.catch_warnings():
688+
warnings.simplefilter("ignore", DeprecationWarning)
689+
690+
# this package
691+
from domdf_python_tools.terminal_colours import Fore
692+
693+
removed_colour = removed_colour or Fore.RED
694+
added_colour = added_colour or Fore.GREEN
695+
682696
buf = StringList()
683697
diff = difflib.unified_diff(a, b, fromfile, tofile, fromfiledate, tofiledate, n, lineterm)
684698

tests/test_paths.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,18 @@ def test_dump_json(tmpdir):
580580
""")
581581

582582

583+
def test_dump_json_gzip(tmpdir):
584+
tmpdir_p = PathPlus(tmpdir)
585+
586+
tmp_file = tmpdir_p / "test.txt"
587+
588+
tmp_file.dump_json({"key": "value", "int": 1234, "float": 12.34}, compress=True)
589+
assert tmp_file.load_json(decompress=True) == {"key": "value", "int": 1234, "float": 12.34}
590+
591+
tmp_file.dump_json({"key": "value", "int": 1234, "float": 12.34}, indent=2, compress=True)
592+
assert tmp_file.load_json(decompress=True) == {"key": "value", "int": 1234, "float": 12.34}
593+
594+
583595
def test_load_json(tmpdir):
584596
tmpdir_p = PathPlus(tmpdir)
585597

0 commit comments

Comments
 (0)