PY-4717 Format Numpy docstrings via napoleon and existing rest_formatter script
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Fri, 28 Aug 2015 10:04:06 +0000 (13:04 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Wed, 2 Sep 2015 11:35:32 +0000 (14:35 +0300)
python/helpers/numpy_formatter.py [new file with mode: 0644]
python/src/com/jetbrains/python/documentation/PyStructuredDocstringFormatter.java

diff --git a/python/helpers/numpy_formatter.py b/python/helpers/numpy_formatter.py
new file mode 100644 (file)
index 0000000..fbbe023
--- /dev/null
@@ -0,0 +1,19 @@
+import sys
+
+from sphinxcontrib.napoleon.docstring import NumpyDocstring
+
+import rest_formatter
+
+
+def main(text=None):
+  try:
+    src = sys.stdin.read() if text is None else text
+    import textwrap
+    rest_formatter.main(str(NumpyDocstring(textwrap.dedent(src))))
+  except:
+    exc_type, exc_value, exc_traceback = sys.exc_info()
+    sys.stderr.write("Error calculating docstring: " + str(exc_value))
+
+
+if __name__ == '__main__':
+  main()
index 5b680bfe721d1920036b0bdd0d50458e6b1d4797..cb1ddf6e0845b509d5969e286524b4fc33f247cf 100644 (file)
@@ -46,7 +46,7 @@ import java.util.Map;
  * @author yole
  */
 public class PyStructuredDocstringFormatter {
-  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.documentation.PyStructuredDocstringFormatter");
+  private static final Logger LOG = Logger.getInstance(PyStructuredDocstringFormatter.class);
 
   private PyStructuredDocstringFormatter() {
   }
@@ -68,7 +68,15 @@ public class PyStructuredDocstringFormatter {
 
     final String formatter;
     final StructuredDocString structuredDocString;
-    if (documentationSettings.isEpydocFormat(element.getContainingFile()) || DocStringUtil.isEpydocDocString(preparedDocstring)) {
+    if (documentationSettings.isGoogleFormat(element.getContainingFile()) || DocStringUtil.isGoogleDocString(preparedDocstring)) {
+      formatter = PythonHelpersLocator.getHelperPath("google_formatter.py");
+      structuredDocString = DocStringUtil.parseDocString(DocStringFormat.GOOGLE, preparedDocstring);
+    }
+    else if (documentationSettings.isNumpyFormat(element.getContainingFile()) || DocStringUtil.isNumpyDocstring(preparedDocstring)) {
+      formatter = PythonHelpersLocator.getHelperPath("numpy_formatter.py");
+      structuredDocString = DocStringUtil.parseDocString(DocStringFormat.NUMPY, preparedDocstring);
+    }
+    else if (documentationSettings.isEpydocFormat(element.getContainingFile()) || DocStringUtil.isEpydocDocString(preparedDocstring)) {
       formatter = PythonHelpersLocator.getHelperPath("epydoc_formatter.py");
       structuredDocString = DocStringUtil.parseDocString(DocStringFormat.EPYTEXT, preparedDocstring);
       result.add(formatStructuredDocString(structuredDocString));
@@ -77,10 +85,7 @@ public class PyStructuredDocstringFormatter {
       formatter = PythonHelpersLocator.getHelperPath("rest_formatter.py");
       structuredDocString = DocStringUtil.parseDocString(DocStringFormat.REST, preparedDocstring);
     }
-    else if (documentationSettings.isGoogleFormat(element.getContainingFile()) || DocStringUtil.isGoogleDocString(preparedDocstring)) {
-      formatter = PythonHelpersLocator.getHelperPath("google_formatter.py");
-      structuredDocString = DocStringUtil.parseDocString(DocStringFormat.GOOGLE, preparedDocstring);
-    }
+
     else {
       return null;
     }