From b6b46a69367770698171281ba341d12cdc838483 Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Mon, 3 Jun 2024 19:07:19 +0900 Subject: [PATCH 1/2] Rewrite scripts in TypeScript --- .github/workflows/CI.yaml | 22 ++++++---------- README.adoc | 23 +++-------------- README.ja.adoc | 21 +++------------- assets/toml/.gitkeep | 0 deno.json | 7 ++++++ deno.json.license | 3 +++ justfile | 53 +++++++++------------------------------ scripts/yaml2json.py | 23 ----------------- scripts/yaml2json.ts | 25 ++++++++++++++++++ scripts/yaml2toml.py | 32 ----------------------- 10 files changed, 62 insertions(+), 147 deletions(-) delete mode 100644 assets/toml/.gitkeep create mode 100644 deno.json create mode 100644 deno.json.license delete mode 100755 scripts/yaml2json.py create mode 100755 scripts/yaml2json.ts delete mode 100755 scripts/yaml2toml.py diff --git a/.github/workflows/CI.yaml b/.github/workflows/CI.yaml index 8866af9..aab2819 100644 --- a/.github/workflows/CI.yaml +++ b/.github/workflows/CI.yaml @@ -40,25 +40,19 @@ jobs: - name: Check no lint warnings run: yamllint -s assets/yaml/*.yaml - python-test: - name: Test Python scripts + deno-test: + name: Test Deno scripts runs-on: ubuntu-22.04 steps: - name: Checkout code uses: actions/checkout@v4 - - name: Setup Python - uses: actions/setup-python@v5 + - name: Setup Deno + uses: denoland/setup-deno@v1.1.4 with: - python-version: "3.12" - cache: "pip" - - name: Install dependencies - run: | - pip install PyYAML mypy ruff tomli-w types-PyYAML - ruff version - mypy -V + deno-version: v1.x - name: Check code formatted - run: ruff format --check . + run: deno fmt --check scripts/*.ts - name: Check no lint warnings - run: ruff check . + run: deno lint scripts/*.ts - name: Type-check - run: mypy --allow-redefinition . + run: deno check scripts/*.ts diff --git a/README.adoc b/README.adoc index e43698a..58f47b3 100644 --- a/README.adoc +++ b/README.adoc @@ -12,8 +12,7 @@ :enwp-article-url: {enwp-url}/wiki/Japanese_era_name :assets-dir: assets :yaml-dir: {assets-dir}/yaml -:pyyaml-url: {github-url}/yaml/pyyaml -:tomli-w-url: {github-url}/hukkin/tomli-w +:deno-url: https://deno.com/ :jawp-url: https://ja.wikipedia.org :jawp-article-url: {jawp-url}/wiki/%E5%85%83%E5%8F%B7%E4%B8%80%E8%A6%A7_(%E6%97%A5%E6%9C%AC) :reuse-spec-url: https://reuse.software/spec/ @@ -125,31 +124,17 @@ link:{yaml-dir}/modern.yaml[]:: === Convert to other formats -This project contains Python scripts for converting YAML files to JSON files or -TOML files. - -IMPORTANT: {pyyaml-url}[_PyYAML_] is required for conversion. +This project contains {deno-url}[Deno] scripts for converting YAML files to +JSON files. .To convert to JSON files [source,sh] ---- -./scripts/yaml2json.py +./scripts/yaml2json.ts ---- This will output JSON files in the `{assets-dir}/json` directory. -.To convert to TOML files -[source,sh] ----- -./scripts/yaml2toml.py ----- - -This will output TOML files in the `{assets-dir}/toml` directory. - -IMPORTANT: {tomli-w-url}[_Tomli-W_] is required for conversion. - -NOTE: Keys with `null` values ​​are removed. - == Acknowledgment .This dataset was created using the following: diff --git a/README.ja.adoc b/README.ja.adoc index 7d5e95e..9b702d2 100644 --- a/README.ja.adoc +++ b/README.ja.adoc @@ -12,8 +12,7 @@ :jawp-article-url: {jawp-url}/wiki/%E5%85%83%E5%8F%B7%E4%B8%80%E8%A6%A7_(%E6%97%A5%E6%9C%AC) :assets-dir: assets :yaml-dir: {assets-dir}/yaml -:pyyaml-url: {github-url}/yaml/pyyaml -:tomli-w-url: {github-url}/hukkin/tomli-w +:deno-url: https://deno.com/ :enwp-url: https://en.wikipedia.org :enwp-article-url: {enwp-url}/wiki/Japanese_era_name :reuse-spec-url: https://reuse.software/spec/ @@ -125,30 +124,16 @@ link:{yaml-dir}/modern.yaml[]:: === 他のフォーマットへの変換 -このプロジェクトにはYAMLファイルをJSONファイルやTOMLファイルに変換するためのPythonスクリプトが含まれています。 - -IMPORTANT: 変換にはlink:{pyyaml-url}[_PyYAML_]が必要です。 +このプロジェクトにはYAMLファイルをJSONファイルに変換するためのlink:{deno-url}[Deno]スクリプトが含まれています。 .JSONファイルへ変換 [source,sh] ---- -./scripts/yaml2json.py +./scripts/yaml2json.ts ---- JSONファイルは``{assets-dir}/json``ディレクトリに出力されます。 -.TOMLファイルへ変換 -[source,sh] ----- -./scripts/yaml2toml.py ----- - -TOMLファイルは``{assets-dir}/toml``ディレクトリに出力されます。 - -IMPORTANT: 変換にはlink:{tomli-w-url}[_Tomli-W_]が必要です。 - -NOTE: 値が``null``のキーは取り除かれます。 - == 謝辞 .このデータセットは以下を参照して作成しました: diff --git a/assets/toml/.gitkeep b/assets/toml/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..58c6c60 --- /dev/null +++ b/deno.json @@ -0,0 +1,7 @@ +{ + "imports": { + "@std/path": "jsr:@std/path@^0.225.2", + "@std/yaml": "jsr:@std/yaml@^0.224.1" + }, + "lock": false +} diff --git a/deno.json.license b/deno.json.license new file mode 100644 index 0000000..fe27c4f --- /dev/null +++ b/deno.json.license @@ -0,0 +1,3 @@ +SPDX-FileCopyrightText: None + +SPDX-License-Identifier: CC0-1.0 diff --git a/justfile b/justfile index 4c814bd..7ac1783 100644 --- a/justfile +++ b/justfile @@ -12,53 +12,24 @@ default: fmt npx prettier -w assets/yaml/*.yaml # Run the linter -lint: - #!/usr/bin/env bash - source venv/bin/activate +@lint: yamllint -s assets/yaml/*.yaml -# Configure a development environment for the Python scripts -setup-python: - #!/usr/bin/env bash - python3 -m venv venv - source venv/bin/activate - pip3 install PyYAML mypy ruff tomli-w types-PyYAML yamllint +# Run `deno fmt` +@deno-fmt: + deno fmt scripts/*.ts -# Run the formatter for the Python scripts -python-fmt: - #!/usr/bin/env bash - source venv/bin/activate - ruff format . +# Run `deno lint` +@deno-lint: + deno lint scripts/*.ts -# Run the linter for the Python scripts -python-lint: - #!/usr/bin/env bash - source venv/bin/activate - ruff check . - -# Apply lint suggestions for the Python scripts -python-lint-fix: - #!/usr/bin/env bash - source venv/bin/activate - ruff check --fix . - -# Run `mypy` -python-type-check: - #!/usr/bin/env bash - source venv/bin/activate - mypy --allow-redefinition . +# Run `deno check` +@deno-type-check: + deno check scripts/*.ts # Generate the JSON files -generate-json: - #!/usr/bin/env bash - source venv/bin/activate - python3 scripts/yaml2json.py - -# Generate the TOML files -generate-toml: - #!/usr/bin/env bash - source venv/bin/activate - python3 scripts/yaml2toml.py +@generate-json: + deno run --allow-read --allow-write scripts/yaml2json.ts # Run the linter for GitHub Actions workflow files @lint-github-actions: diff --git a/scripts/yaml2json.py b/scripts/yaml2json.py deleted file mode 100755 index c04e519..0000000 --- a/scripts/yaml2json.py +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python3 - -# SPDX-FileCopyrightText: None -# -# SPDX-License-Identifier: CC0-1.0 - -import json -from pathlib import Path -from typing import Final - -import yaml - -ASSETS_DIR: Final[Path] = Path(__file__).resolve().parent.parent / "assets" -YAML_DIR: Final[Path] = ASSETS_DIR / "yaml" -JSON_DIR: Final[Path] = ASSETS_DIR / "json" - -for yaml_file in YAML_DIR.iterdir(): - with yaml_file.open("rb") as f: - data = yaml.safe_load(f) - - json_file = JSON_DIR / (yaml_file.stem + ".json") - with json_file.open("w") as f: - json.dump(data, f, indent=2, default=str) diff --git a/scripts/yaml2json.ts b/scripts/yaml2json.ts new file mode 100755 index 0000000..e0acf49 --- /dev/null +++ b/scripts/yaml2json.ts @@ -0,0 +1,25 @@ +#!/usr/bin/env -S deno run --allow-read --allow-write + +// SPDX-FileCopyrightText: None +// +// SPDX-License-Identifier: CC0-1.0 + +import * as path from "@std/path"; +import * as yaml from "@std/yaml"; + +const assetsDir = path.join( + path.dirname(path.dirname(path.fromFileUrl(import.meta.url))), + "assets", +); +const yamlDir = path.join(assetsDir, "yaml"); +const jsonDir = path.join(assetsDir, "json"); + +for await (const dirEntry of Deno.readDir(yamlDir)) { + const yamlFile = path.join(yamlDir, dirEntry.name); + const inputText = await Deno.readTextFile(yamlFile); + const data = yaml.parse(inputText); + + const jsonFile = path.join(jsonDir, path.parse(yamlFile).name + ".json"); + const jsonString = JSON.stringify(data, null, 2); + await Deno.writeTextFile(jsonFile, jsonString); +} diff --git a/scripts/yaml2toml.py b/scripts/yaml2toml.py deleted file mode 100755 index 39a8bc0..0000000 --- a/scripts/yaml2toml.py +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python3 - -# SPDX-FileCopyrightText: None -# -# SPDX-License-Identifier: CC0-1.0 - -from pathlib import Path -from typing import Any, Final - -import tomli_w -import yaml - -ASSETS_DIR: Final[Path] = Path(__file__).resolve().parent.parent / "assets" -YAML_DIR: Final[Path] = ASSETS_DIR / "yaml" -TOML_DIR: Final[Path] = ASSETS_DIR / "toml" - - -def remove_none(data: dict[str, Any]) -> dict[str, Any]: - if isinstance(data, dict): - return {k: remove_none(v) for k, v in data.items() if v is not None} - return data - - -for yaml_file in YAML_DIR.iterdir(): - with yaml_file.open("rb") as f: - data = yaml.safe_load(f) - - data = remove_none(data) - - toml_file = TOML_DIR / (yaml_file.stem + ".toml") - with toml_file.open("wb") as f: - tomli_w.dump(data, f) From af54c5f83def4b563c4360433a870b956dfd29a3 Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Mon, 3 Jun 2024 19:17:30 +0900 Subject: [PATCH 2/2] Update `.gitignore` --- .gitignore | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/.gitignore b/.gitignore index cfdb3bd..b189935 100644 --- a/.gitignore +++ b/.gitignore @@ -4,19 +4,6 @@ # Generated files assets/json/*.json -assets/toml/*.toml # Node.js dependency directory node_modules/ - -# Python - -## Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -## Environments -.venv -venv/ -venv.bak/