Source code for openmdao.gui.handlers_workspace

import sys
import os
import re
import ast

import jsonpickle

from tornado import web

from openmdao.gui.handlers import ReqHandler
from openmdao.main.publisher import publish

[docs]class AddOnsHandler(ReqHandler): ''' addon installation utility Eventually we will probably wrap the OpenMDAO plugin functions to work through here. ''' addons_url = '' @web.authenticated
[docs] def post(self): ''' easy_install the POST'd addon ''' pass
[docs] def get(self): ''' show available plugins, prompt for plugin to be installed ''' self.render('workspace/addons.html')
[docs]class ReqHandler(ReqHandler): ''' render the base template ''' @web.authenticated
[docs] def post(self): ''' render the base template with the posted content ''' attributes = {} for field in ['head']: if field in self.request.arguments.keys(): attributes[field] = self.request.arguments[field][0] else: attributes[field] = False self.render('workspace/base.html', **attributes)
[docs] def get(self): attributes = {} print 'self.request.arguments:', self.request.arguments for field in ['head_script']: if field in self.request.arguments.keys(): s = self.request.arguments[field][0] s = re.sub(r'^"|"$', '', s) # strip leading/trailing quotes s = re.sub(r"^'|'$", "", s) # strip leading/trailing quotes attributes[field] = s else: attributes[field] = False self.render('workspace/base.html', **attributes)
[docs]class GeometryHandler(ReqHandler): @web.authenticated
[docs] def get(self): ''' geometry viewer ''' filename = self.get_argument('path') self.render('workspace/o3dviewer.html', filename=filename)
[docs]class CloseHandler(ReqHandler): @web.authenticated
[docs] def get(self): self.delete_server() self.redirect('/')
[docs]class CommandHandler(ReqHandler): ''' get the command, send it to the cserver, return response ''' @web.authenticated
[docs] def post(self): history = '' command = self.get_argument('command') # if there is a command, execute it & get the result if command: result = '' try: cserver = self.get_server() result = cserver.onecmd(command) except Exception, e: print e result = sys.exc_info() if result: history = history + str(result) + '\n' self.content_type = 'text/html' self.write(history)
[docs] def get(self): self.content_type = 'text/html' self.write('') # not used for now, could render a form
[docs]class ComponentHandler(ReqHandler): ''' add, get, or remove a component ''' @web.authenticated
[docs] def post(self, name): type = self.get_argument('type') if 'parent' in self.request.arguments.keys(): parent = self.get_argument('parent') else: parent = '' result = '' try: cserver = self.get_server() cserver.add_component(name, type, parent) except Exception, e: result = sys.exc_info() cserver._error(e, result) self.content_type = 'text/html' self.write(result)
[docs] def delete(self, name): cserver = self.get_server() result = '' try: result = cserver.onecmd('del ' + name) except Exception, e: print e result = sys.exc_info() self.content_type = 'text/html' self.write(result)
[docs] def get(self, name): cserver = self.get_server() attr = {} try: attr = cserver.get_attributes(name) except Exception, err: exc_type, exc_value, exc_traceback = sys.exc_info() print 'Error getting attributes on', name, ':', err self.content_type = 'application/javascript' self.write(attr)
[docs]class ObjectHandler(ReqHandler): ''' get the data for a slotable object (including components) ''' @web.authenticated
[docs] def get(self, name): cserver = self.get_server() attr = {} try: attr = cserver.get_attributes(name) except Exception, err: exc_type, exc_value, exc_traceback = sys.exc_info() print 'Error getting attributes on', name, ':', err self.content_type = 'application/javascript' self.write(attr)
[docs]class ReplaceHandler(ReqHandler): ''' replace a component ''' @web.authenticated
[docs] def post(self, pathname): type = self.get_argument('type') result = '' try: cserver = self.get_server() cserver.replace_component(pathname, type) except Exception, e: print e result = sys.exc_info() self.content_type = 'text/html' self.write(result)
[docs]class ComponentsHandler(ReqHandler): @web.authenticated
[docs] def get(self): cserver = self.get_server() json = cserver.get_components() self.content_type = 'application/javascript' self.write(json)
[docs]class ConnectionsHandler(ReqHandler): ''' get connections between two components in an assembly ''' @web.authenticated
[docs] def get(self, pathname): cserver = self.get_server() connects = {} try: src_name = self.get_argument('src_name', default=None) dst_name = self.get_argument('dst_name', default=None) connects = cserver.get_connections(pathname, src_name, dst_name) except Exception, e: print e self.content_type = 'application/javascript' self.write(connects)
[docs]class DataflowHandler(ReqHandler): ''' get the structure of the specified assembly, or of the global namespace if no pathname is specified, consisting of the list of components and the connections between them (i.e. the dataflow) ''' @web.authenticated
[docs] def get(self, name): cserver = self.get_server() json = cserver.get_dataflow(name) self.content_type = 'application/javascript' self.write(json)
[docs]class EditorHandler(ReqHandler): @web.authenticated
[docs] def get(self): '''Code Editor ''' filename = self.get_argument('filename', default=None) self.render('workspace/editor.html', filename=filename)
[docs]class ExecHandler(ReqHandler): ''' if a filename is POST'd, have the cserver execute the file otherwise just run() the project ''' @web.authenticated
[docs] def post(self): result = '' cserver = self.get_server() filename = self.get_argument('filename', default=None) if filename: try: result = cserver.execfile(filename) except Exception, e: print e result = result + str(sys.exc_info()) + '\n' else: try: except Exception, e: print e result = result + str(sys.exc_info()) + '\n' if result: self.content_type = 'text/html' self.write(result)
[docs]class FileHandler(ReqHandler): ''' get/set the specified file/folder ''' @web.authenticated
[docs] def post(self, filename): cserver = self.get_server() isFolder = self.get_argument('isFolder', default=None) if isFolder: self.write(cserver.ensure_dir(filename)) else: contents = self.get_argument('contents', default='') force = int(self.get_argument('force', default=0)) if filename.endswith('.py'): if not contents.endswith('\n'): text = contents + '\n' # to make ast.parse happy else: text = contents try: ast.parse(text, filename=filename, mode='exec') except Exception as err: cserver.send_pub_msg(str(err), 'file_errors') self.send_error(400) return if not force: ret = cserver.file_has_instances(filename) if ret: self.send_error(409) # user will be prompted to overwrite classes return self.write(str(cserver.write_file(filename, contents)))
[docs] def delete(self, filename): cserver = self.get_server() self.content_type = 'text/html' self.write(str(cserver.delete_file(filename)))
[docs] def get(self, filename): cserver = self.get_server() self.content_type = 'text/html' self.write(str(cserver.get_file(filename)))
[docs]class FilesHandler(ReqHandler): ''' get a list of the users files in JSON format ''' @web.authenticated
[docs] def get(self): cserver = self.get_server() filedict = cserver.get_files() json = jsonpickle.encode(filedict) self.content_type = 'application/javascript' self.write(json)
[docs]class ModelHandler(ReqHandler): ''' POST: get a new model (delete existing console server) GET: get JSON representation of the model ''' @web.authenticated
[docs] def post(self): self.delete_server() self.redirect('/')
[docs] def get(self): cserver = self.get_server() json = cserver.get_JSON() self.content_type = 'application/javascript' self.write(json)
[docs]class OutstreamHandler(ReqHandler): ''' return the url of the zmq outstream server, ''' @web.authenticated
[docs] def get(self): url = self.application.server_manager.\ get_out_server_url(self.get_sessionid(), '/workspace/outstream') self.write(url)
[docs]class ProjectHandler(ReqHandler): ''' GET: load model fom the given project archive, or reload remembered project for session if no file given POST: save project archive of the current project ''' @web.authenticated
[docs] def post(self): cserver = self.get_server() cserver.save_project() self.write('Saved.')
[docs] def get(self): filename = self.get_argument('filename', default=None) if filename: self.set_secure_cookie('filename', filename) else: filename = self.get_secure_cookie('filename') if filename: self.delete_server() cserver = self.get_server() filename = os.path.join(self.get_project_dir(), filename) cserver.load_project(filename) self.redirect(self.application.reverse_url('workspace')) else: self.redirect('/')
[docs]class PlotHandler(ReqHandler): ''' GET: open a websocket server to supply updated valaues for the specified variable ''' @web.authenticated
[docs] def get(self, name): cserver = self.get_server() port = cserver.get_varserver(name) self.write(port)
[docs]class PublishHandler(ReqHandler): ''' GET: tell the server to publish the specified topic/variable ''' @web.authenticated
[docs] def get(self): topic = self.get_argument('topic') publish = self.get_argument('publish', default=True) publish = publish in [True, 'true', 'True'] cserver = self.get_server() cserver.add_subscriber(topic, publish)
[docs]class PubstreamHandler(ReqHandler): ''' return the url of the zmq publisher server ''' @web.authenticated
[docs] def get(self): url = self.application.server_manager.\ get_pub_server_url(self.get_sessionid(), '/workspace/pubstream') self.write(url)
[docs]class TypesHandler(ReqHandler): ''' get hierarchy of package/types to populate the Palette ''' @web.authenticated
[docs] def get(self): cserver = self.get_server() types = cserver.get_types() self.content_type = 'application/javascript' self.write(jsonpickle.encode(types))
[docs]class UploadHandler(ReqHandler): ''' file upload utility ''' @web.authenticated
[docs] def post(self): path = self.get_argument('path', default=None) cserver = self.get_server() files = self.request.files['file'] if files: for file_ in files: filename = file_['filename'] if len(filename) > 0: if path: filename = os.path.sep.join([path, filename]) cserver.add_file(filename, file_['body']) self.render('closewindow.html') else: self.render('workspace/upload.html', path=path)
[docs] def get(self): path = self.get_argument('path', default=None) self.render('workspace/upload.html', path=path)
[docs]class ValueHandler(ReqHandler): ''' GET: get a value for the given pathname TODO: combine with ComponentHandler? handle Containers as well? ''' @web.authenticated
[docs] def get(self, name): cserver = self.get_server() value = cserver.get_value(name) self.content_type = 'application/javascript' self.write(value)
[docs]class WorkflowHandler(ReqHandler): @web.authenticated
[docs] def get(self, name): cserver = self.get_server() json = cserver.get_workflow(name) self.content_type = 'application/javascript' self.write(json)
[docs]class WorkspaceHandler(ReqHandler): ''' render the workspace ''' @web.authenticated
[docs] def get(self): self.render('workspace/workspace.html')
[docs]class TestHandler(ReqHandler): ''' initialize the server manager & render the workspace ''' @web.authenticated
[docs] def get(self): self.render('workspace/test.html')
handlers = [ web.url(r'/workspace/?', WorkspaceHandler, name='workspace'), web.url(r'/workspace/addons/?', AddOnsHandler), web.url(r'/workspace/base/?', ReqHandler), web.url(r'/workspace/close/?', CloseHandler), web.url(r'/workspace/command', CommandHandler), web.url(r'/workspace/components/?', ComponentsHandler), web.url(r'/workspace/component/(.*)', ComponentHandler), web.url(r'/workspace/connections/(.*)', ConnectionsHandler), web.url(r'/workspace/dataflow/(.*)/?', DataflowHandler), web.url(r'/workspace/editor/?', EditorHandler), web.url(r'/workspace/exec/?', ExecHandler), web.url(r'/workspace/file/(.*)', FileHandler), web.url(r'/workspace/files/?', FilesHandler), web.url(r'/workspace/geometry', GeometryHandler), web.url(r'/workspace/model/?', ModelHandler), web.url(r'/workspace/object/(.*)', ObjectHandler), web.url(r'/workspace/outstream/?', OutstreamHandler), web.url(r'/workspace/plot/?', PlotHandler), web.url(r'/workspace/project/?', ProjectHandler), web.url(r'/workspace/publish/?', PublishHandler), web.url(r'/workspace/pubstream/?', PubstreamHandler), web.url(r'/workspace/replace/(.*)', ReplaceHandler), web.url(r'/workspace/types/?', TypesHandler), web.url(r'/workspace/upload/?', UploadHandler), web.url(r'/workspace/value/(.*)', ValueHandler), web.url(r'/workspace/workflow/(.*)', WorkflowHandler), web.url(r'/workspace/test/?', TestHandler), ]
OpenMDAO Home