File: samplesvalidator.py

package info (click to toggle)
ckeditor 4.22.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 290,692 kB
  • sloc: javascript: 237,176; sh: 202; makefile: 64; python: 37; php: 15; xml: 5
file content (78 lines) | stat: -rw-r--r-- 2,420 bytes parent folder | download
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
# Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
# For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license

# Validates HTML files in a directory with W3C validator.
# To use this script simply call:
# python samplesvalidator.py
#
# By default this script validates samples directory ( 'project/samples' ).
# To validate some other directory an environmental variable must be set:
# export CKSAMPLESPATH=/home/me/some/path/to/be/validated
#
# To change validation service url you can also set an environmental variable:
# export CKSAMPLESURL='http://my.validation.servi.ce'
#
# To revert any kind of variable type:
# unset VARIABLE

import urllib, urllib2
import json
import os
import re

pathEnvVar = 'CKSAMPLESPATH'
urlEnvVar = 'CKSAMPLESURL'
scriptPath = os.path.dirname( os.path.realpath( __file__ ) )

# Let's move to the desired directory.
# \-> Look for ENV variable or use default samples path.
if pathEnvVar in os.environ:
	path = os.environ[ pathEnvVar ]
else:
	path = os.path.abspath( os.path.join( scriptPath, '../../samples/' ) )

os.chdir( path )

# Let's determine validator url.
# \-> Look for ENV variable or use default url.
url = os.environ[ urlEnvVar ] if urlEnvVar in os.environ else 'http://validator.w3.org/check'

# Find all HTML files in path.
directoryFiles = os.listdir( '.' )
htmlRegex = re.compile( '.html$', re.IGNORECASE )
htmlFiles = filter( htmlRegex.search, directoryFiles )

# Iterate over HTML files.
for index, fileName in enumerate( htmlFiles ):
	# Determine the full path of the file.
	filePath = os.path.join( path, fileName )

	print '(%(index)s/%(total)s) Validating %(filePath)s...' % {
		'filePath': filePath,
		'total': len( htmlFiles ),
		'index': index + 1
	}

	# Open the file.
	fileHandler = open( filePath, 'r' )

	# Prepare POST request.
	postData = {
		'fragment': fileHandler.read(),
		'charset': 'utf-8',
		'output': 'json'
	}

	# Close file.
	fileHandler.close()

	# Do the request. Keep the response.
	data = urllib.urlencode( postData )
	request = urllib2.Request( url, data )
	response = json.loads( urllib2.urlopen( request ).read() )

	# Print validation messages.
	for message in response[ 'messages' ]:
		message[ 'type' ] = message[ 'type' ].upper()
		message[ 'message' ] = message[ 'message' ].title()
		print '\t* %(type)s (Last line: %(lastLine)s, Last column: %(lastColumn)s): %(message)s' % message