plint.py (2012B)
1 #!/usr/bin/python3 -u 2 3 from plint import localization, error, template, diaeresis 4 import sys 5 6 7 def run(): 8 ok = True 9 f2 = None 10 nsyl = None 11 offset = 0 12 if len(sys.argv) >= 4: 13 f2 = open(sys.argv[3], 'w') 14 if len(sys.argv) >= 5: 15 nsyl = int(sys.argv[4]) 16 if len(sys.argv) == 6: 17 offset = int(sys.argv[5]) 18 should_end = False 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=nsyl, offset=offset) 25 if errors: 26 print(errors.report(), file=sys.stderr) 27 ok = False 28 if should_end: 29 break 30 return ok 31 32 33 if __name__ == '__main__': 34 localization.init_locale() 35 if len(sys.argv) < 2 or len(sys.argv) > 6: 36 print(_("Usage: %s TEMPLATE [DFILE [OCONTEXT [NSYL [OFFSET]]]]") % sys.argv[0], 37 file=sys.stderr) 38 print(_("Check stdin according to TEMPLATE, report errors on stdout"), 39 file=sys.stderr) 40 print(_("For internal use:"), 41 file=sys.stderr) 42 print(_("DFILE is the diaeresis file, OCONTEXT is the context output file"), 43 file=sys.stderr) 44 print(_("NSYL is the assigned weight to the last chunk (diaeresis training)"), 45 file=sys.stderr) 46 print(_("OFFSET is to add after the last chunk (diaeresis training)"), 47 file=sys.stderr) 48 sys.exit(2) 49 50 template_name = sys.argv[1] 51 if len(sys.argv) > 2: 52 diaeresis_name = sys.argv[2] 53 else: 54 diaeresis_name = "../data/diaeresis.json" 55 diaeresis.set_diaeresis(diaeresis_name) 56 57 f = open(template_name) 58 x = f.read() 59 f.close() 60 61 try: 62 template = template.Template(x) 63 except error.TemplateLoadError as e: 64 print("Could not load template %s: %s" % (template_name, e.msg), file=sys.stderr) 65 sys.exit(2) 66 67 ok = run() 68 sys.exit(0 if ok else 1)