Orb/python/doxygen/lib.py
changeset 1 82f11024044a
parent 0 42188c7ea2d9
equal deleted inserted replaced
0:42188c7ea2d9 1:82f11024044a
    10 #
    10 #
    11 # Description:
    11 # Description:
    12 #
    12 #
    13 import os
    13 import os
    14 import unittest
    14 import unittest
       
    15 import xml
       
    16 import re
       
    17 import sys
    15 from optparse import OptionParser
    18 from optparse import OptionParser
    16 
    19 from cStringIO import StringIO
       
    20 from xml.etree import ElementTree as etree
       
    21 
       
    22 nmtoken_regex = re.compile("[^a-zA-Z0-9_\.]")
    17 
    23 
    18 def scan(dir):
    24 def scan(dir):
    19     for root, _, files in os.walk(dir):
    25     for root, _, files in os.walk(dir):
    20         for fname in files:
    26         for fname in files:
    21             yield os.path.join(root, fname) 
    27             yield os.path.join(root, fname) 
    43         return """<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">"""
    49         return """<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">"""
    44     elif doctype == "bookmap":
    50     elif doctype == "bookmap":
    45         return """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""" 
    51         return """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""" 
    46     # cxxapiref DITA specialisation Doctype Identifiers
    52     # cxxapiref DITA specialisation Doctype Identifiers
    47     elif doctype == "cxxUnion":
    53     elif doctype == "cxxUnion":
    48         return """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type//EN" "dtd/cxxUnion.dtd">"""   
    54         return """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type v0.5.0//EN" "dtd/cxxUnion.dtd">"""   
    49     elif doctype == "cxxStruct":
    55     elif doctype == "cxxStruct":
    50         return """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type//EN" "dtd/cxxStruct.dtd">"""
    56         return """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type v0.5.0//EN" "dtd/cxxStruct.dtd">"""
    51     elif doctype == "cxxPackage":
    57     elif doctype == "cxxPackage":
    52         return """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type//EN" "dtd/cxxPackage.dtd">"""
    58         return """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type v0.5.0//EN" "dtd/cxxPackage.dtd">"""
    53     elif doctype == "cxxFile":
    59     elif doctype == "cxxFile":
    54         return """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type//EN" "dtd/cxxFile.dtd">"""
    60         return """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type v0.5.0//EN" "dtd/cxxFile.dtd">"""
    55     elif doctype == "cxxClass":
    61     elif doctype == "cxxClass":
    56         return """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type//EN" "dtd/cxxClass.dtd">"""
    62         return """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd">"""
    57     elif doctype == "cxxAPIMap":
    63     elif doctype == "cxxAPIMap":
    58         return """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type//EN" "dtd/cxxAPIMap.dtd" >"""
    64         return """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >"""
    59     else:
    65     else:
    60         raise Exception('Unknown Doctype \"%s\"' % doctype)
    66         raise Exception('Unknown Doctype \"%s\"' % doctype)
    61 
    67 
       
    68 def get_valid_nmtoken(attribute_value):
       
    69     new_value = attribute_value
       
    70     matches = nmtoken_regex.findall(new_value)
       
    71     for char in set(matches):
       
    72         new_value = new_value.replace(char,"")
       
    73     return new_value
       
    74 
       
    75 class XmlParser(object):
       
    76     """
       
    77     Simple class that reads an XML and returns its id
       
    78 
       
    79     >>> xp = XmlParser()
       
    80     >>> xp.parse(StringIO("<root id='rootid'>some content</root>"))
       
    81     'rootid'
       
    82     """
       
    83     def parse(self, xmlfile):
       
    84         try:
       
    85             root = etree.parse(xmlfile).getroot()
       
    86         except xml.parsers.expat.ExpatError, e:
       
    87             sys.stderr.write("ERROR: %s could not be parse: %s\n" % (xmlfile, str(e)))
       
    88             return ""
       
    89         if 'id' not in root.attrib:
       
    90             return ""
       
    91         return root.attrib['id']
    62 
    92 
    63 def main(func, version):
    93 def main(func, version):
    64     usage = "usage: %prog <Path to the XML content>"
    94     usage = "usage: %prog <Path to the XML content>"
    65     parser = OptionParser(usage, version='%prog ' + version)
    95     parser = OptionParser(usage, version='%prog ' + version)
    66     (options, args) = parser.parse_args()
    96     (options, args) = parser.parse_args()
    75 
   105 
    76 class Testxml_decl(unittest.TestCase):
   106 class Testxml_decl(unittest.TestCase):
    77     def testi_can_return_anxml_declaration(self):
   107     def testi_can_return_anxml_declaration(self):
    78         self.assertEquals(xml_decl(), """<?xml version="1.0" encoding="UTF-8"?>""")
   108         self.assertEquals(xml_decl(), """<?xml version="1.0" encoding="UTF-8"?>""")
    79     
   109     
       
   110     
    80 class Testdoctype_identifier(unittest.TestCase):
   111 class Testdoctype_identifier(unittest.TestCase):
    81     
   112     
    82     def test_i_raise_an_exception_for_an_unknown_doctype(self):
   113     def test_i_raise_an_exception_for_an_unknown_doctype(self):
    83         self.assertRaises(Exception, doctype_identifier, "invaliddoctype")
   114         self.assertRaises(Exception, doctype_identifier, "invaliddoctype")
    84         
   115         
   102         
   133         
   103     def test_i_can_return_a_bookmap_doctype_identifier(self):        
   134     def test_i_can_return_a_bookmap_doctype_identifier(self):        
   104         self.assertEquals(doctype_identifier("bookmap"), """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""")
   135         self.assertEquals(doctype_identifier("bookmap"), """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""")
   105 
   136 
   106     def test_i_can_return_a_cxxUnion_doctype_identifier(self):        
   137     def test_i_can_return_a_cxxUnion_doctype_identifier(self):        
   107         self.assertEquals(doctype_identifier("cxxUnion"), """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type//EN" "dtd/cxxUnion.dtd">""")
   138         self.assertEquals(doctype_identifier("cxxUnion"), """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type v0.5.0//EN" "dtd/cxxUnion.dtd">""")
   108     
   139     
   109     def test_i_can_return_a_cxxStruct_doctype_identifier(self):        
   140     def test_i_can_return_a_cxxStruct_doctype_identifier(self):        
   110         self.assertEquals(doctype_identifier("cxxStruct"), """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type//EN" "dtd/cxxStruct.dtd">""")
   141         self.assertEquals(doctype_identifier("cxxStruct"), """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type v0.5.0//EN" "dtd/cxxStruct.dtd">""")
   111         
   142         
   112     def test_i_can_return_a_cxxPackage_doctype_identifier(self):        
   143     def test_i_can_return_a_cxxPackage_doctype_identifier(self):        
   113         self.assertEquals(doctype_identifier("cxxPackage"), """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type//EN" "dtd/cxxPackage.dtd">""")
   144         self.assertEquals(doctype_identifier("cxxPackage"), """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type v0.5.0//EN" "dtd/cxxPackage.dtd">""")
   114         
   145         
   115     
       
   116     def test_i_can_return_a_cxxFile_doctype_identifier(self):        
   146     def test_i_can_return_a_cxxFile_doctype_identifier(self):        
   117         self.assertEquals(doctype_identifier("cxxFile"), """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type//EN" "dtd/cxxFile.dtd">""")
   147         self.assertEquals(doctype_identifier("cxxFile"), """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type v0.5.0//EN" "dtd/cxxFile.dtd">""")
   118         
   148         
   119     def test_i_can_return_a_cxxClass_doctype_identifier(self):        
   149     def test_i_can_return_a_cxxClass_doctype_identifier(self):        
   120         self.assertEquals(doctype_identifier("cxxClass"), """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type//EN" "dtd/cxxClass.dtd">""")
   150         self.assertEquals(doctype_identifier("cxxClass"), """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd">""")
   121         
   151         
   122     def test_i_can_return_a_cxxAPIMap_doctype_identifier(self):        
   152     def test_i_can_return_a_cxxAPIMap_doctype_identifier(self):        
   123         self.assertEquals(doctype_identifier("cxxAPIMap"), """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type//EN" "dtd/cxxAPIMap.dtd" >""")
   153         self.assertEquals(doctype_identifier("cxxAPIMap"), """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >""")
       
   154         
   124                 
   155                 
   125         
   156 class Testget_valid_nmtoken(unittest.TestCase):
       
   157     
       
   158     def test_i_remove_non_alpha_numeric_characters(self):
       
   159         input = "this is an alphanumeric string with non alpha numeric characters inside.()_+=-string0123456789"
       
   160         expout = "thisisanalphanumericstringwithnonalphanumericcharactersinside._string0123456789"
       
   161         output = get_valid_nmtoken(input)
       
   162         self.assertEquals(output, expout)        
       
   163         
       
   164         
       
   165 class StubXmlParser(object):
       
   166     def parse(self, path):
       
   167         return "GUID-BED8A733-2ED7-31AD-A911-C1F4707C67F"
       
   168 
       
   169 
       
   170 class TestXmlParser(unittest.TestCase):
       
   171     def test_i_issue_a_warning_and_continue_if_a_file_is_invalid(self):
       
   172         xml = XmlParser()
       
   173         try:
       
   174             xml.parse(StringIO("<foo><bar</foo>"))
       
   175         except Exception, e:
       
   176             self.fail("I shouldn't have raised an exception. Exception was %s" % e) 
       
   177 
       
   178     def test_i_issue_a_warning_and_continue_if_a_file_does_not_have_an_id(self):
       
   179         xml = XmlParser()
       
   180         try:
       
   181             id = xml.parse(StringIO(brokencxxclass))
       
   182         except Exception:
       
   183             self.fail("I shouldn't have raised an exception")
       
   184         self.assertTrue(id == "") 
       
   185 
       
   186     def test_i_return_a_files_id(self):
       
   187         xml = XmlParser()
       
   188         id = xml.parse(StringIO(cxxclass))
       
   189         self.assertTrue(id == "class_c_active_scheduler")
       
   190         
       
   191 brokencxxclass = """<?xml version='1.0' encoding='UTF-8' standalone='no'?>
       
   192 <!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd" >
       
   193 <cxxClass>
       
   194     <apiName>CActiveScheduler</apiName>
       
   195     <shortdesc/>
       
   196 </cxxClass>
       
   197 """
       
   198 
       
   199 cxxclass = """<?xml version='1.0' encoding='UTF-8' standalone='no'?>
       
   200 <!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd" >
       
   201 <cxxClass id="class_c_active_scheduler">
       
   202     <apiName>CActiveScheduler</apiName>
       
   203     <shortdesc/>
       
   204 </cxxClass>
       
   205 """