diff --git a/stanza b/stanza index c49901e..75c3fa8 100755 --- a/stanza +++ b/stanza @@ -36,39 +36,36 @@ def debug(line): debug(pformat(args)) # Set up the file handle, or stdin -if cmd.filename: - handle=open(cmd.filename) -else: - handle=sys.stdin -# Go over the handle, line by line -for line in handle: - # Start buffering if we hit the start regex - if re.search(start,line): - buffering=True - debug('Started buffering on '+start) - - # Continue buffering if we've already started - if buffering: - output+=line +with open(cmd.filename,'r') if cmd.filename else sys.stdin as handle: + # Go over the handle, line by line + for line in handle: + # Start buffering if we hit the start regex + if re.search(start,line): + buffering=True + debug('Started buffering on '+start) + + # Continue buffering if we've already started + if buffering: + output+=line - # If a line in the stanza matches, we output the stanza - if buffering and re.search(match,line): - matched=True - debug('Matched on '+match) + # If a line in the stanza matches, we output the stanza + if buffering and re.search(match,line): + matched=True + debug('Matched on '+match) - # Output/clear buffer when we encounter an end to the stanza - if re.search(end,line) and buffering==True: - debug('Finished buffering on '+end) - if matched: - # Strip newlines if requrired - if cmd.oneline: - print output.replace("\n",'') - else: - print output - buffering=False - matched=False - output='' - else: - output='' - buffering=False + # Output/clear buffer when we encounter an end to the stanza + if re.search(end,line) and buffering==True: + debug('Finished buffering on '+end) + if matched: + # Strip newlines if requrired + if cmd.oneline: + print output.replace("\n",'') + else: + print output + buffering=False + matched=False + output='' + else: + output='' + buffering=False