json-repair 0.38.0__tar.gz → 0.39.0__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
Files changed (20) hide show
  1. {json_repair-0.38.0/src/json_repair.egg-info → json_repair-0.39.0}/PKG-INFO +1 -1
  2. {json_repair-0.38.0 → json_repair-0.39.0}/pyproject.toml +1 -1
  3. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair/json_repair.py +20 -7
  4. {json_repair-0.38.0 → json_repair-0.39.0/src/json_repair.egg-info}/PKG-INFO +1 -1
  5. {json_repair-0.38.0 → json_repair-0.39.0}/tests/test_json_repair.py +12 -1
  6. {json_repair-0.38.0 → json_repair-0.39.0}/LICENSE +0 -0
  7. {json_repair-0.38.0 → json_repair-0.39.0}/README.md +0 -0
  8. {json_repair-0.38.0 → json_repair-0.39.0}/setup.cfg +0 -0
  9. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair/__init__.py +0 -0
  10. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair/__main__.py +0 -0
  11. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair/json_context.py +0 -0
  12. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair/json_parser.py +0 -0
  13. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair/py.typed +0 -0
  14. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair/string_file_wrapper.py +0 -0
  15. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair.egg-info/SOURCES.txt +0 -0
  16. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair.egg-info/dependency_links.txt +0 -0
  17. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair.egg-info/entry_points.txt +0 -0
  18. {json_repair-0.38.0 → json_repair-0.39.0}/src/json_repair.egg-info/top_level.txt +0 -0
  19. {json_repair-0.38.0 → json_repair-0.39.0}/tests/test_coverage.py +0 -0
  20. {json_repair-0.38.0 → json_repair-0.39.0}/tests/test_performance.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: json_repair
3
- Version: 0.38.0
3
+ Version: 0.39.0
4
4
  Summary: A package to repair broken json strings
5
5
  Author-email: Stefano Baccianella <4247706+mangiucugna@users.noreply.github.com>
6
6
  License: MIT License
@@ -3,7 +3,7 @@ requires = ["setuptools>=61.0"]
3
3
  build-backend = "setuptools.build_meta"
4
4
  [project]
5
5
  name = "json_repair"
6
- version = "0.38.0"
6
+ version = "0.39.0"
7
7
  license = {file = "LICENSE"}
8
8
  authors = [
9
9
  { name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
@@ -160,7 +160,7 @@ def cli(inline_args: Optional[List[str]] = None) -> int:
160
160
 
161
161
  Args:
162
162
  inline_args (Optional[List[str]]): List of command-line arguments for testing purposes. Defaults to None.
163
- - filename (str): The JSON file to repair
163
+ - filename (str): The JSON file to repair. If omitted, the JSON is read from stdin.
164
164
  - -i, --inline (bool): Replace the file inline instead of returning the output to stdout.
165
165
  - -o, --output TARGET (str): If specified, the output will be written to TARGET filename instead of stdout.
166
166
  - --ensure_ascii (bool): Pass ensure_ascii=True to json.dumps(). Will pass False otherwise.
@@ -174,9 +174,15 @@ def cli(inline_args: Optional[List[str]] = None) -> int:
174
174
 
175
175
  Example:
176
176
  >>> cli(['example.json', '--indent', '4'])
177
+ >>> cat json.txt | json_repair
177
178
  """
178
179
  parser = argparse.ArgumentParser(description="Repair and parse JSON files.")
179
- parser.add_argument("filename", help="The JSON file to repair")
180
+ # Make the filename argument optional; if omitted, we will read from stdin.
181
+ parser.add_argument(
182
+ "filename",
183
+ nargs="?",
184
+ help="The JSON file to repair (if omitted, reads from stdin)",
185
+ )
180
186
  parser.add_argument(
181
187
  "-i",
182
188
  "--inline",
@@ -204,9 +210,12 @@ def cli(inline_args: Optional[List[str]] = None) -> int:
204
210
  if inline_args is None: # pragma: no cover
205
211
  args = parser.parse_args()
206
212
  else:
207
- args = parser.parse_args(
208
- inline_args
209
- ) # This is needed so this function is testable
213
+ args = parser.parse_args(inline_args)
214
+
215
+ # Inline mode requires a filename, so error out if none was provided.
216
+ if args.inline and not args.filename: # pragma: no cover
217
+ print("Error: Inline mode requires a filename", file=sys.stderr)
218
+ sys.exit(1)
210
219
 
211
220
  if args.inline and args.output: # pragma: no cover
212
221
  print("Error: You cannot pass both --inline and --output", file=sys.stderr)
@@ -217,8 +226,12 @@ def cli(inline_args: Optional[List[str]] = None) -> int:
217
226
  ensure_ascii = True
218
227
 
219
228
  try:
220
- result = from_file(args.filename)
221
-
229
+ # Use from_file if a filename is provided; otherwise read from stdin.
230
+ if args.filename:
231
+ result = from_file(args.filename)
232
+ else:
233
+ data = sys.stdin.read()
234
+ result = loads(data)
222
235
  if args.inline or args.output:
223
236
  with open(args.output or args.filename, mode="w") as fd:
224
237
  json.dump(result, fd, indent=args.indent, ensure_ascii=ensure_ascii)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: json_repair
3
- Version: 0.38.0
3
+ Version: 0.39.0
4
4
  Summary: A package to repair broken json strings
5
5
  Author-email: Stefano Baccianella <4247706+mangiucugna@users.noreply.github.com>
6
6
  License: MIT License
@@ -3,6 +3,7 @@ from unittest.mock import patch
3
3
  import os.path
4
4
  import pathlib
5
5
  import tempfile
6
+ import io
6
7
 
7
8
  def test_basic_types_valid():
8
9
  assert repair_json("True", return_objects=True) == ""
@@ -316,4 +317,14 @@ def test_cli(capsys):
316
317
  finally:
317
318
  # Clean up - delete the temporary file
318
319
  os.remove(temp_path)
319
- os.remove(tempout_path)
320
+ os.remove(tempout_path)
321
+
322
+ # Prepare a JSON string that needs to be repaired.
323
+ test_input = "{key:value"
324
+ # Expected output when running cli with --indent 0.
325
+ expected_output = '{\n"key": "value"\n}\n'
326
+ # Patch sys.stdin so that cli() reads from it instead of a file.
327
+ with patch('sys.stdin', new=io.StringIO(test_input)):
328
+ cli(inline_args=['--indent', 0])
329
+ captured = capsys.readouterr()
330
+ assert captured.out == expected_output
File without changes
File without changes
File without changes