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

Use tomlkit for TOML read/write operations #238

Merged
merged 2 commits into from
Oct 26, 2024
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
15 changes: 7 additions & 8 deletions config/config-package.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from shared.git import git_branch
from shared.git import git_server_url
from shared.path import change_dir
from shared.toml_encoder import TomlArraySeparatorEncoderWithNewline

import argparse
import collections
Expand All @@ -16,7 +15,7 @@
import pathlib
import re
import shutil
import toml
import tomlkit
import validate_pyproject
import yaml

Expand Down Expand Up @@ -144,7 +143,8 @@ def _read_meta_configuration(self):
"""Read and update meta configuration"""
meta_toml_path = self.path / '.meta.toml'
if meta_toml_path.exists():
meta_cfg = toml.load(meta_toml_path)
with open(meta_toml_path, 'rb') as meta_f:
meta_cfg = tomlkit.load(meta_f)
meta_cfg = collections.defaultdict(dict, **meta_cfg)
else:
meta_cfg = collections.defaultdict(dict)
Expand Down Expand Up @@ -519,10 +519,7 @@ def remove_toml_empty_sections(self):
with open('.meta.toml', 'w') as meta_f:
meta_f.write(META_HINT.format(config_type=self.config_type))
meta_f.write('\n')
toml.dump(
meta_cfg, meta_f,
TomlArraySeparatorEncoderWithNewline(
separator=',\n ', indent_first_line=True))
tomlkit.dump(meta_cfg, meta_f)

def run_tox(self):
with change_dir(self.path) as cwd:
Expand All @@ -544,7 +541,9 @@ def validate_files(self, files_changed):
def _validate_toml(self, file_obj):
"""Validate files that are in TOML format"""
with change_dir(self.path):
data = toml.load(file_obj)

with open(file_obj, 'rb') as meta_f:
data = tomlkit.load(meta_f)

if self.path.stem == 'pyproject':
validator = validate_pyproject.api.Validator()
Expand Down
5 changes: 3 additions & 2 deletions config/drop-legacy-python.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pathlib
import shutil
import sys
import toml
import tomlkit


parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -50,7 +50,8 @@
with change_dir(path) as cwd_str:
cwd = pathlib.Path(cwd_str)
bin_dir = cwd / 'bin'
meta_cfg = collections.defaultdict(dict, **toml.load('.meta.toml'))
with open('.meta.toml', 'rb') as meta_f:
meta_cfg = collections.defaultdict(dict, **tomlkit.load(meta_f))
config_type = meta_cfg['meta']['template']
branch_name = get_branch_name(args.branch_name, config_type)
updating = git_branch(branch_name)
Expand Down
2 changes: 1 addition & 1 deletion config/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
check-python-versions==0.22.0
Jinja2==3.1.4
pyupgrade==3.15.2
toml==0.10.2
tomlkit==0.13.2
tox==4.15.0
zest.releaser==9.1.3
towncrier==23.11.0
Expand Down
44 changes: 0 additions & 44 deletions config/shared/toml_encoder.py

This file was deleted.

3 changes: 3 additions & 0 deletions news/229.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Use tomlkit for TOML read/write operations
it allow custom regexes with backslahes in meta.toml
it is a port of https://github.com/zopefoundation/meta/pull/215