Skip to content

Commit

Permalink
Simplify code a bit
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat committed Jan 30, 2023
1 parent 07dc827 commit ef2647c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
29 changes: 5 additions & 24 deletions src/pyproject_fmt/formatter/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Optional, cast

from tomlkit.container import OutOfOrderTableProxy
from tomlkit.items import Array, String, Table
from tomlkit.toml_document import TOMLDocument

Expand All @@ -15,6 +16,8 @@ def fmt_project(parsed: TOMLDocument, conf: Config) -> None:
if project is None:
return

key_order = ["name", "version", "description", "readme", "keywords", "license", "license-files"]
key_order.extend(["maintainers", "authors", "requires-python", "classifiers", "dynamic", "dependencies"])
if "name" in project: # normalize names to underscore so sdist / wheel have the same prefix
name = project["name"]
assert isinstance(name, str)
Expand Down Expand Up @@ -49,30 +52,8 @@ def fmt_project(parsed: TOMLDocument, conf: Config) -> None:
# order maintainers and authors table
# update classifiers depending on requires
# handle readme table

order_keys(
project,
(
"name",
"version",
"description",
"readme",
"keywords",
"license",
"license-files",
"maintainers",
"authors",
"requires-python",
"dependencies",
"dynamic",
"classifiers",
"optional-dependencies",
"urls",
"scripts",
"gui-scripts",
"entry-points",
),
)
key_order.extend(["optional-dependencies", "urls", "scripts", "gui-scripts", "entry-points"])
order_keys(project, key_order)


__all__ = [
Expand Down
5 changes: 4 additions & 1 deletion src/pyproject_fmt/formatter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass, field
from typing import Any, Callable, Sequence

from tomlkit.container import OutOfOrderTableProxy
from tomlkit.items import (
AbstractTable,
Array,
Expand Down Expand Up @@ -34,10 +35,12 @@ def __gt__(self, __other: Any) -> bool: # noqa: U101


def order_keys(
table: AbstractTable,
table: AbstractTable | OutOfOrderTableProxy,
to_pin: Sequence[str] | None = None,
sort_key: None | Callable[[tuple[str, tuple[Key, Item]]], SupportsDunderLT | SupportsDunderGT] = None,
) -> None:
if isinstance(table, OutOfOrderTableProxy):
return # pragma: no cover
body = table.value.body
entries = {i.key: (i, v) for (i, v) in body if isinstance(i, Key)}
body.clear()
Expand Down
11 changes: 10 additions & 1 deletion whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ autoclass
autodoc
capsys
chdir
conf
configs
deps
difflib
dunder
extlinks
fmt
formatter
fromfile
func
intersphinx
iread
iwrite
Expand All @@ -15,10 +22,12 @@ pygments
pyproject
raws
readouterr
req
sdist
skipif
tmp
tofile
toml
tomlkit
tomls
typehints
util

0 comments on commit ef2647c

Please sign in to comment.