forked from capooti/w3c-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathw3c-validator.py
executable file
·156 lines (139 loc) · 5.2 KB
/
w3c-validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
'''
w3c-validator - Validate HTML and CSS files using the WC3 validators
Copyright: Stuart Rackham (c) 2011
License: MIT
Email: [email protected]
'''
'''
Fork from https://github.com/srackham/w3c-validator
Modified by pulse-mind
Email: [email protected]
Example : python w3c-validator.py --cssonly --file_url_or_directory=/mnt/d/projects/example/resources/css
'''
import os
import sys
import time
import json
import commands
import urllib
import getopt
html_validator_url = 'http://validator.w3.org/check'
css_validator_url = 'http://jigsaw.w3.org/css-validator/validator'
verbose_option = False
def message(msg):
print >> sys.stderr, msg
def verbose(msg):
if verbose_option:
message(msg)
def validate(filename):
'''
Validate file and return JSON result as dictionary.
'filename' can be a file name or an HTTP URL.
Return '' if the validator does not return valid JSON.
Raise OSError if curl command returns an error status.
'''
quoted_filename = urllib.quote(filename)
if filename.startswith('http://'):
# Submit URI with GET.
if filename.endswith('.css'):
cmd = ('curl -sG -d uri=%s -d output=json -d warning=0 %s'
% (quoted_filename, css_validator_url))
else:
cmd = ('curl -sG -d uri=%s -d output=json %s'
% (quoted_filename, html_validator_url))
else:
# Upload file as multipart/form-data with POST.
if filename.endswith('.css'):
cmd = ('curl -sF "file=@%s;type=text/css" -F output=json -F warning=0 %s'
% (quoted_filename, css_validator_url))
else:
cmd = ('curl -sF "uploaded_file=@%s;type=text/html" -F output=json %s'
% (quoted_filename, html_validator_url))
verbose(cmd)
status,output = commands.getstatusoutput(cmd)
if status != 0:
raise OSError (status, 'failed: %s' % cmd)
verbose(output)
try:
result = json.loads(output)
except ValueError:
result = ''
time.sleep(2) # Be nice and don't hog the free validator service.
return result
def usage(argv):
message('usage: %s [--verbose] [--cssonly] --file_url_or_directory=FILE|URL|PATH' % os.path.basename(argv))
exit(1)
if __name__ == '__main__':
verbose_option = False
css_only_option = False
file_or_directory = None
try:
opts, args = getopt.getopt(sys.argv[1:], "hcvf:", ["help", "cssonly", "verbose", "file_url_or_directory="])
except getopt.GetoptError:
usage(sys.argv[0])
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage(sys.argv[0])
sys.exit()
elif opt in ("-v", "--verbose"):
verbose_option = True
elif opt in ("-c", "--cssonly"):
css_only_option = True
elif opt in ("-f", "--file_url_or_directory"):
file_or_directory = arg
if file_or_directory is None :
usage(sys.argv[0])
sys.exit(2)
files = []
if os.path.isdir(file_or_directory):
# r=root, d=directories, f = files
for r, d, f in os.walk(file_or_directory):
for file in f:
if css_only_option and file.endswith('.css') or not css_only_option:
if file.endswith('.css') or file.endswith('.html') or file.endswith('.htm'):
files.append(os.path.join(r, file))
print("Found these files in %s: " % (file_or_directory))
for f in files:
print(f)
else:
files.append(file_or_directory)
errors = 0
warnings = 0
for f in files:
message('validating: %s ...' % f)
retrys = 0
while retrys < 2:
result = validate(f)
if result:
break
retrys += 1
message('| retrying: %s ...' % f)
else:
message('| failed: %s' % f)
errors += 1
continue
if f.endswith('.css'):
errorcount = result['cssvalidation']['result']['errorcount']
warningcount = result['cssvalidation']['result']['warningcount']
# print(str(result))
errors += errorcount
warnings += warningcount
if errorcount > 0:
message('| errors: %d' % errorcount)
for error in result['cssvalidation']['errors']:
print("|-- Ligne %s : %s" % (error['line'], error['message']))
print("| %s" % (error['context']))
if warningcount > 0:
message('| warnings: %d' % warningcount)
else:
for msg in result['messages']:
if 'lastLine' in msg:
message('%(type)s: line %(lastLine)d: %(message)s' % msg)
else:
message('%(type)s: %(message)s' % msg)
if msg['type'] == 'error':
errors += 1
else:
warnings += 1