Mercurial > hg > python
comparison repair.py @ 60:bc1acb1416ab
working on fixing gnus home foulup, see /disk/scratch/{mail,gnus}
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Wed, 13 Dec 2023 17:31:28 +0000 |
parents | |
children | c82a8743fd48 |
comparison
equal
deleted
inserted
replaced
54:dd63412fc882 | 60:bc1acb1416ab |
---|---|
1 import lisparser | |
2 | |
3 def readAlist(fn): | |
4 with open(fn,'r') as f: | |
5 sline = f.readline() | |
6 alines = [l for l in f if (L:=l).startswith("(")] | |
7 return sline, alist(alines), L | |
8 | |
9 def alist(lines): | |
10 res = {} | |
11 for l in lines: | |
12 ll = lisparser.get_ast(lisparser.normalize_str(l))[0] | |
13 k = ll.pop(0) | |
14 n = ll.pop(0) | |
15 t = ll.pop(0) | |
16 pp = dict((a[0],a[1:]) for a in ll.pop(0)) if ll else None | |
17 res[eval(k)]=(n, t, pp, ll) | |
18 return res | |
19 | |
20 def p2l(pl, f, top = False): | |
21 if isinstance(pl,list) or isinstance(pl,tuple): | |
22 if len(pl) == 0: | |
23 f.write('nil') | |
24 else: | |
25 f.write('(') | |
26 space = False | |
27 for e in pl: | |
28 if space: | |
29 f.write(' ') | |
30 p2l(e,f) | |
31 else: | |
32 p2l(e,f) | |
33 space = True | |
34 f.write(')') | |
35 elif isinstance(pl,dict): | |
36 if top: | |
37 f.write("(setq gnus-newsrc-alist '(\n") | |
38 space = False | |
39 for k, v in pl.items(): | |
40 if space: | |
41 f.write('%s('%('\n' if top else ' ')) | |
42 else: | |
43 f.write('(' if top else '((') | |
44 space = True | |
45 if top: | |
46 f.write('"%s"'%k) | |
47 else: | |
48 f.write(k) | |
49 for e in v: | |
50 if e: | |
51 # 4th tuple, from tail of original (ref 'll' in alist), may be empty | |
52 f.write(' ') | |
53 p2l(e,f) | |
54 f.write(')') | |
55 if top: | |
56 f.write('\n))\n') | |
57 else: | |
58 f.write(')') | |
59 elif isinstance(pl,str): | |
60 if pl in ['.','nil']: | |
61 f.write(pl) | |
62 else: | |
63 try: | |
64 int(pl) | |
65 f.write(pl) | |
66 except ValueError: | |
67 f.write('"%s"'%pl) | |
68 else: | |
69 f.write(pl) |