def WithErrorHandling(action, errorMessage, output=None, showErrorMessageBox=False): result = None try: result = action() except Exception, e: if output is not None: output() output(errorMessage) exception_util.LogOutputErrorDetails(e, output) if showErrorMessageBox: fullErrorMessage = StringBuilder() fullErrorMessage.AppendLine(errorMessage) fullErrorMessage.AppendLine() fullErrorMessage.AppendLine(exception_util.GetExceptionDetails(e)) ShowScriptErrorMessageBox(fullErrorMessage.ToString()) SetDataInCurrentDomain(SCRIPT_HOST_ERROR_DATA_VARIABLE, e)
class StreamOperator(object): def __init__(self, maxsize): self._maxsize = maxsize self._data = Array.CreateInstance(System.Byte, self._maxsize) Random().NextBytes(self._data) self._log = StringBuilder() self._currentoffset = 0 self._currentlength = 0 def setLength(self, streams): newlength = random.randrange(0,self._maxsize) self._currentlength = newlength self._currentoffset = min(self._currentoffset, newlength) self._trace('stream.SetLength(%d)' % newlength) for s in streams: s.SetLength(newlength) def setPosition(self, streams): newposition = random.randrange(0,self._maxsize) self._currentoffset = newposition self._trace('stream.Position = %d' % newposition) for s in streams: s.Position = newposition def seek(self, streams): origin = random.choice([SeekOrigin.Begin, SeekOrigin.Current, SeekOrigin.End]) newoffset = random.randrange(0,self._maxsize) if SeekOrigin.Begin == origin: delta = newoffset - 0 elif SeekOrigin.Current == origin: delta = newoffset - self._currentoffset elif SeekOrigin.End == origin: delta = newoffset - self._currentlength self._currentoffset = newoffset self._trace('stream.Seek(%d, %s)' % (delta, origin)) for s in streams: p = s.Seek(delta, origin) self._check(p == self._currentoffset, 'got offset %d from seek. expected %d' % (p, self._currentoffset)) def write(self, streams): count = random.randrange(0, self._maxsize - self._currentoffset) maxoffset = self._maxsize - count offset = random.randrange(0, maxoffset) self._currentoffset += count self._currentlength = max(self._currentlength, self._currentoffset) self._trace('stream.Write(data, %d, %d)' % (offset, count)) for s in streams: s.Write(self._data, offset, count) def read(self, streams): count = random.randrange(0, self._maxsize) buffer = Array.CreateInstance(System.Byte, self._maxsize) maxoffset = self._maxsize - count offset = random.randrange(0, maxoffset) toread = min(count, self._currentlength - self._currentoffset) toread = max(0, toread) self._currentoffset += toread self._trace('stream.Read(data, %d, %d)' % (offset, count)) for s in streams: r = s.Read(buffer, offset, count) self._check(r == toread, 'got %d bytes from read. expected %d' % (r, toread)) def checkLength(self, streams): for s in streams: l = s.Length self._check(l == self._currentlength, 'stream is %d bytes. expected %d' % (l, self._currentlength)) def checkPosition(self, streams): for s in streams: p = s.Position self._check(p == self._currentoffset, 'position is %d bytes. expected %d' % (p, self._currentoffset)) def rewind(self, streams): self._currentoffset = 0 self._trace('stream.Seek(0, SeekOrigin.Begin)') for s in streams: p = s.Seek(0, SeekOrigin.Begin) self._check(p == self._currentoffset, 'got offset %d from seek. expected %d' % (p, self._currentoffset)) def clear(self, streams): self._currentlength = 0 self._trace('stream.SetLength(0)') self._currentoffset = 0 self._trace('stream.Seek(0, SeekOrigin.Begin)') for s in streams: s.SetLength(0) p = s.Seek(0, SeekOrigin.Begin) self._check(p == self._currentoffset, 'got offset %d from seek. expected %d' % (p, self._currentoffset)) def compare(self, streams): self.checkLength(streams) self.checkPosition(streams) self.rewind(streams) expected = None hash = SHA512Managed() for s in streams: actual = hash.ComputeHash(s) if None <> expected: self._check(self._compareHashValues(expected, actual), 'hash mismatch %s/%s' % (expected, actual)) expected = actual def _compareHashValues(self, a, b): for i in xrange(len(a)): if a[i] <> b[i]: return False return True def _trace(self, s): self._log.AppendLine('%s # position = %d, length = %d' % (s, self._currentoffset, self._currentlength)) def _check(self, condition, message): if not condition: print message print self._log raise AssertionError
def DialogShowingEventHandler(sender, eventArgs, output): try: dialogResult = IDOK msg = StringBuilder() msg.AppendLine() msg.AppendLine("Dialog box shown:") msg.AppendLine() if isinstance(eventArgs, TaskDialogShowingEventArgs): msg.AppendLine("\tMessage: " + str(eventArgs.Message)) if eventArgs.DialogId == "TaskDialog_Missing_Third_Party_Updater": dialogResult = 1001 # Continue working with the file. elif eventArgs.DialogId == "TaskDialog_Location_Position_Changed": dialogResult = 1002 # Do not save. elif isinstance(eventArgs, MessageBoxShowingEventArgs): msg.AppendLine("\tMessage: " + str(eventArgs.Message)) msg.AppendLine("\tDialogType: " + str(eventArgs.DialogType)) dialogId = Try( lambda: eventArgs.DialogId ) # Available on DialogBoxShowingEventArgs in Revit 2017+ if dialogId is not None: msg.AppendLine("\tDialogId: " + str(dialogId)) helpId = Try( lambda: eventArgs.HelpId) # No longer available in Revit 2018+ if helpId is not None: msg.AppendLine("\tHelpId: " + str(helpId)) output(msg.ToString()) eventArgs.OverrideResult(dialogResult) except Exception, e: errorMsg = StringBuilder() errorMsg.AppendLine() errorMsg.AppendLine("Caught exception in dialog event handler!") errorMsg.AppendLine("Exception message: " + e.message) output(errorMsg.ToString()) exception_util.LogOutputErrorDetails(e, output)
path = r"lib/EnyimMemcached/build/CommonProperties.targets" sr = StreamReader(path) sb = StringBuilder() line = sr.ReadLine() while not line is None: #I'm sure this could all be done in a couple of lines with a nice multi-line Regex #All this does is comment out property groups that attempt to set signing if line.Trim().StartsWith("<PropertyGroup") and line.Contains("PrivateKey"): sb.AppendFormat("<!--{0}\r\n", line) while line is not None and line.Trim() != "</PropertyGroup>": sb.AppendLine(line) line = sr.ReadLine() else: sb.AppendFormat("{0}-->\r\n", line) else: sb.AppendLine(line) line = sr.ReadLine() content = sb.ToString() content = content.Replace("<DelaySign>true</DelaySign>", "<DelaySign>false</DelaySign>") content = content.Replace("<SignAssembly>true</SignAssembly>", "<SignAssembly>false</SignAssembly>") sr.Dispose() sw = StreamWriter(path, False) sw.Write(sb.ToString())