json-repair 0.29.0__tar.gz → 0.29.1__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.29.0
3
+ Version: 0.29.1
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
@@ -156,24 +156,24 @@ Install the library for command-line with:
156
156
  ```
157
157
  pipx install json-repair
158
158
  ```
159
- then run
159
+ to know all options available:
160
160
  ```
161
161
  $ json_repair -h
162
-
163
- usage: json_repair [-h] [-i] [--ensure_ascii] [--indent INDENT] filename
162
+ usage: json_repair [-h] [-i] [-o TARGET] [--ensure_ascii] [--indent INDENT] filename
164
163
 
165
164
  Repair and parse JSON files.
166
165
 
167
166
  positional arguments:
168
- filename The JSON file to repair
167
+ filename The JSON file to repair
169
168
 
170
169
  options:
171
- -h, --help show this help message and exit
172
- -i, --inline Replace the file inline instead of returning the output to stdout
173
- --ensure_ascii Pass the ensure_ascii parameter to json.dumps()
174
- --indent INDENT Number of spaces for indentation (Default 2)
170
+ -h, --help show this help message and exit
171
+ -i, --inline Replace the file inline instead of returning the output to stdout
172
+ -o TARGET, --output TARGET
173
+ If specified, the output will be written to TARGET filename instead of stdout
174
+ --ensure_ascii Pass ensure_ascii=True to json.dumps()
175
+ --indent INDENT Number of spaces for indentation (Default 2)
175
176
  ```
176
- to learn how to use it
177
177
 
178
178
  ## Adding to requirements
179
179
  **Please pin this library only on the major version!**
@@ -118,24 +118,24 @@ Install the library for command-line with:
118
118
  ```
119
119
  pipx install json-repair
120
120
  ```
121
- then run
121
+ to know all options available:
122
122
  ```
123
123
  $ json_repair -h
124
-
125
- usage: json_repair [-h] [-i] [--ensure_ascii] [--indent INDENT] filename
124
+ usage: json_repair [-h] [-i] [-o TARGET] [--ensure_ascii] [--indent INDENT] filename
126
125
 
127
126
  Repair and parse JSON files.
128
127
 
129
128
  positional arguments:
130
- filename The JSON file to repair
129
+ filename The JSON file to repair
131
130
 
132
131
  options:
133
- -h, --help show this help message and exit
134
- -i, --inline Replace the file inline instead of returning the output to stdout
135
- --ensure_ascii Pass the ensure_ascii parameter to json.dumps()
136
- --indent INDENT Number of spaces for indentation (Default 2)
132
+ -h, --help show this help message and exit
133
+ -i, --inline Replace the file inline instead of returning the output to stdout
134
+ -o TARGET, --output TARGET
135
+ If specified, the output will be written to TARGET filename instead of stdout
136
+ --ensure_ascii Pass ensure_ascii=True to json.dumps()
137
+ --indent INDENT Number of spaces for indentation (Default 2)
137
138
  ```
138
- to learn how to use it
139
139
 
140
140
  ## Adding to requirements
141
141
  **Please pin this library only on the major version!**
@@ -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.29.0"
6
+ version = "0.29.1"
7
7
  license = {file = "LICENSE"}
