def justErrors(value): """Check value and ensure it contains no text, only errors (or nothing).""" if not value: return None if not isinstance(value, list): value = [value] result = [] for part in flatten(value): if part: if not isinstance(part, Error): raise Exception('Got %r when expecting only errors' % part) else: result.append(part) return value
def stringsAndErrors(value): """Aggregate both unparsed strings and errors.""" if not value: return None if not isinstance(value, list): value = [value] result = [] lastStringPart = None for part in flatten(value): if isinstance(part, basestring): lastStringPart = lastStringPart or StringIO.StringIO() lastStringPart.write(part) else: if lastStringPart: result.append(lastStringPart.getvalue()) lastStringPart = None if part: result.append(part) if lastStringPart: result.append(lastStringPart.getvalue()) return result