Changeset 1635 for TI00-utils


Ignore:
Timestamp:
26/10/06 17:01:01 (13 years ago)
Author:
spascoe
Message:

Modified xmlHandler so that the test case uses unittest and works with
eggs. Added ElementTree as a requirement but this might give strange
behaviour if ET is available as a non-egg. Added a test module and
registered as a testsuite with setup.py.

You should now be able to run the xmlHandler tests in one of 3 ways:

$ python ndg/utils/xmlHandler.py
$ python ndg/utils/test.py
$ python setup.py test

Location:
TI00-utils/trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • TI00-utils/trunk/ndg/utils/xmlHandler.py

    r1627 r1635  
    55except ImportError: 
    66    try: 
    7         import ElementTree 
     7        import ElementTree as ET 
    88    except ImportError: 
    9         from leonardo.thirdParty import ElementTree as ET 
     9        # For some reason when I install ElementTree with easyinstall it 
     10        # is called "elementree". 
     11        try: 
     12            import elementtree.ElementTree as ET 
     13        except ImportError: 
     14            from leonardo.thirdParty import ElementTree as ET 
    1015 
    1116import StringIO 
     17 
    1218 
    1319class xmlHandler: 
     
    1824    def __init__(self,arg,nsMap={},string=0,tagDict=None): 
    1925         
    20         ''' Take an xml file or xml string (string=1) argument (arg), and load it into an element  
     26        """ Take an xml file or xml string (string=1) argument (arg), and load it into an element  
    2127        tree  instance which becomes the (.tree) attribute of the class. The original nameserver 
    2228        map is added as an attribute to the tree, for use by external code. 
    23           
     29         
    2430        At the same time, create an html and text renditions of the xml.  
    2531        This usage is for text or html pretty printing. Note that this text and html 
     
    4349            comments are stripped 
    4450            empty elements do not preserver the <blah/> syntax 
    45         ''' 
     51        """ 
    4652         
    4753        if tagDict is None: 
     
    138144             
    139145    def __lines(self,text): 
    140         ''' Based on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061 
     146        """ Based on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/148061 
    141147        but avoiding the reduce (coz I don't understand it without too much thinking, 
    142148        and we know it will be history in future versions of python). Also ignoring  
    143149        internal line breaks ... for the moment ... 
    144         Note the possibility of using concepts of recipe 358117 if unicode is an issue ''' 
     150        Note the possibility of using concepts of recipe 358117 if unicode is an issue """ 
    145151        text=text.strip() 
    146152        if text =='': return text 
     
    159165        return s 
    160166         
     167 
     168import unittest 
     169class TestCase(unittest.TestCase): 
     170    """Try to generate HTML and TXT from the test xml. 
     171    """ 
     172 
     173    inputFile = 'instance0.xml' 
     174     
     175    def setUp(self): 
     176        # If pkg_resources is available assume the module is eggified and 
     177        # get a stream to the input data from the egg. 
     178        try: 
     179            import pkg_resources 
     180            f = pkg_resources.resource_stream(__name__, self.inputFile) 
     181        except ImportError: 
     182            # Else take the input file from __file__ 
     183            import os 
     184            f=file(os.path.join(os.path.basepath(__file__), self.inputFile)) 
     185 
     186        self.xml = xmlHandler(f) 
     187 
     188    def testHtml(self): 
     189        g = file('instance0.html', 'w') 
     190        g.write(self.xml.html) 
     191 
     192    def testTxt(self): 
     193        h = file('instance0.txt', 'w') 
     194        h.write(self.xml.text) 
     195 
    161196if __name__=="__main__": 
    162     import sys 
    163     f=file(sys.argv[1]) 
    164     xml=xmlHandler(f) 
    165     g=file('instance0.html','w') 
    166     g.write(xml.html) 
    167     h=file('instance0.txt','w') 
    168     h.write(xml.text) 
     197    unittest.main() 
  • TI00-utils/trunk/setup.py

    r1626 r1635  
    2121    url='http://proj.badc.rl.ac.uk/ndg', 
    2222 
     23    install_requires = ['ElementTree'], 
     24 
    2325    # Packages will be automatically detected if placed in the same directory 
    2426    # as this script.  To be part of the ndg.utils package you should place 
    2527    # code in ndg/utils 
    2628    packages = find_packages(), 
    27     namespace_packages = ['ndg'] 
     29    namespace_packages = ['ndg'], 
    2830 
    29     # If any data files need installing we will need to add some of these 
    30     # parameters. 
    31     # include_package_data = True 
    32     # package_data = {} 
     31    # This flag will include all files under SVN control or included in 
     32    # MANIFEST.in. 
     33    include_package_data = True, 
     34    # Finer grained control of data file inclusion can be achieved with 
     35    # these parameters.  See the setuptools docs. 
     36    #package_data = {} 
     37    #exclude_package_data = {} 
     38 
     39    test_suite = 'ndg.utils.test.suite', 
    3340) 
Note: See TracChangeset for help on using the changeset viewer.