Orb/python/orb/lib.py
changeset 4 468f4c8d3d5b
parent 2 932c358ece3e
equal deleted inserted replaced
3:d8fccb2cd802 4:468f4c8d3d5b
    15 import xml
    15 import xml
    16 import re
    16 import re
    17 import sys
    17 import sys
    18 from optparse import OptionParser
    18 from optparse import OptionParser
    19 from cStringIO import StringIO
    19 from cStringIO import StringIO
    20 from xml.etree import ElementTree as etree
    20 try:
       
    21     from xml.etree import cElementTree as etree
       
    22 except ImportError:
       
    23     from xml.etree import ElementTree as etree
       
    24 
    21 
    25 
    22 nmtoken_regex = re.compile("[^a-zA-Z0-9_\.]")
    26 nmtoken_regex = re.compile("[^a-zA-Z0-9_\.]")
       
    27 # Version to use in the DOCTYPE declaration
       
    28 # Should match regex: v(\d+)\.(\d+)\.(\d+)(\S*)
       
    29 # See Doxygen ditaelementprefix.cpp
       
    30 #const char *DOCTYPE_VERSION = "v0.6.0";
       
    31 doctype_version = "v0.6.0"
    23 
    32 
    24 def scan(dir):
    33 def scan(dir):
    25     for root, _, files in os.walk(dir):
    34     for root, _, files in os.walk(dir):
    26         for fname in files:
    35         for fname in files:
    27             yield os.path.join(root, fname) 
    36             yield os.path.join(root, fname) 
    49         return """<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">"""
    58         return """<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">"""
    50     elif doctype == "bookmap":
    59     elif doctype == "bookmap":
    51         return """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""" 
    60         return """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""" 
    52     # cxxapiref DITA specialisation Doctype Identifiers
    61     # cxxapiref DITA specialisation Doctype Identifiers
    53     elif doctype == "cxxUnion":
    62     elif doctype == "cxxUnion":
    54         return """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type v0.5.0//EN" "dtd/cxxUnion.dtd">"""   
    63         return """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type %s//EN" "dtd/cxxUnion.dtd">""" % doctype_version
    55     elif doctype == "cxxStruct":
    64     elif doctype == "cxxStruct":
    56         return """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type v0.5.0//EN" "dtd/cxxStruct.dtd">"""
    65         return """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type %s//EN" "dtd/cxxStruct.dtd">""" % doctype_version
    57     elif doctype == "cxxPackage":
    66     elif doctype == "cxxPackage":
    58         return """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type v0.5.0//EN" "dtd/cxxPackage.dtd">"""
    67         return """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type %s//EN" "dtd/cxxPackage.dtd">""" % doctype_version
    59     elif doctype == "cxxFile":
    68     elif doctype == "cxxFile":
    60         return """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type v0.5.0//EN" "dtd/cxxFile.dtd">"""
    69         return """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type %s//EN" "dtd/cxxFile.dtd">""" % doctype_version
    61     elif doctype == "cxxClass":
    70     elif doctype == "cxxClass":
    62         return """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd">"""
    71         return """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type %s//EN" "dtd/cxxClass.dtd">""" % doctype_version
    63     elif doctype == "cxxAPIMap":
    72     elif doctype == "cxxAPIMap":
    64         return """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >"""
    73         return """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type %s//EN" "dtd/cxxAPIMap.dtd" >""" % doctype_version
    65     else:
    74     else:
    66         raise Exception('Unknown Doctype \"%s\"' % doctype)
    75         raise Exception('Unknown Doctype \"%s\"' % doctype)
    67 
    76 
    68 def get_valid_nmtoken(attribute_value):
    77 def get_valid_nmtoken(attribute_value):
    69     new_value = attribute_value
    78     new_value = attribute_value
    81     'rootid'
    90     'rootid'
    82     """
    91     """
    83     def parse(self, xmlfile):
    92     def parse(self, xmlfile):
    84         try:
    93         try:
    85             root = etree.parse(xmlfile).getroot()
    94             root = etree.parse(xmlfile).getroot()
    86         except xml.parsers.expat.ExpatError, e:
    95         except Exception, e:
       
    96         #except xml.parsers.expat.ExpatError, e:
    87             sys.stderr.write("ERROR: %s could not be parse: %s\n" % (xmlfile, str(e)))
    97             sys.stderr.write("ERROR: %s could not be parse: %s\n" % (xmlfile, str(e)))
    88             return ""
    98             return ""
    89         if 'id' not in root.attrib:
    99         if 'id' not in root.attrib:
    90             return ""
   100             return ""
    91         return root.attrib['id']
   101         return root.attrib['id']
    92 
   102 
    93 def main(func, version):
   103 def main(func, version):
    94     usage = "usage: %prog <Path to the XML content>"
   104     usage = "usage: %prog <Path to the XML content>"
    95     parser = OptionParser(usage, version='%prog ' + version)
   105     parser = OptionParser(usage, version='%prog ' + version)
    96     (options, args) = parser.parse_args()
   106     (_, args) = parser.parse_args()
    97     if len(args) < 1:
   107     if len(args) < 1:
    98         parser.print_help()
   108         parser.print_help()
    99         parser.error("Please supply the path to the XML content")
   109         parser.error("Please supply the path to the XML content")
   100     func(args[0])
   110     func(args[0])
   101     
   111     
   133         
   143         
   134     def test_i_can_return_a_bookmap_doctype_identifier(self):        
   144     def test_i_can_return_a_bookmap_doctype_identifier(self):        
   135         self.assertEquals(doctype_identifier("bookmap"), """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""")
   145         self.assertEquals(doctype_identifier("bookmap"), """<!DOCTYPE bookmap PUBLIC "-//OASIS//DTD DITA BookMap//EN" "bookmap.dtd">""")
   136 
   146 
   137     def test_i_can_return_a_cxxUnion_doctype_identifier(self):        
   147     def test_i_can_return_a_cxxUnion_doctype_identifier(self):        
   138         self.assertEquals(doctype_identifier("cxxUnion"), """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type v0.5.0//EN" "dtd/cxxUnion.dtd">""")
   148         self.assertEquals(doctype_identifier("cxxUnion"), """<!DOCTYPE cxxUnion PUBLIC "-//NOKIA//DTD DITA C++ API Union Reference Type %s//EN" "dtd/cxxUnion.dtd">""" % doctype_version)
   139     
   149     
   140     def test_i_can_return_a_cxxStruct_doctype_identifier(self):        
   150     def test_i_can_return_a_cxxStruct_doctype_identifier(self):        
   141         self.assertEquals(doctype_identifier("cxxStruct"), """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type v0.5.0//EN" "dtd/cxxStruct.dtd">""")
   151         self.assertEquals(doctype_identifier("cxxStruct"), """<!DOCTYPE cxxStruct PUBLIC "-//NOKIA//DTD DITA C++ API Struct Reference Type %s//EN" "dtd/cxxStruct.dtd">""" % doctype_version)
   142         
   152         
   143     def test_i_can_return_a_cxxPackage_doctype_identifier(self):        
   153     def test_i_can_return_a_cxxPackage_doctype_identifier(self):        
   144         self.assertEquals(doctype_identifier("cxxPackage"), """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type v0.5.0//EN" "dtd/cxxPackage.dtd">""")
   154         self.assertEquals(doctype_identifier("cxxPackage"), """<!DOCTYPE cxxPackage PUBLIC "-//NOKIA//DTD DITA cxx API Package Reference Type %s//EN" "dtd/cxxPackage.dtd">""" % doctype_version)
   145         
   155         
   146     def test_i_can_return_a_cxxFile_doctype_identifier(self):        
   156     def test_i_can_return_a_cxxFile_doctype_identifier(self):        
   147         self.assertEquals(doctype_identifier("cxxFile"), """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type v0.5.0//EN" "dtd/cxxFile.dtd">""")
   157         self.assertEquals(doctype_identifier("cxxFile"), """<!DOCTYPE cxxFile PUBLIC "-//NOKIA//DTD DITA C++ API File Reference Type %s//EN" "dtd/cxxFile.dtd">""" % doctype_version)
   148         
   158         
   149     def test_i_can_return_a_cxxClass_doctype_identifier(self):        
   159     def test_i_can_return_a_cxxClass_doctype_identifier(self):        
   150         self.assertEquals(doctype_identifier("cxxClass"), """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type v0.5.0//EN" "dtd/cxxClass.dtd">""")
   160         self.assertEquals(doctype_identifier("cxxClass"), """<!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type %s//EN" "dtd/cxxClass.dtd">""" % doctype_version)
   151         
   161         
   152     def test_i_can_return_a_cxxAPIMap_doctype_identifier(self):        
   162     def test_i_can_return_a_cxxAPIMap_doctype_identifier(self):        
   153         self.assertEquals(doctype_identifier("cxxAPIMap"), """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type v0.5.0//EN" "dtd/cxxAPIMap.dtd" >""")
   163         self.assertEquals(doctype_identifier("cxxAPIMap"), """<!DOCTYPE cxxAPIMap PUBLIC "-//NOKIA//DTD DITA C++ API Map Reference Type %s//EN" "dtd/cxxAPIMap.dtd" >""" % doctype_version)
   154         
   164         
   155                 
   165                 
   156 class Testget_valid_nmtoken(unittest.TestCase):
   166 class Testget_valid_nmtoken(unittest.TestCase):
   157     
   167     
   158     def test_i_remove_non_alpha_numeric_characters(self):
   168     def test_i_remove_non_alpha_numeric_characters(self):
   187         xml = XmlParser()
   197         xml = XmlParser()
   188         id = xml.parse(StringIO(cxxclass))
   198         id = xml.parse(StringIO(cxxclass))
   189         self.assertTrue(id == "class_c_active_scheduler")
   199         self.assertTrue(id == "class_c_active_scheduler")
   190         
   200         
   191 brokencxxclass = """<?xml version='1.0' encoding='UTF-8' standalone='no'?>
   201 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" >
   202 <!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type %s//EN" "dtd/cxxClass.dtd" >
   193 <cxxClass>
   203 <cxxClass>
   194     <apiName>CActiveScheduler</apiName>
   204     <apiName>CActiveScheduler</apiName>
   195     <shortdesc/>
   205     <shortdesc/>
   196 </cxxClass>
   206 </cxxClass>
   197 """
   207 """ % doctype_version
   198 
   208 
   199 cxxclass = """<?xml version='1.0' encoding='UTF-8' standalone='no'?>
   209 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" >
   210 <!DOCTYPE cxxClass PUBLIC "-//NOKIA//DTD DITA C++ API Class Reference Type %s//EN" "dtd/cxxClass.dtd" >
   201 <cxxClass id="class_c_active_scheduler">
   211 <cxxClass id="class_c_active_scheduler">
   202     <apiName>CActiveScheduler</apiName>
   212     <apiName>CActiveScheduler</apiName>
   203     <shortdesc/>
   213     <shortdesc/>
   204 </cxxClass>
   214 </cxxClass>
   205 """
   215 """ % doctype_version