commit 227fee0b661409571cfab627bba8fdd6acef9cc2
parent 2113f564aed5d458c292d5fc358bc61a3a761f1f
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Tue, 2 Aug 2011 14:02:33 -0400
make it right
Diffstat:
boilint.py | | | 111 | +++++++++++++++++++++++++++++++++++++++++++------------------------------------ |
1 file changed, 60 insertions(+), 51 deletions(-)
diff --git a/boilint.py b/boilint.py
@@ -8,6 +8,62 @@ import template
from pprint import pprint
from common import normalize
+buf = ""
+lbuf = []
+
+def output(l):
+ print(' '.join(l[1:]))
+ print(' '.join(l[1:]), file=f)
+
+def manage(line, silent=False):
+ """manage one line, indicate if an error occurred"""
+ global buf
+ global lbuf
+ usebuf = False
+ l = line.rstrip().split(' ')
+ text = ' '.join(l[2:])
+ if normalize(text.strip()) == '':
+ return True # no text
+ first = [a for a in line.split(' ')[2:] if a != ''][0]
+ # TODO ignore leading symbols except '...' and '/me'
+ if first[-1] == ':' or first[0].upper() != first[0]:
+ return True # ignore non-poem lines
+ if first == '/me':
+ # always accept actions
+ if len(lbuf) > 0:
+ lbuf.append(l)
+ else:
+ if not silent:
+ print(' '.join(l[1:]), file=f)
+ f.flush()
+ return True
+ if first[0] == '/':
+ return True # ignore other commands
+ if first.lstrip().startswith("..."):
+ text = buf+text
+ usebuf = True
+ errors = template.check(text)
+ if len(errors) > 0 and text.rstrip().endswith("..."):
+ # it might be a call
+ buf = text
+ if usebuf:
+ lbuf.append(l)
+ else:
+ lbuf = [l]
+ return True
+ for error in errors:
+ print(error.report())
+ if len(errors) == 0:
+ buf = ""
+ if not silent:
+ if usebuf:
+ for bl in lbuf:
+ output(bl)
+ output(l)
+ f.flush()
+ lbuf = []
+ return len(errors) == 0
+
if len(sys.argv) != 3:
print("Usage: %s TEMPLATE POEM" % sys.argv[0], file=sys.stderr)
print("Check stdin according to template, report errors on stdout",
@@ -18,71 +74,24 @@ f = open(sys.argv[1])
template = template.Template(f)
f.close()
+template.reject_errors = True
+
f = open(sys.argv[2], 'r')
for line in f.readlines():
- l = line.split(' ')
- if l[1] == '/me':
- continue # ignore /me
- errors = template.check(' '.join(l[1:]))
- if len(errors) > 0:
+ if not manage(line, True):
print("Existing poem is wrong!", file=sys.stderr)
sys.exit(2)
f.close()
f = open(sys.argv[2], 'a')
-template.reject_errors = True
-
-buf = ""
-lbuf = []
-
-def output(l):
- print(' '.join(l[1:]))
- print(' '.join(l[1:]), file=f)
-
def run():
global lbuf
while True:
- usebuf = False
line = sys.stdin.readline()
if not line:
break
- l = line.rstrip().split(' ')
- text = ' '.join(l[2:])
- if normalize(text.strip()) == '':
- continue
- first = [a for a in line.split(' ')[2:] if a != ''][0]
- if first[-1] == ':' or first[0].upper() != first[0]:
- continue # ignore non-poem lines
- if first == '/me':
- # always accept actions
- if len(lbuf) > 0:
- lbuf.append(l)
- else:
- print(' '.join(l[1:]), file=f)
- f.flush()
- continue
- if first[0] == '/':
- continue # ignore other commands
- if first.lstrip().startswith("..."):
- text = buf+text
- usebuf = True
- errors = template.check(text)
- if len(errors) > 0 and text.rstrip().endswith("..."):
- # it might be a call
- buf = text
- lbuf.append(l)
- continue
- for error in errors:
- print(error.report())
- if len(errors) == 0:
- buf = ""
- if usebuf:
- for bl in lbuf:
- output(bl)
- output(l)
- lbuf = []
- f.flush()
+ manage(line)
run()