Skip to content

Commit

Permalink
feat: add beet.contrib.format_json plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jun 10, 2021
1 parent 68cf41f commit 4d579be
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 5 deletions.
55 changes: 55 additions & 0 deletions beet/contrib/format_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""Plugin that formats json files."""


__all__ = [
"format_json",
]


import json
from typing import Any, Callable, cast

from beet import Context, JsonFileBase, Plugin
from beet.core.utils import JsonDict


def beet_default(ctx: Context):
config = ctx.meta.get("format_json", cast(JsonDict, {}))

ensure_ascii = config.get("ensure_ascii", True)
allow_nan = config.get("allow_nan", True)
indent = config.get("indent", 2)
separators = config.get("separators")
sort_keys = config.get("sort_keys", False)
final_newline = config.get("final_newline", True)

if separators:
separators = (separators[0], separators[1])

suffix = "\n" if final_newline else ""

ctx.require(
format_json(
lambda content: json.dumps(
content,
ensure_ascii=ensure_ascii,
allow_nan=allow_nan,
indent=indent,
separators=separators,
sort_keys=sort_keys,
)
+ suffix
)
)


def format_json(formatter: Callable[[Any], str]) -> Plugin:
"""Return a plugin that formats json files with the given formatter."""

def plugin(ctx: Context):
for pack in ctx.packs:
for _, json_file in pack.list_files(extend=JsonFileBase[Any]):
json_file.serializer = formatter
json_file.text = formatter(json_file.data)

return plugin
8 changes: 3 additions & 5 deletions beet/contrib/minify_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@


import json
from typing import Any

from beet import Context, JsonFileBase
from beet import Context
from beet.contrib.format_json import format_json


def beet_default(ctx: Context):
for pack in ctx.packs:
for _, json_file in pack.list_files(extend=JsonFileBase[Any]):
json_file.text = json.dumps(json_file.data, separators=(",", ":"))
ctx.require(format_json(lambda content: json.dumps(content, separators=(",", ":"))))
3 changes: 3 additions & 0 deletions examples/code_minify_json/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pipeline": ["demo", "beet.contrib.minify_json"]
}
5 changes: 5 additions & 0 deletions examples/code_minify_json/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from beet import Context, Function


def beet_default(ctx: Context):
ctx.data["demo:foo"] = Function(["say hello"], tags=["minecraft:load"])
12 changes: 12 additions & 0 deletions examples/load_format_json/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"resource_pack": {
"load": ["src"]
},
"pipeline": ["beet.contrib.format_json"],
"meta": {
"format_json": {
"indent": 4,
"sort_keys": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"zzz": "0",
"something": "A",
"cool": "B",
"other": "C"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
say hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"values":["demo:foo"]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pack":{"pack_format":6,"description":""}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"pack":{"pack_format":6,"description":""}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"description": "",
"pack_format": 6
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cool": "B",
"other": "C",
"something": "A",
"zzz": "0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"description": "",
"pack_format": 6
}
}

0 comments on commit 4d579be

Please sign in to comment.