+class ControlFile:
+
+ def __init__(self):
+ self.control = re.compile(r'^(C[ONPpRrC])([A-Za-z0-9]*):\s*(.*)$')
+ self.fileformat = re.compile(r'^((\>\>?)[,\s]\s*)?([^\s]+)\s*$')
+ self.cntrfile = dict()
+ # Map keytest marker to pattern-file-marker for searchPatterns.pl
+ self.convertSearchMark = { 'CN': 'Comment: ',
+ 'CP': 'Simple: ', 'Cp': 'ErrSimple: ',
+ 'CR': 'Regex: ', 'Cr': 'ErrRegex: '}
+
+ def __open(self, handle, filename):
+ if handle in self.cntrfile:
+ self.cntrfile[handle].close()
+ del self.cntrfile[handle]
+ m = self.fileformat.match(filename)
+ if m:
+ type = m.group(2)
+ filename = m.group(3)
+ if type == '>>':
+ append = True
+ else:
+ append = False
+ else:
+ append = False
+ if append:
+ self.cntrfile[handle] = open(filename, 'a')
+ else:
+ self.cntrfile[handle] = open(filename, 'w')
+
+ def closeall(self):
+ handles = self.cntrfile.keys()
+ for handle in handles:
+ self.__close(handle)
+
+ def __close(self, handle):
+ if handle in self.cntrfile:
+ name = self.cntrfile[handle].name
+ self.cntrfile[handle].close()
+ del self.cntrfile[handle]
+ print("Closed ctrl " + handle + " (" + name + ")")
+
+ # make the method below 'private'
+ def __addline(self, handle, pat):
+ self.cntrfile[handle].writelines(pat + "\n")
+
+ def dispatch(self, c):
+ m = self.control.match(c)
+ if not m:
+ return False
+ command = m.group(1)
+ handle = m.group(2)
+ if handle is None:
+ handle = ""
+ text = m.group(3)
+ if command == "CO":
+ self.__open(handle, text);
+ elif command == "CC":
+ self.__close(handle)
+ else:
+ if handle in self.cntrfile:
+ if command in self.convertSearchMark:
+ self.__addline(handle, self.convertSearchMark[command] + text)
+ else:
+ die(1,"Error, Unrecognised Command '" + command + "'")
+ elif handle != "":
+ die(1, "Ctrl-file " + handle + " not in use")
+ return True
+
+