pycl.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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
  21. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  22. class Handler(BaseHTTPRequestHandler):
  23. def do_GET(self):
  24. self.send_error(404, "Not Found: %s" % self.path)
  25. def do_POST(self):
  26. try:
  27. (content, params) = cgi.parse_header(self.headers.
  28. getheader('content-type'))
  29. clength = 0
  30. cl = self.headers.getheader('content-length')
  31. if cl != None:
  32. clength = int(cl)
  33. else:
  34. self.send_response(411)
  35. self.end_headers()
  36. return
  37. body = self.rfile.read(clength)
  38. print body
  39. l = [s for s in self.path.split('/') if s]
  40. print l
  41. # write text into file
  42. f = tempfile.NamedTemporaryFile(delete=False, suffix='.txt')
  43. f.write(body)
  44. f.close()
  45. # spawn editor...
  46. print "Spawning editor... ", f.name
  47. p = subprocess.Popen(["/usr/bin/emacsclient", f.name], close_fds=True)
  48. # hold connection open until editor finishes
  49. p.wait()
  50. self.send_response(200)
  51. self.end_headers()
  52. f = file(f.name, 'r')
  53. s = f.read()
  54. f.close()
  55. try:
  56. os.unlink(fname)
  57. except :
  58. pass
  59. self.wfile.write(s)
  60. except :
  61. self.send_error(404, "Not Found: %s" % self.path)
  62. def main():
  63. import platform
  64. t = platform.python_version_tuple()
  65. if int(t[0]) == 2 and int(t[1]) < 6:
  66. print "Python 2.6+ required"
  67. # uses tempfile.NamedTemporaryFile delete param
  68. return
  69. try:
  70. httpserv = HTTPServer(('localhost', 9292), Handler)
  71. httpserv.table = {}
  72. httpserv.serve_forever()
  73. except KeyboardInterrupt:
  74. httpserv.socket.close()
  75. if __name__ == '__main__':
  76. main()