plint

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

plint.py (1370B)


      1 #!/usr/bin/python3 -uO
      2 
      3 import diaeresis
      4 import localization
      5 import sys
      6 import template
      7 import error
      8 
      9 def run():
     10   ok = True
     11   f2 = None
     12   if len(sys.argv) == 4:
     13     f2 = open(sys.argv[3], 'w')
     14   should_end = False
     15   while True:
     16     line = sys.stdin.readline()
     17     if not line:
     18       should_end = True
     19       line = ""
     20     errors = template.check(line, f2, last=should_end)
     21     if errors:
     22       print(errors.report(), file=sys.stderr)
     23       ok = False
     24     if should_end:
     25       break
     26   return ok
     27 
     28 if __name__ == '__main__':
     29   localization.init_locale()
     30   if len(sys.argv) < 2 or len(sys.argv) > 4:
     31     print(_("Usage: %s TEMPLATE [DFILE [OCONTEXT]]") % sys.argv[0],
     32         file=sys.stderr)
     33     print(_("Check stdin according to TEMPLATE, report errors on stdout"),
     34         file=sys.stderr)
     35     print(_("DFILE is the diaeresis file, OCONTEXT is the context output file"),
     36         file=sys.stderr)
     37     sys.exit(1)
     38 
     39   template_name = sys.argv[1]
     40   if len(sys.argv) > 2:
     41     diaeresis_name = sys.argv[2]
     42   else:
     43     diaeresis_name = "diaeresis.json"
     44 
     45   diaeresis.load_diaeresis(diaeresis_name)
     46 
     47   f = open(template_name)
     48   x = f.read()
     49   f.close()
     50 
     51   try:
     52     template = template.Template(x)
     53   except error.TemplateLoadError as e:
     54     print("Could not load template %s: %s" % (template_name, e.msg), file=sys.stderr)
     55     sys.exit(2)
     56 
     57   ok = run()
     58   sys.exit(0 if ok else 1)
     59