-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
136 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" | |
|
||
[project] | ||
name = "py416" | ||
version = "0.61" | ||
version = "0.62" | ||
authors = [ | ||
{ name="Ezio416", email="[email protected]" }, | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
''' | ||
| Author: Ezio416 | ||
| Created: 2024-01-31 | ||
| Updated: 2024-01-31 | ||
- Functions for interacting with json objects | ||
''' | ||
import json | ||
import os | ||
|
||
from .files import getpath, listdir | ||
|
||
|
||
def pretty(path: str = '.', indent: int = 4, sort_keys: bool = True, overwrite: bool = True) -> int: | ||
''' | ||
- takes one or more .json files and makes them human-readable | ||
- will only work on files ending with .json, all other files are skipped | ||
- appends '_pretty' to the end of the base filename, i.e. 'data.json' becomes 'data_pretty.json' | ||
- skips files already ending with '_pretty.json' - we assume those are already done | ||
Parameters | ||
---------- | ||
path: str | ||
- path to file or folder to work on | ||
- if a file, will only do that file | ||
- if a folder, will do every file in that folder | ||
- does not search subfolders | ||
- default: current working directory | ||
indent: int | ||
- number of spaces to indent by | ||
- default: 4 | ||
sort_keys: bool | ||
- whether to sort keys | ||
- default: True | ||
overwrite: bool | ||
- whether to overwrite a '_pretty.json' if it already exists | ||
- default: True | ||
Returns | ||
------- | ||
int | ||
- number of files that were formatted | ||
''' | ||
if path == '.': | ||
files: tuple[str] = tuple(getpath(f) for f in listdir(dirs=False)) | ||
else: | ||
if os.path.exists(path): | ||
if os.path.isdir(path): | ||
files: tuple[str] = tuple(getpath(file) for file in listdir(path, dirs=False)) | ||
elif os.path.isfile(path): | ||
files: tuple[str] = tuple(getpath(path)) | ||
else: | ||
print(f'invalid path given: {path}') | ||
return | ||
|
||
total: int = 0 | ||
|
||
for file in files: | ||
parts: list[str] = file.split('.') | ||
pre_ext: str = '.'.join(parts[:-1]) | ||
ext: str = parts[-1] | ||
|
||
if pre_ext.endswith('_pretty') or ext != 'json': | ||
continue | ||
|
||
with open(file, 'r') as f: | ||
contents: dict = json.loads(f.read()) | ||
|
||
new_file: str = pre_ext + '_pretty.json' | ||
|
||
if not overwrite and os.path.exists(new_file): | ||
continue | ||
|
||
with open(new_file, 'w') as f: | ||
json.dump(contents, f, indent=indent, sort_keys=sort_keys) | ||
|
||
total += 1 | ||
|
||
return total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import os | ||
import sys | ||
|
||
import pytest | ||
import pytest_check as check | ||
|
||
sys.path.append(os.path.dirname(os.path.dirname(__file__))) | ||
import src.py416.json as p4j | ||
|
||
|
||
def test_pretty(tmp_path): | ||
pass | ||
# print(p4j.pretty(tmp_path)) | ||
|
||
|
||
# if __name__ == '__main__': | ||
# test_pretty(r'C:\Users\Ezio\Code\trackmania-json-tracking') |