pycl.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2009 David Hilley <davidhi@cc.gatech.edu>
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. import cgi, urlparse
  18. import subprocess
  19. import tempfile, time
  20. import os, sys
  21. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  22. temp_has_delete=True
  23. class Handler(BaseHTTPRequestHandler):
  24. global temp_has_delete
  25. def do_GET(self):
  26. self.send_error(404, "Not Found: %s" % self.path)
  27. def do_POST(self):
  28. try:
  29. (content, params) = cgi.parse_header(self.headers.
  30. getheader('content-type'))
  31. clength = 0
  32. cl = self.headers.getheader('content-length')
  33. if cl != None:
  34. clength = int(cl)
  35. else:
  36. self.send_response(411)
  37. self.end_headers()
  38. return
  39. body = self.rfile.read(clength)
  40. print body
  41. l = [s for s in self.path.split('/') if s]
  42. print l
  43. # write text into file
  44. if temp_has_delete==True:
  45. f = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
  46. fname = f.name
  47. else:
  48. tf = tempfile.mkstemp(suffix='.txt')
  49. f = os.fdopen(tf[0],"w")
  50. fname = tf[1]
  51. f.write(body)
  52. f.close()
  53. # spawn editor...
  54. print "Spawning editor... ", fname
  55. p = subprocess.Popen(["/usr/bin/emacsclient", "-c", fname], close_fds=True)
  56. # hold connection open until editor finishes
  57. p.wait()
  58. self.send_response(200)
  59. self.end_headers()
  60. f = file(fname, 'r')
  61. s = f.read()
  62. f.close()
  63. try:
  64. os.unlink(fname)
  65. except :
  66. print "Unable to unlink:", fname
  67. pass
  68. self.wfile.write(s)
  69. except :
  70. print "Error: ", sys.exc_info()[0]
  71. self.send_error(404, "Not Found: %s" % self.path)
  72. def main():
  73. global temp_has_delete
  74. import platform
  75. t = platform.python_version_tuple()
  76. if int(t[0]) == 2 and int(t[1]) < 6:
  77. temp_has_delete = False;
  78. print "Handling lack of delete for NamedTemporaryFile:", temp_has_delete
  79. try:
  80. httpserv = HTTPServer(('localhost', 9292), Handler)
  81. httpserv.table = {}
  82. httpserv.serve_forever()
  83. except KeyboardInterrupt:
  84. httpserv.socket.close()
  85. if __name__ == '__main__':
  86. main()