view 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
line wrap: on
line source

import lisparser

def readAlist(fn):
  with open(fn,'r') as f:
    sline = f.readline()
    alines = [l for l in f if (L:=l).startswith("(")]
  return sline, alist(alines), L

def alist(lines):
  res = {}
  for l in lines:
    ll = lisparser.get_ast(lisparser.normalize_str(l))[0]
    k = ll.pop(0)
    n = ll.pop(0)
    t = ll.pop(0)
    pp = dict((a[0],a[1:]) for a in ll.pop(0)) if ll else None
    res[eval(k)]=(n, t, pp, ll)
  return res

def p2l(pl, f, top = False):
  if isinstance(pl,list) or isinstance(pl,tuple):
    if len(pl) == 0:
      f.write('nil')
    else:
      f.write('(')
      space = False
      for e in pl:
        if space:
          f.write(' ')
          p2l(e,f)
        else:
          p2l(e,f)
          space = True
      f.write(')')
  elif isinstance(pl,dict):
    if top:
      f.write("(setq gnus-newsrc-alist '(\n")
    space = False
    for k, v in pl.items():
      if space:
        f.write('%s('%('\n' if top else ' '))
      else:
        f.write('(' if top else '((')
        space = True
      if top:
        f.write('"%s"'%k)
      else:
        f.write(k)
      for e in v:
        if e:
          # 4th tuple, from tail of original (ref 'll' in alist), may be empty
          f.write(' ')
          p2l(e,f)
      f.write(')')
    if top:
      f.write('\n))\n')
    else:
      f.write(')')
  elif isinstance(pl,str):
    if pl in ['.','nil']:
      f.write(pl)
    else:
      try:
        int(pl)
        f.write(pl)
      except ValueError:
        f.write('"%s"'%pl)
  else:
    f.write(pl)