Skip to content

Commit

Permalink
feat: support docstring as description
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazawazi committed Dec 23, 2023
1 parent 1ba6417 commit cf2637c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
9 changes: 5 additions & 4 deletions backend/funix/decorator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
from funix.session import get_global_variable, set_global_variable
from funix.theme import get_dict_theme, parse_theme
from funix.util.module import funix_menu_to_safe_function_name
from funix.util.text import un_indent
from funix.util.uri import is_valid_uri
from funix.widget import generate_frontend_widget_config

Expand Down Expand Up @@ -814,10 +815,10 @@ def decorator(function: callable) -> callable:
function_title = title if title is not None else function_name

function_description = description
# if function_description == "":
# function_docstring = getattr(function, "__doc__")
# if function_docstring:
# function_description = dedent(function_docstring.strip())
if function_description == "":
function_docstring = getattr(function, "__doc__")
if function_docstring:
function_description = un_indent(function_docstring)

if not theme:
if "__default" in __parsed_themes:
Expand Down
57 changes: 57 additions & 0 deletions backend/funix/util/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copied from [indoc](https://github.com/dtolnay/indoc)


def count_space(line: str) -> None | int:
"""
Document is on the way
"""
for i in range(len(line)):
if line[i] != " " and line[i] != "\t":
return i
return None


def un_indent(message: str) -> str:
"""
Document is on the way
"""
new_message = message

ignore_first_line = new_message.startswith("\n") or new_message.startswith("\r\n")

lines = new_message.splitlines()

min_spaces = []

for i in lines[1:]:
result = count_space(i)
if result is not None:
min_spaces.append(result)

if len(min_spaces) > 0:
min_space = sorted(min_spaces)[0]
else:
min_space = 0

result = ""

for i in range(len(lines)):
if i > 1 or (i == 1 and not ignore_first_line):
result += "\n"

if i == 0:
result += lines[i]
elif len(lines) > min_space:
result += lines[i][min_space:]

return result.strip()


# if __name__ == "__main__":
# a = """
# Return 0
#
# Parameters:
# """
#
# print(un_indent(a))

0 comments on commit cf2637c

Please sign in to comment.