-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEAT: automatically update settings in
pyproject.toml
(#143)
* FEAT: automatically update `black` configuration * FEAT: automatically update `nbqa.addopts` settings * MAINT: switch to `tomlkit` Just like `ruamel.yaml`, `tomlkit` perserves formatting of the TOML file when dumping it
- Loading branch information
Showing
6 changed files
with
109 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,7 @@ | |
"showcode", | ||
"showtags", | ||
"taplo", | ||
"tomlkit", | ||
"unittests", | ||
"venv" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,46 @@ | ||
"""Tools for loading and inspecting :code:`pyproject.toml`.""" | ||
from collections import OrderedDict | ||
from typing import Optional | ||
"""Tools for loading, inspecting, and updating :code:`pyproject.toml`.""" | ||
import os | ||
from typing import Any, Iterable, Optional | ||
|
||
import toml | ||
import tomlkit | ||
from tomlkit.container import Container | ||
from tomlkit.items import Array, Table | ||
from tomlkit.toml_document import TOMLDocument | ||
|
||
from repoma.utilities import CONFIG_PATH | ||
|
||
|
||
def load_pyproject(content: Optional[str] = None) -> dict: | ||
def load_pyproject(content: Optional[str] = None) -> TOMLDocument: | ||
if not os.path.exists(CONFIG_PATH.pyproject): | ||
return TOMLDocument() | ||
if content is None: | ||
with open(CONFIG_PATH.pyproject) as stream: | ||
return toml.load(stream, _dict=OrderedDict) | ||
return toml.loads(content, _dict=OrderedDict) | ||
return tomlkit.loads(stream.read()) | ||
return tomlkit.loads(content) | ||
|
||
|
||
def get_sub_table(config: Container, dotted_header: str, create: bool = False) -> Table: | ||
"""Get a TOML sub-table through a dotted header key.""" | ||
current_table: Any = config | ||
for header in dotted_header.split("."): | ||
if header not in current_table: | ||
if create: | ||
current_table[header] = tomlkit.table() | ||
else: | ||
raise KeyError(f"TOML data does not contain {dotted_header!r}") | ||
current_table = current_table[header] | ||
return current_table | ||
|
||
|
||
def write_pyproject(config: TOMLDocument) -> None: | ||
src = tomlkit.dumps(config, sort_keys=True) | ||
with open(CONFIG_PATH.pyproject, "w") as stream: | ||
stream.write(src) | ||
|
||
|
||
def to_toml_array(items: Iterable[Any]) -> Array: | ||
array = tomlkit.array() | ||
array.extend(items) | ||
if len(array) > 1: | ||
array.multiline(True) | ||
return array |
Oops, something went wrong.