0

I have the below python script, which accepts text input, format it (to html) and produces output.

It works ok when piping a static text input. However, it freezes (or probably waits for some kind of EOF) when the input is a stream of text.

I would like it to stream the output, instead of waiting the text to finish.

EDIT: It seems that I wasn't patient enough, and it waits on some buffer to fill before dumping the output. Any idea on how to dump the output for every line

Thanks

#!/usr/bin/env python

"""
Convert a piped in text input to html, while replacing file/path names with URLs
"""
import sys
import os
import re

def file_html(field):
    field = field.group()
    if os.path.isfile(field) or os.path.isdir(field):
        return """<a href="file://{0}"  target="_blank">{0}  </a>""".format(field)
    else:
        return field

if __name__ == "__main__":

    for line in sys.stdin:
        line = '<p><tt>' + re.sub('/[^\s]*', file_html, line) + '</tt></p>'
        sys.stdout.write(line)
Yuval Atzmon
  • 5,645
  • 3
  • 41
  • 74
  • 1
    The script looks fine. How are you piping a stream of text into the script? If I run, for example `curl https://www.gnu.org/licenses/gpl-3.0.txt | python yourscript.py` it works without hanging. – larsks Mar 21 '17 at 11:41
  • Thanks. It seems that I wasn't patient enough, and it waits on some buffer to fill before dumping the output. Any idea on how to dump the output for every line – Yuval Atzmon Mar 21 '17 at 11:49
  • Thanks, indeed after my edit, this question became a duplicate. As a side note: only the following solution was relevant to my problem: http://stackoverflow.com/a/18235323/2476373 – Yuval Atzmon Mar 21 '17 at 12:55

0 Answers0