Code

Ticket #5703: rnv.diff

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

Patch to enable RNV validator

Line 
1Index: django/conf/global_settings.py
2===================================================================
3--- django/conf/global_settings.py      (revision 6465)
4+++ django/conf/global_settings.py      (working copy)
5@@ -209,9 +209,14 @@
6 # loudly.
7 SECRET_KEY = ''
8 
9-# Path to the "jing" executable -- needed to validate XMLFields
10+# Either JING or RNV exectable is needed to validate XMLFields (JING is preferred)
11+
12+# Path to the "jing" executable
13 JING_PATH = "/usr/bin/jing"
14 
15+# Path to the "rnv" executable
16+RNV_PATH = "/usr/bin/rnv"
17+
18 # Absolute path to the directory that holds media.
19 # Example: "/home/media/media.lawrence.com/"
20 MEDIA_ROOT = ''
21Index: django/core/validators.py
22===================================================================
23--- django/core/validators.py   (revision 6465)
24+++ django/core/validators.py   (working copy)
25@@ -556,47 +556,60 @@
26         fp = open(filename, 'w')
27         fp.write(field_data)
28         fp.close()
29-        if not os.path.exists(settings.JING_PATH):
30-            raise Exception, "%s not found!" % settings.JING_PATH
31-        p = os.popen('%s -c %s %s' % (settings.JING_PATH, self.schema_path, filename))
32-        errors = [line.strip() for line in p.readlines()]
33-        p.close()
34-        os.unlink(filename)
35-        display_errors = []
36-        lines = field_data.split('\n')
37-        for error in errors:
38-            ignored, line, level, message = error.split(':', 3)
39-            # Scrape the Jing error messages to reword them more nicely.
40-            m = re.search(r'Expected "(.*?)" to terminate element starting on line (\d+)', message)
41-            if m:
42-                display_errors.append(_('Please close the unclosed %(tag)s tag from line %(line)s. (Line starts with "%(start)s".)') % \
43-                    {'tag':m.group(1).replace('/', ''), 'line':m.group(2), 'start':lines[int(m.group(2)) - 1][:30]})
44-                continue
45-            if message.strip() == 'text not allowed here':
46-                display_errors.append(_('Some text starting on line %(line)s is not allowed in that context. (Line starts with "%(start)s".)') % \
47-                    {'line':line, 'start':lines[int(line) - 1][:30]})
48-                continue
49-            m = re.search(r'\s*attribute "(.*?)" not allowed at this point; ignored', message)
50-            if m:
51-                display_errors.append(_('"%(attr)s" on line %(line)s is an invalid attribute. (Line starts with "%(start)s".)') % \
52-                    {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
53-                continue
54-            m = re.search(r'\s*unknown element "(.*?)"', message)
55-            if m:
56-                display_errors.append(_('"<%(tag)s>" on line %(line)s is an invalid tag. (Line starts with "%(start)s".)') % \
57-                    {'tag':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
58-                continue
59-            if message.strip() == 'required attributes missing':
60-                display_errors.append(_('A tag on line %(line)s is missing one or more required attributes. (Line starts with "%(start)s".)') % \
61-                    {'line':line, 'start':lines[int(line) - 1][:30]})
62-                continue
63-            m = re.search(r'\s*bad value for attribute "(.*?)"', message)
64-            if m:
65-                display_errors.append(_('The "%(attr)s" attribute on line %(line)s has an invalid value. (Line starts with "%(start)s".)') % \
66-                    {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
67-                continue
68-            # Failing all those checks, use the default error message.
69-            display_error = 'Line %s: %s [%s]' % (line, message, level.strip())
70-            display_errors.append(display_error)
71+        if os.path.exists(settings.JING_PATH):
72+            p = os.popen('%s -c %s %s' % (settings.JING_PATH, self.schema_path, filename))
73+            errors = [line.strip() for line in p.readlines()]
74+            p.close()
75+            os.unlink(filename)
76+            display_errors = []
77+            lines = field_data.split('\n')
78+            for error in errors:
79+                ignored, line, level, message = error.split(':', 3)
80+                # Scrape the Jing error messages to reword them more nicely.
81+                m = re.search(r'Expected "(.*?)" to terminate element starting on line (\d+)', message)
82+                if m:
83+                    display_errors.append(_('Please close the unclosed %(tag)s tag from line %(line)s. (Line starts with "%(start)s".)') % \
84+                        {'tag':m.group(1).replace('/', ''), 'line':m.group(2), 'start':lines[int(m.group(2)) - 1][:30]})
85+                    continue
86+                if message.strip() == 'text not allowed here':
87+                    display_errors.append(_('Some text starting on line %(line)s is not allowed in that context. (Line starts with "%(start)s".)') % \
88+                        {'line':line, 'start':lines[int(line) - 1][:30]})
89+                    continue
90+                m = re.search(r'\s*attribute "(.*?)" not allowed at this point; ignored', message)
91+                if m:
92+                    display_errors.append(_('"%(attr)s" on line %(line)s is an invalid attribute. (Line starts with "%(start)s".)') % \
93+                        {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
94+                    continue
95+                m = re.search(r'\s*unknown element "(.*?)"', message)
96+                if m:
97+                    display_errors.append(_('"<%(tag)s>" on line %(line)s is an invalid tag. (Line starts with "%(start)s".)') % \
98+                        {'tag':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
99+                    continue
100+                if message.strip() == 'required attributes missing':
101+                    display_errors.append(_('A tag on line %(line)s is missing one or more required attributes. (Line starts with "%(start)s".)') % \
102+                        {'line':line, 'start':lines[int(line) - 1][:30]})
103+                    continue
104+                m = re.search(r'\s*bad value for attribute "(.*?)"', message)
105+                if m:
106+                    display_errors.append(_('The "%(attr)s" attribute on line %(line)s has an invalid value. (Line starts with "%(start)s".)') % \
107+                        {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
108+                    continue
109+                # Failing all those checks, use the default error message.
110+                display_error = 'Line %s: %s [%s]' % (line, message, level.strip())
111+                display_errors.append(display_error)
112+        elif os.path.exists(settings.RNV_PATH):
113+            p = os.popen('%s -q %s %s 2>&1' % (settings.RNV_PATH, self.schema_path, filename))
114+            errors = [line.strip() for line in p.readlines()]
115+            p.close()
116+            os.unlink(filename)
117+            display_errors = []
118+            lines = field_data.split('\n')
119+            for error in errors:
120+                fname, line, col, message = error.split(':', 3)
121+                start = lines[int(line) - 1][:max(int(col) + 10, 30)]
122+                display_error = 'Line %s:%s - %s (Line starts with "%s")' % (line, col, message, start)
123+                display_errors.append(display_error)
124+        else:
125+            raise Exception, "Neither %s nor %s found!" % (settings.JING_PATH, settings.RNV_PATH)
126         if len(display_errors) > 0:
127             raise ValidationError, display_errors