pycl.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, re
  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. url = self.headers.getheader('x-url')
  45. print "url:", url
  46. prefix = "chrome_" + re.sub("[^.\w]", "_", re.sub("^.*?//","",url))
  47. if temp_has_delete==True:
  48. f = tempfile.NamedTemporaryFile(
  49. delete=False, prefix=prefix, suffix='.txt')
  50. fname = f.name
  51. else:
  52. tf = tempfile.mkstemp(prefix=prefix, suffix='.txt')
  53. f = os.fdopen(tf[0],"w")
  54. fname = tf[1]
  55. f.write(body)
  56. f.close()
  57. # spawn editor...
  58. print "Spawning editor... ", fname
  59. p = subprocess.Popen(["/usr/bin/emacsclient", "-c", fname], close_fds=True)
  60. # hold connection open until editor finishes
  61. p.wait()
  62. self.send_response(200)
  63. self.end_headers()
  64. f = file(fname, 'r')
  65. s = f.read()
  66. f.close()
  67. try:
  68. os.unlink(fname)
  69. except :
  70. print "Unable to unlink:", fname
  71. pass
  72. self.wfile.write(s)
  73. except :
  74. print "Error: ", sys.exc_info()[0]
  75. self.send_error(404, "Not Found: %s" % self.path)
  76. def main():
  77. global temp_has_delete
  78. import platform
  79. t = platform.python_version_tuple()
  80. if int(t[0]) == 2 and int(t[1]) < 6:
  81. temp_has_delete = False;
  82. print "Handling lack of delete for NamedTemporaryFile:", temp_has_delete
  83. try:
  84. httpserv = HTTPServer(('localhost', 9292), Handler)
  85. httpserv.table = {}
  86. httpserv.serve_forever()
  87. except KeyboardInterrupt:
  88. httpserv.socket.close()
  89. if __name__ == '__main__':
  90. main()