forked from levxyca/diciotech
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_data.py
36 lines (26 loc) · 1.14 KB
/
format_data.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
import json
from argparse import ArgumentParser
from pathlib import Path
from unicodedata import category, normalize
def strip_accents(s: str) -> str:
return ''.join(c for c in normalize('NFD', s) if category(c) != 'Mn')
def main(file: Path):
with open(file) as f:
data = json.load(f)
# sort cards by 'title' value
data['cards'] = sorted(data['cards'], key=lambda x: strip_accents(x['title'].lower()))
# sort tags inside each card by value
for card in data['cards']:
card['tags'] = sorted(card['tags'], key=lambda x: strip_accents(x.lower()))
# sort keys in the cards by reverse key order
data['cards'] = [{k: v for k, v in sorted(card.items(), reverse=True)} for card in data['cards']]
# save data back to the file
with open(file, 'w') as f:
json.dump(data, f, indent=2, sort_keys=False, ensure_ascii=False)
f.write('\n')
if __name__ == '__main__':
parser = ArgumentParser(description="Sort data.")
parser.add_argument('-f', '--file', type=Path, required=True,
help='Path to the file containing the data')
args = parser.parse_args()
main(args.file)