NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

Changeset 367

Show
Ignore:
Timestamp:
09/02/10 19:38:16
Author:
cbc
Message:

Created Data, Device, Sensor, and Point classes in parse module.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spongenet/trunk/spongenet/parse.py

    r366 r367  
    2222import sys 
    2323import os 
     24import re 
    2425import glob 
    2526import hashlib 
     
    3132USAGE = "\n".join(__doc__.splitlines()[3:8]) 
    3233TEST_PATH = os.path.join("tests", "parse") 
     34XMLNS_PATTERN = re.compile(r"(\{.*\})(.*)") 
    3335 
    3436 
     
    164166    Verify the test document matches the reference data. 
    165167 
    166     >>> _xmldoc.splitlines()[0] == '<?xml version="1.0" encoding="utf-8"?>' 
    167     True 
    168     >>> _xmldoc.splitlines()[-1] == '</root>' 
    169     True 
    170     >>> len(_xmldoc.splitlines()) == xml_doc_lens[os.path.basename(_xmldoc_path)] 
     168    >>> xmldoc_lines = _xmldoc.splitlines() 
     169    >>> xmldoc_lines[0] == '<?xml version="1.0" encoding="utf-8"?>' 
     170    True 
     171    >>> xmldoc_lines[-1] == '</root>' 
     172    True 
     173    >>> len(xmldoc_lines) == xml_doc_lens[os.path.basename(_xmldoc_path)] 
    171174    True 
    172175    >>> doc_hash = hashlib.md5() 
     
    183186 
    184187 
     188class Point(object): 
     189    """ 
     190    A data point for a sponge sensor sample." 
     191    """ 
     192 
     193    def __init__(self, point): 
     194        for key, value in point.attrib.items(): 
     195            key = key.lower() 
     196            super(Point, self).__setattr__(key, value) 
     197        for elem in point.getchildren(): 
     198            tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() 
     199            super(Point, self).__setattr__(tag, elem.text) 
     200 
     201 
     202class Sensor(object): 
     203    """ 
     204    A collection of data points for a sponge sensor sample. 
     205    """ 
     206 
     207    def __init__(self, sensor, xmlns): 
     208        for key, value in sensor.attrib.items(): 
     209            key = key.lower() 
     210            super(Sensor, self).__setattr__(key, value) 
     211        for elem in sensor.getchildren(): 
     212            tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() 
     213            if tag == "parameters": 
     214                self.points = [Point(point) for point 
     215                                in elem.findall(xmlns + "Point")] 
     216            else: 
     217                super(Sensor, self).__setattr__(tag, elem.text) 
     218 
     219 
     220class Device(object): 
     221    """ 
     222    Data from a collection of sponge sensors for a single time sample. 
     223    """ 
     224 
     225    def __init__(self, device, xmlns): 
     226        for key, value in device.attrib.items(): 
     227            key = key.lower() 
     228            super(Device, self).__setattr__(key, value) 
     229        for elem in device.getchildren(): 
     230            tag = XMLNS_PATTERN.search(elem.tag).groups()[1].lower() 
     231            if tag == "siteinfo": 
     232                for subelem in elem.getchildren(): 
     233                    tag = XMLNS_PATTERN.search(subelem.tag).groups()[1].lower() 
     234                    super(Device, self).__setattr__(tag, subelem.text) 
     235            elif tag == "data": 
     236                for key, value in elem.attrib.items(): 
     237                    key = key.lower() 
     238                    if key == "time": 
     239                        key = "data_time" 
     240                    elif key == "sessionid": 
     241                        key = "data_sessionid" 
     242                    super(Device, self).__setattr__(key, value) 
     243                self.sensors = [Sensor(sensor, xmlns) for sensor 
     244                                in elem.findall(xmlns + "SensorData")] 
     245            else: 
     246                super(Device, self).__setattr__(tag, elem.text) 
     247 
     248 
     249class Data(object): 
     250    """ 
     251    A collection of sponge data samples from a collection of sensors. 
     252    """ 
     253 
     254    def __init__(self, _xmldoc): 
     255        """ 
     256        Initialize a new sponge data tree. 
     257        """ 
     258 
     259        tree = ET.XML(_xmldoc) 
     260        self.xmlns = XMLNS_PATTERN.search( 
     261                         tree.getchildren()[0].tag).groups()[0] 
     262        self.devices = [Device(device, self.xmlns) for device 
     263                        in tree.findall(self.xmlns + "Device")] 
     264 
     265 
    185266def _main(): 
    186267    """ 
     
    191272    >>> from parse import _main 
    192273    """ 
     274 
     275    data = None 
    193276 
    194277    _xmldoc_path = xmldoc_path() 
    195278    if _xmldoc_path: 
    196279        _xmldoc = xmldoc(_xmldoc_path) 
    197         print len(_xmldoc) 
    198  
    199     return 
     280        data = Data(_xmldoc) 
     281 
     282    return data 
    200283 
    201284if __name__ == "__main__": 
    202     _main() 
     285    DATA = _main() 
     286    print "DATA =", DATA