- 新增图像生成接口,支持试用、积分和自定义API Key模式 - 实现生成图片结果异步上传至MinIO存储,带重试机制 - 优化积分预扣除和异常退还逻辑,保障用户积分准确 - 添加获取生成历史记录接口,支持时间范围和分页 - 提供本地字典配置接口,支持模型、比例、提示模板和尺寸 - 实现图片批量上传接口,支持S3兼容对象存储 feat(admin): 增加管理员角色管理与权限分配接口 - 实现角色列表查询、角色创建、更新及删除功能 - 增加权限列表查询接口 - 实现用户角色分配接口,便于统一管理用户权限 - 增加系统字典增删查改接口,支持分类过滤和排序 - 权限控制全面覆盖管理接口,保证安全访问 feat(auth): 完善用户登录注册及权限相关接口与页面 - 实现手机号验证码发送及校验功能,保障注册安全 - 支持手机号注册、登录及退出接口,集成日志记录 - 增加修改密码功能,验证原密码后更新 - 提供动态导航菜单接口,基于权限展示不同菜单 - 实现管理界面路由及日志、角色、字典管理页面访问权限控制 - 添加系统日志查询接口,支持关键词和等级筛选 feat(app): 初始化Flask应用并配置蓝图与数据库 - 创建应用程序工厂,加载配置,初始化数据库和Redis客户端 - 注册认证、API及管理员蓝图,整合路由 - 根路由渲染主页模板 - 应用上下文中自动创建数据库表,保证运行环境准备完毕 feat(database): 提供数据库创建与迁移支持脚本 - 新增数据库创建脚本,支持自动检测是否已存在 - 添加数据库表初始化脚本,支持创建和删除所有表 - 实现RBAC权限初始化,包含基础权限和角色创建 - 新增字段手动修复脚本,添加用户API Key和积分字段 - 强制迁移脚本支持清理连接和修复表结构,初始化默认数据及角色分配 feat(config): 新增系统配置参数 - 配置数据库、Redis、Session和MinIO相关参数 - 添加AI接口地址及试用Key配置 - 集成阿里云短信服务配置及开发模式相关参数 feat(extensions): 初始化数据库、Redis和MinIO客户端 - 创建全局SQLAlchemy数据库实例和Redis客户端 - 配置基于boto3的MinIO兼容S3客户端 chore(logs): 添加示例系统日志文件 - 记录用户请求、验证码发送成功与失败的日志信息
302 lines
9.5 KiB
Python
302 lines
9.5 KiB
Python
from __future__ import annotations
|
|
|
|
import collections.abc as cabc
|
|
from contextlib import contextmanager
|
|
from gettext import gettext as _
|
|
|
|
from ._compat import term_len
|
|
from .parser import _split_opt
|
|
|
|
# Can force a width. This is used by the test system
|
|
FORCED_WIDTH: int | None = None
|
|
|
|
|
|
def measure_table(rows: cabc.Iterable[tuple[str, str]]) -> tuple[int, ...]:
|
|
widths: dict[int, int] = {}
|
|
|
|
for row in rows:
|
|
for idx, col in enumerate(row):
|
|
widths[idx] = max(widths.get(idx, 0), term_len(col))
|
|
|
|
return tuple(y for x, y in sorted(widths.items()))
|
|
|
|
|
|
def iter_rows(
|
|
rows: cabc.Iterable[tuple[str, str]], col_count: int
|
|
) -> cabc.Iterator[tuple[str, ...]]:
|
|
for row in rows:
|
|
yield row + ("",) * (col_count - len(row))
|
|
|
|
|
|
def wrap_text(
|
|
text: str,
|
|
width: int = 78,
|
|
initial_indent: str = "",
|
|
subsequent_indent: str = "",
|
|
preserve_paragraphs: bool = False,
|
|
) -> str:
|
|
"""A helper function that intelligently wraps text. By default, it
|
|
assumes that it operates on a single paragraph of text but if the
|
|
`preserve_paragraphs` parameter is provided it will intelligently
|
|
handle paragraphs (defined by two empty lines).
|
|
|
|
If paragraphs are handled, a paragraph can be prefixed with an empty
|
|
line containing the ``\\b`` character (``\\x08``) to indicate that
|
|
no rewrapping should happen in that block.
|
|
|
|
:param text: the text that should be rewrapped.
|
|
:param width: the maximum width for the text.
|
|
:param initial_indent: the initial indent that should be placed on the
|
|
first line as a string.
|
|
:param subsequent_indent: the indent string that should be placed on
|
|
each consecutive line.
|
|
:param preserve_paragraphs: if this flag is set then the wrapping will
|
|
intelligently handle paragraphs.
|
|
"""
|
|
from ._textwrap import TextWrapper
|
|
|
|
text = text.expandtabs()
|
|
wrapper = TextWrapper(
|
|
width,
|
|
initial_indent=initial_indent,
|
|
subsequent_indent=subsequent_indent,
|
|
replace_whitespace=False,
|
|
)
|
|
if not preserve_paragraphs:
|
|
return wrapper.fill(text)
|
|
|
|
p: list[tuple[int, bool, str]] = []
|
|
buf: list[str] = []
|
|
indent = None
|
|
|
|
def _flush_par() -> None:
|
|
if not buf:
|
|
return
|
|
if buf[0].strip() == "\b":
|
|
p.append((indent or 0, True, "\n".join(buf[1:])))
|
|
else:
|
|
p.append((indent or 0, False, " ".join(buf)))
|
|
del buf[:]
|
|
|
|
for line in text.splitlines():
|
|
if not line:
|
|
_flush_par()
|
|
indent = None
|
|
else:
|
|
if indent is None:
|
|
orig_len = term_len(line)
|
|
line = line.lstrip()
|
|
indent = orig_len - term_len(line)
|
|
buf.append(line)
|
|
_flush_par()
|
|
|
|
rv = []
|
|
for indent, raw, text in p:
|
|
with wrapper.extra_indent(" " * indent):
|
|
if raw:
|
|
rv.append(wrapper.indent_only(text))
|
|
else:
|
|
rv.append(wrapper.fill(text))
|
|
|
|
return "\n\n".join(rv)
|
|
|
|
|
|
class HelpFormatter:
|
|
"""This class helps with formatting text-based help pages. It's
|
|
usually just needed for very special internal cases, but it's also
|
|
exposed so that developers can write their own fancy outputs.
|
|
|
|
At present, it always writes into memory.
|
|
|
|
:param indent_increment: the additional increment for each level.
|
|
:param width: the width for the text. This defaults to the terminal
|
|
width clamped to a maximum of 78.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
indent_increment: int = 2,
|
|
width: int | None = None,
|
|
max_width: int | None = None,
|
|
) -> None:
|
|
self.indent_increment = indent_increment
|
|
if max_width is None:
|
|
max_width = 80
|
|
if width is None:
|
|
import shutil
|
|
|
|
width = FORCED_WIDTH
|
|
if width is None:
|
|
width = max(min(shutil.get_terminal_size().columns, max_width) - 2, 50)
|
|
self.width = width
|
|
self.current_indent: int = 0
|
|
self.buffer: list[str] = []
|
|
|
|
def write(self, string: str) -> None:
|
|
"""Writes a unicode string into the internal buffer."""
|
|
self.buffer.append(string)
|
|
|
|
def indent(self) -> None:
|
|
"""Increases the indentation."""
|
|
self.current_indent += self.indent_increment
|
|
|
|
def dedent(self) -> None:
|
|
"""Decreases the indentation."""
|
|
self.current_indent -= self.indent_increment
|
|
|
|
def write_usage(self, prog: str, args: str = "", prefix: str | None = None) -> None:
|
|
"""Writes a usage line into the buffer.
|
|
|
|
:param prog: the program name.
|
|
:param args: whitespace separated list of arguments.
|
|
:param prefix: The prefix for the first line. Defaults to
|
|
``"Usage: "``.
|
|
"""
|
|
if prefix is None:
|
|
prefix = f"{_('Usage:')} "
|
|
|
|
usage_prefix = f"{prefix:>{self.current_indent}}{prog} "
|
|
text_width = self.width - self.current_indent
|
|
|
|
if text_width >= (term_len(usage_prefix) + 20):
|
|
# The arguments will fit to the right of the prefix.
|
|
indent = " " * term_len(usage_prefix)
|
|
self.write(
|
|
wrap_text(
|
|
args,
|
|
text_width,
|
|
initial_indent=usage_prefix,
|
|
subsequent_indent=indent,
|
|
)
|
|
)
|
|
else:
|
|
# The prefix is too long, put the arguments on the next line.
|
|
self.write(usage_prefix)
|
|
self.write("\n")
|
|
indent = " " * (max(self.current_indent, term_len(prefix)) + 4)
|
|
self.write(
|
|
wrap_text(
|
|
args, text_width, initial_indent=indent, subsequent_indent=indent
|
|
)
|
|
)
|
|
|
|
self.write("\n")
|
|
|
|
def write_heading(self, heading: str) -> None:
|
|
"""Writes a heading into the buffer."""
|
|
self.write(f"{'':>{self.current_indent}}{heading}:\n")
|
|
|
|
def write_paragraph(self) -> None:
|
|
"""Writes a paragraph into the buffer."""
|
|
if self.buffer:
|
|
self.write("\n")
|
|
|
|
def write_text(self, text: str) -> None:
|
|
"""Writes re-indented text into the buffer. This rewraps and
|
|
preserves paragraphs.
|
|
"""
|
|
indent = " " * self.current_indent
|
|
self.write(
|
|
wrap_text(
|
|
text,
|
|
self.width,
|
|
initial_indent=indent,
|
|
subsequent_indent=indent,
|
|
preserve_paragraphs=True,
|
|
)
|
|
)
|
|
self.write("\n")
|
|
|
|
def write_dl(
|
|
self,
|
|
rows: cabc.Sequence[tuple[str, str]],
|
|
col_max: int = 30,
|
|
col_spacing: int = 2,
|
|
) -> None:
|
|
"""Writes a definition list into the buffer. This is how options
|
|
and commands are usually formatted.
|
|
|
|
:param rows: a list of two item tuples for the terms and values.
|
|
:param col_max: the maximum width of the first column.
|
|
:param col_spacing: the number of spaces between the first and
|
|
second column.
|
|
"""
|
|
rows = list(rows)
|
|
widths = measure_table(rows)
|
|
if len(widths) != 2:
|
|
raise TypeError("Expected two columns for definition list")
|
|
|
|
first_col = min(widths[0], col_max) + col_spacing
|
|
|
|
for first, second in iter_rows(rows, len(widths)):
|
|
self.write(f"{'':>{self.current_indent}}{first}")
|
|
if not second:
|
|
self.write("\n")
|
|
continue
|
|
if term_len(first) <= first_col - col_spacing:
|
|
self.write(" " * (first_col - term_len(first)))
|
|
else:
|
|
self.write("\n")
|
|
self.write(" " * (first_col + self.current_indent))
|
|
|
|
text_width = max(self.width - first_col - 2, 10)
|
|
wrapped_text = wrap_text(second, text_width, preserve_paragraphs=True)
|
|
lines = wrapped_text.splitlines()
|
|
|
|
if lines:
|
|
self.write(f"{lines[0]}\n")
|
|
|
|
for line in lines[1:]:
|
|
self.write(f"{'':>{first_col + self.current_indent}}{line}\n")
|
|
else:
|
|
self.write("\n")
|
|
|
|
@contextmanager
|
|
def section(self, name: str) -> cabc.Iterator[None]:
|
|
"""Helpful context manager that writes a paragraph, a heading,
|
|
and the indents.
|
|
|
|
:param name: the section name that is written as heading.
|
|
"""
|
|
self.write_paragraph()
|
|
self.write_heading(name)
|
|
self.indent()
|
|
try:
|
|
yield
|
|
finally:
|
|
self.dedent()
|
|
|
|
@contextmanager
|
|
def indentation(self) -> cabc.Iterator[None]:
|
|
"""A context manager that increases the indentation."""
|
|
self.indent()
|
|
try:
|
|
yield
|
|
finally:
|
|
self.dedent()
|
|
|
|
def getvalue(self) -> str:
|
|
"""Returns the buffer contents."""
|
|
return "".join(self.buffer)
|
|
|
|
|
|
def join_options(options: cabc.Sequence[str]) -> tuple[str, bool]:
|
|
"""Given a list of option strings this joins them in the most appropriate
|
|
way and returns them in the form ``(formatted_string,
|
|
any_prefix_is_slash)`` where the second item in the tuple is a flag that
|
|
indicates if any of the option prefixes was a slash.
|
|
"""
|
|
rv = []
|
|
any_prefix_is_slash = False
|
|
|
|
for opt in options:
|
|
prefix = _split_opt(opt)[0]
|
|
|
|
if prefix == "/":
|
|
any_prefix_is_slash = True
|
|
|
|
rv.append((len(prefix), opt))
|
|
|
|
rv.sort(key=lambda x: x[0])
|
|
return ", ".join(x[1] for x in rv), any_prefix_is_slash
|