1 # Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
25 import string, sys, fpformat, re, os
29 from xml.sax.handler import *
30 from omniidl import idlast, idltype, idlvisitor, idlutil, output
32 # parameters not found in IDL file, user's specified in optional parameters
33 common_data={"AUTHOR" : "",
44 #--------------------------------------------------
45 # extract value of <param_name> from <args> list
46 # it's proposed that the matching <args> item
47 # looks like <param_name>=<value>, for example,
48 # catalog=/tmp/myxml.xml
49 #--------------------------------------------------
50 def getParamValue( param_name, default_value, args ):
51 pattern=param_name+"="
53 res = default_value #initial value
55 s = re.compile(pattern).search(opt)
63 #--------------------------------------------------
65 #--------------------------------------------------
67 print "ERROR : ", message
70 #--------------------------------------------------
71 # base class implementing tree
72 #--------------------------------------------------
75 def __init__(self, name = '', content = ''):
77 self.content = content
81 def addChild(self, tree):
83 self.childs.append(tree)
87 def addNamedChild(self, name, content = ''):
88 return self.addChild(Tree(name, content))
90 def replaceChild(self, tree):
94 if i.name == tree.name:
96 self.childs.insert(pos, tree)
100 return self.addChild(tree)
102 def insertFirstChild(self, tree):
104 self.childs.insert(0, tree)
107 def insertFirstNamedChild(self, name, content = ''):
108 return self.insertFirstChild(Tree(name, content))
110 def output_xml(self, f, depth=0):
113 s = string.ljust('', 4*depth)
114 s += '<' + self.name + '>'
115 if self.content != '':
118 if len(self.childs) > 0:
123 for i in self.childs:
127 s = '</' + self.name + '>\n'
128 if len(self.childs) > 0 :
129 s = string.ljust('', 4*depth) + s
132 def Dump(self, levels=-1, depth=0):
133 #Dumps the tree contents
135 if levels == 0: return
137 s = string.ljust('', 4*depth)
138 print s, self, self.content
139 for i in self.childs:
140 i.Dump(levels-1, depth+1)
143 #Returns list of the parents
152 def getChild(self, name, content=None):
154 # content == None, don't compare content
155 for i in self.childs:
157 if (content is None) | (i.content == content):
161 def getNode(self, name, content='', depth=-1):
164 # content == None, don't compare content
165 if (self.name == name):
166 if (content is None) | (self.content == content):
170 for i in self.childs:
171 n = i.getNode(name, content, depth-1)
172 if n: return n #return a value
188 #--------------------------------------------------
189 # implements inParameter tree
190 #--------------------------------------------------
191 class inParameter(Tree):
193 def __init__(self, name=None, type='', comment='unknown'):
194 Tree.__init__(self, 'inParameter')
195 if name is None: return
197 self.addNamedChild('inParameter-type', type)
198 self.addNamedChild('inParameter-name', name)
199 self.addNamedChild('inParameter-comment', comment)
203 T = P.getChild('inParameter-type')
206 #--------------------------------------------------
207 # implements outParameter tree
208 #--------------------------------------------------
209 class outParameter(Tree):
211 def __init__(self, name=None, type='', comment = 'unknown'):
213 Tree.__init__(self, 'outParameter')
214 if name is None: return
216 self.addNamedChild('outParameter-type', type)
217 self.addNamedChild('outParameter-name', name)
218 self.addNamedChild('outParameter-comment', comment)
222 T = P.getChild('outParameter-type')
225 #--------------------------------------------------
226 # implements service tree
227 #--------------------------------------------------
230 def __init__(self, name=None, comment = 'unknown'):
232 Tree.__init__(self, 'component-service')
233 if name is None: return
235 self.addNamedChild('service-name', name)
236 self.addNamedChild('service-author',common_data["AUTHOR"])
237 self.addNamedChild('service-version',common_data["VERSION"])
238 self.addNamedChild('service-comment', comment)
239 self.addNamedChild('service-by-default', "0")
240 self.addNamedChild('inParameter-list')
241 self.addNamedChild('outParameter-list')
243 def createInParameter(self, name, type):
244 L = self.getChild('inParameter-list')
246 error ("Service.createInParameter() : 'inParameter-list' is not found"); return None;
247 p = inParameter(name, type)
251 def createOutParameter(self, name, type):
252 L = self.getChild('outParameter-list')
254 error ("Service.createOutParameter() : 'outParameter-list' is not found"); return None;
255 p = outParameter(name, type)
262 L_ext = S.getChild('inParameter-list')
263 L_int = self.getChild('inParameter-list')
265 if L_ext is not None and L_int is not None:
267 L_merge = Tree('inParameter-list')
269 for i_ext in L_ext.childs:
270 # i_ext = <inParameter>
271 n_ext = i_ext.getChild('inParameter-name')
272 if n_ext is None: continue
275 for i_int in L_int.childs:
276 # i_int = <inParameter>
277 n_int = i_int.getChild('inParameter-name')
279 if n_int is None: continue
281 if (n_int.content == n_ext.content):
287 L_merge.addChild(i_int)
289 L_merge.addChild(i_ext)
291 self.replaceChild(L_merge)
293 else : error("Service.merge(): 'inParameter-list' is not found") #either L_ext or L_int is None
295 L_ext = S.getChild('outParameter-list')
296 L_int = self.getChild('outParameter-list')
298 if L_ext is None or L_int is None:
299 error ("Service.merge() : 'outParameter-list' is not found")
301 L_merge = Tree('outParameter-list')
303 for i_ext in L_ext.childs:
304 #i_ext = <outParameter>
306 n_ext = i_ext.getChild('outParameter-name')
307 if n_ext is None: continue
308 for i_int in L_int.childs:
309 n_int = i_int.getChild('outParameter-name')
310 if n_int is None: continue
311 if (n_int.content == n_ext.content):
317 L_merge.addChild(i_int)
319 L_merge.addChild(i_ext)
321 self.replaceChild(L_merge)
324 #--------------------------------------------------
325 # implements interface tree
326 #--------------------------------------------------
327 class Interface(Tree):
329 def __init__(self, name=None, comment='unknown'):
333 if name is None: return
335 self.addNamedChild('component-interface-name', name)
336 self.addNamedChild('component-interface-comment', comment);
337 self.addNamedChild('component-service-list')
339 def createService(self, name):
340 L = self.getChild('component-service-list')
343 error ("Interface.createService() : 'component-service-list' is not found")
352 L_ext = I.getChild('component-service-list')
353 L_int = self.getChild('component-service-list')
355 if L_ext is None or L_int is None:
356 error("Interface.merge() : 'component-service-list' is not found!")
359 L_merge = Tree('component-service-list')
361 for i_ext in L_ext.childs:
364 n_ext = i_ext.getChild('service-name')
365 if n_ext is None: continue
367 for i_int in L_int.childs:
368 n_int = i_int.getChild('service-name')
369 if n_int is None: continue
370 if (n_int.content == n_ext.content):
376 L_merge.addChild(i_int)
378 L_merge.addChild(i_ext)
380 self.replaceChild(L_merge)
383 #--------------------------------------------------
384 # implements Component tree
385 #--------------------------------------------------
386 class Component(Tree):
388 Tree.__init__(self, 'component')
390 self.addNamedChild('component-name', common_data["COMP_NAME"])
391 self.addNamedChild('component-username', common_data["COMP_UNAME"])
392 self.addNamedChild('component-type', common_data["COMP_TYPE"])
393 self.addNamedChild('component-author', common_data["AUTHOR"])
394 self.addNamedChild('component-version', common_data["VERSION"])
395 self.addNamedChild('component-comment', 'unknown')
396 self.addNamedChild('component-multistudy', common_data["COMP_MULT"])
397 self.addNamedChild('component-icone', common_data["ICON"])
398 self.addNamedChild('constraint')
399 self.addNamedChild('component-interface-list')
401 def createInterface(self, name):
402 L = self.getChild('component-interface-list')
404 error("createInterface: No component-interface-list is found")
412 for i in ['component-username', 'component-author',
413 'component-type', 'component-icone', 'component-version',
414 'component-comment', 'component-multistudy', 'constraint']:
416 int = self.getChild(i)
419 elif ext is not None and len(ext.content):
420 int.content = ext.content
422 L_ext = C.getChild('component-interface-list')
423 L_int = self.getChild('component-interface-list')
424 if L_ext is None or L_int is None:
425 error("Component.merge : No component-interface-list is found")
427 L_merge = Tree('component-interface-list')
429 for i_ext in L_ext.childs:
431 n_ext = i_ext.getChild('component-interface-name')
433 if n_ext is None: continue
435 for i_int in L_int.childs:
436 n_int = i_int.getChild('component-interface-name')
437 if n_int is None: continue
438 if (n_int.content == n_ext.content):
444 L_merge.addChild(i_int)
446 L_merge.addChild(i_ext)
448 self.replaceChild(L_merge)
450 #--------------------------------------------------
451 # implements document tree
452 #--------------------------------------------------
453 class Catalog(ContentHandler, Tree):
454 def __init__(self, filename = None):
459 parser = xml.sax.make_parser()
460 parser.setContentHandler(self)
461 parser.parse(filename)
463 t = self.addNamedChild('begin-catalog')
464 t.addNamedChild('component-list')
466 n = self.getChild('begin-catalog')
468 error("Catalog.__init__ : No 'begin-catalog' is found!")
470 if n.getChild('path-prefix-list') is None:
471 n.insertFirstNamedChild('path-prefix-list')
472 if n.getChild('component-list') is None:
473 n.addNamedChild('component-list')
475 def removeComponent(self, name):
476 complist = self.getNode('component-list')
479 print "Catalog.removeComponent() : 'component-list' is not found"
481 for comp in complist.childs:
482 cname = comp.getChild('component-name')
483 if cname is not None:
484 if cname.content == name:
485 complist.childs.pop(idx)
486 print "Component " + name + " is removed"
489 def startDocument(self):
490 self.list.append(self)
492 def startElement(self, name, attrs):
493 p = self.list[len(self.list)-1]
495 if name == 'component':
496 e = p.addChild(Component())
497 elif name == 'component-interface-name':
498 e = p.addNamedChild(name)
499 elif name == 'component-service':
500 e = p.addChild(Service())
501 elif name == 'inParameter':
502 e = p.addChild(inParameter())
503 elif name == 'outParameter':
504 e = p.addChild(outParameter())
506 e = p.addNamedChild(name)
510 def endElement(self, name):
511 self.buffer = string.join(string.split(self.buffer), ' ')
512 p = self.list[len(self.list)-1]
513 p.content = self.buffer
518 def characters(self, ch):
521 def mergeComponent(self, comp):
523 L_int = self.getNode('component-list')
525 error("Catalog.mergeComponent : 'component-list' is not found")
530 n_ext = i_ext.getChild('component-name')
532 error("Catalog.mergeComponent : 'component-name' is not found")
534 for i_int in L_int.childs:
535 n_int = i_int.getChild('component-name')
536 if n_int is None: continue
538 if (n_int.content == n_ext.content):
543 print ' add component', n_ext.content
544 L_int.addChild(i_ext)
546 print ' replace component', n_ext.content
555 idltype.tk_void: "void",
556 idltype.tk_short: "short",
557 idltype.tk_long: "long",
558 idltype.tk_ushort: "unsigned short",
559 idltype.tk_ulong: "unsigned long",
560 idltype.tk_float: "float",
561 idltype.tk_double: "double",
562 idltype.tk_boolean: "boolean",
563 idltype.tk_char: "char",
564 idltype.tk_octet: "octet",
565 idltype.tk_any: "any",
566 idltype.tk_TypeCode: "CORBA::TypeCode",
567 idltype.tk_Principal: "CORBA::Principal",
568 idltype.tk_longlong: "long long",
569 idltype.tk_ulonglong: "unsigned long long",
570 idltype.tk_longdouble: "long double",
571 idltype.tk_wchar: "wchar"
575 #--------------------------------------------------
576 # class ModuleCatalogVisitor
577 #--------------------------------------------------
578 class ModuleCatalogVisitor (idlvisitor.AstVisitor):
580 def __init__(self, catalog):
581 self.catalog = catalog
584 def visitAST(self, node):
585 for n in node.declarations():
588 def visitModule(self, node):
589 for n in node.definitions():
592 def visitInterface(self, node):
597 for i in node.inherits():
599 if ((s[0] == "Engines") & (s[1] == "Component")):
600 self.EngineType = 1; break
604 self.currentInterface = Comp.createInterface(node.identifier())
606 for c in node.callables():
607 if isinstance(c, idlast.Operation):
610 if (self.EngineType):
612 nb_components = nb_components + 1
613 self.catalog.mergeComponent(Comp)
617 def visitOperation(self, node):
619 self.currentService = self.currentInterface.createService \
622 for c in node.parameters():
625 node.returnType().accept(self)
626 if (self.currentType != "void"):
627 self.currentService.createOutParameter \
628 ("return", self.currentType)
631 def visitDeclaredType(self, type):
632 self.currentType = type.name()
634 def visitBaseType(self, type):
635 self.currentType = ttsMap[type.kind()]
637 def visitStringType(self, type):
638 self.currentType = "string"
640 def visitParameter(self, node):
641 node.paramType().accept(self)
643 self.currentService.createInParameter \
644 (node.identifier(), self.currentType)
646 self.currentService.createOutParameter \
647 (node.identifier(), self.currentType)
649 #--------------------------------------------------
650 # parse idl and store xml file
651 #--------------------------------------------------
655 CatalogFileName=getParamValue("catalog", "CatalogModulePersonnel.xml", args)
656 print CatalogFileName
657 if re.compile(".*?.xml$").match(CatalogFileName, 1) is None:
658 CatalogFileName = CatalogFileName + '.xml'
660 #========= Read parameters ======================
661 common_data["ICON"] = getParamValue("icon", "", args)
662 common_data["AUTHOR"] = getParamValue("author", os.getenv("USER"), args)
663 common_data["VERSION"] = getParamValue("version", "1", args)
664 common_data["COMP_NAME"] = getParamValue("name", "", args)
665 common_data["COMP_UNAME"] = getParamValue("username", "", args)
666 common_data["COMP_TYPE"] = getParamValue("type", "OTHER", args)
667 common_data["COMP_MULT"] = getParamValue("multistudy", "1", args)
671 remove_comp = getParamValue("remove", "", args)
673 #==================================================
675 if (os.path.exists(CatalogFileName)):
676 print "Importing", CatalogFileName
677 C = Catalog(CatalogFileName)
679 print "Creating ",CatalogFileName
682 print "Reading idl file"
684 visitor = ModuleCatalogVisitor(C)
688 C.removeComponent(remove_comp)
690 if (os.path.exists(CatalogFileName)):
691 print "Updating", CatalogFileName
692 CatalogFileName_old = CatalogFileName + '_old'
693 os.rename(CatalogFileName, CatalogFileName_old)
695 CatalogFileName_old = ""
696 print "Writing", CatalogFileName
698 CatalogFileName_new = CatalogFileName + '_new'
699 f=open(CatalogFileName_new, 'w')
700 f.write("<?xml version='1.0' encoding='us-ascii' ?>\n\n")
704 os.rename(CatalogFileName_new, CatalogFileName)
705 if ((CatalogFileName_old != "") & os.path.exists(CatalogFileName_old)):
706 os.unlink(CatalogFileName_old)
711 if __name__ == "__main__":
713 print "Usage : omniidl -bIDLparser [-I<catalog files directory>]* -Wbcatalog=<my_catalog.xml>[,icon=<pngfile>][,version=<num>][,author=<name>][,name=<component_name>][,username=<component_username>][,multistudy=<component_multistudy>] <file.idl>"