1
2 """Helps formatting output for restructured text"""
3
4 import os
5
6 from PyFoam.Error import error
7
9 """Helper class that formats stuff for restructured text"""
10
11 LevelPart = 0
12 LevelChapter = 1
13 LevelSection = 2
14 LevelSubSection = 3
15 LevelSubSubSection = 4
16 LevelParagraph = 5
17
18 - def __init__(self,defaultHeading=LevelSection):
19 self.defaultHeading=defaultHeading
20
21 - def buildHeading(self,*text,**keywords):
22 """General method to build a heading
23 @param text: list of items that build the heading text
24 @param level: The level of the heading"""
25
26 level=RestructuredTextHelper.LevelSection
27 if "level" in keywords:
28 level=keywords["level"]
29
30 header=None
31 for t in text:
32 if header==None:
33 header=""
34 else:
35 header+=" "
36 header+=str(t)
37 overline=False
38 if level==RestructuredTextHelper.LevelPart:
39 c="#"
40 overline=True
41 elif level==RestructuredTextHelper.LevelChapter:
42 c="*"
43 overline=True
44 elif level==RestructuredTextHelper.LevelSection:
45 c="="
46 elif level==RestructuredTextHelper.LevelSubSection:
47 c="-"
48 elif level==RestructuredTextHelper.LevelSubSubSection:
49 c="^"
50 elif level==RestructuredTextHelper.LevelParagraph:
51 c='"'
52 else:
53 error("Unknown level",level,"for headers")
54
55 underline=c*len(header)
56
57 result="\n"
58
59 if overline:
60 result+=underline+"\n"
61
62 result+=header+"\n"
63 result+=underline+"\n"
64
65 return result
66
67 - def heading(self,*text):
68 """Build a heading on the default level"""
69
70 keys={"level":self.defaultHeading}
71
72 return self.buildHeading(*text,**keys)
73
75 """Creates a new ReSTTable-object"""
76 return ReSTTable()
77
79 """Class that administrates a two-dimensional table and prints it as
80 a restructured text-table when asked"""
81
83 self.data=[[]]
84 self.lines=set()
85 self.head=-1;
86
87 - def addLine(self,val=None,head=False):
88 """Add a line after that row
89 @param val: the row after which to add. If None a line will be added after the
90 current last row"""
91 if val==None:
92 now=len(self.data)-1
93 else:
94 now=int(val)
95 self.lines.add(now)
96 if head:
97 self.head=now
98
100 """Output the actual table"""
101 widths=[1]*len(self.data[0])
102 for r in self.data:
103 for i,v in enumerate(r):
104 try:
105 widths[i]=max(widths[i],len(v))
106 except TypeError:
107 if i==0:
108 widths[i]=max(widths[i],2)
109 else:
110 widths[i]=max(widths[i],1)
111
112 head=None
113 for w in widths:
114 if head==None:
115 head=""
116 else:
117 head+=" "
118 head+="="*w
119
120 inter=head.replace("=","-")
121
122 txt=head+"\n"
123
124 for i,r in enumerate(self.data):
125 line=""
126 for j,v in enumerate(r):
127 if v==None or v=="":
128 if j==0:
129 t=".."
130 else:
131 t=""
132 else:
133 t=v
134 if j>0:
135 line+=" "
136 line+=t+" "*(widths[j]-len(t))
137 txt+=line+"\n"
138 if i==(len(self.data)-1):
139 txt+=head+"\n"
140 elif i in self.lines:
141 if i==self.head:
142 txt+=head+"\n"
143 else:
144 txt+=inter+"\n"
145
146 return "\n"+txt
147
149 """Sets an item of the table
150 @param index: a tuple with a row and a column. If it is a single integer then the
151 row is assumed
152 @param value: the value to set. If only the row was specified it is a list with the column
153 values"""
154
155 try:
156 row,col=index
157 self.setElement(row,col,value)
158 except TypeError:
159 row=index
160 for col,v in enumerate(value):
161 self.setElement(row,col,v)
162
164 """Sets a specific element
165 @param row: the row
166 @param col: column
167 @param value: the used value"""
168
169 if len(self.data)<=row:
170 self.data+=[[None]*len(self.data[0])]*(row-len(self.data)+1)
171 if len(self.data[row])<=col:
172 for r in self.data:
173 r+=[None]*(col-len(r)+1)
174
175 self.data[row][col]=str(value)
176