PKa*N"*  sphinxify.py#!/usr/bin/env python3 """Convert Javadoc to Sphinx docstrings.""" import re import sys import textwrap from typing import List, Tuple __version__ = "0.4" FIND_FUNC_RE = r"(.*)\n\s*((?:(?:public|protected|private|static|final|synchronized|abstract|default|native)\s+)+)(?:([\w<>[\]]+)\s+)?(\w+)\s*\(([^)]*)\)" TYPE_MAPPING = { "ArrayList": "List", "<": "[", ">": "]", "boolean": "bool", "Boolean": "bool", "Integer": "int", "Long": "int", "long": "int", "Short": "int", "short": "int", "Byte": "int", "byte": "int", "double": "float", "Double": "float", "Float": "float", "String": "str", "ByteBuffer": "bytearray", # java.nio } def trim_lines(lines: List[str]) -> List[str]: return "\n".join(lines).strip().split("\n") def process_doc(txt: str) -> str: """Convert a doc comment to Sphinx reST.""" params: List[Tuple[str, List[str]]] = [] returns = [] paramidx = -1 found_returns = False lines = txt.splitlines() new_lines = [] for line in lines: line = line.strip() line = line.replace("/*", "").replace("*/", "") if line.startswith("*"): line = line[1:].lstrip() if line.startswith((r"\fn ", r"\class ")): continue if line.startswith(r"\brief "): line = line[len(r"\brief ") :] elif line.startswith(r"\enum "): line = " ".join(line.split()[2:]) else: line = line.replace("

", "") p = re.search(r"^[\\@]param\W+(\w+)", line) if p: line = re.sub(r"^[\\@]param\W+\w+(\W+)?", "", line) paramidx += 1 params.append((p.group(1), [])) else: r = re.search(r"^[\\@]returns?\W*", line) if r: line = re.sub(r"^[\\@]returns?\W*", "", line) found_returns = True line = re.sub(r"{@link\W+#(\w+?)\(.*?\)\W*}", r":meth:`.\1`", line) line = re.sub(r"{@link\W+(\w+)#(\w+)\(.*?\)\W*}", r":meth:`.\1.\2`", line) line = re.sub(r"{@link\W+#(\w+?)\W*}", r":class:`.\1`", line) if found_returns: returns.append(line) elif paramidx != -1: params[paramidx][1].append(line) else: new_lines.append(line) text = "\n".join(new_lines).strip() text = re.sub("\n{2,}", "\n\n", text) to_append = [""] # indent each parameter evenly pindent = max((len(p[0]) for p in params), default=0) pindent_str = " " * pindent # indent each parameter and append for name, lines in params: pname = f":param {name}: " pp = trim_lines(lines) if not pp: continue to_append.append(f'\n{pname}{" " * (pindent - len(pname))}{pp[0]}') for line in pp[1:]: if line: to_append.append(f"{pindent_str}{line}") else: to_append.append("") if returns: returns = trim_lines(returns) # returns always needs a newline before it to_append.append("\n:returns: " + returns[0]) for line in returns[1:]: if line: to_append.append(" " * 10 + line) else: to_append.append("") return text + "\n".join(to_append) def java_type_to_python(type: str) -> str: """Turn a Java type into a Python type hint.""" if type == "void": return "None" if type == "byte[]": return "bytes" is_array = False if type.endswith("[]"): is_array = True type = type[:-2] type_words = re.split(r"(\w+)", type) for i, word in enumerate(type_words): if word in TYPE_MAPPING: type_words[i] = TYPE_MAPPING[word] type = "".join(type_words) if is_array: type = f"List[{type}]" return type def format_docstring(text: str, indent: str = " " * 8) -> str: """Wrap a doc string to be valid Python source for a docstring.""" if text.count("\n") == 0: return f'{indent}"""{text}"""' return textwrap.indent('"""{}\n"""'.format(text), indent) def process_raw(txt: str) -> str: """Convert the first doc comment in txt into Sphinx reST.""" # remove diff formatting if present txt = re.sub(r"(?m)^\+", "", txt) # try to find a function definition s = re.split(FIND_FUNC_RE, txt) return process_doc(s[0]) def process_yamlgen(txt: str) -> str: t = process_raw(txt) t = " doc: |\n" + textwrap.indent(t, " " * 4) return t def process_cstring(txt: str) -> str: t = process_raw(txt) return '"{}"'.format(t.replace("\n", '\\n"\n"')) def process_comment(txt: str) -> str: t = process_raw(txt) return textwrap.indent(t, " " * 4 + "#: ") def process(txt: str) -> str: """Convert the first Java method in txt into a Python stub.""" # remove diff formatting if present txt = re.sub(r"(?m)^\+", "", txt) # try to find a function definition s = re.split(FIND_FUNC_RE, txt) t = process_doc(s[0]) t = format_docstring(t) # add the function definition in if present if len(s) > 1: modifiers, ret_type, func_name, args = s[2:6] if args: py_args = [] for arg in args.split(", "): split = arg.split() if split[0] != "final": arg_type, arg_name = split else: arg_type, arg_name = split[1:] py_args.append(f"{arg_name}: {java_type_to_python(arg_type)}") args = ", " + ", ".join(py_args) if ret_type: ret_type = java_type_to_python(ret_type) else: func_name = "__init__" ret_type = "None" if "static" in modifiers: t = f" @classmethod\n def {func_name}(cls{args}) -> {ret_type}:\n{t}" else: t = f" def {func_name}(self{args}) -> {ret_type}:\n{t}" return t def main(): text = sys.stdin.read() if len(sys.argv) > 1: modes = { "yaml": process_yamlgen, "raw": process_raw, "cstring": process_cstring, "comment": process_comment, } print(modes[sys.argv[1]](text)) else: print(process(text)) if __name__ == "__main__": main() PK!H#r&,(sphinxify-0.4.dist-info/entry_points.txtN+I/N.,()*.̫Lr3PKeL0#  sphinxify-0.4.dist-info/LICENSEBSD 3-Clause License Copyright (c) 2016-2018, Dustin Spicuzza Copyright (c) 2017-2018, David Vo Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the names of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY NONINFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. PK!H>*RQsphinxify-0.4.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,rzd&Y)r$[)T&UrPK!Hc sphinxify-0.4.dist-info/METADATATn0+ȱ6]]ܸE&M!ZK%R%rJ!'-͐gHRI:eB5fR{vyJC]KeplMl9y)|*FLX5>L MeX'2x~4z;9ψOYrW\;vnTr]FLe0Vpe RWck[j .w}rqo􍘡ϝnw %a;4ͯ+Q[lSs-IRepVY8Xӟ=g Gsk/@ %K8-ׅc8@'jV_Krrg,(2m>X1y\)PK!H.f(sphinxify-0.4.dist-info/RECORDu϶B@}ς$-BF%HdӡF 5nܝ}|PsMϐ<w:/9g 1{|N]qxG@2FŌsAWmj)hG&\PpV\XVP[\'+W&ytGuoí(|jmT U=j$ld[Ranu_=> T[k{Qt64/L9"L{Bl^ɮKU \&D nB N®9.d/PKa*N"*  sphinxify.pyPK!H#r&,( sphinxify-0.4.dist-info/entry_points.txtPKeL0#  usphinxify-0.4.dist-info/LICENSEPK!H>*RQsphinxify-0.4.dist-info/WHEELPK!Hc L sphinxify-0.4.dist-info/METADATAPK!H.f(?#sphinxify-0.4.dist-info/RECORDPK$