Skip to content

Commit 1a90f98

Browse files
committed
Added file dialog for selecting JSON files and setuptools installation support.
1 parent 07b9342 commit 1a90f98

9 files changed

Lines changed: 140 additions & 48 deletions

File tree

.github/workflows/build_executable.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ jobs:
1515
uses: actions/setup-python@v5
1616
with:
1717
python-version: 3.11
18-
- name: Install PyInstaller and Pillow
18+
- name: Install PyInstaller and requirements
1919
run: |
2020
pip install pyinstaller
2121
pip install pillow
22+
pip install rich
2223
- name: Run PyInstaller for BDSP_ColorVariation_JSONParser
2324
run: |
2425
set PYTHONOPTIMIZE=2
25-
pyinstaller --onefile --name="BDSP_ColorVariation_JSONParser" --console --icon=images\BDSP_ColorVariation_JSONParser.tga bdsp_colorvariation_jsonparser.py
26+
pyinstaller --onefile --name="BDSP_ColorVariation_JSONParser" --console --icon=src\BDSP_ColorVariation_JSONParser\images\json_parser.tga src\BDSP_ColorVariation_JSONParser\json_parser.py
2627
- name: Move Executable from dist to root directory
2728
run: |
2829
move dist\BDSP_ColorVariation_JSONParser.exe .

.github/workflows/build_executable_nuitka.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ jobs:
1515
uses: actions/setup-python@v5
1616
with:
1717
python-version: 3.11
18+
- name: Install requirements
19+
run: |
20+
pip install pillow
21+
pip install rich
1822
- name: Install imageio
1923
run: pip install imageio
2024
- name: Run Nuitka for BDSP_ColorVariation_JSONParser
2125
uses: Nuitka/Nuitka-Action@main
2226
with:
2327
nuitka-version: main
24-
script-name: bdsp_colorvariation_jsonparser.py
28+
script-name: src/BDSP_ColorVariation_JSONParser/json_parser.py
2529
mode: app
26-
windows-icon-from-ico: "images/BDSP_ColorVariation_JSONParser.tga"
30+
enable-plugins: tk-inter
31+
windows-icon-from-ico: "src/BDSP_ColorVariation_JSONParser/images/json_parser.tga"
2732
- name: Move Executable from build to root directory
2833
run: |
2934
move build\BDSP_ColorVariation_JSONParser.exe .

.github/workflows/greetings.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
# Pokémon Brilliant Diamond/Shining Pearl ColorVariation Parser
2-
This script outputs color information from `ColorVariation.json` files originated from Pokémon BD/SP video games.
2+
Console tool for parsing color information from `ColorVariation.json` files originated from Pokémon BD/SP video games.
33

