1 from xmlTagWriter import XmlTagWriter
2 import time
3
4 """
5 XmlTreeWriter API
6 =====================================
7 *API found in writers/xmlTreeWriter.py*
8
9 :Author: Karl Norby <knorby@uchicago.edu>
10
11 **Basic API for the writer. Use the class itself as a straight foreward xml tree writer,
12 or use this api as reference for transforms.**
13
14 The XmlTreeWriter class and the connected XmlTagWriter class will write a
15 standard xml tree given a standard set of variables. XmlTreeWriter must be
16 passed a root element, which is the highest level element in an xml tree.
17 This element must contain in its dictionary the following variables:
18
19 *_name_* : String
20 A string that is the name of the element. The name will appear in as the first word after the '<' symbol. If the name is set to `_comment_`, the element will be treated as a comment.
21
22 *_attribs_* : Dictionary
23 A dictionary with the keys as the names as you will want them to appear in the document, and the values of the dictionary should be the attributes values. The values will have the str() function called on them, so you should make sure that the value will be formatted in the correct way.
24
25 *_children_* : List
26 A list with the child elements (of this same type). This should be a blank list if there are no child elements.
27
28 *_value_* : List or NoneType
29 either a list or None. The values should be in a from that will easily be converted to a string. The value for a given element should be its non-element child. Each value will be an entry in the list. These values are commonly numbers.
30
31
32 To start writing, instantiate the class as follows:
33
34 XmlTreeWriter(root, outputFile)
35
36 Variable Quick Reference:
37 -------------------------
38 +------------+-----------------+
39 |*_name_* |String |
40 +------------+-----------------+
41 |*_children_*|List |
42 +------------+-----------------+
43 |*_attribs_* |Dictionary |
44 +------------+-----------------+
45 |*_values_* |List or NoneType |
46 +------------+-----------------+
47 """
48
50
51
52
54 """
55 Initialize the writer.
56
57 Parameters:
58
59 - `root`: The root instance of a tree. Must be formatted in program's tree structure.
60 - `output`: The file object to write the tree to.
61
62 """
63
64 self.output = output
65
66 self.writeHeaderInfo()
67
68 XmlTreeWriter.passTagToTagWriter(root, 0, self.output)
69
70
71
72
74
75 """
76 Extracts element variables and initializes the tag writer for the element.
77 Recursively calls itself on its element children. Writes the ending tag if it has any values or any children.
78
79 Parameters:
80 - `element`: An element instance that follows the program's tree structure.
81 - `tabs`: An integer that specifies how many tabs preceed an element. Starts at zero for the root element.
82 - `output`: The file object to write the tree to.
83 """
84
85
86 name = element._name_
87
88 children = element._children_
89
90 attribs = element._attribs_
91
92 value = element._value_
93
94 hasChildren = len(children) > 0
95
96 if len(children) == 1:
97 if children[0]._name_ == '_comment_':
98 hasChildren = False
99
100 hasValue = (not value == None)
101
102 tagWriter = XmlTagWriter(name, attribs, value, hasChildren, hasValue, tabs, output)
103
104 tabs+=1
105
106 for child in children:
107
108 XmlTreeWriter.passTagToTagWriter(child, tabs, output)
109
110 if hasChildren:
111
112 tagWriter.writeEndTag()
113
114
115 passTagToTagWriter = staticmethod(passTagToTagWriter)
116
117
118
120 """
121 Writes a comment at the top of the file with the creation information. Includes data and time information.
122
123 Takes no arguments
124 """
125
126 print >>self.output, "<!--File created by PyXSD at %s on %s-->" % (time.strftime('%X'), time.strftime('%x'))
127