pycl.py 3.4 KB

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