1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
#!/usr/bin/env python
############################################################################
##
## Copyright (C) 2006-2008 University of Utah. All rights reserved.
##
## This file is part of VisTrails.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file. Please review the following to ensure GNU General Public
## Licensing requirements will be met:
## http://www.opensource.org/licenses/gpl-license.php
##
## If you are unsure which license is appropriate for your use (for
## instance, you are interested in developing a commercial derivative
## of VisTrails), please contact us at contact@vistrails.org.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
############################################################################
def convert_to_vt_type(t):
vt_type_dict = {'str':'core.modules.basic_modules.String',
'float': 'core.modules.basic_modules.Float',
'int':'core.modules.basic_modules.Integer',
'bool':'core.modules.basic_modules.Boolean',
#'numpy.ndarray':'reg.get_module_by_name("edu.utah.sci.vistrails.numpyscipy", "Numpy Array", namespace="numpy|array")',
}
if vt_type_dict.has_key(t):
return vt_type_dict[t]
else:
print "Type %s not found!" % t
return None
do_not_cache_me = ['png','plot','isofill','isoline','boxfill']
class CDATModule:
_extra_modules = []
_extra_vistrails_modules = {}
def __init__(self, author=None, language=None, type=None, url=None,
codepath=None, version=None, actions=None):
self._author = author
self._language = language
self._type = type
self._url = url
self._codepath = codepath
self._version = version
self._actions = actions
(self._namespace, self._name) = self.split(codepath)
@staticmethod
def split(codepath):
dot = codepath.rfind('.')
if dot != -1:
name = codepath[dot+1:]
data = codepath[:dot]
namespace = data.replace('.','|')
else:
name = codepath
namespace = codepath
return (namespace,name)
def write_extra_module_definition(self, lines, name):
lines.append("%s = new_module(Module,'%s')\n"%(name,name))
@staticmethod
def write_extra_module_definitions_init(lines):
lines.append("vt_type_dict = {}\n")
lines.append("def get_late_type(type):\n")
lines.append(" return vt_type_dict[type]\n\n")
@staticmethod
def write_extra_module_definitions(lines):
for t in CDATModule._extra_modules:
namespace,name = CDATModule.split(t)
lines.append("%s = new_module(Module,'%s')\n"%(name,name))
lines.append("vt_type_dict['%s'] = %s\n"%(t,name))
CDATModule._extra_vistrails_modules[name] = namespace
lines.append("\n\n")
@staticmethod
def register_extra_vistrails_modules(lines, ident=''):
for (name,namespace) in CDATModule._extra_vistrails_modules.iteritems():
lines.append(ident + " reg.add_module(%s,namespace='%s')\n"%(name,
namespace))
def register_extra_vistrails_module(self, lines, name, ident=''):
lines.append(ident + " reg.add_module(%s,namespace='%s')\n"%(name,
self._namespace))
def write_module_definitions(self, lines):
for a in self._actions:
a.write_module_definition(lines)
def register_vistrails_modules(self, lines):
for a in self._actions:
a.register_itself(lines,self._namespace)
def build_vistrails_modules_dict(self):
for a in self._actions:
types = a.check_output_types()
for t in types:
if t not in CDATModule._extra_modules:
CDATModule._extra_modules.append(t)
def add_extra_input_port_to_all_modules(self, lines, port_name, port_type,
doc, optional = False):
lines.append("\n #extra input ports not available in the xml file\n")
for a in self._actions:
a.register_extra_input_port(port_name, port_type, lines, doc,
optional)
def add_extra_output_port_to_all_modules(self, lines, port_name, port_type,
doc, optional = False):
lines.append("\n #extra output ports not available in the xml file\n")
for a in self._actions:
a.register_extra_output_port(port_name, port_type, lines, doc,
optional)
class CDATAction:
def __init__(self, name=None, type=None, options=None, inputs=None,
outputs=None, doc=None):
self._name = name
self._type = type
self._options = options
self._inputs = inputs
self._outputs = outputs
self._doc = doc
def write_module_definition(self, lines, ident='', compute_method=None):
def write_compute_method(self,lines, ident):
lines.append(ident + "def compute(self):\n")
lines.append(ident + " if self.hasInputFromPort('canvas'):\n")
lines.append(ident + " canvas = self.getInputFromPort('canvas')\n")
lines.append(ident + " else:\n")
lines.append(ident + " canvas = vcs.init()\n")
lines.append(ident + " args = []\n")
for inp in self._inputs:
lines.append(ident + " %s = None\n"%inp._name)
for inst in inp._valid_instances:
if inp._valid_instances.index(inst) == 0:
lines.append(ident + " if self.hasInputFromPort('%s'):\n" % inst)
lines.append(ident + " %s = self.getInputFromPort('%s')\n" % (inp._name, inst))
lines.append(ident + " args.append(%s)\n"%inp._name)
else:
lines.append(ident + " elif self.hasInputFromPort('%s'):\n" % inst)
lines.append(ident + " %s = self.getInputFromPort('%s')\n" % (inp._name, inst))
lines.append(ident + " args.append(%s)\n"%inp._name)
if inp._required:
lines.append("\n"+ ident +" # %s is a required port\n" % inp._name)
lines.append(ident + " if %s == None:\n" % inp._name)
lines.append(ident + " raise ModuleError(self, \"'%s' is a mandatory port\")\n" % inp._name)
lines.append("\n"+ident +" # build up the keyword arguments from the optional inputs.\n")
lines.append(ident +" kwargs = {}\n")
for opt in self._options:
for inst in opt._valid_instances:
if opt._valid_instances.index(inst) == 0:
lines.append(ident +" if self.hasInputFromPort('%s'):\n" % inst)
lines.append(ident +" kwargs['%s'] = self.getInputFromPort('%s')\n" % (opt._name, inst))
else:
lines.append(ident +" elif self.hasInputFromPort('%s'):\n" % inst)
lines.append(ident +" kwargs['%s'] = self.getInputFromPort('%s')\n" % (opt._name, inst))
lines.append(ident + " #force images to be created in the background\n")
lines.append(ident + " kwargs['bg'] = 1\n")
lines.append(ident + " res = canvas.%s(*args,**kwargs)\n"%self._name)
lines.append(ident + " self.setResult('%s',res)\n"%(self._outputs[0]._name))
lines.append(ident + " self.setResult('canvas',canvas)\n")
lines.append("\n")
if self._name in do_not_cache_me:
lines.append(ident + "class %s(Module,NotCacheable):\n" % self._name)
else:
lines.append(ident + "class %s(Module):\n" % self._name)
lines.append(ident + ' """%s\n'%self._doc)
lines.append(ident + ' """\n')
if not compute_method:
write_compute_method(self,lines,ident=" ")
else:
lines.extend(compute_method)
def register_itself(self,lines, namespace):
lines.append("\n #Module %s\n" % self._name)
lines.append(" reg.add_module(%s,namespace='%s')\n" % (self._name,namespace))
for inp in self._inputs:
inp.write_input_ports(self._name, lines)
for opt in self._options:
opt.write_input_ports(self._name, lines, True, force=False)
for out in self._outputs:
out.write_output_ports(self._name, lines, force=True)
def check_output_types(self):
types = []
for out in self._outputs:
if out._instance[0] not in types:
types.append(out._instance[0])
return types
def register_extra_input_port(self, port_name, port_type, lines, doc,
optional=False):
self._write_port('input', port_name, port_type, lines, doc, optional)
def register_extra_output_port(self, port_name, port_type, lines, doc,
optional=False):
self._write_port('output', port_name, port_type, lines, doc, optional)
#private methods
def _write_port(self, io_type, port_name, port_type,
lines, doc, optional=False):
lines.append(" reg.add_%s_port(%s, '%s', \n" % (io_type,
self._name,
port_name))
lines.append(" ")
lines.append("(%s,\n" % port_type)
lines.append(" ")
if not optional:
lines.append("\"%s\"))\n" % doc)
else:
lines.append("\"%s\"), True)\n" % doc)
class CDATItem:
def __init__(self, tag=None, doc=None, instance=None, required=False):
self._name = tag
self._doc = doc
self._instance = [i.strip(" \t\n") for i in instance.split('/')]
self._valid_instances = []
if required == None:
self._required = False
else:
self._required = required
def write_input_ports(self, module_name, lines, optional=False, force=False):
self._write_ports('input',module_name, lines, optional, force)
def write_output_ports(self, module_name, lines, optional=False, force=False):
self._write_ports('output',module_name, lines, optional, force)
def _write_ports(self, port_type, module_name, lines, optional=False,
force=False):
if len(self._instance) == 1:
type = convert_to_vt_type(self._instance[0])
if type == None and self._instance[0] in CDATModule._extra_modules:
force = True
if force and type == None:
type = "get_late_type('%s')"%self._instance[0]
if type != None:
self._valid_instances.append(self._name)
lines.append(" reg.add_%s_port(%s, '%s', \n" % (port_type,
module_name,
self._name))
lines.append(" ")
lines.append("(%s,\n" % type)
lines.append(" ")
if not optional:
lines.append("\"%s\"))\n" % self._doc)
else:
lines.append("\"%s\"), True)\n" % self._doc)
else:
count = 0
for i in xrange(len(self._instance)):
type = convert_to_vt_type(self._instance[i])
new_force = force
if type == None and self._instance[i] in CDATModule._extra_modules:
new_force = True
if new_force and type == None:
type = "get_late_type('%s')"%self._instance[i]
if type != None:
name = "%s_%s"%(self._name,count)
self._valid_instances.append(name)
count += 1
lines.append(" reg.add_input_port(%s, '%s', \n" % (module_name,
name))
lines.append(" ")
lines.append("(%s,\n" % type)
lines.append(" ")
if not optional:
lines.append("\"%s\"))\n" % self._doc)
else:
lines.append("\"%s\"), True)\n" % self._doc)
class CDATOption(CDATItem):
def __init__(self, tag=None, default=None, doc=None, instance=None):
CDATItem.__init__(self, tag, doc, instance, False)
self._default = default
class CDATPort(CDATItem):
def __init__(self, tag=None, doc=None, instance=None, position=None, required=False):
CDATItem.__init__(self, tag, doc, instance, required)
self._position = position
|