1 | import cElementTree as ET |
---|
2 | import csml.parser_extra |
---|
3 | |
---|
4 | '''CSML v2 Parser ''' |
---|
5 | |
---|
6 | |
---|
7 | nsCSML = 'http://ndg.nerc.ac.uk/csml' |
---|
8 | nsGML = 'http://www.opengis.net/gml' |
---|
9 | nsOM = 'http://www.opengis.net/om' |
---|
10 | nsXLINK = 'http://www.w3.org/1999/xlink' |
---|
11 | nsXML = 'http://ndg.nerc.ac.uk/csml' |
---|
12 | nsMOLES='http://ndg.nerc.ac.uk/moles' |
---|
13 | |
---|
14 | def myQName(uri,tag): |
---|
15 | return "{"+uri+"}"+tag |
---|
16 | |
---|
17 | def CSML(tag): |
---|
18 | return myQName(nsCSML,tag) |
---|
19 | |
---|
20 | def GML(tag): |
---|
21 | return myQName(nsGML,tag) |
---|
22 | |
---|
23 | def Merge(dict1, dict2): |
---|
24 | '''merges (adds) two dictionary objects, used in attribute 'inheritance.''' |
---|
25 | dict3={} |
---|
26 | for item in dict1: |
---|
27 | dict3[item] =dict1[item] |
---|
28 | for item in dict2: |
---|
29 | dict3[item] =dict2[item] |
---|
30 | return dict3 |
---|
31 | |
---|
32 | |
---|
33 | class csElement(object): |
---|
34 | ''' main csElement class - all other elements inherit from this baseclass |
---|
35 | all the from/to XML conversion is done by this class''' |
---|
36 | |
---|
37 | def __init__(self, **kwargs): |
---|
38 | if not hasattr(self, 'ATTRIBUTES'): |
---|
39 | self.ATTRIBUTES=[] |
---|
40 | |
---|
41 | def toXML(self, csmlfrag): |
---|
42 | #process self... and convert  to XML |
---|
43 | for item in self.__dict__: |
---|
44 | if item in self.ATTRIBUTES: |
---|
45 | csmlfrag.set(item, self.__dict__[item]) |
---|
46 | # self.CHILDREN (recursive - calls the toXML method of children) |
---|
47 | for att in self.__dict__: |
---|
48 | if att not in ['NAMESPACE', 'ATTRIBUTES', 'CHILDREN', 'CONTENT']: |
---|
49 | for child in self.CHILDREN: |
---|
50 | if child == att: |
---|
51 | #found a match. |
---|
52 | ename = self.CHILDREN[child][0] #Element Name |
---|
53 | etype = self.CHILDREN[child][1] #Element Type |
---|
54 | if type(self.__dict__[att]) is list: |
---|
55 | for item in self.__dict__[att]: |
---|
56 | frag=ET.Element(ename) |
---|
57 | if self.CONTENT is not None: |
---|
58 | frag.text=self.CONTENT |
---|
59 | item.toXML(frag) |
---|
60 | csmlfrag.append(frag) |
---|
61 | else: |
---|
62 | frag=ET.Element(ename) |
---|
63 | if self.CONTENT is not None: |
---|
64 | frag.text=self.CONTENT |
---|
65 | self.__dict__[att].toXML(frag) |
---|
66 | csmlfrag.append(frag) |
---|
67 | return csmlfrag |
---|
68 | |
---|
69 | def fromXML(self,csmlfrag): |
---|
70 | # deal with attributes, e.g. gml id's |
---|
71 | for item in csmlfrag.items(): |
---|
72 | if item[0] in self.ATTRIBUTES: |
---|
73 | setattr(self, item[0], item[1]) |
---|
74 | # self.CHILDREN (recursive - calls the fromXML method of children |
---|
75 | for frag in csmlfrag[:]: |
---|
76 | if frag.text is not None: |
---|
77 | self.CONTENT = frag.text |
---|
78 | for child in self.CHILDREN: |
---|
79 | ename = self.CHILDREN[child][0] #Element Name |
---|
80 | if frag.tag == myQName(self.NAMESPACE, ename): |
---|
81 | etype = self.CHILDREN[child][1] |
---|
82 | #Element Type #elem = ET.Element(ename) |
---|
83 | childobj=eval(etype)() |
---|
84 | childobj.fromXML(frag) |
---|
85 | if hasattr(self, child): |
---|
86 | if type(self.__dict__[child]) is not list: |
---|
87 | tmp=self.__dict__[child] |
---|
88 | setattr(self, child, [tmp]) #convert to list |
---|
89 | self.__dict__[child].append(childobj) |
---|
90 | else: |
---|
91 | setattr(self, child, childobj) |
---|
92 | |
---|
93 | class csString(csElement): |
---|
94 | pass |
---|
95 | |
---|
96 | class AbstractGML(csElement): |
---|
97 | |
---|
98 | def __init__(self, **kwargs): |
---|
99 | self.ATTRIBUTES=[GML('id'), 'description'] # etc |
---|
100 | #ATTRIBUTES not implemented yet. |
---|
101 | |
---|
102 | |
---|
103 | class AbstractFileExtract(AbstractGML, csElement): |
---|
104 | def __init__(self,**kwargs): |
---|
105 | AbstractGML.__init__(self,**kwargs) |
---|
106 | self.NAMESPACE= nsCSML |
---|
107 | self.CHILDREN = {'fileName':['fileName', 'csString']} |
---|
108 | |
---|
109 | class NetCDFExtract(AbstractFileExtract, csElement): |
---|
110 | def __init__(self,**kwargs): |
---|
111 | AbstractFileExtract.__init__(self, **kwargs) |
---|
112 | self.NAMESPACE= nsCSML |
---|
113 | self.CHILDREN = Merge(self.CHILDREN,{'variableName':['variableName', 'csString']}) |
---|
114 | #use Merge() when inheriting CHILDREN attribute to extend rather than replace |
---|
115 | |
---|
116 | |
---|
117 | class Dataset(AbstractGML, csElement): |
---|
118 | ''' Dataset class, needed as root of tree''' |
---|
119 | def __init__(self, **kwargs): |
---|
120 | AbstractGML.__init__(self,**kwargs) |
---|
121 | self.NAMESPACE=nsCSML |
---|
122 | self.CHILDREN = {'ncExtract':['NetCDFExtract', 'NetCDFExtract']} |
---|
123 | def toXML(self): |
---|
124 | csmlfrag=ET.Element('Dataset') |
---|
125 | csElement.toXML(self, csmlfrag) |
---|
126 | return csmlfrag |
---|
127 | |
---|
128 | def main(): |
---|
129 | '''round trip for testing purposes:''' |
---|
130 | tree=ET.ElementTree(file='test.xml') |
---|
131 | ds=Dataset() |
---|
132 | ds.fromXML(tree.getroot()) |
---|
133 | csmltree=ds.toXML() |
---|
134 | #print csmltree |
---|
135 | csmlout=csml.parser_extra.PrettyPrint(csmltree) |
---|
136 | print csmlout |
---|
137 | #print dir(csmlout) |
---|
138 | |
---|
139 | |
---|
140 | if __name__=='__main__': |
---|
141 | main() |
---|