From 15de38c8d489c53578d62bb999e1e1659c0877b4 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sun, 28 Feb 2021 22:27:31 -0500 Subject: [PATCH 1/3] Move w3c_validator to use it with just w3c_validator --- install.sh | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 install.sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..03c9404 --- /dev/null +++ b/install.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +# Installation move file tu /usr/local/bin in order to use it with - +# simple syntax, example --> w3c_validator file +mv w3c_validator /usr/local/bin From 79b25cd6fe4ee85a716f94868fd5973205b1acda Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sun, 28 Feb 2021 22:28:52 -0500 Subject: [PATCH 2/3] Changed extension to use it easier --- w3c_validator | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100755 w3c_validator diff --git a/w3c_validator b/w3c_validator new file mode 100755 index 0000000..ec7ec71 --- /dev/null +++ b/w3c_validator @@ -0,0 +1,131 @@ +#!/usr/bin/python3 +""" +W3C validator for Holberton School + +For HTML and CSS files. + +Based on 1 API: +- https://validator.w3.org/docs/api.html + +Usage: + +Simple file: + +``` +./w3c_validator.py index.html +``` + +Multiple files: + +``` +./w3c_validator.py index.html header.html styles/common.css +``` + +All errors are printed in `STDERR` + +Return: +Exit status is the # of errors, 0 on Success +""" +import sys +import requests +import os + + +def __print_stdout(msg): + """Print message in STDOUT + """ + sys.stdout.buffer.write(msg.encode('utf-8')) + + +def __print_stderr(msg): + """Print message in STDERR + """ + sys.stderr.buffer.write(msg.encode('utf-8')) + + +def __is_empty(file): + if os.path.getsize(file) == 0: + raise OSError("File '{}' is empty.".format(file)) + + +def __validate(file_path, type): + """ + Start validation of files + """ + h = {'Content-Type': "{}; charset=utf-8".format(type)} + # Open files in binary mode: + # https://requests.readthedocs.io/en/master/user/advanced/ + d = open(file_path, "rb").read() + u = "https://validator.w3.org/nu/?out=json" + r = requests.post(u, headers=h, data=d) + + if not r.status_code < 400: + raise ConnectionError("Unable to connect to API endpoint.") + + res = [] + messages = r.json().get('messages', []) + for m in messages: + # Capture files that have incomplete or broken HTML + if m['type'] == 'error' or m['type'] == 'info': + res.append("'{}' => {}".format(file_path, m['message'])) + else: + res.append("[{}:{}] {}".format( + file_path, m['lastLine'], m['message'])) + return res + + +def __analyse(file_path): + """Start analyse of a file and print the result + """ + nb_errors = 0 + try: + result = None + + if file_path.endswith(".css"): + __is_empty(file_path) + result = __validate(file_path, "text/css") + elif file_path.endswith((".html", ".htm")): + __is_empty(file_path) + result = __validate(file_path, "text/html") + elif file_path.endswith(".svg"): + __is_empty(file_path) + result = __validate(file_path, "image/svg+xml") + else: + allowed_files = "'.css', '.html', '.htm' and '.svg'" + raise OSError( + "File {} does not have a valid file extension.\nOnly {} are " + "allowed.".format(file_path, allowed_files) + ) + + if len(result) > 0: + for msg in result: + __print_stderr("{}\n".format(msg)) + nb_errors += 1 + else: + __print_stdout("'{}' => OK\n".format(file_path)) + + except Exception as e: + __print_stderr("'{}' => {}\n".format(e.__class__.__name__, e)) + return nb_errors + + +def __files_loop(): + """Loop that analyses for each file from input arguments + """ + nb_errors = 0 + for file_path in sys.argv[1:]: + nb_errors += __analyse(file_path) + + return nb_errors + + +if __name__ == "__main__": + """Main + """ + if len(sys.argv) < 2: + __print_stderr("usage: w3c_validator.py file1 file2 ...\n") + exit(1) + + """execute tests, then exit. Exit status = # of errors (0 on success) + """ + sys.exit(__files_loop()) From ffa55f1310cb54c1f2307316677b4886269ca3fd Mon Sep 17 00:00:00 2001 From: Nicolas Date: Sun, 28 Feb 2021 22:31:48 -0500 Subject: [PATCH 3/3] This was no needed --- w3c_validator | 131 -------------------------------------------------- 1 file changed, 131 deletions(-) delete mode 100755 w3c_validator diff --git a/w3c_validator b/w3c_validator deleted file mode 100755 index ec7ec71..0000000 --- a/w3c_validator +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/python3 -""" -W3C validator for Holberton School - -For HTML and CSS files. - -Based on 1 API: -- https://validator.w3.org/docs/api.html - -Usage: - -Simple file: - -``` -./w3c_validator.py index.html -``` - -Multiple files: - -``` -./w3c_validator.py index.html header.html styles/common.css -``` - -All errors are printed in `STDERR` - -Return: -Exit status is the # of errors, 0 on Success -""" -import sys -import requests -import os - - -def __print_stdout(msg): - """Print message in STDOUT - """ - sys.stdout.buffer.write(msg.encode('utf-8')) - - -def __print_stderr(msg): - """Print message in STDERR - """ - sys.stderr.buffer.write(msg.encode('utf-8')) - - -def __is_empty(file): - if os.path.getsize(file) == 0: - raise OSError("File '{}' is empty.".format(file)) - - -def __validate(file_path, type): - """ - Start validation of files - """ - h = {'Content-Type': "{}; charset=utf-8".format(type)} - # Open files in binary mode: - # https://requests.readthedocs.io/en/master/user/advanced/ - d = open(file_path, "rb").read() - u = "https://validator.w3.org/nu/?out=json" - r = requests.post(u, headers=h, data=d) - - if not r.status_code < 400: - raise ConnectionError("Unable to connect to API endpoint.") - - res = [] - messages = r.json().get('messages', []) - for m in messages: - # Capture files that have incomplete or broken HTML - if m['type'] == 'error' or m['type'] == 'info': - res.append("'{}' => {}".format(file_path, m['message'])) - else: - res.append("[{}:{}] {}".format( - file_path, m['lastLine'], m['message'])) - return res - - -def __analyse(file_path): - """Start analyse of a file and print the result - """ - nb_errors = 0 - try: - result = None - - if file_path.endswith(".css"): - __is_empty(file_path) - result = __validate(file_path, "text/css") - elif file_path.endswith((".html", ".htm")): - __is_empty(file_path) - result = __validate(file_path, "text/html") - elif file_path.endswith(".svg"): - __is_empty(file_path) - result = __validate(file_path, "image/svg+xml") - else: - allowed_files = "'.css', '.html', '.htm' and '.svg'" - raise OSError( - "File {} does not have a valid file extension.\nOnly {} are " - "allowed.".format(file_path, allowed_files) - ) - - if len(result) > 0: - for msg in result: - __print_stderr("{}\n".format(msg)) - nb_errors += 1 - else: - __print_stdout("'{}' => OK\n".format(file_path)) - - except Exception as e: - __print_stderr("'{}' => {}\n".format(e.__class__.__name__, e)) - return nb_errors - - -def __files_loop(): - """Loop that analyses for each file from input arguments - """ - nb_errors = 0 - for file_path in sys.argv[1:]: - nb_errors += __analyse(file_path) - - return nb_errors - - -if __name__ == "__main__": - """Main - """ - if len(sys.argv) < 2: - __print_stderr("usage: w3c_validator.py file1 file2 ...\n") - exit(1) - - """execute tests, then exit. Exit status = # of errors (0 on success) - """ - sys.exit(__files_loop())