Skip to content
Snippets Groups Projects
Commit 77c5d4ca authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[common][utils] Pre-commit cleanup

Also move all imports at the top, it's counter-productive
to have try/except import clauses on a package that is
mandatory for the code to work.
parent 28d74607
No related branches found
No related tags found
1 merge request!327Refactor update creation api
...@@ -32,21 +32,19 @@ from django.core.exceptions import ValidationError ...@@ -32,21 +32,19 @@ from django.core.exceptions import ValidationError
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils import six from django.utils import six
from docutils import utils
from docutils.nodes import Element
from docutils.core import Publisher
from ..ui.templatetags.markup import restructuredtext from ..ui.templatetags.markup import restructuredtext
def validate_restructuredtext(value): def validate_restructuredtext(value):
"""Validates a piece of restructuredtext for strict conformance""" """Validates a piece of restructuredtext for strict conformance"""
try:
from docutils import utils
from docutils.nodes import Element
from docutils.core import Publisher
except ImportError:
raise forms.ValidationError("Error in 'reStructuredText' validator: The Python docutils package isn't installed.")
# Generate a new parser (copying `rst2html.py` flow) # Generate a new parser (copying `rst2html.py` flow)
pub = Publisher(None, None, None, settings=None) pub = Publisher(None, None, None, settings=None)
pub.set_components('standalone', 'restructuredtext', 'pseudoxml') pub.set_components("standalone", "restructuredtext", "pseudoxml")
# Configure publisher # Configure publisher
settings = pub.get_settings(halt_level=5) settings = pub.get_settings(halt_level=5)
...@@ -63,12 +61,13 @@ def validate_restructuredtext(value): ...@@ -63,12 +61,13 @@ def validate_restructuredtext(value):
# Collect errors via an observer # Collect errors via an observer
errors = [] errors = []
def error_collector(data): def error_collector(data):
# Mutate the data since it was just generated # Mutate the data since it was just generated
data.line = data['line'] data.line = data["line"]
data.source = data['source'] data.source = data["source"]
data.level = data['level'] data.level = data["level"]
data.type = data['type'] data.type = data["type"]
data.message = Element.astext(data.children[0]) data.message = Element.astext(data.children[0])
data.full_message = Element.astext(data) data.full_message = Element.astext(data)
...@@ -82,8 +81,8 @@ def validate_restructuredtext(value): ...@@ -82,8 +81,8 @@ def validate_restructuredtext(value):
# Apply transforms (and more collect errors) # Apply transforms (and more collect errors)
document.transformer.populate_from_components( document.transformer.populate_from_components(
(pub.source, pub.reader, pub.reader.parser, pub.writer, (pub.source, pub.reader, pub.reader.parser, pub.writer, pub.destination)
pub.destination)) )
transformer = document.transformer transformer = document.transformer
while transformer.transforms: while transformer.transforms:
if not transformer.sorted: if not transformer.sorted:
...@@ -99,25 +98,33 @@ def validate_restructuredtext(value): ...@@ -99,25 +98,33 @@ def validate_restructuredtext(value):
# errors should be ready # errors should be ready
if errors: if errors:
validation_list = [] validation_list = []
msg = 'Line %(line)d (severity %(level)d): %(message)s' msg = "Line %(line)d (severity %(level)d): %(message)s"
for error in errors: for error in errors:
#from docutils.parsers.rst module: # from docutils.parsers.rst module:
#debug(0), info(1), warning(2), error(3), severe(4) # debug(0), info(1), warning(2), error(3), severe(4)
if error.level > 1: if error.level > 1:
validation_list.append(ValidationError( validation_list.append(
msg, code=error.type.lower(), ValidationError(
params=dict(line=error.line, level=error.level, message=error.message))) msg,
code=error.type.lower(),
params=dict(
line=error.line, level=error.level, message=error.message
),
)
)
if validation_list: if validation_list:
raise ValidationError(validation_list) raise ValidationError(validation_list)
def ensure_html(text): def ensure_html(text):
try: try:
validate_restructuredtext(text) validate_restructuredtext(text)
except ValidationError as e: except ValidationError:
return '<pre>{}</pre>'.format(text) return "<pre>{}</pre>".format(text)
else: else:
return restructuredtext(text) return restructuredtext(text)
def ensure_string(data): def ensure_string(data):
""" """
Ensure that we have a str object from data which can be either a str in Ensure that we have a str object from data which can be either a str in
...@@ -125,6 +132,6 @@ def ensure_string(data): ...@@ -125,6 +132,6 @@ def ensure_string(data):
""" """
if data is not None: if data is not None:
if isinstance(data, six.binary_type): if isinstance(data, six.binary_type):
return data.decode('utf-8') return data.decode("utf-8")
return data return data
return '' return ""
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment