-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwrite-grid-json.py
More file actions
executable file
·62 lines (55 loc) · 2 KB
/
Copy pathwrite-grid-json.py
File metadata and controls
executable file
·62 lines (55 loc) · 2 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
#!/usr/bin/env python3
from argparse import ArgumentParser
from collections import OrderedDict
import errno
import json
import os
import sys
aparser = ArgumentParser(description='Write icon grid json file')
aparser.add_argument('-o', '--output', help='output file')
aparser.add_argument('input', help='input template file')
aparser.add_argument('blacklist', help='blacklist file')
aparser.add_argument('cpu', help='CPU for blacklisting')
args = aparser.parse_args()
# Load with template and blacklist. Use OrderedDict for the grid to
# maintain sorting of the keys.
with open(args.input, 'r') as infile:
grid = json.load(infile, object_pairs_hook=OrderedDict)
with open(args.blacklist, 'r') as blfile:
blacklist = json.load(blfile)
# Open the a temporary version of the output file if specified, ensuring
# that leading directories are created first.
if args.output is None:
outfile = sys.stdout
else:
outdir = os.path.dirname(args.output)
if len(outdir) > 0:
try:
os.makedirs(outdir)
except OSError as err:
if err.errno != errno.EEXIST:
raise
outfile = open(args.output + '.tmp', 'w')
# Strip out blacklisted apps
cpu_blacklist = blacklist.get(args.cpu, [])
for app in cpu_blacklist:
if app in grid:
del grid[app]
for sect, apps in grid.items():
if app in apps:
grid[sect].remove(app)
# Check that all directories are in the top-level "desktop" pseudo-directory
desktop = grid["desktop"]
for directory in grid:
if directory != "desktop" and directory not in desktop:
raise ValueError(
'"{}" defined but not in "desktop" directory'.format(directory)
)
# Write out the new json, keeping the indentation, separators and
# trailing newline from the original. If successful, rename the
# temporary file to the final name.
json.dump(grid, outfile, indent=2, separators=(',', ' : '))
outfile.write('\n')
if args.output is not None:
outfile.close()
os.rename(args.output + '.tmp', args.output)