plint

French poetry validator (local mirror of https://gitlab.com/a3nm/plint)
git clone https://a3nm.net/git/plint/
Log | Files | Refs | README

__main__.py (2810B)


      1 #!/usr/bin/python3 -u
      2 
      3 from plint import localization, error, template, diaeresis
      4 import sys
      5 import argparse
      6 import json
      7 
      8 
      9 def run(ocontext=None, weight=None, offset=0, fmt="text"):
     10     is_ok = True
     11     f2 = None
     12     n_syllables = None
     13     if ocontext:
     14         f2 = open(ocontext, 'w')
     15     if weight:
     16         n_syllables = int(weight)
     17     should_end = False
     18     ret = []
     19     while True:
     20         line = sys.stdin.readline()
     21         if not line:
     22             should_end = True
     23             line = ""
     24         errors = template.check(line, f2, last=should_end, n_syllables=n_syllables, offset=offset)
     25         if errors:
     26             if not errors.isEmpty():
     27                 is_ok = False
     28             if not errors.isEmpty():
     29                 if fmt == "text":
     30                     print(errors.report(fmt=fmt), file=sys.stderr)
     31                 elif fmt == "json":
     32                     ret.append(errors.report(fmt=fmt))
     33                 else:
     34                     raise ValueError("bad format")
     35         if should_end:
     36             break
     37     if fmt == "json":
     38         print(json.dumps(ret, sort_keys=True, indent=4,
     39             separators={',', ': '}))
     40     return is_ok
     41 
     42 
     43 def main():
     44     global template
     45     localization.init_locale()
     46     parser = argparse.ArgumentParser(
     47             description=_("Check poem on stdin according to a template"))
     48     parser.add_argument("template",
     49             help=_("the file containing the template for the input poem"),
     50             type=str)
     51     parser.add_argument("--format", type=str,
     52             help=_("error output format (text or json)"),
     53             choices = ["text", "json"],
     54             default="text")
     55     parser.add_argument("--diaeresis", type=str,
     56             help=_("diaeresis training: diaeresis file to use"),
     57             default="data/diaeresis.json")
     58     parser.add_argument("--ocontext", type=str,
     59             help=_("diaeresis training: output file where to write the contexts"),
     60             default=None)
     61     parser.add_argument("--weight", type=int,
     62             help=_("diaeresis training: fixed weight for a specific chunk"),
     63             default=None)
     64     parser.add_argument("--offset", type=int,
     65             help=_("diaeresis training: position of the specific chunk from the end"),
     66             default=0)
     67     args = parser.parse_args()
     68 
     69     template_name = args.template
     70     diaeresis.set_diaeresis(args.diaeresis)
     71     
     72     f = open(template_name)
     73     x = f.read()
     74     f.close()
     75 
     76     try:
     77         template = template.Template(x)
     78     except error.TemplateLoadError as e:
     79         print(_("Could not load template %s: %s") % (template_name, e.msg), file=sys.stderr)
     80         sys.exit(2)
     81     ok = run(ocontext=args.ocontext, weight=args.weight, offset=args.offset,
     82             fmt=args.format)
     83     sys.exit(0 if ok else 1)
     84 
     85 
     86 if __name__ == '__main__':
     87     main()