fixed: gcc8 compile issues
[opensg.git] / Tools / fcd2code / FCDElement.py
blob00326747bb0afd81d5d8560522befe73038d7535
2 import logging;
3 import re;
5 class FCDElement(object):
6 """Base class for elements in a .fcd file.
8 Defines two dictionaries, m_fcdDict holds entries read from the .fcd
9 file, while m_tmplDict is filled during a call to finalize() with values
10 that can be accessed through the [] operator.
11 """
13 def __init__(self):
14 """Create new instance of FCDElement and initialize both dictionaries.
15 """
16 self.m_log = logging.getLogger("FCDElement");
17 self.m_fcdDict = {};
18 self.m_tmplDict = {};
20 def getFCD(self, key):
21 """Returns the <key> entry from m_fcdDict.
22 """
23 return self.m_fcdDict[key];
25 def setFCD(self, key, value, allowNew = False):
26 """Sets the <key> entry of m_fcdDict to <value>.
27 """
28 if allowNew:
29 self.m_fcdDict[key] = value;
30 else:
31 if self.m_fcdDict.has_key(key):
32 self.m_fcdDict[key] = value;
33 else:
34 self.m_log.warning("setFCD: Unknown key [%s]." % key);
36 def _getFCDDict(self):
37 """Returns m_fcdDict.
38 """
39 return self.m_fcdDict;
41 def getTmpl(self, key):
42 """Returns the <key> entry from m_tmplDict.
43 """
44 return self.m_tmplDict[key];
46 def setTmpl(self, key, value):
47 """Sets the <key> entry of m_tmplDict to <value>.
48 """
49 self.m_tmplDict[key] = value;
51 def _getTmplDict(self):
52 """Returns m_tmplDict.
53 """
54 return self.m_tmplDict;
56 def has_key(self, key):
57 """Returns if m_tmplDict contains <key>.
58 """
59 return self.m_tmplDict.has_key(key);
61 def __getitem__(self, key):
62 """Emulate a mapping type, same as getTmpl(key).
63 """
64 return self.getTmpl(key);
66 def __setitem__(self, key, value):
67 """Emulate a mapping type, same as setTmpl(key, value).
68 """
69 self.setTmpl(key, value);
71 def __contains__(self, key):
72 """Emulate a mapping type, returns if m_tmplDict contains <key>.
73 """
74 return key in self.m_tmplDict;
76 def _upcaseFirst(self, textValue):
77 """Returns a copy of textValue with the first character converted to
78 upper case.
79 """
80 if len(textValue) > 1:
81 return textValue[0].upper() + textValue[1:];
82 else:
83 return textValue.upper();
85 def _extractParagraphs(self, descText):
86 """Splits descText into a list of paragraphs.
87 """
88 start = 0;
89 end = 0;
90 paraList = [];
91 paraEndRE = re.compile(r"\n[ \t]*\n");
93 for paraEndMatch in paraEndRE.finditer(descText):
94 end = paraEndMatch.start();
95 para = descText[start:end];
96 start = paraEndMatch.end();
98 # ignore empty paragraphs.
99 if para.strip() != "":
100 paraList.append(para);
102 if (len(paraList) == 0) and (descText.strip != ""):
103 paraList.append(descText);
105 return paraList;
107 def _formatString(self, descText, indent):
108 """Formats the description string.
110 indentStr = " " * indent;
111 paraList = self._extractParagraphs(descText);
112 paraListLen = len(paraList);
114 for paraNum in range(paraListLen):
115 lineList = paraList[paraNum].split("\n");
116 lineListLen = len(lineList);
118 while lineListLen > 0 and lineList[0].strip() == "":
119 lineList = lineList[1:];
120 lineListLen -= 1;
122 while lineListLen > 0 and lineList[-1].strip() == "":
123 lineList = lineList[0:-1];
124 lineListLen -= 1;
126 for lineNum in range(lineListLen):
127 if paraNum > 0 or lineNum > 0:
128 lineList[lineNum] = indentStr + lineList[lineNum];
130 paraList[paraNum] = "\n".join(lineList);
132 return "\n\n".join(paraList);
134 def _formatSafeString(self, descText, indent):
135 """Formats the safe description string.
137 indentStr = " " * indent;
138 lineList = descText.split("\n");
139 lineListLen = len(lineList);
141 while lineListLen > 0 and lineList[0].strip() == "":
142 lineList = lineList[1:];
143 lineListLen -= 1;
145 while lineListLen > 0 and lineList[lineListLen-1].strip() == "":
146 lineList = lineList[0:lineListLen-1];
147 lineListLen -= 1;
149 for lineNum, lineText in enumerate(lineList):
150 lineText = lineText.replace("\\", "\\\\");
151 lineText = lineText.replace("\t", "\\t");
152 lineText = lineText.replace("\n", "\\n");
153 lineText = lineText.replace("\"", "\\\"");
155 if lineNum == 0:
156 lineText = "\"" + lineText + "\\n\"";
157 else:
158 lineText = indentStr + "\"" + lineText + "\\n\"";
160 if lineNum < lineListLen - 1:
161 lineText += "\n";
163 lineList[lineNum] = lineText;
165 return "".join(lineList);
167 def _formatXML(self, lines, indent):
168 """Formats the .fcd XML contents.
170 linesLen = len(lines);
171 indentStr = " " * indent;
172 output = [];
174 for lineNum, line in enumerate(lines):
175 line = line.replace("\\", "\\\\");
176 line = line.replace("\t", "\\t");
177 line = line.replace("\n", "");
178 line = line.replace("\r", "");
179 line = line.replace("\"", "\\\"");
181 if lineNum == 0:
182 output.append( "\"" + line + "\\n\"");
183 else:
184 output.append(indentStr + "\"" + line + "\\n\"");
186 if lineNum < linesLen - 1:
187 output[lineNum] += "\n";
189 return "".join(output);
191 def _dumpValues(self, log):
192 """Prints the contents of m_fcdDict and m_tmplDict to <log>
194 sortFCDKeys = self._getFCDDict().keys();
195 sortFCDKeys.sort();
196 for key in sortFCDKeys:
197 log.info(key + " >" + str(self._getFCDDict()[key]) + "<");
199 sortTmplKeys = self._getTmplDict().keys();
200 sortTmplKeys.sort();
201 for key in sortTmplKeys:
202 log.info("\t" + key + " >" + str(self._getTmplDict()[key]) + "<");