py416
Advanced tools
| ''' | ||
| | 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' file 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] = getpath(path), | ||
| else: | ||
| print(f'invalid path given: {path}') | ||
| return 0 | ||
| 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 |
| 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 #TODO | ||
| # print(p4j.pretty(tmp_path)) | ||
| # if __name__ == '__main__': | ||
| # test_pretty(r'C:\Users\Ezio\Code\trackmania-json-tracking') | ||
| # from src.py416.scripts import jsp | ||
| # args: list[str] = ['-i', 4, '-o', 0, '-p', r'C:\Users\Ezio\Code\trackmania-json-tracking'] | ||
| # jsp(args) |
+1
-1
| Metadata-Version: 2.1 | ||
| Name: py416 | ||
| Version: 0.61 | ||
| Version: 0.62 | ||
| Summary: don't question my methods | ||
@@ -5,0 +5,0 @@ Author-email: Ezio416 <ezezio416@gmail.com> |
+2
-1
@@ -7,3 +7,3 @@ [build-system] | ||
| name = "py416" | ||
| version = "0.61" | ||
| version = "0.62" | ||
| authors = [ | ||
@@ -32,3 +32,4 @@ { name="Ezio416", email="ezezio416@gmail.com" }, | ||
| [project.scripts] | ||
| jsp = "py416.scripts:jsp" | ||
| rmd = "py416.scripts:rmd" | ||
| uzd = "py416.scripts:uzd" |
| [console_scripts] | ||
| jsp = py416.scripts:jsp | ||
| rmd = py416.scripts:rmd | ||
| uzd = py416.scripts:uzd |
| Metadata-Version: 2.1 | ||
| Name: py416 | ||
| Version: 0.61 | ||
| Version: 0.62 | ||
| Summary: don't question my methods | ||
@@ -5,0 +5,0 @@ Author-email: Ezio416 <ezezio416@gmail.com> |
@@ -7,2 +7,3 @@ LICENSE.txt | ||
| src/py416/general.py | ||
| src/py416/json.py | ||
| src/py416/scripts.py | ||
@@ -19,2 +20,3 @@ src/py416/sysinfo.py | ||
| tests/test_general.py | ||
| tests/test_json.py | ||
| tests/test_sysinfo.py |
@@ -5,4 +5,4 @@ ''' | ||
| Created: 2022-08-15 | ||
| Updated: 2023-03-14 | ||
| Version: 0.60 | ||
| Updated: 2024-01-31 | ||
| Version: 0.62 | ||
@@ -12,3 +12,3 @@ A collection of various functions | ||
| from .general import * | ||
| __version__ = 0, 60 | ||
| __version__ = 0, 62 | ||
| v = __version__ |
| ''' | ||
| | Author: Ezio416 | ||
| | Created: 2022-08-16 | ||
| | Updated: 2023-03-14 | ||
| | Updated: 2024-01-31 | ||
@@ -964,2 +964,4 @@ - Functions for filesystem and path string manipulation | ||
| path = forslash(os.path.dirname(getcwd())) | ||
| elif path == '.' * len(path): # >2 dots - current directory | ||
| path = getcwd() | ||
| elif (parts := path.split('/'))[-1] == '.': # folder/. is just folder | ||
@@ -966,0 +968,0 @@ path = path[:-2] |
+92
-1
| ''' | ||
| | Author: Ezio416 | ||
| | Created: 2023-03-14 | ||
| | Updated: 2023-03-14 | ||
| | Updated: 2024-01-31 | ||
@@ -12,4 +12,95 @@ - Functions behind console scripts | ||
| from .files import rmdir, unzipdir | ||
| from .json import pretty | ||
| def jsp(): | ||
| ''' | ||
| - takes one or more .json files and makes them human-readable | ||
| - will only work on files ending with .json, all other files are ignored | ||
| - pass arguments directly after their corresponding flags, i.e. "-p path/to/file -i 2 -s False -o False" | ||
| Flags | ||
| --------- | ||
| - -p (str): path to file/folder (default current working directory) | ||
| - if a file, will only do that file | ||
| - if a folder, will do every file in that folder | ||
| - -i (int): number of spaces to indent by (default 4) | ||
| - -s (bool): whether to sort keys (default True) | ||
| - -o (bool): whether to overwrite an existing '_pretty.json' file (default True) | ||
| ''' | ||
| if len(sys.argv) > 1: | ||
| args_given: list[str] = sys.argv[1:] | ||
| args_to_pass: list = ['.', 4, True, True] | ||
| while True: | ||
| if len(args_given) == 0: | ||
| break | ||
| if args_given[0] == '-p': | ||
| if len(args_given) < 2: | ||
| print('path flag passed but no path given!') | ||
| return | ||
| if args_given[1] in ('-i', '-s', '-o'): | ||
| print(f'invalid arguments: {args_given}') | ||
| return | ||
| args_to_pass[0] = args_given[1] | ||
| elif args_given[0] == '-i': | ||
| if len(args_given) < 2: | ||
| print('indent flag passed but no indent value given!') | ||
| return | ||
| if args_given[1] in ('-p', '-s', '-o'): | ||
| print(f'invalid arguments: {args_given}') | ||
| return | ||
| try: | ||
| indent: int = int(args_given[1]) | ||
| except Exception as e: | ||
| print(f'error with indent value: {e}') | ||
| return | ||
| if indent < 0: | ||
| print('given indent value is negative, setting to 0') | ||
| indent = 0 | ||
| args_to_pass[1] = indent | ||
| elif args_given[0] == '-s': | ||
| if len(args_given) < 2: | ||
| print('sort flag passed but no sort value given!') | ||
| return | ||
| if args_given[1] in ('-p', '-i', '-o'): | ||
| print(f'invalid arguments: {args_given}') | ||
| return | ||
| args_to_pass[2] = bool(args_given[1]) | ||
| elif args_given[0] == '-o': | ||
| if len(args_given) < 2: | ||
| print('overwrite flag passed but no overwrite value given!') | ||
| return | ||
| if args_given[1] in ('-p', '-i', '-s'): | ||
| print(f'invalid arguments: {args_given}') | ||
| return | ||
| args_to_pass[3] = args_given[1] | ||
| else: | ||
| print(f'invalid arguments given: {args_given}') | ||
| return | ||
| args_given.pop(0) | ||
| args_given.pop(0) | ||
| print(f'formatted {pretty(*args_to_pass)} .json files') | ||
| return | ||
| print(f'formatted {pretty()} .json files') | ||
| def rmd(): | ||
@@ -16,0 +107,0 @@ ''' |
@@ -126,3 +126,3 @@ import os | ||
| ('/dir1/dir2/..', '/dir1'), | ||
| #Windows | ||
| # Windows | ||
| ('D:', 'D:/'), | ||
@@ -138,3 +138,3 @@ ('D:/', 'D:/'), | ||
| ('//bdi-az-data01/Projects/..', '//bdi-az-data01'), | ||
| # None | ||
| # Any | ||
| ('', ''), | ||
@@ -170,3 +170,3 @@ ('folder/..', ''), | ||
| (('//bdi', 'drive'), '//bdi/drive'), | ||
| # None | ||
| # Any | ||
| ('', ''), | ||
@@ -413,3 +413,3 @@ (('', ''), ''), | ||
| ('//bdi-az-data01/Projects/..', '//bdi-az-data01'), | ||
| # None | ||
| # Any | ||
| ('', ''), | ||
@@ -494,3 +494,3 @@ ('folder/..', ''), | ||
| ('//bdi-az-data01/Projects/..', ('//bdi-az-data01',)), | ||
| # None | ||
| # Any | ||
| ('', ('',)), | ||
@@ -497,0 +497,0 @@ ('folder/..', ('',)) |
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
91606
7.1%22
10%2419
6.56%