Add documentation request functions and use hints to admin utils 40/72140/3
authorSawyer Bergeron <sbergeron@iol.unh.edu>
Wed, 3 Mar 2021 17:10:02 +0000 (12:10 -0500)
committerSawyer Bergeron <sbergeron@iol.unh.edu>
Wed, 3 Mar 2021 18:27:23 +0000 (13:27 -0500)
Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
Change-Id: I3211194fea5dc7d0c3569db6c1d42fe2f4aa53e1
Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
src/dashboard/admin_utils.py

index 222ccd3..76db762 100644 (file)
@@ -17,6 +17,9 @@ from resource_inventory.models import (
 )
 
 import json
+import sys
+import inspect
+import pydoc
 
 from django.contrib.auth.models import User
 
@@ -388,3 +391,38 @@ def extend_booking(booking_id, days=0, hours=0, minutes=0, weeks=0):
     booking = Booking.objects.get(id=booking_id)
     booking.end = booking.end + timedelta(days=days, hours=hours, minutes=minutes, weeks=weeks)
     booking.save()
+
+
+def docs(function=None, fulltext=False):
+    fn = None
+
+    if isinstance(function, str):
+        try:
+            fn = globals()[function]
+        except KeyError:
+            print("Couldn't find a function by the given name")
+            return
+    elif callable(function):
+        fn = function
+    else:
+        print("docs(function: callable | str, fulltext: bool) was called with a 'function' that was neither callable nor a string name of a function")
+        print("usage: docs('some_function_in_admin_utils', fulltext=True)")
+        print("The 'fulltext' argument is used to choose if you want the complete source of the function printed. If this argument is false then you will only see the pydoc rendered documentation for the function")
+        return
+
+    if not fn:
+        print("couldn't find a function by that name")
+
+    if not fulltext:
+        print("Pydoc documents the function as such:")
+        print(pydoc.render_doc(fn))
+    else:
+        print("The full source of the function is this:")
+        print(inspect.getsource(fn))
+
+
+def admin_functions():
+    return [name for name, func in inspect.getmembers(sys.modules[__name__]) if (inspect.isfunction(func) and func.__module__ == __name__)]
+
+
+print("Hint: call `docs(<function name>)` or `admin_functions()` for help on using the admin utils")