Some of the repetitive violations were suppressed.
2.37KiB; Python | 2019-04-30 02:33:11+02 | SLOC 62
1
#!/usr/bin/python
2
3
# Consolidates duplicate key-value pairs in a JSON file.
4
# If the same key is used with different values, no warning is given,
5
# and there is no guarantee about which key-value pair will be output.
6
# There is also no guarantee as to the order of the key-value pairs
7
# output.
8
#
9
# Copyright 2013 Google Inc.
10
# https://developers.google.com/blockly/
11
#
12
# Licensed under the Apache License, Version 2.0 (the "License");
13
# you may not use this file except in compliance with the License.
14
# You may obtain a copy of the License at
15
#
16
#   http://www.apache.org/licenses/LICENSE-2.0
17
#
18
# Unless required by applicable law or agreed to in writing, software
19
# distributed under the License is distributed on an "AS IS" BASIS,
20
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
# See the License for the specific language governing permissions and
22
# limitations under the License.
23
24
import argparse
25
import codecs
26
import json
27
from common import InputError
28
29
30
def main():
31 1
  """Parses arguments and iterates over files.
32
33
  Raises:
34
    IOError: An I/O error occurred with an input or output file.
35
    InputError: Input JSON could not be parsed.
36
  """
37
38
  # Set up argument parser.
39 1
  parser = argparse.ArgumentParser(
40
      description='Removes duplicate key-value pairs from JSON files.')
41 1
  parser.add_argument('--suffix', default='',
42
                      help='optional suffix for output files; '
43
                      'if empty, files will be changed in place')
44 1
  parser.add_argument('files', nargs='+', help='input files')
45 1
  args = parser.parse_args()
46
47
  # Iterate over files.
48 1
  for filename in args.files:
49
    # Read in json using Python libraries.  This eliminates duplicates.
50 1
    print('Processing ' + filename + '...')
51 1
    try:
bad-indentation Bad indentation. Found 4 spaces, expected 8 (kritika/Pylint) Filter like this
52 1
      with codecs.open(filename, 'r', 'utf-8') as infile:
53 1
        j = json.load(infile)
54
    except ValueError as e:
55
      print('Error reading ' + filename)
56
      raise InputError(filename, str(e))
57
58
    # Built up output strings as an array to make output of delimiters easier.
59
    output = []
60
    for key in j:
61
      if key != '@metadata':
62
        output.append('\t"' + key + '": "' +
63
                      j[key].replace('\n', '\\n') + '"')
64
65
    # Output results.
66
    with codecs.open(filename + args.suffix, 'w', 'utf-8') as outfile:
67
      outfile.write('{\n')
68
      outfile.write(',\n'.join(output))
69
      outfile.write('\n}\n')
70
71
72
if __name__ == '__main__':
73
  main()