forked from miracle2k/linuxutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinisort.py
executable file
·50 lines (43 loc) · 1.01 KB
/
inisort.py
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
#!/usr/bin/python
"""
From: http://code.activestate.com/recipes/576587-sort-sections-and-keys-in-ini-file/
Original Author: Michal Niklas
"""
import sys
USAGE = 'USAGE:\n\tinisort.py file.ini'
def sort_ini(stream):
"""sort .ini file: sorts sections and in each section sorts keys"""
lines = stream.readlines()
section = ''
sections = {}
for line in lines:
line = line.strip()
if line:
if line.startswith('['):
section = line
continue
if section:
try:
sections[section].append(line)
except KeyError:
sections[section] = [line, ]
if sections:
sk = sections.keys()
sk.sort()
for k in sk:
vals = sections[k]
vals.sort()
print k
print '\n'.join(vals)
print
def main():
if '-h' in sys.argv or '--help' in sys.argv or len(sys.argv) > 2:
print USAGE
else:
if len(sys.argv) == 2:
with open(sys.argv[1]) as f:
sort_ini(f)
else:
sort_ini(sys.stdin)
if __name__ == '__main__':
sys.exit(main() or 0)