Example #1
0
def cli():
   if len(sys.argv) < 2:
      print "Give the name of a feed to generate"
      sys.exit()

   # hack to support old-style from cmd line
   if "&" not in sys.argv[1]:
      sys.argv[1] = "feed=%s" % sys.argv[1]

   os.environ['SERVER_NAME'] = "cli/"
   os.environ['REQUEST_URI'] = sys.argv[1]
   os.environ['QUERY_STRING'] = sys.argv[1]

   args = cgi.parse()

   feed = feed_cache(args, flush=True)

   if not lxml:
      print feed
   else:
      feed = etree.tostring(etree.fromstring(feed), pretty_print = True, encoding="UTF-8", xml_declaration=True)
      print feed

   try:
      import feedvalidator
      from feedvalidator.formatter.text_plain import Formatter
      from feedvalidator import compatibility
      from feedvalidator.logging import ValidValue
      events = (x for x in feedvalidator.validateString(feed, firstOccurrenceOnly=True, base=self_url())['loggedEvents'] if not isinstance(x, ValidValue))
      print "\n".join(Formatter(events)).encode("UTF-8")
   except ImportError:
      pass
Example #2
0
 def _isValidFeed(self, s):
   events = feedvalidator.validateString(s)['loggedEvents']
   fil = "A"
   filterFunc = getattr(compatibility, fil)
   events = filterFunc(events)
   output = Formatter(events)
   if output:
     print "\n".join(output)
   self.assertEqual(len(output), 0)
Example #3
0
 def _isValidFeed(self, s):
     events = feedvalidator.validateString(s)['loggedEvents']
     fil = "A"
     filterFunc = getattr(compatibility, fil)
     events = filterFunc(events)
     output = Formatter(events)
     if output:
         print "\n".join(output)
     self.assertEqual(len(output), 0)
Example #4
0
def buildTestCase(xmlfile, xmlBase, description, method, exc, params):
  """factory to create functions which validate `xmlfile`

  the returned function asserts that validating `xmlfile` (an XML file)
  will return a list of exceptions that include an instance of
  `exc` (an Exception class)
  """
  func = lambda self, xmlfile=xmlfile, exc=exc, params=params: \
       method(self, exc, params, feedvalidator.validateString(open(xmlfile).read(), fallback='US-ASCII', base=xmlBase)['loggedEvents'])
  func.__doc__ = description
  return func
def buildTestCase(xmlfile, xmlBase, description, method, exc, params):
    """factory to create functions which validate `xmlfile`

  the returned function asserts that validating `xmlfile` (an XML file)
  will return a list of exceptions that include an instance of
  `exc` (an Exception class)
  """
    func = lambda self, xmlfile=xmlfile, exc=exc, params=params: \
         method(self, exc, params, feedvalidator.validateString(open(xmlfile).read(), fallback='US-ASCII', base=xmlBase)['loggedEvents'])
    func.__doc__ = description
    return func
Example #6
0
def validate(string):
    """
    Read feed from string and raise ValidationError if feedvalidator founds
    any errors.
    """

    check_xml_wellformedness(string)

    for event in validateString(string)['loggedEvents']:
        if isinstance(event, Error):
            raise ValidationError(event)
Example #7
0
def test_feedvalidator():
    xml = prettify_xml(dummy_feed_xml())
    result = feedvalidator.validateString(xml)

    ok_messages = (feedvalidator.ValidValue, feedvalidator.MissingSelf)

    assert result['feedType'] == feedvalidator.TYPE_ATOM

    for message in result['loggedEvents']:
        if not isinstance(message, ok_messages):
            print(xml)
            print(message.params)
        assert isinstance(message, ok_messages), message
Example #8
0
def test_feedvalidator():
    xml = prettify_xml(dummy_feed_xml())
    result = feedvalidator.validateString(xml)

    ok_messages = (feedvalidator.ValidValue, feedvalidator.MissingSelf)

    assert result['feedType'] == feedvalidator.TYPE_ATOM

    for message in result['loggedEvents']:
        if not isinstance(message, ok_messages):
            print(xml)
            print(message.params)
        assert isinstance(message, ok_messages), message
