plint_extra

various extra tools around plint
git clone https://a3nm.net/git/plint_extra/
Log | Files | Refs | README

plint_irc.py (3214B)


      1 #!/usr/bin/python3 -uO
      2 
      3 from plint import localization, rhyme, diaeresis
      4 import re
      5 import sys
      6 from plint.template import Template
      7 from pprint import pprint
      8 from plint.common import normalize
      9 
     10 buf = ""
     11 lbuf = []
     12 
     13 def write(l, descriptor=None):
     14   if descriptor:
     15     f = descriptor
     16   else:
     17     f = open(sys.argv[2], 'a')
     18   print(' '.join(l), file=f)
     19   if not descriptor:
     20     f.close()
     21 
     22 def output(l, descriptor):
     23   print(' '.join(l), file=descriptor)
     24   write(l, descriptor if descriptor != sys.stdout else None)
     25 
     26 def leading_cap(text):
     27   for c in text:
     28     if c.upper() == c.lower():
     29       continue # symbol
     30     if c != c.lower():
     31       return True
     32     if c != c.upper():
     33       return False
     34   return False
     35 
     36 def manage(line, descriptor=sys.stdout):
     37   """manage one line, indicate if an error occurred"""
     38   global buf
     39   global lbuf
     40   usebuf = False
     41   l = line.rstrip().split(' ')
     42   text = ' '.join(l[1:])
     43   if normalize(text.strip()) == '':
     44     return True # no text
     45   first = [a for a in l[1:] if a != ''][0]
     46   if first == '/me' and len(l) >= 3 and l[2] == 'plint':
     47     # always accept actions
     48     if len(lbuf) > 0:
     49       lbuf.append(l)
     50     else:
     51       write(l, descriptor)
     52     return True
     53   if first[0] == '/':
     54     return False # ignore other commands
     55   if first.lstrip().startswith("...") or first.lstrip().startswith("…"):
     56     text = buf+text
     57     usebuf = True
     58   if not usebuf:
     59     if first[-1] == ':':
     60       return False
     61     if not leading_cap(text):
     62       return False
     63     if (not (text.rstrip().endswith("...") or text.rstrip().endswith("…")
     64         or text.lstrip().endswith("...") or text.lstrip().endswith("…")) and
     65         len(text) < 13):
     66       return False # too short
     67     if len(text) > 130:
     68       return False # too long
     69   if (text.rstrip().endswith("...") or
     70       text.rstrip().endswith("…")):
     71     # it might be a call
     72     buf = text
     73     if usebuf:
     74       lbuf.append(l)
     75     else:
     76       lbuf = [l]
     77     return True
     78   errors = template.check(text)
     79   quiet = False
     80   if errors:
     81     print(errors.report())
     82   if not errors:
     83     buf = ""
     84     if usebuf:
     85       for bl in lbuf:
     86         output(bl, descriptor)
     87     output(l, descriptor)
     88     lbuf = []
     89   return not errors
     90 
     91 if len(sys.argv) not in [3, 4]:
     92   print("Usage: %s TEMPLATE POEM [OFFSET]" % sys.argv[0], file=sys.stderr)
     93   print("Check POEM according to TEMPLATE, add valid verse from stdin to POEM",
     94       file=sys.stderr)
     95   print("Ignore OFFSET lines from POEM",
     96       file=sys.stderr)
     97   sys.exit(1)
     98 
     99 localization.init_locale()
    100 
    101 f = open(sys.argv[1])
    102 x = f.read()
    103 f.close()
    104 template = Template(x)
    105 
    106 template.reject_errors = True
    107 
    108 offset = 0
    109 if len(sys.argv) == 4:
    110   offset = int(sys.argv[3])
    111 
    112 pos = 0
    113 
    114 localization.init_locale()
    115 f = open(sys.argv[2], 'r')
    116 for line in f.readlines():
    117   pos += 1
    118   if pos <= offset:
    119     continue # ignore first lines
    120   print("%s (read)" % line.rstrip(), file=sys.stderr)
    121   if not manage(line, sys.stderr):
    122     print("Existing poem is wrong!", file=sys.stderr)
    123     sys.exit(2)
    124 f.close()
    125 
    126 print("ready", file=sys.stderr)
    127 
    128 def run():
    129   global lbuf
    130   while True:
    131     line = sys.stdin.readline()
    132     if not line:
    133       break
    134     print("Seen: %s" % line, file=sys.stderr)
    135     manage(' '.join(line.split(' ')[1:]))
    136 
    137 run()
    138