8
8
  authors = [
9
9
  { name="Stefano Baccianella", email="4247706+mangiucugna@users.noreply.github.com" },
@@ -760,7 +760,7 @@ def from_file(
760
760
  return jsonobj
761
761
 
762
762
 
763
- def cli(): # pragma: no cover
763
+ def cli(inline_args: Optional[List[str]] = None) -> int:
764
764
  parser = argparse.ArgumentParser(description="Repair and parse JSON files.")
765
765
  parser.add_argument("filename", help="The JSON file to repair")
766
766
  parser.add_argument(
@@ -769,10 +769,16 @@ def cli(): # pragma: no cover
769
769
  action="store_true",
770
770
  help="Replace the file inline instead of returning the output to stdout",
771
771
  )
772
+ parser.add_argument(
773
+ "-o",
774
+ "--output",
775
+ metavar="TARGET",
776
+ help="If specified, the output will be written to TARGET filename instead of stdout",
777
+ )
772
778
  parser.add_argument(
773
779
  "--ensure_ascii",
774
780
  action="store_true",
775
- help="Pass the ensure_ascii parameter to json.dumps()",
781
+ help="Pass ensure_ascii=True to json.dumps()",
776
782
  )
777
783
  parser.add_argument(
778
784
  "--indent",
@@ -781,24 +787,36 @@ def cli(): # pragma: no cover
781
787
  help="Number of spaces for indentation (Default 2)",
782
788
  )
783
789
 
784
- args = parser.parse_args()
790
+ if inline_args is None: # pragma: no cover
791
+ args = parser.parse_args()
792
+ else:
793
+ args = parser.parse_args(
794
+ inline_args
795
+ ) # This is needed so this function is testable
796
+
797
+ if args.inline and args.output: # pragma: no cover
798
+ print("Error: You cannot pass both --inline and --output", file=sys.stderr)
799
+ sys.exit(1)
785
800
 
786
801
  ensure_ascii = False
787
802
  if args.ensure_ascii:
788
803
  ensure_ascii = True
804
+
789
805
  try:
790
806
  result = from_file(args.filename)
791
807
 
792
- if args.inline:
793
- fd = open(args.filename, mode="w")
808
+ if args.inline or args.output:
809
+ fd = open(args.output or args.filename, mode="w")
794
810
  json.dump(result, fd, indent=args.indent, ensure_ascii=ensure_ascii)
795
811
  fd.close()
796
812
  else:
797
813
  print(json.dumps(result, indent=args.indent, ensure_ascii=ensure_ascii))
798
- except Exception as e:
814
+ except Exception as e: # pragma: no cover
799
815
  print(f"Error: {str(e)}", file=sys.stderr)
800
816
  sys.exit(1)
801
817
 
818
+ return 0 # Success
819
+
802
820
 
803
821
  if __name__ == "__main__": # pragma: no cover
804
- cli()
822
+ sys.exit(cli())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: json_repair
3
- Version: 0.29.0
3
+ Version: 0.29.1
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
@@ -156,24 +156,24 @@ Install the library for command-line with:
156
156
  ```
157
157
  pipx install json-repair
158
158
  ```
159
- then run
159
+ to know all options available:
160
160
  ```
161
161
  $ json_repair -h
162
-
163
- usage: json_repair [-h] [-i] [--ensure_ascii] [--indent INDENT] filename
162
+ usage: json_repair [-h] [-i] [-o TARGET] [--ensure_ascii] [--indent INDENT] filename
164
163
 
165
164
  Repair and parse JSON files.
166
165
 
167
166
  positional arguments:
168
- filename The JSON file to repair
167
+ filename The JSON file to repair
169
168
 
170
169
  options:
171
- -h, --help show this help message and exit
172
- -i, --inline Replace the file inline instead of returning the output to stdout
173
- --ensure_ascii Pass the ensure_ascii parameter to json.dumps()
174
- --indent INDENT Number of spaces for indentation (Default 2)
170
+ -h, --help show this help message and exit
171
+ -i, --inline Replace the file inline instead of returning the output to stdout
172
+ -o TARGET, --output TARGET
173
+ If specified, the output will be written to TARGET filename instead of stdout
174
+ --ensure_ascii Pass ensure_ascii=True to json.dumps()
175
+ --indent INDENT Number of spaces for indentation (Default 2)
175
176
  ```
176
- to learn how to use it
177
177
 
178
178
  ## Adding to requirements
179
179
  **Please pin this library only on the major version!**
@@ -1,4 +1,8 @@
1
- from src.json_repair.json_repair import from_file, repair_json, loads
1
+ from src.json_repair.json_repair import from_file, repair_json, loads, cli
2
+ from unittest.mock import patch
3
+ import os.path
4
+ import pathlib
5
+ import tempfile
2
6
 
3
7
  def test_basic_types_valid():
4
8
  assert repair_json("True", return_objects=True) == ""
@@ -225,10 +229,6 @@ def test_repair_json_skip_json_loads():
225
229
 
226
230
 
227
231
  def test_repair_json_from_file():
228
- import os.path
229
- import pathlib
230
- import tempfile
231
-
232
232
  path = pathlib.Path(__file__).parent.resolve()
233
233
 
234
234
  # Use chunk_length 2 to test the buffering feature
@@ -263,3 +263,50 @@ def test_repair_json_from_file():
263
263
 
264
264
  def test_ensure_ascii():
265
265
  assert repair_json("{'test_中国人_ascii':'统一码'}", ensure_ascii=False) == '{"test_中国人_ascii": "统一码"}'
266
+
267
+
268
+
269
+ def test_cli(capsys):
270
+ # Create a temporary file
271
+ temp_fd, temp_path = tempfile.mkstemp(suffix=".json")
272
+ try:
273
+ # Write content to the temporary file
274
+ with os.fdopen(temp_fd, 'w') as tmp:
275
+ tmp.write("{key:value")
276
+ cli(inline_args=[temp_path, '--indent', 0, '--ensure_ascii'])
277
+ captured = capsys.readouterr()
278
+ assert captured.out == '{\n"key": "value"\n}\n'
279
+
280
+ # Test the output option
281
+ tempout_fd, tempout_path = tempfile.mkstemp(suffix=".json")
282
+ cli(inline_args=[temp_path, '--indent', 0, '-o', tempout_path])
283
+ with open(tempout_path, 'r') as tmp:
284
+ out = tmp.read()
285
+ assert out == '{\n"key": "value"\n}'
286
+
287
+ # Test the inline option
288
+ cli(inline_args=[temp_path, '--indent', 0, '-i'])
289
+ with open(temp_path, 'r') as tmp:
290
+ out = tmp.read()
291
+ assert out == '{\n"key": "value"\n}'
292
+
293
+
294
+ finally:
295
+ # Clean up - delete the temporary file
296
+ os.remove(temp_path)
297
+ os.remove(tempout_path)
298
+
299
+ """
300
+ def test_cli_inline(sample_json_file):
301
+ with patch('sys.argv', ['json_repair', sample_json_file, '-i']):
302
+ cli()
303
+ with open(sample_json_file, 'r') as f:
304
+ assert json.load(f) == {"key": "value"}
305
+
306
+ def test_cli_output_file(sample_json_file, tmp_path):
307
+ output_file = tmp_path / "output.json"
308
+ with patch('sys.argv', ['json_repair', sample_json_file, '-o', str(output_file)]):
309
+ cli()
310
+ with open(output_file, 'r') as f:
311
+ assert json.load(f) == {"key": "value"}
312
+ """
File without changes
File without changes