Example #9
0
def kml_errors(kmlstring):
    import feedvalidator
    from feedvalidator import compatibility
    events = feedvalidator.validateString(
        kmlstring, firstOccurrenceOnly=1)['loggedEvents']

    # Three levels of compatibility
    # "A" is most basic level
    # "AA" mimics online validator
    # "AAA" is experimental; these rules WILL change or disappear in future versions
    filterFunc = getattr(compatibility, "AA")
    events = filterFunc(events)

    # there are a few annoyances with feedvalidator; specifically it doesn't recognize
    # KML ExtendedData element
    # or our custom 'mm' namespance
    # or our custom atom link relation
    # or space-delimited Icon states
    # so we ignore all related events
    events = [
        x for x in events if not (
            (isinstance(x, feedvalidator.logging.UndefinedElement)
             and x.params['element'] == u'ExtendedData') or
            (isinstance(x, feedvalidator.logging.UnregisteredAtomLinkRel)
             and x.params['value'] == u'marinemap.update_form') or
            (isinstance(x, feedvalidator.logging.UnregisteredAtomLinkRel)
             and x.params['value'] == u'marinemap.create_form') or
            (isinstance(x, feedvalidator.logging.UnknownNamespace) and
             x.params['namespace'] == u'http://marinemap.org') or
            (isinstance(x, feedvalidator.logging.UnknownNamespace) and
             x.params['namespace'] == u'http://www.google.com/kml/ext/2.2') or
            (isinstance(x, feedvalidator.logging.InvalidItemIconState) and
             x.params['element'] == u'state' and ' ' in x.params['value']) or
            (isinstance(x, feedvalidator.logging.UnregisteredAtomLinkRel)
             and x.params['element'] == u'atom:link'
             and 'workspace' in x.params['value']))
    ]

    from feedvalidator.formatter.text_plain import Formatter
    output = Formatter(events)

    if output:
        errors = []
        for i in range(len(output)):
            errors.append(
                (events[i], events[i].params, output[i],
                 kmlstring.splitlines()[events[i].params['backupline']]))
        return errors
    else:
        return None
Example #10
0
def kml_errors(kmlstring):
    import feedvalidator
    from feedvalidator import compatibility
    events = feedvalidator.validateString(kmlstring, firstOccurrenceOnly=1)['loggedEvents'] 

    # Three levels of compatibility
    # "A" is most basic level
    # "AA" mimics online validator
    # "AAA" is experimental; these rules WILL change or disappear in future versions
    filterFunc = getattr(compatibility, "AA")
    events = filterFunc(events)

    # there are a few annoyances with feedvalidator; specifically it doesn't recognize 
    # KML ExtendedData element 
    # or our custom 'mm' namespance 
    # or our custom atom link relation
    # or space-delimited Icon states
    # so we ignore all related events
    events = [x for x in events if not (
                (isinstance(x,feedvalidator.logging.UndefinedElement) 
                    and x.params['element']==u'ExtendedData') or
                (isinstance(x,feedvalidator.logging.UnregisteredAtomLinkRel) 
                    and x.params['value']==u'marinemap.update_form') or
                (isinstance(x,feedvalidator.logging.UnregisteredAtomLinkRel) 
                    and x.params['value']==u'marinemap.create_form') or
                (isinstance(x,feedvalidator.logging.UnknownNamespace) 
                    and x.params['namespace']==u'http://marinemap.org') or
                (isinstance(x,feedvalidator.logging.UnknownNamespace) 
                    and x.params['namespace']==u'http://www.google.com/kml/ext/2.2') or
                (isinstance(x,feedvalidator.logging.InvalidItemIconState) 
                    and x.params['element']==u'state' and ' ' in x.params['value']) or
                (isinstance(x,feedvalidator.logging.UnregisteredAtomLinkRel) 
                    and x.params['element']==u'atom:link' and 'workspace' in x.params['value'])
                )]

    from feedvalidator.formatter.text_plain import Formatter
    output = Formatter(events)

    if output:
        errors = []
        for i in range(len(output)):
            errors.append( (events[i],events[i].params,output[i],kmlstring.splitlines()[events[i].params['backupline']]) )
        return errors
    else:
        return None
Example #11
0
 def func(self, xmlfile=xmlfile, exc=exc, params=params):
     with open(xmlfile, 'rb') as stream:
         xmldoc = stream.read()
         loggedEvents = feedvalidator.validateString(
             xmldoc, fallback='US-ASCII', base=xmlBase)['loggedEvents']
         method(self, exc, params, loggedEvents)