Skip to content

Instantly share code, notes, and snippets.

@paulodiovani
Forked from jtojnar/linter.md
Created April 17, 2014 22:14
Show Gist options
  • Save paulodiovani/11014567 to your computer and use it in GitHub Desktop.
Save paulodiovani/11014567 to your computer and use it in GitHub Desktop.
import re
from base_linter import BaseLinter, INPUT_METHOD_FILE
CONFIG = {
'language': 'XML',
'executable': 'xmllintschema',
'lint_args': ['-noout', '{filename}'],
'input_method': INPUT_METHOD_FILE
}
class Linter(BaseLinter):
def __init__(self, config):
super(Linter, self).__init__(config)
def parse_errors(self, view, errors, lines, errorUnderlines, violationUnderlines, warningUnderlines, errorMessages, violationMessages, warningMessages):
for line in errors.splitlines():
match = re.match(r'[^:]*\:(?P<line>\d+): (?P<error>.+)', line)
if match:
error, line = match.group('error'), match.group('line')
self.add_message(int(line), lines, error, errorMessages)
#!/usr/bin/env python
import os
import sys
import re
import subprocess
schema = None
schemaline = -1
i = 0
with open(sys.argv[-1], 'r') as f:
for line in f:
i += 1
match = re.match(r'.* xsi:noNamespaceSchemaLocation="([^"]+)".*', line)
if match:
schema = match.group(1)
schemaline = i
if schema is not None:
online = schema.find('http://') == 0 or schema.find('https://') == 0
if not (online or os.path.isabs(schema)):
schema = os.path.dirname(sys.argv[-1]) + '/' + schema
if not online and not os.path.isfile(schema):
exit('-:' + str(schemaline) + ': Schema file does not exist')
args = ['xmllint', '--schema', schema]
args.extend(sys.argv[1:])
else:
args = ['xmllint']
args.extend(sys.argv[1:])
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
result = process.communicate()[0]
if result.find('Failed to locate the main schema resource at') > -1:
exit('-:' + str(schemaline) + ': Schema file does not exist')
if result.find('WXS schema ' + schema + ' failed to compile') > -1:
exit('-:' + str(schemaline) + ': Error in parsing schema')
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment