view repair.py @ 62:c82a8743fd48

taking notes on how to merge
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Thu, 14 Dec 2023 00:13:19 +0000
parents bc1acb1416ab
children 721bd7a04efb
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)
    if ll:
      pp = ll.pop(0)
      if pp[0] == '(':
        pp = dict((a[0],a[1:]) for a in pp)
      # Otherwise only "nil", I think, so pass through
    else:
      pp = None
    res[eval(k)]=(n, t, pp, ll)
  return res

# alist fields are: group, rank, read, marks, method, params
                                                    
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)
        for e in v[:3]: # v is [n, t, pp, ll], pp may be None
          if e:
            f.write(' ')
            p2l(e,f)
        # ll may be empty
        for e in v[3]:
          if e:
            f.write(' ')
            p2l(e,f)
      else:
        f.write(k)
        for e in v:
          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(pl)
  else:
    f.write(pl)

def merge(gnus, mail):
  '''
   rank, read, marks, method, params
   
   read is everything that is unmarked (nothing in left column)
   marks.seen is everything that has ever been looked it and not DELETED
   marks.tick is a !, marks.forward is F, marks.reply is A.

   rank: deeper in gnus wins
   read: if unequal
           if mail is nil, use gnus
           otherwise use mail
   marks: merge unseen keys, unequal values for same key prefer gnus
          EXCEPT bogus, w3c-ac-forum, handle by hand
   method: change "ht" to "nnml+ht", flag anything else
   params: TBD

 Comparison tool:

   export P='\(nil\|(\(\([0-9]\+\|([^)]*)\) \?\)*)\)'
   paste <(cat shared) <(fgrep -f shared mail/alist.fixed | sed 's/ \([0-9]\) '"$P $P/ \1  \2      \5      /g" | cut -f 3)  <(fgrep -f shared gnus/alist.fixed | sed 's/ \([0-9]\) '"$P $P/ \1     \2      \5      /g" | cut -f 3) | { IFS='       ' ; while read gn g m; do if [ "$g" != "$m" ]; then printf "=----%s------\n%s\n%s\n" "$gn" "$g" "$m"; fi; done ; } | less

 Watch out for tabs!
 To look for overlap, change 2nd \n in printf to \t

'''