def validateStream(aFile, firstOccurrenceOnly=0, contentType=None, base=""): loggedEvents = [] if contentType: (mediaType, charset) = mediaTypes.checkValid(contentType, loggedEvents) else: (mediaType, charset) = (None, None) rawdata = aFile.read(MAXDATALENGTH) if aFile.read(1): raise ValidationFailure( logging.ValidatorLimit( {'limit': 'feed length > ' + str(MAXDATALENGTH) + ' bytes'})) encoding, rawdata = xmlEncoding.decode(mediaType, charset, rawdata, loggedEvents, fallback='utf-8') validator = _validate(rawdata, firstOccurrenceOnly, loggedEvents, base, encoding, mediaType=mediaType) if mediaType and validator.feedType: mediaTypes.checkAgainstFeedType(mediaType, validator.feedType, validator.loggedEvents) return { "feedType": validator.feedType, "loggedEvents": validator.loggedEvents }
def validateString(aString, firstOccurrenceOnly=0, fallback=None, base=""): loggedEvents = [] if type(aString) != unicode: encoding, aString = xmlEncoding.decode("", None, aString, loggedEvents, fallback) else: encoding = "utf-8" # setting a sane (?) default if aString is not None: validator = _validate(aString, firstOccurrenceOnly, loggedEvents, base, encoding) return {"feedType":validator.feedType, "loggedEvents":validator.loggedEvents} else: return {"loggedEvents": loggedEvents}
def validateStream(aFile, firstOccurrenceOnly=0, contentType=None, base=""): loggedEvents = [] if contentType: (mediaType, charset) = mediaTypes.checkValid(contentType, loggedEvents) else: (mediaType, charset) = (None, None) rawdata = aFile.read(MAXDATALENGTH) if aFile.read(1): raise ValidationFailure(logging.ValidatorLimit({'limit': 'feed length > ' + str(MAXDATALENGTH) + ' bytes'})) encoding, rawdata = xmlEncoding.decode(mediaType, charset, rawdata, loggedEvents, fallback='utf-8') validator = _validate(rawdata, firstOccurrenceOnly, loggedEvents, base, encoding) if mediaType and validator.feedType: mediaTypes.checkAgainstFeedType(mediaType, validator.feedType, validator.loggedEvents) return {"feedType":validator.feedType, "loggedEvents":validator.loggedEvents}
# Get baseURI from content-location and/or redirect information if usock.headers.get('content-location', None): from urlparse import urljoin baseURI=urljoin(baseURI,usock.headers.get('content-location', "")) elif usock.headers.get('location', None): from urlparse import urljoin baseURI=urljoin(baseURI,usock.headers.get('location', "")) if not baseURI in selfURIs: selfURIs.append(baseURI) usock.close() usock = None mediaTypes.contentSniffing(mediaType, rawdata, loggedEvents) encoding, rawdata = xmlEncoding.decode(mediaType, charset, rawdata, loggedEvents, fallback='utf-8') if rawdata is None: return {'loggedEvents': loggedEvents} rawdata = rawdata.replace('\r\n', '\n').replace('\r', '\n') # normalize EOL validator = _validate(rawdata, firstOccurrenceOnly, loggedEvents, baseURI, encoding, selfURIs) # Warn about mismatches between media type and feed version if mediaType and validator.feedType: mediaTypes.checkAgainstFeedType(mediaType, validator.feedType, validator.loggedEvents) params = {"feedType":validator.feedType, "loggedEvents":validator.loggedEvents} if wantRawData: params['rawdata'] = rawdata return params