PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/cdat/scripts/cdat_domain.py

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