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