-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathprocess_json.py
More file actions
117 lines (95 loc) · 3.74 KB
/
process_json.py
File metadata and controls
117 lines (95 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
'''
you don't need to run this script,
unless you'd like to update the files here with the latest
version of VSCode.
usage:
1) install and open the latest VSCode
2) press Ctrl-Shift-P or Cmd-Shift-P to open 'quick start'
3) type 'Open Default Keyboard Shortcuts (JSON)' into the box and press Enter
4) copy and paste the resulting json file into
scripts/linux.keybindings.raw.json
scripts/windows.keybindings.raw.json
scripts/macos.keybindings.raw.json
, based on your OS
5) uncomment one of the lines like
processRawFile('linux.keybindings.raw.json')
at the bottom of this file
by removing the # and saving changes.
6) run this script, for example, in a terminal, run
cd scripts
python3 process_json.py
with any recent version of Python.
the script will replace/generate files like
linux.keybindings.json
linux.negative.keybindings.json
in the parent directory.
'''
import re
import os
def makeCommandsNegatives(s):
# it would be less fragile to actually parse the json,
# but this should be good enough for now because even if a string
# like this were to occur in a command, the quotes would be escaped.
# note: allow any number of spaces after the ".
sNegative = re.sub(r'(", *")command": "', r'\1command": "-', s)
if s == sNegative:
print('Warning: no commands found to make negative.')
return sNegative
def finishingTouches(filename, osname):
with open(filename, 'rb') as fIn:
b = fIn.read()
# fix line-endings
if osname == 'windows':
b = b.replace(b'\r\n', b'\n').replace(b'\n', b'\r\n')
else:
b = b.replace(b'\r\n', b'\n')
# for macos, indent everything but the opening [ by 4 spaces
if osname == 'macos':
b = b.replace(b'\n', b'\n ')
b = b.replace(b'\n [\n', b'\n[\n')
# for windows, indent everything but the opening [ by 2 spaces
if osname == 'windows':
b = b.replace(b'\r\n', b'\r\n ')
b = b.replace(b'\r\n [\r\n', b'\r\n[\r\n')
# for linux, indent everything but the opening [ by 2 spaces
if osname == 'linux' :
b = b.replace(b'\n', b'\n ')
b = b.replace(b'\n [\n', b'\n[\n')
# remove unnecessary line
b = b.replace(b'// Override key bindings by placing them into your key bindings file.', b'')
# remove list of available commands
b = b.split(b'// Here are other available commands:')[0]
with open(filename, 'wb') as fOut:
fOut.write(b)
def processRawFile(inputFile):
if not os.path.isfile(inputFile):
print('Not found: ' + inputFile)
return
osname = inputFile.split('.')[0]
if osname not in ['linux', 'windows', 'macos']:
print('Expected the filename to start with linux, windows, or macos')
return
with open(inputFile, encoding='utf-8') as fIn:
s = fIn.read()
# get rid of opening/closing whitespace
s = s.strip()
outputFile = '../' + osname + '.keybindings.json'
with open(outputFile, 'w', encoding='utf-8') as fOut:
fOut.write(s)
outputNegFile = '../' + osname + '.negative.keybindings.json'
with open(outputNegFile, 'w', encoding='utf-8') as fOut:
fOut.write(makeCommandsNegatives(s))
finishingTouches(outputFile, osname)
finishingTouches(outputNegFile, osname)
print('Wrote to ' + outputFile)
print('Wrote to ' + outputNegFile)
if __name__ == '__main__':
print('Processing json')
if os.path.exists('linux.keybindings.raw.json'):
processRawFile('linux.keybindings.raw.json')
if os.path.exists('windows.keybindings.raw.json'):
processRawFile('windows.keybindings.raw.json')
if os.path.exists('macos.keybindings.raw.json'):
processRawFile('macos.keybindings.raw.json')
print('Done')