Mercurial > repos > remy-d1 > api_tools
changeset 1:33bb43de178f draft default tip
1st upload for print.py
author | remy-d1 |
---|---|
date | Tue, 05 Mar 2013 05:24:01 -0500 |
parents | ec6fdbac353f |
children | |
files | print.py |
diffstat | 1 files changed, 43 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/print.py Tue Mar 05 05:24:01 2013 -0500 @@ -0,0 +1,43 @@ +#!/usr/bin/env python + +import sys +import subprocess +import json + +def sendcommand(cmd): + """ + Take a shell bash command + Return the stdout value for a pipe subprocess + """ + proc = subprocess.Popen("bash",shell=True,stdin=subprocess.PIPE\ +,stdout=subprocess.PIPE,stderr=subprocess.PIPE) + return proc.communicate(cmd)[0] + + +def nl2br(string, is_xhtml= True ): + if is_xhtml: + return string.replace('\n','<br />\n') + else: + return string.replace('\n','<br>\n') + + +if __name__=="__main__": + nb_args = len(sys.argv) + html = "<html><head><title>Display.py API wrapper</title></head><body><p>" + if nb_args == 1: + html += sys.argv[1] + else: + cmd = sys.argv[1]+" "+sys.argv[2]+" "+sys.argv[3] + html += cmd+"<br />" + html += "Result(s) :<br />" + content = sendcommand(sys.argv[1]+" "+sys.argv[2]+" "+sys.argv[3]) + content = nl2br(content) + if sys.argv[4] == 'false': + #content = json.dumps(json.loads(content), sort_keys = True, indent = 2) + #content = json.dumps(content, sort_keys=True, indent=4, separators=(',', ': ') + html += content + else: + html += "<pre>"+content+"</pre>" + html += "</p></body>" + html += "</html>" + print html