forked from libretro/libretro-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clrmamepro-sorter.py
executable file
·42 lines (32 loc) · 1.15 KB
/
clrmamepro-sorter.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
#!/usr/bin/env python3
# This implements a very stupid ClrMamePro DAT file sorter. It expects a DAT
# file to be "well formed", so everything except the first level should be
# indented, since that is used to lump lines together into one "entry".
import re
import sys
from typing import List
START_STANZA = re.compile(r'([a-z0-9]+)\s*\(', re.IGNORECASE)
VALID_START = {'clrmamepro', 'game'}
def sortdat(lines: str) -> str:
data: List[str] = []
curline: str = ''
for line in lines.split('\n'):
start = START_STANZA.match(line)
if start:
if start.group(1) not in VALID_START:
raise Exception("File doesn't look like a valid DAT file!")
if curline != '':
data.append(curline)
curline = line
else:
curline += '\n' + line
if curline != '':
data.append(curline)
return '\n'.join(sorted(data))
if __name__ == '__main__':
for file in sys.argv[1:]:
print(f'Sorting {file}')
with open(file, 'r') as infile:
content = sortdat(infile.read())
with open(file, 'w') as outfile:
outfile.write(content)