4-
Multiple JSON files are supported if provided as launch arguments.
4+
Multiple JSON files are supported if provided as launch arguments or selected with file dialog.
5+
## Installation
6+
### Option 1: Pre-compiled builds
7+
#### Latest Windows releases:
8+
- [PyInstaller](https://github.com/Shararamosh/BDSP_ColorVariation_JSONParser/releases/tag/latest)
9+
- [Nuitka](https://github.com/Shararamosh/BDSP_ColorVariation_JSONParser/releases/tag/latest-nuitka)
10+
### Option 2: Installing using pip
11+
```pip install git+https://github.com/Shararamosh/BDSP_ColorVariation_JSONParser.git```
12+
#### Requirements:
13+
- [Pillow](https://pypi.org/project/pillow/)
14+
- [Rich](https://pypi.org/project/rich)

pyproject.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"
4+
[project]
5+
name="BDSP_ColorVariation_JSONParser"
6+
dynamic = ["version"]
7+
requires-python = ">= 3.11"
8+
dependencies = [
9+
"pillow",
10+
"rich"
11+
]
12+
authors = [{name = "Shararamosh"}]
13+
description = "Console tool for parsing color information from Pokémon BD/SP ColorVariation.json files."
14+
classifiers = [
15+
"Development Status :: 5 - Production/Stable",
16+
"Environment :: Console",
17+
"Intended Audience :: Developers",
18+
"Intended Audience :: End Users/Desktop",
19+
"Natural Language :: English",
20+
"Operating System :: OS Independent",
21+
"Programming Language :: Python :: 3.11",
22+
"Topic :: Utilities"
23+
]
24+
readme = "README.MD"
25+
[project.urls]
26+
Repository = "https://github.com/Shararamosh/BDSP_ColorVariation_JSONParser.git"
27+
[project.scripts]
28+
json_parser = "BDSP_ColorVariation_JSONParser.json_parser:main"
Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
"""
2-
Script for parsing color information from Pokémon BD/SP ColorVariation.json files.
2+
Script containing functions to deal with JSON files from Pokémon BD/SP games.
33
"""
4+
# pylint: disable=import-error, wrong-import-position
45
import os
56
import sys
67
import json
7-
import argparse
8+
import logging
9+
10+
sys.path.append(os.path.join(os.path.dirname(__file__), "."))
11+
12+
from general import RICH_CONSOLE
813

914

1015
def print_json_color_data(file_path: str):
@@ -13,33 +18,33 @@ def print_json_color_data(file_path: str):
1318
:param file_path: Path to JSON file.
1419
"""
1520
if not os.path.exists(file_path):
16-
print("Path does not exist.")
21+
logging.warning("Path %s does not exist.", file_path)
1722
return
1823
if not os.path.isfile(file_path):
19-
print("Path is not a file.")
24+
logging.warning("Path %s is not a file.", file_path)
2025
return
2126
if not file_path.lower().endswith(".json"):
22-
print("Path is not a JSON file.")
27+
logging.warning("Path %s is not a JSON file.", file_path)
2328
return
2429
with open(file_path, encoding="utf-8") as json_file:
30+
RICH_CONSOLE.print(f"{file_path}:")
2531
json_data = json.load(json_file)
2632
k = 0
2733
s = "00"
2834
while "Property" + s in json_data:
2935
property_data = json_data["Property" + s][0]
30-
print("Property" + s + ":")
36+
RICH_CONSOLE.print(f"Property {s}:")
3137
colors_data = property_data["colors"]
3238
current_material_index = -1
3339
for color_data in colors_data:
3440
if color_data is not None:
3541
c = color_data["channel"]
3642
if color_data["materialIndex"] > current_material_index:
3743
current_material_index = color_data["materialIndex"]
38-
print("Material Index: " + str(current_material_index) + ".")
44+
RICH_CONSOLE.print(f"Material Index: {current_material_index}.")
3945
color_dict = color_data["color"]
4046
if color_dict is not None:
41-
color_dict_str = get_color_dict_str(color_dict)
42-
print(f"[{c:d}]: {color_dict_str}.")
47+
RICH_CONSOLE.print(f"[{c:d}]: {get_color_dict_str(color_dict)}.")
4348
k = k + 1
4449
if k < 10:
4550
s = "0" + str(k)
@@ -62,20 +67,3 @@ def get_color_dict_str(color_dict: dict) -> str:
6267
hc = hc + format(round(float(color_dict["g"]) * 255.0), "x").upper()
6368
hc = hc + format(round(float(color_dict["b"]) * 255.0), "x").upper()
6469
return f"{lc} -> {hc}"
65-
66-
67-
if __name__ == "__main__":
68-
parser = argparse.ArgumentParser(prog="BD/SP ColorVariation Parser",
69-
description="Parser for Pokémon BD/SP "
70-
"ColorVariation.json files.")
71-
parser.add_argument("input_paths", nargs="*", type=str, default="",
72-
help="Input JSON files")
73-
args = parser.parse_args()
74-
if len(args.input_paths) < 1:
75-
input_path = input("JSON file path: ")
76-
print_json_color_data(input_path)
77-
else:
78-
for input_path in args.input_paths:
79-
print(f"{input_path}:")
80-
print_json_color_data(input_path)
81-
sys.exit(os.EX_OK)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Script containing general functions.
3+
"""
4+
import os
5+
import sys
6+
import logging
7+
from tkinter import Tk
8+
from PIL.ImageTk import PhotoImage
9+
from rich.logging import RichHandler
10+
from rich.console import Console
11+
12+
RICH_CONSOLE = Console()
13+
14+
15+
def get_resource_path(file_path: str) -> str:
16+
"""
17+
Returns path to file or directory inside project, when used PyInstaller or Nuitka are used.
18+
:param file_path: Original path to file or directory.
19+
:return: Modified path to file or directory.
20+
"""
21+
if "NUITKA_ONEFILE_PARENT" in os.environ:
22+
base_path = os.path.dirname(sys.executable)
23+
elif hasattr(sys, "_MEIPASS"):
24+
base_path = getattr(sys, "_MEIPASS")
25+
else:
26+
base_path = os.path.dirname(__file__)
27+
return os.path.join(base_path, file_path)
28+
29+
30+
def init_app(icon_path: str):
31+
"""
32+
Initiating app for launch.
33+
:param icon_path: Path to icon file.
34+
"""
35+
logging.basicConfig(format="%(message)s", level=logging.INFO, handlers=[
36+
RichHandler(level=logging.INFO, console=RICH_CONSOLE, rich_tracebacks=True)])
37+
root = Tk()
38+
root.withdraw()
39+
root.iconphoto(True, PhotoImage(file=get_resource_path(icon_path)))
File renamed without changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Script for parsing color information from Pokémon BD/SP ColorVariation.json files.
3+
"""
4+
# pylint: disable=import-error, wrong-import-position
5+
import os
6+
import sys
7+
import argparse
8+
from tkinter.filedialog import askopenfilenames
9+
10+
from bdsp_json import print_json_color_data
11+
12+
sys.path.append(os.path.join(os.path.dirname(__file__), "."))
13+
14+
from general import init_app, get_resource_path
15+
16+
17+
def main() -> int | str:
18+
"""
19+
Program for parsing color information from Pokémon BD/SP ColorVariation.json files.
20+
:return: Status code or string.
21+
"""
22+
init_app(get_resource_path(os.path.join("images", "json_parser.tga")))
23+
parser = argparse.ArgumentParser(description="Parser for Pokémon BD/SP "
24+
"ColorVariation.json files.")
25+
parser.add_argument("input_paths", nargs="*", type=str, default="",
26+
help="Input JSON files")
27+
args = parser.parse_args()
28+
if len(args.input_paths) < 1:
29+
args.input_paths = askopenfilenames(title="Select JSON files",
30+
filetypes=[("JSON", "*.json")])
31+
for input_path in args.input_paths:
32+
print_json_color_data(input_path)
33+
return os.EX_OK
34+
35+
36+
if __name__ == "__main__":
37+
sys.exit(main())

0 commit comments

Comments
 (0)