Orb/python/doxygen/linkinserter.py
changeset 1 82f11024044a
parent 0 42188c7ea2d9
equal deleted inserted replaced
0:42188c7ea2d9 1:82f11024044a
    16 from optparse import OptionParser
    16 from optparse import OptionParser
    17 from xml.etree import ElementTree as etree
    17 from xml.etree import ElementTree as etree
    18 import os.path
    18 import os.path
    19 import logging
    19 import logging
    20 import shutil
    20 import shutil
       
    21 from guidiser import Guidiser
    21 
    22 
    22 __version__ = "0.1"
    23 __version__ = "0.1"
    23 
    24 
    24 
    25 
    25 class LinkFile(object):
    26 class LinkFile(object):
    26     """
    27     """
    27     Parses string representations of cxxapiref XML files and returns an updated
    28     Parses string representations of cxxapiref XML files and returns an updated
    28     string representation with inserted ids for linking to cxxFunctions.
    29     string representation with inserted ids for linking to cxxFunctions.
    29     """
    30     """
    30     
    31     
       
    32     def __init__(self, guidise=False):
       
    33         self.guidiser = Guidiser()
       
    34         self.guidise = guidise
    31     def _get_cxxfunction_elems(self, elem):
    35     def _get_cxxfunction_elems(self, elem):
    32         """
    36         """
    33         Takes an elements and generates a list of all child elements that are called cxxFunction 
    37         Takes an elements and generates a list of all child elements that are called cxxFunction 
    34         """
    38         """
    35         return [e for e in elem.getiterator() if e.tag == "cxxFunction"]
    39         return [e for e in elem.getiterator() if e.tag == "cxxFunction"]
    80             function_scoped_name = ""
    84             function_scoped_name = ""
    81         else:
    85         else:
    82             function_scoped_name+="::"
    86             function_scoped_name+="::"
    83         
    87         
    84         apiname_id = "".join([function_scoped_name,func_elem.find("apiName").text,"()"])
    88         apiname_id = "".join([function_scoped_name,func_elem.find("apiName").text,"()"])
       
    89        
       
    90         if self.guidise:
       
    91             apiname_id = self.guidiser._get_guid(apiname_id)
    85 
    92 
    86         func_elem.find("apiName").attrib["id"] = apiname_id
    93         func_elem.find("apiName").attrib["id"] = apiname_id
    87         return func_elem
    94         return func_elem
    88         
    95         
    89     def get_func_elems_to_linkify(self, root):
    96     def get_func_elems_to_linkify(self, root):
   158             raise IOError("Directory to linkify does not exist: %s" % dir) 
   165             raise IOError("Directory to linkify does not exist: %s" % dir) 
   159         self._do_linkifying(dir)
   166         self._do_linkifying(dir)
   160         
   167         
   161 
   168 
   162 def insertlinks(xml_dir):
   169 def insertlinks(xml_dir):
   163     link_inserter = LinkInserter(LinkFile())
   170     link_inserter = LinkInserter(LinkFile(guidise=True))
   164     link_inserter.linkify_dir(xml_dir)
   171     link_inserter.linkify_dir(xml_dir)
   165 
   172 
   166 
   173 
   167 def main():
   174 def main():
   168     usage = "usage: %prog <Path to the XML content>"
   175     usage = "usage: %prog <Path to the XML content>"
   389     </cxxFunction>
   396     </cxxFunction>
   390         </cxxClass>"""
   397         </cxxClass>"""
   391         returned = self.link_file.get_linkified(file_as_string)
   398         returned = self.link_file.get_linkified(file_as_string)
   392         self.assertEquals(etree.fromstring(returned).find("cxxFunction/apiName").attrib["id"], "BTrace::Init0()")
   399         self.assertEquals(etree.fromstring(returned).find("cxxFunction/apiName").attrib["id"], "BTrace::Init0()")
   393 
   400 
   394 
   401         
       
   402     def test__insert_a_guidised_id_into_cxxfunction_apiname(self):
       
   403         self.link_file = LinkFile(guidise=True)
       
   404         func_str = """<cxxFunction id="class_b_trace_1a7217f2fa88e99af3dbb5827fdc8507b7">
       
   405         <apiName>Init0</apiName>
       
   406         <shortdesc/>
       
   407         <cxxFunctionDetail>
       
   408             <cxxFunctionDefinition>
       
   409                 <cxxFunctionAccessSpecifier value="public"/>
       
   410                 <cxxFunctionStorageClassSpecifierStatic/>
       
   411                 <cxxFunctionDeclaredType>void</cxxFunctionDeclaredType>
       
   412                 <cxxFunctionScopedName>BTrace</cxxFunctionScopedName>
       
   413                 <cxxFunctionPrototype>static void Init0(TUint32 a0)</cxxFunctionPrototype>
       
   414                 <cxxFunctionNameLookup>BTrace::Init0(TUint32 a0)</cxxFunctionNameLookup>
       
   415                 <cxxFunctionParameters>
       
   416                     <cxxFunctionParameter>
       
   417                         <cxxFunctionParameterDeclaredType>
       
   418                             <apiRelation keyref="_always_online_manager_client_8cpp_1a8240e11f17c80b6b222fc2af50234da4">TUint32</apiRelation>
       
   419                         </cxxFunctionParameterDeclaredType>
       
   420                         <cxxFunctionParameterDeclarationName>a0</cxxFunctionParameterDeclarationName>
       
   421                         <apiDefNote/>
       
   422                     </cxxFunctionParameter>
       
   423                 </cxxFunctionParameters>
       
   424                 <cxxFunctionAPIItemLocation>
       
   425                     <cxxFunctionDeclarationFile name="filePath" value="D:/epoc32/include/e32btrace.h"/>
       
   426                     <cxxFunctionDeclarationFileLine name="lineNumber" value="3882"/>
       
   427                     <cxxFunctionDefinitionFile name="filePath" value="D:/EPOC/master/sf/mw/messagingmw/messagingfw/alwaysonline/AlwaysOnlineManager/src/AlwaysOnlineManagerClient.cpp"/>
       
   428                     <cxxFunctionDefinitionFileLineStart name="lineNumber" value="-1"/>
       
   429                     <cxxFunctionDefinitionFileLineEnd name="lineNumber" value="-1"/>
       
   430                 </cxxFunctionAPIItemLocation>
       
   431             </cxxFunctionDefinition>
       
   432             <apiDesc/>
       
   433         </cxxFunctionDetail>
       
   434     </cxxFunction>"""
       
   435         func_elem = etree.fromstring(func_str)
       
   436         returned = self.link_file._insert_id_into_cxxfunction_apiname(func_elem)
       
   437         self.assertEquals(returned.find("apiName").attrib["id"], "GUID-C8A77671-4E2F-3290-9592-84A70B14F88D")
       
   438 
       
   439     def test__insert_a_guidised_id_into_cxxfunction_apiname_when_the_cxxfunction_has_no_scoped_name(self):
       
   440         self.link_file = LinkFile(guidise=True)
       
   441         func_str = """<cxxFunction id="class_b_trace_1a7217f2fa88e99af3dbb5827fdc8507b7">
       
   442         <apiName>Init0</apiName>
       
   443         <shortdesc/>
       
   444         <cxxFunctionDetail>
       
   445             <cxxFunctionDefinition>
       
   446                 <cxxFunctionAccessSpecifier value="public"/>
       
   447                 <cxxFunctionStorageClassSpecifierStatic/>
       
   448                 <cxxFunctionDeclaredType>void</cxxFunctionDeclaredType>
       
   449                 <cxxFunctionScopedName/>
       
   450                 <cxxFunctionPrototype>static void Init0(TUint32 a0)</cxxFunctionPrototype>
       
   451                 <cxxFunctionNameLookup>BTrace::Init0(TUint32 a0)</cxxFunctionNameLookup>
       
   452                 <cxxFunctionParameters>
       
   453                     <cxxFunctionParameter>
       
   454                         <cxxFunctionParameterDeclaredType>
       
   455                             <apiRelation keyref="_always_online_manager_client_8cpp_1a8240e11f17c80b6b222fc2af50234da4">TUint32</apiRelation>
       
   456                         </cxxFunctionParameterDeclaredType>
       
   457                         <cxxFunctionParameterDeclarationName>a0</cxxFunctionParameterDeclarationName>
       
   458                         <apiDefNote/>
       
   459                     </cxxFunctionParameter>
       
   460                 </cxxFunctionParameters>
       
   461                 <cxxFunctionAPIItemLocation>
       
   462                     <cxxFunctionDeclarationFile name="filePath" value="D:/epoc32/include/e32btrace.h"/>
       
   463                     <cxxFunctionDeclarationFileLine name="lineNumber" value="3882"/>
       
   464                     <cxxFunctionDefinitionFile name="filePath" value="D:/EPOC/master/sf/mw/messagingmw/messagingfw/alwaysonline/AlwaysOnlineManager/src/AlwaysOnlineManagerClient.cpp"/>
       
   465                     <cxxFunctionDefinitionFileLineStart name="lineNumber" value="-1"/>
       
   466                     <cxxFunctionDefinitionFileLineEnd name="lineNumber" value="-1"/>
       
   467                 </cxxFunctionAPIItemLocation>
       
   468             </cxxFunctionDefinition>
       
   469             <apiDesc/>
       
   470         </cxxFunctionDetail>
       
   471     </cxxFunction>"""
       
   472         func_elem = etree.fromstring(func_str)
       
   473         returned = self.link_file._insert_id_into_cxxfunction_apiname(func_elem)
       
   474         self.assertEquals(returned.find("apiName").attrib["id"], "GUID-76C4A377-8597-3952-9E6B-3E61D068EE38")
       
   475         
       
   476     def test_i_can_insert_a_guidised_id_to_a_cxxfunction_apiname(self):
       
   477         self.link_file = LinkFile(guidise=True)
       
   478         file_as_string = """    
       
   479         <cxxClass>
       
   480 <cxxFunction id="class_b_trace_1a7217f2fa88e99af3dbb5827fdc8507b7">
       
   481         <apiName>Init0</apiName>
       
   482         <shortdesc/>
       
   483         <cxxFunctionDetail>
       
   484             <cxxFunctionDefinition>
       
   485                 <cxxFunctionAccessSpecifier value="public"/>
       
   486                 <cxxFunctionStorageClassSpecifierStatic/>
       
   487                 <cxxFunctionDeclaredType>void</cxxFunctionDeclaredType>
       
   488                 <cxxFunctionScopedName>BTrace</cxxFunctionScopedName>
       
   489                 <cxxFunctionPrototype>static void Init0(TUint32 a0)</cxxFunctionPrototype>
       
   490                 <cxxFunctionNameLookup>BTrace::Init0(TUint32 a0)</cxxFunctionNameLookup>
       
   491                 <cxxFunctionParameters>
       
   492                     <cxxFunctionParameter>
       
   493                         <cxxFunctionParameterDeclaredType>
       
   494                             <apiRelation keyref="_always_online_manager_client_8cpp_1a8240e11f17c80b6b222fc2af50234da4">TUint32</apiRelation>
       
   495                         </cxxFunctionParameterDeclaredType>
       
   496                         <cxxFunctionParameterDeclarationName>a0</cxxFunctionParameterDeclarationName>
       
   497                         <apiDefNote/>
       
   498                     </cxxFunctionParameter>
       
   499                 </cxxFunctionParameters>
       
   500                 <cxxFunctionAPIItemLocation>
       
   501                     <cxxFunctionDeclarationFile name="filePath" value="D:/epoc32/include/e32btrace.h"/>
       
   502                     <cxxFunctionDeclarationFileLine name="lineNumber" value="3882"/>
       
   503                     <cxxFunctionDefinitionFile name="filePath" value="D:/EPOC/master/sf/mw/messagingmw/messagingfw/alwaysonline/AlwaysOnlineManager/src/AlwaysOnlineManagerClient.cpp"/>
       
   504                     <cxxFunctionDefinitionFileLineStart name="lineNumber" value="-1"/>
       
   505                     <cxxFunctionDefinitionFileLineEnd name="lineNumber" value="-1"/>
       
   506                 </cxxFunctionAPIItemLocation>
       
   507             </cxxFunctionDefinition>
       
   508             <apiDesc/>
       
   509         </cxxFunctionDetail>
       
   510     </cxxFunction>
       
   511         </cxxClass>"""
       
   512         returned = self.link_file.get_linkified(file_as_string)
       
   513         self.assertEquals(etree.fromstring(returned).find("cxxFunction/apiName").attrib["id"], "GUID-C8A77671-4E2F-3290-9592-84A70B14F88D")
       
   514         
   395 basic_class_file_str = """\    
   515 basic_class_file_str = """\    
   396 <cxxClass>
   516 <cxxClass>
   397     <cxxFunction id="function_id">
   517     <cxxFunction id="function_id">
   398         <apiName>Init0</apiName>
   518         <apiName>Init0</apiName>
   399         <shortdesc/>
   519         <shortdesc/>