Skip to content

Commit

Permalink
feat: Use xml2rfc (#28)
Browse files Browse the repository at this point in the history
This replaces dependency on rfctools-common with xml2rfc.
  • Loading branch information
kesara authored Jan 25, 2023
1 parent d87a26f commit ac1cc51
Show file tree
Hide file tree
Showing 16 changed files with 192 additions and 101 deletions.
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
lxml>=4.1.1
requests>=2.5.0
six
rfctools_common>=0.6.0
xml2rfc>=3.16.0
1 change: 0 additions & 1 deletion svgcheck/Results/colors.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Parsing file Tests/colors.svg
Tests/colors.svg:2: The attribute 'fill' does not allow the value 'red', replaced with 'black'
Tests/colors.svg:3: The attribute 'fill' does not allow the value 'rgb(0,0,0)', replaced with 'black'
Tests/colors.svg:4: The attribute 'fill' does not allow the value 'rgb(255,255,255)', replaced with 'white'
Expand Down
1 change: 0 additions & 1 deletion svgcheck/Results/full-tiny-02.err
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Parsing file Temp/full-tiny.xml
INFO: File conforms to SVG requirements.
1 change: 0 additions & 1 deletion svgcheck/Results/full-tiny.err
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Parsing file Tests/full-tiny.xml
http://example.org/label:2: The element 'svg' does not allow the attribute 'pointer-events', attribute to be removed.
http://example.org/label:2: The element 'svg' does not allow the attribute 'audio-level', attribute to be removed.
http://example.org/label:2: The element 'svg' does not allow the attribute 'focusHighlight', attribute to be removed.
Expand Down
1 change: 0 additions & 1 deletion svgcheck/Results/good.err
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Parsing file Tests/good.svg
INFO: File conforms to SVG requirements.
1 change: 0 additions & 1 deletion svgcheck/Results/rfc-01.err
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Parsing file Tests/rfc.xml
Tests/rfc.xml:24: The attribute 'fill' does not allow the value 'red', replaced with 'black'
ERROR: File does not conform to SVG requirements
1 change: 0 additions & 1 deletion svgcheck/Results/rfc-02.err
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
Parsing file Tests/rfc.xml
Tests/rfc.xml:24: The attribute 'fill' does not allow the value 'red', replaced with 'black'
ERROR: File does not conform to SVG requirements
1 change: 0 additions & 1 deletion svgcheck/Results/rfc-svg.err
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Parsing file Tests/rfc-svg.xml
INFO: File conforms to SVG requirements.
2 changes: 0 additions & 2 deletions svgcheck/Results/rfc.out

This file was deleted.

39 changes: 0 additions & 39 deletions svgcheck/Results/rfc.xml

This file was deleted.

1 change: 0 additions & 1 deletion svgcheck/Results/utf8-2.err
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Parsing file Temp/utf8-1.svg
INFO: File conforms to SVG requirements.
4 changes: 0 additions & 4 deletions svgcheck/Results/utf8.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
Parsing file Tests/utf8.svg
Tests/utf8.svg:3: The element 'svg' does not allow the attribute 'zoomAndPan', attribute to be removed.
Tests/utf8.svg:3: The element 'svg' does not allow the attribute 'contentScriptType', attribute to be removed.
Tests/utf8.svg:3: The element 'svg' does not allow the attribute 'contentStyleType', attribute to be removed.
Tests/utf8.svg:5: The element 'clipPath' is not allowed as a child of 'defs'
Tests/utf8.svg:8: The element 'clipPath' is not allowed as a child of 'defs'
Tests/utf8.svg:13: Element 'g' does not allow attributes with namespace 'http://xml.openoffice.org/svg/export'
Expand Down
2 changes: 1 addition & 1 deletion svgcheck/checksvg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# From a simple original version by Joe Hildebrand

from rfctools_common import log
from svgcheck import log

import re

Expand Down
171 changes: 171 additions & 0 deletions svgcheck/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# --------------------------------------------------
# Copyright The IETF Trust 2011-2019, All Rights Reserved
# --------------------------------------------------

""" Module Singleton which handles output of warnings and errors to
stdout/stderr, or alternatively to specified file paths.
If warn_error is set, then any warnings submitted will raise a
python exception.
"""

import sys
import os
import six
import io

quiet = False
verbose = False
debug = False

write_out = sys.stdout
write_err = sys.stderr

logging_codePage = 'utf8'

if not six.PY2 and os.name == 'nt' and os.isatty(2):
logging_codePage = sys.stdout.encoding


def write_to(file, unicodeString):
if os.name == 'nt':
if six.PY2:
if isinstance(file, io.StringIO):
file.write(unicodeString)
else:
file.write(unicodeString.encode(logging_codePage))
else:
if isinstance(file, io.StringIO):
file.write(unicodeString)
else:
file.buffer.write(unicodeString.encode(logging_codePage))
else:
if six.PY2:
if isinstance(file, io.StringIO):
file.write(unicodeString)
else:
file.write(unicodeString.encode(logging_codePage))
else:
file.write(unicodeString)


def write_on_line(*args):
""" Writes a message without ending the line, i.e. in a loading bar """
write_to(write_err, u' '.join(args))
write_err.flush()


def write(*args):
""" Prints a message to write_out """
# write_err.write(u' '.join(args))
write_to(write_err, u' '.join(args))
write_to(write_err, '\n')


def info(*args, **kwargs):
""" Prints a warning message unless quiet """
prefix = "INFO: "
if 'where' in kwargs:
where = kwargs['where']
fileName = where.base
if fileName.startswith("file:///"):
fileName = os.path.relpath(fileName[8:])
elif fileName[0:6] == 'file:/':
fileName = os.path.relpath(fileName[6:])
elif fileName[0:7] == 'http://' or fileName[0:8] == 'https://':
pass
else:
fileName = os.path.relpath(fileName)
prefix = "{0}:{1}: ".format(fileName, where.sourceline)
write_to(write_err, prefix + u' '.join(args))
write_err.write(u'\n')
write_err.flush()


def note(*args):
""" Call for being verbose only """
if verbose and not quiet:
write_to(write_err, u' '.join(args))
write_err.write('\n')


def warn(*args, **kwargs):
""" Prints a warning message unless quiet """
if not quiet:
prefix = "WARNING: "
if 'where' in kwargs:
where = kwargs['where']
fileName = where.base
if fileName.startswith("file:///"):
fileName = os.path.relpath(fileName[8:])
elif fileName[0:6] == 'file:/':
fileName = os.path.relpath(fileName[6:])
elif fileName[0:7] == 'http://' or fileName[0:8] == 'https://':
pass
else:
fileName = os.path.relpath(fileName)
prefix = "{0}:{1}: ".format(fileName, where.sourceline)
write_to(write_err, prefix + u' '.join(args))
write_err.write(u'\n')
write_err.flush()


def error(*args, **kwargs):
""" This is typically called after an exception was already raised. """
prefix = "ERROR: "
if 'where' in kwargs:
where = kwargs['where']
fileName = make_relative(where.base)
prefix = "{0}:{1}: ".format(fileName, where.sourceline)
if 'file' in kwargs:
fileName = make_relative(kwargs['file'])
prefix = "{0}:{1}: ".format(fileName, kwargs['line'])
if 'additional' in kwargs:
prefix = ' ' * kwargs['additional']

write_to(write_err, (prefix + u' '.join(args)))
write_to(write_err, u'\n')
write_err.flush()


def exception(message, list):
error(message)
if isinstance(list, Exception):
list = [list]
for e in list:
attr = dict([(n, str(getattr(e, n)).replace("\n", " ")) for n in dir(e)
if not n.startswith("_")])
if 'message' in attr:
if attr["message"].endswith(", got "):
attr["message"] += "nothing."
else:
attr['message'] = '-- none --'
if 'filename' in attr:
attr["filename"] = make_relative(attr["filename"])
else:
attr['filename'] = 'unknown'
if 'line' not in attr:
attr['line'] = -1
write_to(write_err, " %(filename)s: Line %(line)s: %(message)s\n" % attr)


def exception_lines(message, list):
if isinstance(list, Exception):
list = [list]
for e in list:
attr = dict([(n, str(getattr(e, n)).replace("\n", " ")) for n in dir(e)
if not n.startswith("_")])
if attr["message"].endswith(", got "):
attr["message"] += "nothing."
attr["filename"] = make_relative(attr["filename"])
write_to(write_err, " %(filename)s: Line %(line)s: %(message)s\n" % attr)


def make_relative(fileName):
if fileName.startswith("file:///"):
fileName = os.path.relpath(fileName[8:])
elif fileName[0:6] == 'file:/':
fileName = os.path.relpath(fileName[6:])
else:
fileName = os.path.relpath(fileName)
return fileName
25 changes: 14 additions & 11 deletions svgcheck/run.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import sys
import optparse
import os
import shutil
import lxml.etree
from svgcheck.checksvg import checkTree
from svgcheck.__init__ import __version__
from rfctools_common import log
from rfctools_common.parser import XmlRfcParser, XmlRfcError, CACHES
from rfctools_common.__init__ import __version__ as toolsVersion
from svgcheck import log
from xml2rfc.parser import XmlRfcParser, XmlRfcError
from xml2rfc import CACHES, CACHE_PREFIX
import svgcheck.word_properties as wp


def display_version(self, opt, value, parser):
print("svgcheck = " + __version__)
print("rfctools_common = " + toolsVersion)
sys.exit()


def clear_cache(cache_path):
XmlRfcParser('', cache_path=cache_path).delete_cache()
# Explicit path given?
paths = [os.path.expanduser(cache_path) for cache_path in CACHES]
caches = cache_path and [cache_path] or paths
for dir in caches:
path = os.path.join(dir, CACHE_PREFIX)
if os.access(path, os.W_OK):
shutil.rmtree(path)
log.write('Deleted cache directory at', path)
sys.exit()


Expand Down Expand Up @@ -96,9 +103,7 @@ def main():
if options.grey_scale:
wp.color_threshold = options.grey_level

sourceText = None
if len(args) < 1:
sourceText = sys.stdin.read()
source = os.getcwd() + "/stdin"
else:
source = args[0]
Expand All @@ -112,14 +117,12 @@ def main():

# Parse the document into an xmlrfc tree instance
parser = XmlRfcParser(source, verbose=options.verbose,
preserve_all_white=True,
quiet=options.quiet,
cache_path=options.cache,
no_network=options.no_network,
no_xinclude=options.no_xinclude)
no_network=options.no_network)
try:
xmlrfc = parser.parse(remove_pis=True, remove_comments=False,
strip_cdata=False, textIn=sourceText)
strip_cdata=False)
except XmlRfcError as e:
log.exception('Unable to parse the XML document: ' + source, e)
sys.exit(1)
Expand Down
Loading

0 comments on commit ac1cc51

Please sign in to comment.