Skip to Content.
Sympa Menu

idok-commit - [idok-commit] idok commit r31 - trunk/python/pydok/test

idok-commit AT lists.psi.ch

Subject: Commit emails of the iDok project

List archive

[idok-commit] idok commit r31 - trunk/python/pydok/test


Chronological Thread 
  • From: "AFS account Roman Geus" <geus AT savannah.psi.ch>
  • To: idok-commit AT lists.psi.ch
  • Subject: [idok-commit] idok commit r31 - trunk/python/pydok/test
  • Date: Fri, 7 Mar 2008 15:29:15 +0100
  • List-archive: <https://lists.web.psi.ch/pipermail/idok-commit/>
  • List-id: Commit emails of the iDok project <idok-commit.lists.psi.ch>

Author: geus
Date: Fri Mar 7 15:29:15 2008
New Revision: 31

Log:
Implemented iDok upload demo web application, first draft

Added:
trunk/python/pydok/test/idok_upload_cgi.py (contents, props changed)

Added: trunk/python/pydok/test/idok_upload_cgi.py
==============================================================================
--- (empty file)
+++ trunk/python/pydok/test/idok_upload_cgi.py Fri Mar 7 15:29:15 2008
@@ -0,0 +1,141 @@
+#! /usr/bin/python
+
+##
+## Copyright (C) 2006-2008 iDok team.
+##
+## This program is free software; you can redistribute it and/or
+## modify it under the terms of the GNU General Public License
+## as published by the Free Software Foundation; either version 2
+## of the License, or (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+##
+## See the GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
+##
+
+"""Simple web application for uploading files to iDok
+"""
+
+import cgi, os, sys, urllib, StringIO
+
+# Enable HTML formatted stack traces
+import cgitb; cgitb.enable()
+
+# Add non-default directories to PYTHONPATH
+sys.path.append("/opt/m2crypto/m2crypto-0.18/lib/python2.5/site-packages")
+sys.path.append("/opt/omniorb/omniorb-4.1.0/lib/python2.5/site-packages")
+from pydok import IdokCorba
+from pydok.handler import ClientHandler_i
+from pydok.corba.common import ServiceException
+
+IDOK_HOST = "dms02.psi.ch"
+GSSAPI_SERVICE_NAME = "HTTP AT dms02.psi.ch"
+BASE_URL = "https://dms02.psi.ch/sandbox/twiki2dms/";
+
+class MyFieldStorage(cgi.FieldStorage):
+ "Subclass of cgi.FieldStorage that supports named access uploaded files"
+ def __init__(self, *args, **kwargs):
+ self.opened_files = []
+ cgi.FieldStorage.__init__(self, *args, **kwargs)
+
+ def __del__(self):
+ "Delete all temporary files"
+ for tempf in self.opened_files:
+ os.unlink(tempf.name)
+
+ def make_file(self, binary=None):
+ "Override method of base class"
+ import tempfile
+ tempf = tempfile.NamedTemporaryFile("w+b")
+ self.opened_files.append(tempf)
+ return tempf
+
+def idok_cli(command):
+ "Call CLI CORBA service and return output as a string"
+ idok = IdokCorba(IDOK_HOST, GSSAPI_SERVICE_NAME)
+ file_buf = StringIO.StringIO()
+ clienthandler = ClientHandler_i(idok, file_buf)
+ clienthandler_servant = clienthandler._this()
+ cli_service = idok.cli_factory_service.create(clienthandler_servant)
+ cli_service.sendCommand(command)
+ return file_buf.getvalue()
+
+def main():
+ "Main CGI function"
+ form = MyFieldStorage()
+ json_mode = form.getfirst("json_mode")
+ callback = form.getfirst("callback")
+ twiki_topic = form.getfirst("topic")
+ twiki_web = form.getfirst("web")
+ if json_mode:
+ fo = StringIO.StringIO()
+ print "Content-Type: application/x-javascript; charset=utf-8"
+ print
+ else:
+ fo = sys.stdout
+ print "Content-Type: text/html; charset=utf-8"
+ print
+ print """<html>
+ <title>Upload to iDok</title>
+ <body>"""
+ fo.write("<h1>Upload to iDok</h1>")
+ if twiki_topic:
+ fo.write("<p><b>TWiki Topic</b>:%s</p>" % twiki_topic)
+ if twiki_web:
+ fo.write("<p><b>TWiki Web</b>:%s</p>" % twiki_web)
+ fo.write('<p>Files will be uploaded to <a href="%s">%s</a></p>' %
+ (BASE_URL, BASE_URL))
+ fo.write("""<form action="idok_upload_cgi.py" method="post"
enctype="multipart/form-data">
+ <p>Choose a file to upload:
+ <input name="upload_file" type="file" size="50">
+ </p>
+ <p><input type="submit" value=" Upload "></p>
+ """)
+ if twiki_topic:
+ fo.write('<input type="hidden" name="topic" value="%s">' %
twiki_topic)
+ if twiki_web:
+ fo.write('<input type="hidden" name="web" value="%s">' % twiki_web)

+ if json_mode:
+ fo.write('<input type="hidden" name="json_mode" value="1">')
+ fo.write("</form>")
+ if form.has_key("upload_file"):
+ fileitem = form["upload_file"]
+ if fileitem.file and fileitem.filename and fileitem.done != -1:
+ head, tail = os.path.split(fileitem.filename)
+ dest_url = BASE_URL + urllib.pathname2url(tail)
+ file_size = os.stat(fileitem.file.name).st_size
+ command = 'put -m "Imported by idok_upload_cgi.py" %s %s' % \
+ (fileitem.file.name, dest_url)
+ try:
+ output = idok_cli(command)
+ fo.write("<h2>iDok CLI service output</h2>")
+ fo.write("<pre>%s</pre>" % output)
+ fo.write('<p>Local file <i>%s</i> uploaded to <a
href="%s">%s</a> (%d bytes)</p>' % \
+ (fileitem.filename, dest_url, dest_url, file_size))
+ except ServiceException, se:
+ fo.write("<h2>iDok CLI service output</h2>")
+ fo.write("<p><b>iDok CLI service returned the following
error</b>: %s</p>" % se.userMessage)
+ if se.detailedMessage:
+ fo.write("<h3>Detailed message</h3><pre>%s</pre>" %
se.detailedMessage)
+ if se.stackTrace:
+ fo.write("<h3>Stack trace</h3><pre>%s</pre>" %
se.stackTrace)
+ if json_mode:
+ import json
+ if callback:
+ print "%s(%s)" % (callback,
+ json.write({"#search": fo.getvalue()}))
+ else:
+ fo.write("""</body>
+ </html>
+ """)
+
+if __name__ == "__main__":
+ main()
+ sys.exit(0)
+
\ No newline at end of file



  • [idok-commit] idok commit r31 - trunk/python/pydok/test, AFS account Roman Geus, 03/07/2008

Archive powered by MHonArc 2.6.19.

Top of Page