- 新增图像生成接口,支持试用、积分和自定义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): 添加示例系统日志文件 - 记录用户请求、验证码发送成功与失败的日志信息
410 lines
12 KiB
Cython
410 lines
12 KiB
Cython
# cyextension/collections.pyx
|
|
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
|
|
# <see AUTHORS file>
|
|
#
|
|
# This module is part of SQLAlchemy and is released under
|
|
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
|
cimport cython
|
|
from cpython.long cimport PyLong_FromLongLong
|
|
from cpython.set cimport PySet_Add
|
|
|
|
from collections.abc import Collection
|
|
from itertools import filterfalse
|
|
|
|
cdef bint add_not_present(set seen, object item, hashfunc):
|
|
hash_value = hashfunc(item)
|
|
if hash_value not in seen:
|
|
PySet_Add(seen, hash_value)
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
cdef list cunique_list(seq, hashfunc=None):
|
|
cdef set seen = set()
|
|
if not hashfunc:
|
|
return [x for x in seq if x not in seen and not PySet_Add(seen, x)]
|
|
else:
|
|
return [x for x in seq if add_not_present(seen, x, hashfunc)]
|
|
|
|
def unique_list(seq, hashfunc=None):
|
|
return cunique_list(seq, hashfunc)
|
|
|
|
cdef class OrderedSet(set):
|
|
|
|
cdef list _list
|
|
|
|
@classmethod
|
|
def __class_getitem__(cls, key):
|
|
return cls
|
|
|
|
def __init__(self, d=None):
|
|
set.__init__(self)
|
|
if d is not None:
|
|
self._list = cunique_list(d)
|
|
set.update(self, self._list)
|
|
else:
|
|
self._list = []
|
|
|
|
cpdef OrderedSet copy(self):
|
|
cdef OrderedSet cp = OrderedSet.__new__(OrderedSet)
|
|
cp._list = list(self._list)
|
|
set.update(cp, cp._list)
|
|
return cp
|
|
|
|
@cython.final
|
|
cdef OrderedSet _from_list(self, list new_list):
|
|
cdef OrderedSet new = OrderedSet.__new__(OrderedSet)
|
|
new._list = new_list
|
|
set.update(new, new_list)
|
|
return new
|
|
|
|
def add(self, element):
|
|
if element not in self:
|
|
self._list.append(element)
|
|
PySet_Add(self, element)
|
|
|
|
def remove(self, element):
|
|
# set.remove will raise if element is not in self
|
|
set.remove(self, element)
|
|
self._list.remove(element)
|
|
|
|
def pop(self):
|
|
try:
|
|
value = self._list.pop()
|
|
except IndexError:
|
|
raise KeyError("pop from an empty set") from None
|
|
set.remove(self, value)
|
|
return value
|
|
|
|
def insert(self, Py_ssize_t pos, element):
|
|
if element not in self:
|
|
self._list.insert(pos, element)
|
|
PySet_Add(self, element)
|
|
|
|
def discard(self, element):
|
|
if element in self:
|
|
set.remove(self, element)
|
|
self._list.remove(element)
|
|
|
|
def clear(self):
|
|
set.clear(self)
|
|
self._list = []
|
|
|
|
def __getitem__(self, key):
|
|
return self._list[key]
|
|
|
|
def __iter__(self):
|
|
return iter(self._list)
|
|
|
|
def __add__(self, other):
|
|
return self.union(other)
|
|
|
|
def __repr__(self):
|
|
return "%s(%r)" % (self.__class__.__name__, self._list)
|
|
|
|
__str__ = __repr__
|
|
|
|
def update(self, *iterables):
|
|
for iterable in iterables:
|
|
for e in iterable:
|
|
if e not in self:
|
|
self._list.append(e)
|
|
set.add(self, e)
|
|
|
|
def __ior__(self, iterable):
|
|
self.update(iterable)
|
|
return self
|
|
|
|
def union(self, *other):
|
|
result = self.copy()
|
|
result.update(*other)
|
|
return result
|
|
|
|
def __or__(self, other):
|
|
return self.union(other)
|
|
|
|
def intersection(self, *other):
|
|
cdef set other_set = set.intersection(self, *other)
|
|
return self._from_list([a for a in self._list if a in other_set])
|
|
|
|
def __and__(self, other):
|
|
return self.intersection(other)
|
|
|
|
def symmetric_difference(self, other):
|
|
cdef set other_set
|
|
if isinstance(other, set):
|
|
other_set = <set> other
|
|
collection = other_set
|
|
elif isinstance(other, Collection):
|
|
collection = other
|
|
other_set = set(other)
|
|
else:
|
|
collection = list(other)
|
|
other_set = set(collection)
|
|
result = self._from_list([a for a in self._list if a not in other_set])
|
|
result.update(a for a in collection if a not in self)
|
|
return result
|
|
|
|
def __xor__(self, other):
|
|
return self.symmetric_difference(other)
|
|
|
|
def difference(self, *other):
|
|
cdef set other_set = set.difference(self, *other)
|
|
return self._from_list([a for a in self._list if a in other_set])
|
|
|
|
def __sub__(self, other):
|
|
return self.difference(other)
|
|
|
|
def intersection_update(self, *other):
|
|
set.intersection_update(self, *other)
|
|
self._list = [a for a in self._list if a in self]
|
|
|
|
def __iand__(self, other):
|
|
self.intersection_update(other)
|
|
return self
|
|
|
|
cpdef symmetric_difference_update(self, other):
|
|
collection = other if isinstance(other, Collection) else list(other)
|
|
set.symmetric_difference_update(self, collection)
|
|
self._list = [a for a in self._list if a in self]
|
|
self._list += [a for a in collection if a in self]
|
|
|
|
def __ixor__(self, other):
|
|
self.symmetric_difference_update(other)
|
|
return self
|
|
|
|
def difference_update(self, *other):
|
|
set.difference_update(self, *other)
|
|
self._list = [a for a in self._list if a in self]
|
|
|
|
def __isub__(self, other):
|
|
self.difference_update(other)
|
|
return self
|
|
|
|
cdef object cy_id(object item):
|
|
return PyLong_FromLongLong(<long long> (<void *>item))
|
|
|
|
# NOTE: cython 0.x will call __add__, __sub__, etc with the parameter swapped
|
|
# instead of the __rmeth__, so they need to check that also self is of the
|
|
# correct type. This is fixed in cython 3.x. See:
|
|
# https://docs.cython.org/en/latest/src/userguide/special_methods.html#arithmetic-methods
|
|
cdef class IdentitySet:
|
|
"""A set that considers only object id() for uniqueness.
|
|
|
|
This strategy has edge cases for builtin types- it's possible to have
|
|
two 'foo' strings in one of these sets, for example. Use sparingly.
|
|
|
|
"""
|
|
|
|
cdef dict _members
|
|
|
|
def __init__(self, iterable=None):
|
|
self._members = {}
|
|
if iterable:
|
|
self.update(iterable)
|
|
|
|
def add(self, value):
|
|
self._members[cy_id(value)] = value
|
|
|
|
def __contains__(self, value):
|
|
return cy_id(value) in self._members
|
|
|
|
cpdef remove(self, value):
|
|
del self._members[cy_id(value)]
|
|
|
|
def discard(self, value):
|
|
try:
|
|
self.remove(value)
|
|
except KeyError:
|
|
pass
|
|
|
|
def pop(self):
|
|
cdef tuple pair
|
|
try:
|
|
pair = self._members.popitem()
|
|
return pair[1]
|
|
except KeyError:
|
|
raise KeyError("pop from an empty set")
|
|
|
|
def clear(self):
|
|
self._members.clear()
|
|
|
|
def __eq__(self, other):
|
|
cdef IdentitySet other_
|
|
if isinstance(other, IdentitySet):
|
|
other_ = other
|
|
return self._members == other_._members
|
|
else:
|
|
return False
|
|
|
|
def __ne__(self, other):
|
|
cdef IdentitySet other_
|
|
if isinstance(other, IdentitySet):
|
|
other_ = other
|
|
return self._members != other_._members
|
|
else:
|
|
return True
|
|
|
|
cpdef issubset(self, iterable):
|
|
cdef IdentitySet other
|
|
if isinstance(iterable, self.__class__):
|
|
other = iterable
|
|
else:
|
|
other = self.__class__(iterable)
|
|
|
|
if len(self) > len(other):
|
|
return False
|
|
for m in filterfalse(other._members.__contains__, self._members):
|
|
return False
|
|
return True
|
|
|
|
def __le__(self, other):
|
|
if not isinstance(other, IdentitySet):
|
|
return NotImplemented
|
|
return self.issubset(other)
|
|
|
|
def __lt__(self, other):
|
|
if not isinstance(other, IdentitySet):
|
|
return NotImplemented
|
|
return len(self) < len(other) and self.issubset(other)
|
|
|
|
cpdef issuperset(self, iterable):
|
|
cdef IdentitySet other
|
|
if isinstance(iterable, self.__class__):
|
|
other = iterable
|
|
else:
|
|
other = self.__class__(iterable)
|
|
|
|
if len(self) < len(other):
|
|
return False
|
|
for m in filterfalse(self._members.__contains__, other._members):
|
|
return False
|
|
return True
|
|
|
|
def __ge__(self, other):
|
|
if not isinstance(other, IdentitySet):
|
|
return NotImplemented
|
|
return self.issuperset(other)
|
|
|
|
def __gt__(self, other):
|
|
if not isinstance(other, IdentitySet):
|
|
return NotImplemented
|
|
return len(self) > len(other) and self.issuperset(other)
|
|
|
|
cpdef IdentitySet union(self, iterable):
|
|
cdef IdentitySet result = self.__class__()
|
|
result._members.update(self._members)
|
|
result.update(iterable)
|
|
return result
|
|
|
|
def __or__(self, other):
|
|
if not isinstance(other, IdentitySet) or not isinstance(self, IdentitySet):
|
|
return NotImplemented
|
|
return self.union(other)
|
|
|
|
cpdef update(self, iterable):
|
|
for obj in iterable:
|
|
self._members[cy_id(obj)] = obj
|
|
|
|
def __ior__(self, other):
|
|
if not isinstance(other, IdentitySet):
|
|
return NotImplemented
|
|
self.update(other)
|
|
return self
|
|
|
|
cpdef IdentitySet difference(self, iterable):
|
|
cdef IdentitySet result = self.__new__(self.__class__)
|
|
if isinstance(iterable, self.__class__):
|
|
other = (<IdentitySet>iterable)._members
|
|
else:
|
|
other = {cy_id(obj) for obj in iterable}
|
|
result._members = {k:v for k, v in self._members.items() if k not in other}
|
|
return result
|
|
|
|
def __sub__(self, other):
|
|
if not isinstance(other, IdentitySet) or not isinstance(self, IdentitySet):
|
|
return NotImplemented
|
|
return self.difference(other)
|
|
|
|
cpdef difference_update(self, iterable):
|
|
cdef IdentitySet other = self.difference(iterable)
|
|
self._members = other._members
|
|
|
|
def __isub__(self, other):
|
|
if not isinstance(other, IdentitySet):
|
|
return NotImplemented
|
|
self.difference_update(other)
|
|
return self
|
|
|
|
cpdef IdentitySet intersection(self, iterable):
|
|
cdef IdentitySet result = self.__new__(self.__class__)
|
|
if isinstance(iterable, self.__class__):
|
|
other = (<IdentitySet>iterable)._members
|
|
else:
|
|
other = {cy_id(obj) for obj in iterable}
|
|
result._members = {k: v for k, v in self._members.items() if k in other}
|
|
return result
|
|
|
|
def __and__(self, other):
|
|
if not isinstance(other, IdentitySet) or not isinstance(self, IdentitySet):
|
|
return NotImplemented
|
|
return self.intersection(other)
|
|
|
|
cpdef intersection_update(self, iterable):
|
|
cdef IdentitySet other = self.intersection(iterable)
|
|
self._members = other._members
|
|
|
|
def __iand__(self, other):
|
|
if not isinstance(other, IdentitySet):
|
|
return NotImplemented
|
|
self.intersection_update(other)
|
|
return self
|
|
|
|
cpdef IdentitySet symmetric_difference(self, iterable):
|
|
cdef IdentitySet result = self.__new__(self.__class__)
|
|
cdef dict other
|
|
if isinstance(iterable, self.__class__):
|
|
other = (<IdentitySet>iterable)._members
|
|
else:
|
|
other = {cy_id(obj): obj for obj in iterable}
|
|
result._members = {k: v for k, v in self._members.items() if k not in other}
|
|
result._members.update(
|
|
[(k, v) for k, v in other.items() if k not in self._members]
|
|
)
|
|
return result
|
|
|
|
def __xor__(self, other):
|
|
if not isinstance(other, IdentitySet) or not isinstance(self, IdentitySet):
|
|
return NotImplemented
|
|
return self.symmetric_difference(other)
|
|
|
|
cpdef symmetric_difference_update(self, iterable):
|
|
cdef IdentitySet other = self.symmetric_difference(iterable)
|
|
self._members = other._members
|
|
|
|
def __ixor__(self, other):
|
|
if not isinstance(other, IdentitySet):
|
|
return NotImplemented
|
|
self.symmetric_difference(other)
|
|
return self
|
|
|
|
cpdef IdentitySet copy(self):
|
|
cdef IdentitySet cp = self.__new__(self.__class__)
|
|
cp._members = self._members.copy()
|
|
return cp
|
|
|
|
def __copy__(self):
|
|
return self.copy()
|
|
|
|
def __len__(self):
|
|
return len(self._members)
|
|
|
|
def __iter__(self):
|
|
return iter(self._members.values())
|
|
|
|
def __hash__(self):
|
|
raise TypeError("set objects are unhashable")
|
|
|
|
def __repr__(self):
|
|
return "%s(%r)" % (type(self).__name__, list(self._members.values()))
|