| 1 |
|
|---|
| 2 |
import logging; |
|---|
| 3 |
import re; |
|---|
| 4 |
|
|---|
| 5 |
class FCDElement(object): |
|---|
| 6 |
"""Base class for elements in a .fcd file. |
|---|
| 7 |
|
|---|
| 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 |
""" |
|---|
| 12 |
|
|---|
| 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 = {}; |
|---|
| 19 |
|
|---|
| 20 |
def getFCD(self, key): |
|---|
| 21 |
"""Returns the <key> entry from m_fcdDict. |
|---|
| 22 |
""" |
|---|
| 23 |
return self.m_fcdDict[key]; |
|---|
| 24 |
|
|---|
| 25 |
def setFCD(self, key, value): |
|---|
| 26 |
"""Sets the <key> entry of m_fcdDict to <value>. |
|---|
| 27 |
""" |
|---|
| 28 |
self.m_fcdDict[key] = value; |
|---|
| 29 |
|
|---|
| 30 |
def _getFCDDict(self): |
|---|
| 31 |
"""Returns m_fcdDict. |
|---|
| 32 |
""" |
|---|
| 33 |
return self.m_fcdDict; |
|---|
| 34 |
|
|---|
| 35 |
def getTmpl(self, key): |
|---|
| 36 |
"""Returns the <key> entry from m_tmplDict. |
|---|
| 37 |
""" |
|---|
| 38 |
return self.m_tmplDict[key]; |
|---|
| 39 |
|
|---|
| 40 |
def setTmpl(self, key, value): |
|---|
| 41 |
"""Sets the <key> entry of m_tmplDict to <value>. |
|---|
| 42 |
""" |
|---|
| 43 |
self.m_tmplDict[key] = value; |
|---|
| 44 |
|
|---|
| 45 |
def _getTmplDict(self): |
|---|
| 46 |
"""Returns m_tmplDict. |
|---|
| 47 |
""" |
|---|
| 48 |
return self.m_tmplDict; |
|---|
| 49 |
|
|---|
| 50 |
def has_key(self, key): |
|---|
| 51 |
"""Returns if m_tmplDict contains <key>. |
|---|
| 52 |
""" |
|---|
| 53 |
return self.m_tmplDict.has_key(key); |
|---|
| 54 |
|
|---|
| 55 |
def __getitem__(self, key): |
|---|
| 56 |
"""Emulate a mapping type, same as getTmpl(key). |
|---|
| 57 |
""" |
|---|
| 58 |
return self.getTmpl(key); |
|---|
| 59 |
|
|---|
| 60 |
def __setitem__(self, key, value): |
|---|
| 61 |
"""Emulate a mapping type, same as setTmpl(key, value). |
|---|
| 62 |
""" |
|---|
| 63 |
self.setTmpl(key, value); |
|---|
| 64 |
|
|---|
| 65 |
def __contains__(self, key): |
|---|
| 66 |
"""Emulate a mapping type, returns if m_tmplDict contains <key>. |
|---|
| 67 |
""" |
|---|
| 68 |
return key in self.m_tmplDict; |
|---|
| 69 |
|
|---|
| 70 |
def _upcaseFirst(self, textValue): |
|---|
| 71 |
"""Returns a copy of textValue with the first character converted to |
|---|
| 72 |
upper case. |
|---|
| 73 |
""" |
|---|
| 74 |
if len(textValue) > 1: |
|---|
| 75 |
return textValue[0].upper() + textValue[1:]; |
|---|
| 76 |
else: |
|---|
| 77 |
return textValue.upper(); |
|---|
| 78 |
|
|---|
| 79 |
def _extractParagraphs(self, descText): |
|---|
| 80 |
"""Splits descText into a list of paragraphs. |
|---|
| 81 |
""" |
|---|
| 82 |
start = 0; |
|---|
| 83 |
end = 0; |
|---|
| 84 |
paraList = []; |
|---|
| 85 |
paraEndRE = re.compile(r"\n[ \t]*\n"); |
|---|
| 86 |
|
|---|
| 87 |
for paraEndMatch in paraEndRE.finditer(descText): |
|---|
| 88 |
end = paraEndMatch.start(); |
|---|
| 89 |
para = descText[start:end]; |
|---|
| 90 |
start = paraEndMatch.end(); |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
if para.strip() != "": |
|---|
| 94 |
paraList.append(para); |
|---|
| 95 |
|
|---|
| 96 |
if (len(paraList) == 0) and (descText.strip != ""): |
|---|
| 97 |
paraList.append(descText); |
|---|
| 98 |
|
|---|
| 99 |
return paraList; |
|---|
| 100 |
|
|---|
| 101 |
def _formatString(self, descText, indent): |
|---|
| 102 |
"""Formats the description string. |
|---|
| 103 |
""" |
|---|
| 104 |
indentStr = " " * indent; |
|---|
| 105 |
paraList = self._extractParagraphs(descText); |
|---|
| 106 |
paraListLen = len(paraList); |
|---|
| 107 |
|
|---|
| 108 |
for paraNum in range(paraListLen): |
|---|
| 109 |
lineList = paraList[paraNum].split("\n"); |
|---|
| 110 |
lineListLen = len(lineList); |
|---|
| 111 |
|
|---|
| 112 |
while lineListLen > 0 and lineList[0].strip() == "": |
|---|
| 113 |
lineList = lineList[1:]; |
|---|
| 114 |
lineListLen -= 1; |
|---|
| 115 |
|
|---|
| 116 |
while lineListLen > 0 and lineList[-1].strip() == "": |
|---|
| 117 |
lineList = lineList[0:-1]; |
|---|
| 118 |
lineListLen -= 1; |
|---|
| 119 |
|
|---|
| 120 |
for lineNum in range(lineListLen): |
|---|
| 121 |
if paraNum > 0 or lineNum > 0: |
|---|
| 122 |
lineList[lineNum] = indentStr + lineList[lineNum]; |
|---|
| 123 |
|
|---|
| 124 |
paraList[paraNum] = "\n".join(lineList); |
|---|
| 125 |
|
|---|
| 126 |
return "\n\n".join(paraList); |
|---|
| 127 |
|
|---|
| 128 |
def _formatSafeString(self, descText, indent): |
|---|
| 129 |
"""Formats the safe description string. |
|---|
| 130 |
""" |
|---|
| 131 |
indentStr = " " * indent; |
|---|
| 132 |
lineList = descText.split("\n"); |
|---|
| 133 |
lineListLen = len(lineList); |
|---|
| 134 |
|
|---|
| 135 |
while lineListLen > 0 and lineList[0].strip() == "": |
|---|
| 136 |
lineList = lineList[1:]; |
|---|
| 137 |
lineListLen -= 1; |
|---|
| 138 |
|
|---|
| 139 |
while lineListLen > 0 and lineList[lineListLen-1].strip() == "": |
|---|
| 140 |
lineList = lineList[0:lineListLen-1]; |
|---|
| 141 |
lineListLen -= 1; |
|---|
| 142 |
|
|---|
| 143 |
for lineNum, lineText in enumerate(lineList): |
|---|
| 144 |
lineText = lineText.replace("\\", "\\\\"); |
|---|
| 145 |
lineText = lineText.replace("\t", "\\t"); |
|---|
| 146 |
lineText = lineText.replace("\n", "\\n"); |
|---|
| 147 |
lineText = lineText.replace("\"", "\\\""); |
|---|
| 148 |
|
|---|
| 149 |
if lineNum == 0: |
|---|
| 150 |
lineText = "\"" + lineText + "\\n\""; |
|---|
| 151 |
else: |
|---|
| 152 |
lineText = indentStr + "\"" + lineText + "\\n\""; |
|---|
| 153 |
|
|---|
| 154 |
if lineNum < lineListLen - 1: |
|---|
| 155 |
lineText += "\n"; |
|---|
| 156 |
|
|---|
| 157 |
lineList[lineNum] = lineText; |
|---|
| 158 |
|
|---|
| 159 |
return "".join(lineList); |
|---|
| 160 |
|
|---|
| 161 |
def _formatXML(self, lines, indent): |
|---|
| 162 |
"""Formats the .fcd XML contents. |
|---|
| 163 |
""" |
|---|
| 164 |
linesLen = len(lines); |
|---|
| 165 |
indentStr = " " * indent; |
|---|
| 166 |
output = []; |
|---|
| 167 |
|
|---|
| 168 |
for lineNum, line in enumerate(lines): |
|---|
| 169 |
line = line.replace("\\", "\\\\"); |
|---|
| 170 |
line = line.replace("\t", "\\t"); |
|---|
| 171 |
line = line.replace("\n", ""); |
|---|
| 172 |
line = line.replace("\r", ""); |
|---|
| 173 |
line = line.replace("\"", "\\\""); |
|---|
| 174 |
|
|---|
| 175 |
if lineNum == 0: |
|---|
| 176 |
output.append( "\"" + line + "\\n\""); |
|---|
| 177 |
else: |
|---|
| 178 |
output.append(indentStr + "\"" + line + "\\n\""); |
|---|
| 179 |
|
|---|
| 180 |
if lineNum < linesLen - 1: |
|---|
| 181 |
output[lineNum] += "\n"; |
|---|
| 182 |
|
|---|
| 183 |
return "".join(output); |
|---|
| 184 |
|
|---|
| 185 |
def _dumpValues(self, log): |
|---|
| 186 |
"""Prints the contents of m_fcdDict and m_tmplDict to <log> |
|---|
| 187 |
""" |
|---|
| 188 |
sortFCDKeys = self._getFCDDict().keys(); |
|---|
| 189 |
sortFCDKeys.sort(); |
|---|
| 190 |
for key in sortFCDKeys: |
|---|
| 191 |
log.info(key + " >" + str(self._getFCDDict()[key]) + "<"); |
|---|
| 192 |
|
|---|
| 193 |
sortTmplKeys = self._getTmplDict().keys(); |
|---|
| 194 |
sortTmplKeys.sort(); |
|---|
| 195 |
for key in sortTmplKeys: |
|---|
| 196 |
log.info("\t" + key + " >" + str(self._getTmplDict()[key]) + "<"); |
|---|
| 197 |
|
|---|