diff options
author | David Robillard <d@drobilla.net> | 2022-06-16 14:30:06 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-07-17 17:08:38 -0400 |
commit | 86835a05f5e4d4ca7e016e29210cd613af0ea2ce (patch) | |
tree | e0f4659b7a519271ae55b8c1bde08d6cb12e7ad8 /lv2specgen | |
parent | 85e480aa08aef7d20617bd084e8a61b745e0aed6 (diff) | |
download | lv2-86835a05f5e4d4ca7e016e29210cd613af0ea2ce.tar.xz |
Clean up and modernize Python support code
Switches to safer and more modern idioms, and addresses many issues raised by
flake8 and pylint.
Diffstat (limited to 'lv2specgen')
-rwxr-xr-x | lv2specgen/lv2docgen.py | 3 | ||||
-rwxr-xr-x | lv2specgen/lv2specgen.py | 60 |
2 files changed, 34 insertions, 29 deletions
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('<pre class="c-code">(.*?)</pre>', re.DOTALL) + code_re = re.compile('<pre class="c-code">(.*?)</pre>', 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('<pre class="turtle-code">(.*?)</pre>', re.DOTALL) + code_re = re.compile('<pre class="turtle-code">(.*?)</pre>', 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 += '<span class="author" property="doap:developer">%s</span>' % d + + devdoc += f'<span class="author" property="doap:developer">{d}</span>' first = False if len(dev) == 1: - doc += ( - '<tr><th class="metahead">Developer</th><td>%s</td></tr>' % devdoc - ) + doc += f'<tr><th class="metahead">Developer</th><td>{devdoc}</td></tr>' elif len(dev) > 0: doc += ( - '<tr><th class="metahead">Developers</th><td>%s</td></tr>' % devdoc + f'<tr><th class="metahead">Developers</th><td>{devdoc}</td></tr>' ) maintdoc = "" @@ -1030,20 +1033,14 @@ def specAuthors(m, subject): for m in sorted(maint): if not first: maintdoc += ", " + maintdoc += ( - '<span class="author" property="doap:maintainer">%s</span>' % m + f'<span class="author" property="doap:maintainer">{m}</span>' ) first = False - if len(maint) == 1: - doc += ( - '<tr><th class="metahead">Maintainer</th><td>%s</td></tr>' - % maintdoc - ) - elif len(maint) > 0: - doc += ( - '<tr><th class="metahead">Maintainers</th><td>%s</td></tr>' - % maintdoc - ) + if len(maint): + label = "Maintainer" if len(maint) == 1 else "Maintainers" + doc += f'<tr><th class="metahead">{label}</th><td>{maintdoc}</td></tr>' 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) |