From 86835a05f5e4d4ca7e016e29210cd613af0ea2ce Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 16 Jun 2022 14:30:06 -0400 Subject: Clean up and modernize Python support code Switches to safer and more modern idioms, and addresses many issues raised by flake8 and pylint. --- lv2specgen/lv2docgen.py | 3 ++- lv2specgen/lv2specgen.py | 60 ++++++++++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 29 deletions(-) (limited to 'lv2specgen') diff --git a/lv2specgen/lv2docgen.py b/lv2specgen/lv2docgen.py index 35237b3..c5e13a7 100755 --- a/lv2specgen/lv2docgen.py +++ b/lv2specgen/lv2docgen.py @@ -38,7 +38,8 @@ rdfs = rdflib.Namespace("http://www.w3.org/2000/01/rdf-schema#") def uri_to_path(uri): - path = uri[uri.find(":") :] + first_colon = uri.find(":") + path = uri[first_colon:] while not path[0].isalpha(): path = path[1:] return path diff --git a/lv2specgen/lv2specgen.py b/lv2specgen/lv2specgen.py index 10b7cf9..a4ccefa 100755 --- a/lv2specgen/lv2specgen.py +++ b/lv2specgen/lv2specgen.py @@ -239,9 +239,9 @@ def linkifyVocabIdentifiers(m, string, classlist, proplist, instalist): def prettifyHtml(m, markup, subject, classlist, proplist, instalist): # Syntax highlight all C code if have_pygments: - code_rgx = re.compile('
(.*?)
', re.DOTALL) + code_re = re.compile('
(.*?)
', re.DOTALL) while True: - code = code_rgx.search(markup) + code = code_re.search(markup) if not code: break match_str = xml.sax.saxutils.unescape(code.group(1)) @@ -250,13 +250,13 @@ def prettifyHtml(m, markup, subject, classlist, proplist, instalist): pygments.lexers.CLexer(), pygments.formatters.HtmlFormatter(), ) - markup = code_rgx.sub(code_str, markup, 1) + markup = code_re.sub(code_str, markup, 1) # Syntax highlight all Turtle code if have_pygments: - code_rgx = re.compile('
(.*?)
', re.DOTALL) + code_re = re.compile('
(.*?)
', re.DOTALL) while True: - code = code_rgx.search(markup) + code = code_re.search(markup) if not code: break match_str = xml.sax.saxutils.unescape(code.group(1)) @@ -265,7 +265,7 @@ def prettifyHtml(m, markup, subject, classlist, proplist, instalist): pygments.lexers.rdf.TurtleLexer(), pygments.formatters.HtmlFormatter(), ) - markup = code_rgx.sub(code_str, markup, 1) + markup = code_re.sub(code_str, markup, 1) # Add links to code documentation for identifiers markup = linkifyCodeIdentifiers(markup) @@ -371,7 +371,9 @@ def getDetailedDocumentation(m, subject, classlist, proplist, instalist): if d: doc = getObject(d) if doc.datatype == lv2.Markdown: - markup += formatDoc(m, subject, doc, classlist, proplist, instalist) + markup += formatDoc( + m, subject, doc, classlist, proplist, instalist + ) else: html = getLiteralString(doc) markup += prettifyHtml( @@ -683,7 +685,9 @@ def extraInfo(term, m): getTermLink(getObject(p), term, getPredicate(p)), first ) elif isLiteral(getObject(p)): - doc += getProperty(linkifyCodeIdentifiers(str(getObject(p))), first) + doc += getProperty( + linkifyCodeIdentifiers(str(getObject(p))), first + ) elif isBlank(getObject(p)): doc += getProperty(str(blankNodeDesc(getObject(p), m)), first) else: @@ -762,7 +766,7 @@ def docTerms(category, list, m, classlist, proplist, instalist): doc = "" for term in list: if not term.startswith(spec_ns_str): - sys.stderr.write("warning: Skipping external term `%s'" % term) + sys.stderr.write("warning: Skipping external term `%s'\n" % term) continue t = termName(m, term) @@ -1014,15 +1018,14 @@ def specAuthors(m, subject): for d in sorted(dev): if not first: devdoc += ", " - devdoc += '%s' % d + + devdoc += f'{d}' first = False if len(dev) == 1: - doc += ( - 'Developer%s' % devdoc - ) + doc += f'Developer{devdoc}' elif len(dev) > 0: doc += ( - 'Developers%s' % devdoc + f'Developers{devdoc}' ) maintdoc = "" @@ -1030,20 +1033,14 @@ def specAuthors(m, subject): for m in sorted(maint): if not first: maintdoc += ", " + maintdoc += ( - '%s' % m + f'{m}' ) first = False - if len(maint) == 1: - doc += ( - 'Maintainer%s' - % maintdoc - ) - elif len(maint) > 0: - doc += ( - 'Maintainers%s' - % maintdoc - ) + if len(maint): + label = "Maintainer" if len(maint) == 1 else "Maintainers" + doc += f'{label}{maintdoc}' return doc @@ -1183,7 +1180,10 @@ def load_tags(path, docdir): def getChildText(elt, tagname): "Return the content of the first child node with a certain tag name." for e in elt.childNodes: - if e.nodeType == xml.dom.Node.ELEMENT_NODE and e.tagName == tagname: + if ( + e.nodeType == xml.dom.Node.ELEMENT_NODE + and e.tagName == tagname + ): return e.firstChild.nodeValue return "" @@ -1457,7 +1457,9 @@ def specgen( # Generate Term HTML classlist = docTerms("Class", classlist, m, classlist, proplist, instalist) - proplist = docTerms("Property", proplist, m, classlist, proplist, instalist) + proplist = docTerms( + "Property", proplist, m, classlist, proplist, instalist + ) if instances: instlist = docTerms( "Instance", instalist, m, classlist, proplist, instalist @@ -1571,7 +1573,9 @@ def specgen( etree.XMLParser(dtd_validation=True, no_network=True), ) except Exception as e: - sys.stderr.write("error: Validation failed for %s: %s" % (specloc, e)) + sys.stderr.write( + "error: Validation failed for %s: %s\n" % (specloc, e) + ) finally: os.chdir(oldcwd) -- cgit v1.2.1