jspprint 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jspprint-0.1.0/.gitignore +220 -0
- jspprint-0.1.0/LICENSE +21 -0
- jspprint-0.1.0/PKG-INFO +84 -0
- jspprint-0.1.0/README.md +65 -0
- jspprint-0.1.0/pyproject.toml +39 -0
- jspprint-0.1.0/src/jsp/__init__.py +0 -0
- jspprint-0.1.0/src/jsp/cli.py +85 -0
- jspprint-0.1.0/src/jsp/io.py +78 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
# Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
# poetry.lock
|
|
109
|
+
# poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
# pdm.lock
|
|
116
|
+
# pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
# pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# Redis
|
|
135
|
+
*.rdb
|
|
136
|
+
*.aof
|
|
137
|
+
*.pid
|
|
138
|
+
|
|
139
|
+
# RabbitMQ
|
|
140
|
+
mnesia/
|
|
141
|
+
rabbitmq/
|
|
142
|
+
rabbitmq-data/
|
|
143
|
+
|
|
144
|
+
# ActiveMQ
|
|
145
|
+
activemq-data/
|
|
146
|
+
|
|
147
|
+
# SageMath parsed files
|
|
148
|
+
*.sage.py
|
|
149
|
+
|
|
150
|
+
# Environments
|
|
151
|
+
.env
|
|
152
|
+
.envrc
|
|
153
|
+
.venv
|
|
154
|
+
env/
|
|
155
|
+
venv/
|
|
156
|
+
ENV/
|
|
157
|
+
env.bak/
|
|
158
|
+
venv.bak/
|
|
159
|
+
|
|
160
|
+
# Spyder project settings
|
|
161
|
+
.spyderproject
|
|
162
|
+
.spyproject
|
|
163
|
+
|
|
164
|
+
# Rope project settings
|
|
165
|
+
.ropeproject
|
|
166
|
+
|
|
167
|
+
# mkdocs documentation
|
|
168
|
+
/site
|
|
169
|
+
|
|
170
|
+
# mypy
|
|
171
|
+
.mypy_cache/
|
|
172
|
+
.dmypy.json
|
|
173
|
+
dmypy.json
|
|
174
|
+
|
|
175
|
+
# Pyre type checker
|
|
176
|
+
.pyre/
|
|
177
|
+
|
|
178
|
+
# pytype static type analyzer
|
|
179
|
+
.pytype/
|
|
180
|
+
|
|
181
|
+
# Cython debug symbols
|
|
182
|
+
cython_debug/
|
|
183
|
+
|
|
184
|
+
# PyCharm
|
|
185
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
186
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
188
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
189
|
+
# .idea/
|
|
190
|
+
|
|
191
|
+
# Abstra
|
|
192
|
+
# Abstra is an AI-powered process automation framework.
|
|
193
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
194
|
+
# Learn more at https://abstra.io/docs
|
|
195
|
+
.abstra/
|
|
196
|
+
|
|
197
|
+
# Visual Studio Code
|
|
198
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
199
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
200
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
201
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
202
|
+
# .vscode/
|
|
203
|
+
|
|
204
|
+
# Ruff stuff:
|
|
205
|
+
.ruff_cache/
|
|
206
|
+
|
|
207
|
+
# PyPI configuration file
|
|
208
|
+
.pypirc
|
|
209
|
+
|
|
210
|
+
# Marimo
|
|
211
|
+
marimo/_static/
|
|
212
|
+
marimo/_lsp/
|
|
213
|
+
__marimo__/
|
|
214
|
+
|
|
215
|
+
# Streamlit
|
|
216
|
+
.streamlit/secrets.toml
|
|
217
|
+
|
|
218
|
+
.DS_Store
|
|
219
|
+
|
|
220
|
+
.claude
|
jspprint-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright @ 2026 Jyrki Kokkola
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included
|
|
13
|
+
in all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
21
|
+
IN THE SOFTWARE.
|
jspprint-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jspprint
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A CLI tool for viewing and manipulating JSON files
|
|
5
|
+
Project-URL: Homepage, https://github.com/jyjokokk/jsp
|
|
6
|
+
Project-URL: Repository, https://github.com/jyjokokk/jsp
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Topic :: Utilities
|
|
15
|
+
Requires-Python: >=3.12
|
|
16
|
+
Requires-Dist: rich>=14.3.3
|
|
17
|
+
Requires-Dist: typer>=0.24.1
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# jsp
|
|
21
|
+
|
|
22
|
+
A CLI tool for viewing and manipulating JSON files. Pretty-prints JSON with syntax highlighting by default, supports filtering by keys, and allows in-memory modifications for easy piping — without altering the original file.
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- **Pretty-print JSON** with syntax highlighting (default behavior)
|
|
27
|
+
- **Filter by key(s)** to extract and display specific parts of a JSON structure
|
|
28
|
+
- **In-memory modifications** — add, update, replace, or delete values in the output without touching the source file
|
|
29
|
+
- **Merge from file or stdin** — set a key's value to the contents of another JSON file, or pipe JSON in via stdin
|
|
30
|
+
- **Stdin as input** — read JSON from stdin instead of a file (`cat data.json | jsp`)
|
|
31
|
+
- **Pipe-friendly** — output can be piped to other commands or redirected to files
|
|
32
|
+
|
|
33
|
+
## Planned
|
|
34
|
+
|
|
35
|
+
- CSV, XML, and YAML support
|
|
36
|
+
|
|
37
|
+
## Tech Stack
|
|
38
|
+
|
|
39
|
+
- **Python**
|
|
40
|
+
- **Rich** for colored/syntax-highlighted output
|
|
41
|
+
- **Typer** for argument parsing
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
> Coming soon — Homebrew tap planned as the first distribution target.
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
# Pretty-print a JSON file with syntax highlighting
|
|
51
|
+
jsp data.json
|
|
52
|
+
|
|
53
|
+
# Read JSON from stdin
|
|
54
|
+
cat data.json | jsp
|
|
55
|
+
|
|
56
|
+
# Filter by key
|
|
57
|
+
jsp data.json name
|
|
58
|
+
|
|
59
|
+
# Filter by nested key path
|
|
60
|
+
jsp data.json users.0.email
|
|
61
|
+
|
|
62
|
+
# Modify a value in the output (file is not changed)
|
|
63
|
+
jsp data.json --set name=updated
|
|
64
|
+
|
|
65
|
+
# Delete a key from the output
|
|
66
|
+
jsp data.json --del obsoleteField
|
|
67
|
+
|
|
68
|
+
# Set a key's value from another JSON file
|
|
69
|
+
jsp data.json --set config=@overrides.json
|
|
70
|
+
|
|
71
|
+
# Same thing via stdin pipe
|
|
72
|
+
cat overrides.json | jsp data.json --set config=@-
|
|
73
|
+
|
|
74
|
+
# Output compact JSON (single line, no highlighting)
|
|
75
|
+
jsp data.json --compact
|
|
76
|
+
jsp data.json -c
|
|
77
|
+
|
|
78
|
+
# Pipe the result
|
|
79
|
+
jsp data.json --set env=prod -c | kubectl apply -f -
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT
|
jspprint-0.1.0/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# jsp
|
|
2
|
+
|
|
3
|
+
A CLI tool for viewing and manipulating JSON files. Pretty-prints JSON with syntax highlighting by default, supports filtering by keys, and allows in-memory modifications for easy piping — without altering the original file.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Pretty-print JSON** with syntax highlighting (default behavior)
|
|
8
|
+
- **Filter by key(s)** to extract and display specific parts of a JSON structure
|
|
9
|
+
- **In-memory modifications** — add, update, replace, or delete values in the output without touching the source file
|
|
10
|
+
- **Merge from file or stdin** — set a key's value to the contents of another JSON file, or pipe JSON in via stdin
|
|
11
|
+
- **Stdin as input** — read JSON from stdin instead of a file (`cat data.json | jsp`)
|
|
12
|
+
- **Pipe-friendly** — output can be piped to other commands or redirected to files
|
|
13
|
+
|
|
14
|
+
## Planned
|
|
15
|
+
|
|
16
|
+
- CSV, XML, and YAML support
|
|
17
|
+
|
|
18
|
+
## Tech Stack
|
|
19
|
+
|
|
20
|
+
- **Python**
|
|
21
|
+
- **Rich** for colored/syntax-highlighted output
|
|
22
|
+
- **Typer** for argument parsing
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
> Coming soon — Homebrew tap planned as the first distribution target.
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
# Pretty-print a JSON file with syntax highlighting
|
|
32
|
+
jsp data.json
|
|
33
|
+
|
|
34
|
+
# Read JSON from stdin
|
|
35
|
+
cat data.json | jsp
|
|
36
|
+
|
|
37
|
+
# Filter by key
|
|
38
|
+
jsp data.json name
|
|
39
|
+
|
|
40
|
+
# Filter by nested key path
|
|
41
|
+
jsp data.json users.0.email
|
|
42
|
+
|
|
43
|
+
# Modify a value in the output (file is not changed)
|
|
44
|
+
jsp data.json --set name=updated
|
|
45
|
+
|
|
46
|
+
# Delete a key from the output
|
|
47
|
+
jsp data.json --del obsoleteField
|
|
48
|
+
|
|
49
|
+
# Set a key's value from another JSON file
|
|
50
|
+
jsp data.json --set config=@overrides.json
|
|
51
|
+
|
|
52
|
+
# Same thing via stdin pipe
|
|
53
|
+
cat overrides.json | jsp data.json --set config=@-
|
|
54
|
+
|
|
55
|
+
# Output compact JSON (single line, no highlighting)
|
|
56
|
+
jsp data.json --compact
|
|
57
|
+
jsp data.json -c
|
|
58
|
+
|
|
59
|
+
# Pipe the result
|
|
60
|
+
jsp data.json --set env=prod -c | kubectl apply -f -
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "jspprint"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "A CLI tool for viewing and manipulating JSON files"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
requires-python = ">=3.12"
|
|
8
|
+
classifiers = [
|
|
9
|
+
"Development Status :: 3 - Alpha",
|
|
10
|
+
"Environment :: Console",
|
|
11
|
+
"Intended Audience :: Developers",
|
|
12
|
+
"License :: OSI Approved :: MIT License",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Topic :: Utilities",
|
|
15
|
+
]
|
|
16
|
+
dependencies = ["rich>=14.3.3", "typer>=0.24.1"]
|
|
17
|
+
|
|
18
|
+
[project.scripts]
|
|
19
|
+
jsp = "jsp.cli:app"
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/jyjokokk/jsp"
|
|
23
|
+
Repository = "https://github.com/jyjokokk/jsp"
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["hatchling"]
|
|
27
|
+
build-backend = "hatchling.build"
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.wheel]
|
|
30
|
+
packages = ["src/jsp"]
|
|
31
|
+
|
|
32
|
+
[tool.hatch.build.targets.sdist]
|
|
33
|
+
include = ["src/jsp/", "README.md", "LICENSE", "pyproject.toml"]
|
|
34
|
+
|
|
35
|
+
[dependency-groups]
|
|
36
|
+
dev = ["pytest>=9.0.2", "ruff>=0.15.5"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
|
File without changes
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import sys
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
|
|
7
|
+
from jsp.io import filter_by_key, print_json, read_json, update_json
|
|
8
|
+
|
|
9
|
+
app = typer.Typer()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@app.command()
|
|
13
|
+
def main(
|
|
14
|
+
file: Optional[str] = typer.Argument(
|
|
15
|
+
None, help="Path to a JSON file. Reads from stdin if omitted."
|
|
16
|
+
),
|
|
17
|
+
key: Optional[str] = typer.Argument(
|
|
18
|
+
None, help="Key to filter by, using dot notation (e.g. users.0.email)."
|
|
19
|
+
),
|
|
20
|
+
compact: bool = typer.Option(
|
|
21
|
+
False, "--compact", "-c", help="Output compact JSON without formatting."
|
|
22
|
+
),
|
|
23
|
+
set_value: Optional[list[str]] = typer.Option(
|
|
24
|
+
None, "--set", "-s", help="Set a value: key=value or key=@file.json."
|
|
25
|
+
),
|
|
26
|
+
delete: Optional[list[str]] = typer.Option(
|
|
27
|
+
None, "--del", "-d", help="Delete a key from the output."
|
|
28
|
+
),
|
|
29
|
+
) -> None:
|
|
30
|
+
"""A CLI tool for viewing and manipulating JSON files."""
|
|
31
|
+
stdin_used = file is None
|
|
32
|
+
try:
|
|
33
|
+
data = read_json(file)
|
|
34
|
+
except ValueError as e:
|
|
35
|
+
raise typer.BadParameter(str(e)) from e
|
|
36
|
+
|
|
37
|
+
if set_value:
|
|
38
|
+
for item in set_value:
|
|
39
|
+
k, _, v = item.partition("=")
|
|
40
|
+
if not k or not v:
|
|
41
|
+
raise typer.BadParameter(
|
|
42
|
+
f"Invalid --set format: '{item}'. Use key=value."
|
|
43
|
+
)
|
|
44
|
+
if v == "@-":
|
|
45
|
+
if stdin_used:
|
|
46
|
+
raise typer.BadParameter(
|
|
47
|
+
"Cannot use @- (stdin) for --set when stdin is already used as primary input."
|
|
48
|
+
)
|
|
49
|
+
v = json.load(sys.stdin)
|
|
50
|
+
elif v.startswith("@"):
|
|
51
|
+
with open(v[1:]) as f:
|
|
52
|
+
v = json.load(f)
|
|
53
|
+
else:
|
|
54
|
+
try:
|
|
55
|
+
v = json.loads(v)
|
|
56
|
+
except json.JSONDecodeError:
|
|
57
|
+
pass # keep as string
|
|
58
|
+
data = update_json(data, k, v)
|
|
59
|
+
|
|
60
|
+
if delete:
|
|
61
|
+
from jsp.io import _traverse
|
|
62
|
+
|
|
63
|
+
for k in delete:
|
|
64
|
+
try:
|
|
65
|
+
parent, last_key = _traverse(data, k, stop_before_last=True)
|
|
66
|
+
if isinstance(parent, dict) and last_key in parent:
|
|
67
|
+
del parent[last_key]
|
|
68
|
+
elif isinstance(parent, list) and last_key.isdigit():
|
|
69
|
+
del parent[int(last_key)]
|
|
70
|
+
else:
|
|
71
|
+
raise KeyError(f"Key '{k}' not found in the data.")
|
|
72
|
+
except KeyError as e:
|
|
73
|
+
raise typer.BadParameter(str(e)) from e
|
|
74
|
+
|
|
75
|
+
if key:
|
|
76
|
+
try:
|
|
77
|
+
data = filter_by_key(data, key)
|
|
78
|
+
except KeyError as e:
|
|
79
|
+
raise typer.BadParameter(str(e)) from e
|
|
80
|
+
|
|
81
|
+
print_json(data, pretty=not compact)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if __name__ == "__main__":
|
|
85
|
+
app()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""Functions for handling STDIN and STDOUT operations."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import sys
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from rich.console import Console
|
|
8
|
+
|
|
9
|
+
console = Console()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def read_json(file_path: str | None = None) -> dict:
|
|
13
|
+
"""Read JSON from a file path or stdin if no path is given."""
|
|
14
|
+
try:
|
|
15
|
+
if file_path:
|
|
16
|
+
with open(file_path) as f:
|
|
17
|
+
return json.load(f)
|
|
18
|
+
|
|
19
|
+
if not sys.stdin.isatty():
|
|
20
|
+
return json.load(sys.stdin)
|
|
21
|
+
except json.JSONDecodeError as e:
|
|
22
|
+
source = file_path or "stdin"
|
|
23
|
+
raise ValueError(f"Invalid JSON from {source}: {e}") from e
|
|
24
|
+
|
|
25
|
+
raise ValueError("No input provided. Pass a file path or pipe JSON via stdin.")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def print_json(data: dict, pretty: bool = True) -> None:
|
|
29
|
+
if pretty:
|
|
30
|
+
console.print_json(json.dumps(data, indent=4))
|
|
31
|
+
else:
|
|
32
|
+
print(json.dumps(data))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _traverse(data, key: str, stop_before_last: bool = False) -> tuple[Any, str] | Any:
|
|
36
|
+
"""Traverse data using dot notation. Returns (parent, last_key) if stop_before_last, else the value."""
|
|
37
|
+
keys = key.split(".")
|
|
38
|
+
traverse_keys = keys[:-1] if stop_before_last else keys
|
|
39
|
+
current = data
|
|
40
|
+
|
|
41
|
+
for k in traverse_keys:
|
|
42
|
+
if isinstance(current, list) and k.isdigit():
|
|
43
|
+
index = int(k)
|
|
44
|
+
if index < len(current):
|
|
45
|
+
current = current[index]
|
|
46
|
+
else:
|
|
47
|
+
raise KeyError(f"Index {index} out of range in '{key}'.")
|
|
48
|
+
elif isinstance(current, dict) and k in current:
|
|
49
|
+
current = current[k]
|
|
50
|
+
else:
|
|
51
|
+
raise KeyError(f"Key '{key}' not found in the data.")
|
|
52
|
+
|
|
53
|
+
if stop_before_last:
|
|
54
|
+
return current, keys[-1]
|
|
55
|
+
return current
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def filter_by_key(data: dict, key: str) -> dict:
|
|
59
|
+
"""Filter a dictionary by a key, supporting dot notation for nested access."""
|
|
60
|
+
return {key: _traverse(data, key)}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def update_json(data: dict, key: str, value) -> dict:
|
|
64
|
+
"""Modify a dictionary with a new value at a specified key, supporting dot notation."""
|
|
65
|
+
parent, last_key = _traverse(data, key, stop_before_last=True)
|
|
66
|
+
|
|
67
|
+
if isinstance(parent, list) and last_key.isdigit():
|
|
68
|
+
index = int(last_key)
|
|
69
|
+
if index < len(parent):
|
|
70
|
+
parent[index] = value
|
|
71
|
+
else:
|
|
72
|
+
raise KeyError(f"Index {index} out of range in '{key}'.")
|
|
73
|
+
elif isinstance(parent, dict):
|
|
74
|
+
parent[last_key] = value
|
|
75
|
+
else:
|
|
76
|
+
raise KeyError(f"Cannot set value at '{key}'.")
|
|
77
|
+
|
|
78
|
+
return data
|