Exemple #1
0
 def __call__(self, field_data, all_data):
     import os, tempfile
     if self.additional_root_element:
         field_data = '<%(are)s>%(data)s\n</%(are)s>' % {
             'are': self.additional_root_element,
             'data': field_data
         }
     filename = tempfile.mktemp() # Insecure, but nothing else worked
     fp = open(filename, 'w')
     fp.write(field_data)
     fp.close()
     if not os.path.exists(settings.JING_PATH):
         raise Exception("%s not found!" % settings.JING_PATH)
     p = os.popen('%s -c %s %s' % (settings.JING_PATH, self.schema_path, filename))
     errors = [line.strip() for line in p.readlines()]
     p.close()
     os.unlink(filename)
     display_errors = []
     lines = field_data.split('\n')
     for error in errors:
         ignored, line, level, message = error.split(':', 3)
         # Scrape the Jing error messages to reword them more nicely.
         m = re.search(r'Expected "(.*?)" to terminate element starting on line (\d+)', message)
         if m:
             display_errors.append(_('Please close the unclosed %(tag)s tag from line %(line)s. (Line starts with "%(start)s".)') % \
                 {'tag':m.group(1).replace('/', ''), 'line':m.group(2), 'start':lines[int(m.group(2)) - 1][:30]})
             continue
         if message.strip() == 'text not allowed here':
             display_errors.append(_('Some text starting on line %(line)s is not allowed in that context. (Line starts with "%(start)s".)') % \
                 {'line':line, 'start':lines[int(line) - 1][:30]})
             continue
         m = re.search(r'\s*attribute "(.*?)" not allowed at this point; ignored', message)
         if m:
             display_errors.append(_('"%(attr)s" on line %(line)s is an invalid attribute. (Line starts with "%(start)s".)') % \
                 {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
             continue
         m = re.search(r'\s*unknown element "(.*?)"', message)
         if m:
             display_errors.append(_('"<%(tag)s>" on line %(line)s is an invalid tag. (Line starts with "%(start)s".)') % \
                 {'tag':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
             continue
         if message.strip() == 'required attributes missing':
             display_errors.append(_('A tag on line %(line)s is missing one or more required attributes. (Line starts with "%(start)s".)') % \
                 {'line':line, 'start':lines[int(line) - 1][:30]})
             continue
         m = re.search(r'\s*bad value for attribute "(.*?)"', message)
         if m:
             display_errors.append(_('The "%(attr)s" attribute on line %(line)s has an invalid value. (Line starts with "%(start)s".)') % \
                 {'attr':m.group(1), 'line':line, 'start':lines[int(line) - 1][:30]})
             continue
         # Failing all those checks, use the default error message.
         display_error = 'Line %s: %s [%s]' % (line, message, level.strip())
         display_errors.append(display_error)
     if len(display_errors) > 0:
         raise ValidationError(display_errors)
 def in_file(filename, content, encoding=None):
     encoding = Check.get('encoding', encoding)
     with open(filename, 'w', encoding=encoding) as f:
         for line in content:
             print(line, file=f)
     old_feedback = Check.current_part['feedback'][:]
     yield
     new_feedback = Check.current_part['feedback'][len(old_feedback):]
     Check.current_part['feedback'] = old_feedback
     if new_feedback:
         new_feedback = [
             '\n    '.join(error.split('\n')) for error in new_feedback
         ]
         Check.error(
             'Pri vhodni datoteki {0} z vsebino\n  {1}\nso se pojavile naslednje napake:\n- {2}',
             filename, '\n  '.join(content), '\n- '.join(new_feedback))
Exemple #3
0
 def input(content, encoding=None):
     old_stdin = sys.stdin
     old_feedback = Check.current_part['feedback'][:]
     sys.stdin = io.StringIO('\n'.join(content))
     try:
         yield
     finally:
         sys.stdin = old_stdin
     new_feedback = Check.current_part['feedback'][len(old_feedback):]
     Check.current_part['feedback'] = old_feedback
     if new_feedback:
         new_feedback = [
             '\n  '.join(error.split('\n')) for error in new_feedback
         ]
         Check.error(
             'Pri vhodu\n  {0}\nso se pojavile naslednje napake:\n- {1}',
             '\n  '.join(content), '\n- '.join(new_feedback))
Exemple #4
0
 def input(content, visible=None):
     old_stdin = sys.stdin
     old_feedback = Check.current_part['feedback'][:]
     try:
         with Check.set_stringio(visible):
             sys.stdin = Check.get('stringio')('\n'.join(content) + '\n')
             yield
     finally:
         sys.stdin = old_stdin
     new_feedback = Check.current_part['feedback'][len(old_feedback):]
     Check.current_part['feedback'] = old_feedback
     if new_feedback:
         new_feedback = [
             '\n  '.join(error.split('\n')) for error in new_feedback
         ]
         Check.error(
             'Pri vhodu\n  {0}\nso se pojavile naslednje napake:\n- {1}',
             '\n  '.join(content), '\n- '.join(new_feedback))
Exemple #5
0
	def __call__(self, field_data, all_data):
		import os, tempfile
		if self.additional_root_element:
			field_data = '<%(are)s>%(data)s\n</%(are)s>' % {
				'are': self.additional_root_element,
				'data': field_data
			}
		filename = tempfile.mktemp()  # Insecure, but nothing else worked
		fp = open(filename, 'w')
		fp.write(field_data)
		fp.close()
		if not os.path.exists(settings.JING_PATH):
			raise Exception("%s not found!" % settings.JING_PATH)
		p = os.popen('%s -c %s %s' % (settings.JING_PATH, self.schema_path, filename))
		errors = [line.strip() for line in p.readlines()]
		p.close()
		os.unlink(filename)
		display_errors = []
		lines = field_data.split('\n')
		for error in errors:
			ignored, line, level, message = error.split(':', 3)
			# Scrape the Jing error messages to reword them more nicely.
			m = re.search(r'Expected "(.*?)" to terminate element starting on line (\d+)', message)
			if m:
				display_errors.append(
					_('Please close the unclosed %(tag)s tag from line %(line)s. (Line starts with "%(start)s".)') % \
					{'tag': m.group(1).replace('/', ''), 'line': m.group(2), 'start': lines[int(m.group(2)) - 1][:30]})
				continue
			if message.strip() == 'text not allowed here':
				display_errors.append(
					_('Some text starting on line %(line)s is not allowed in that context. (Line starts with "%(start)s".)') % \
					{'line': line, 'start': lines[int(line) - 1][:30]})
				continue
			m = re.search(r'\s*attribute "(.*?)" not allowed at this point; ignored', message)
			if m:
				display_errors.append(
					_('"%(attr)s" on line %(line)s is an invalid attribute. (Line starts with "%(start)s".)') % \
					{'attr': m.group(1), 'line': line, 'start': lines[int(line) - 1][:30]})
				continue
			m = re.search(r'\s*unknown element "(.*?)"', message)
			if m:
				display_errors.append(
					_('"<%(tag)s>" on line %(line)s is an invalid tag. (Line starts with "%(start)s".)') % \
					{'tag': m.group(1), 'line': line, 'start': lines[int(line) - 1][:30]})
				continue
			if message.strip() == 'required attributes missing':
				display_errors.append(
					_('A tag on line %(line)s is missing one or more required attributes. (Line starts with "%(start)s".)') % \
					{'line': line, 'start': lines[int(line) - 1][:30]})
				continue
			m = re.search(r'\s*bad value for attribute "(.*?)"', message)
			if m:
				display_errors.append(
					_('The "%(attr)s" attribute on line %(line)s has an invalid value. (Line starts with "%(start)s".)') % \
					{'attr': m.group(1), 'line': line, 'start': lines[int(line) - 1][:30]})
				continue
			# Failing all those checks, use the default error message.
			display_error = 'Line %s: %s [%s]' % (line, message, level.strip())
			display_errors.append(display_error)
		if len(display_errors) > 0:
			raise ValidationError(display_errors)