Ticket #5703: rnv.diff

File rnv.diff, 7.4 KB (added by Jindrich Makovicka <makovick@…>, 8 years ago)

Patch to enable RNV validator

  • django/conf/global_settings.py

     
    209209# loudly.
    210210SECRET_KEY = ''
    211211
    212 # Path to the "jing" executable -- needed to validate XMLFields
     212# Either JING or RNV exectable is needed to validate XMLFields (JING is preferred)
     213
     214# Path to the "jing" executable
    213215JING_PATH = "/usr/bin/jing"
    214216
     217# Path to the "rnv" executable
     218RNV_PATH = "/usr/bin/rnv"
     219
    215220# Absolute path to the directory that holds media.
    216221# Example: "/home/media/media.lawrence.com/"
    217222MEDIA_ROOT = ''
  • django/core/validators.py

     
    556556        fp = open(filename, 'w')
    557557        fp.write(field_data)
    558558        fp.close()
    559         if not os.path.exists(settings.JING_PATH):
    560             raise Exception, "%s not found!" % settings.JING_PATH
    561         p = os.popen('%s -c %s %s' % (settings.JING_PATH, self.schema_path, filename))
    562         errors = [line.strip() for line in p.readlines()]
    563         p.close()
    564         os.unlink(filename)
    565         display_errors = []
    566         lines = field_data.split('\n')
    567         for error in errors:
    568             ignored, line, level, message = error.split(':', 3)
    569             # Scrape the Jing error messages to reword them more nicely.
    570             m = re.search(r'Expected "(.*?)" to terminate element starting on line (\d+)', message)
    571             if m:
    572                 display_errors.append(_('Please close the unclosed %(tag)s tag from line %(line)s. (Line starts with "%(start)s".)') % \
    573                     {'tag':m.group(1).replace('/', ''), 'line':m.group(2), 'start':lines[int(m.group(2)) - 1][:30]})
    574                 continue
    575             if message.strip() == 'text not allowed here':
    576                 display_errors.append(_('Some text starting on line %(line)s is not allowed in that context. (Line starts with "%(start)s".)') % \
    577                     {'line':line, 'start':lines[int(line) - 1][:30]})
    578                 continue
    579             m = re.search(r'\s*attribute "(.*?)" not allowed at this point; ignored', message)
    580             if m:
    581                 display_errors.append(_('"%(attr)s" on line %(line)s is an invalid attribute. (Line starts with "%(start)s".)') % \
    582                     {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
    583                 continue
    584             m = re.search(r'\s*unknown element "(.*?)"', message)
    585             if m:
    586                 display_errors.append(_('"<%(tag)s>" on line %(line)s is an invalid tag. (Line starts with "%(start)s".)') % \
    587                     {'tag':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
    588                 continue
    589             if message.strip() == 'required attributes missing':
    590                 display_errors.append(_('A tag on line %(line)s is missing one or more required attributes. (Line starts with "%(start)s".)') % \
    591                     {'line':line, 'start':lines[int(line) - 1][:30]})
    592                 continue
    593             m = re.search(r'\s*bad value for attribute "(.*?)"', message)
    594             if m:
    595                 display_errors.append(_('The "%(attr)s" attribute on line %(line)s has an invalid value. (Line starts with "%(start)s".)') % \
    596                     {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
    597                 continue
    598             # Failing all those checks, use the default error message.
    599             display_error = 'Line %s: %s [%s]' % (line, message, level.strip())
    600             display_errors.append(display_error)
     559        if os.path.exists(settings.JING_PATH):
     560            p = os.popen('%s -c %s %s' % (settings.JING_PATH, self.schema_path, filename))
     561            errors = [line.strip() for line in p.readlines()]
     562            p.close()
     563            os.unlink(filename)
     564            display_errors = []
     565            lines = field_data.split('\n')
     566            for error in errors:
     567                ignored, line, level, message = error.split(':', 3)
     568                # Scrape the Jing error messages to reword them more nicely.
     569                m = re.search(r'Expected "(.*?)" to terminate element starting on line (\d+)', message)
     570                if m:
     571                    display_errors.append(_('Please close the unclosed %(tag)s tag from line %(line)s. (Line starts with "%(start)s".)') % \
     572                        {'tag':m.group(1).replace('/', ''), 'line':m.group(2), 'start':lines[int(m.group(2)) - 1][:30]})
     573                    continue
     574                if message.strip() == 'text not allowed here':
     575                    display_errors.append(_('Some text starting on line %(line)s is not allowed in that context. (Line starts with "%(start)s".)') % \
     576                        {'line':line, 'start':lines[int(line) - 1][:30]})
     577                    continue
     578                m = re.search(r'\s*attribute "(.*?)" not allowed at this point; ignored', message)
     579                if m:
     580                    display_errors.append(_('"%(attr)s" on line %(line)s is an invalid attribute. (Line starts with "%(start)s".)') % \
     581                        {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
     582                    continue
     583                m = re.search(r'\s*unknown element "(.*?)"', message)
     584                if m:
     585                    display_errors.append(_('"<%(tag)s>" on line %(line)s is an invalid tag. (Line starts with "%(start)s".)') % \
     586                        {'tag':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
     587                    continue
     588                if message.strip() == 'required attributes missing':
     589                    display_errors.append(_('A tag on line %(line)s is missing one or more required attributes. (Line starts with "%(start)s".)') % \
     590                        {'line':line, 'start':lines[int(line) - 1][:30]})
     591                    continue
     592                m = re.search(r'\s*bad value for attribute "(.*?)"', message)
     593                if m:
     594                    display_errors.append(_('The "%(attr)s" attribute on line %(line)s has an invalid value. (Line starts with "%(start)s".)') % \
     595                        {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
     596                    continue
     597                # Failing all those checks, use the default error message.
     598                display_error = 'Line %s: %s [%s]' % (line, message, level.strip())
     599                display_errors.append(display_error)
     600        elif os.path.exists(settings.RNV_PATH):
     601            p = os.popen('%s -q %s %s 2>&1' % (settings.RNV_PATH, self.schema_path, filename))
     602            errors = [line.strip() for line in p.readlines()]
     603            p.close()
     604            os.unlink(filename)
     605            display_errors = []
     606            lines = field_data.split('\n')
     607            for error in errors:
     608                fname, line, col, message = error.split(':', 3)
     609                start = lines[int(line) - 1][:max(int(col) + 10, 30)]
     610                display_error = 'Line %s:%s - %s (Line starts with "%s")' % (line, col, message, start)
     611                display_errors.append(display_error)
     612        else:
     613            raise Exception, "Neither %s nor %s found!" % (settings.JING_PATH, settings.RNV_PATH)
    601614        if len(display_errors) > 0:
    602615            raise ValidationError, display_errors
Back to Top