plint

French poetry validator
git clone https://a3nm.net/git/plint/
Log | Files | Refs | README

plint_irc.py (3260B)


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