Skip to content

Commit

Permalink
ci(linter/formatter): use ruff formatter, replace the black (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
wklken authored Nov 1, 2023
1 parent 6e08387 commit ea2f378
Show file tree
Hide file tree
Showing 76 changed files with 331 additions and 233 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ fail_fast: true
repos:
- repo: local
hooks:
- id: black
name: black
- id: format
name: ruff-formatter
language: python
types: [python]
entry: black --config=src/dashboard/pyproject.toml
entry: bash -c 'cd src/dashboard && ruff format --config=pyproject.toml .'
files: src/dashboard/
- id: ruff
name: ruff
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ init:

.PHONY: lint
lint:
black --config=pyproject.toml .
ruff format --config=pyproject.toml .
ruff --config=pyproject.toml --force-exclude --fix .
mypy --config-file=pyproject.toml .

Expand Down
2 changes: 0 additions & 2 deletions src/dashboard/apigateway/apigateway/account/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@


class ApiGatewayJWTAppMiddleware(BaseApiGatewayJWTAppMiddleware):

App = namedtuple("App", ["app_code"])

def make_app(self, bk_app_code=None, verified=False, **jwt_app):
return self.App(app_code=bk_app_code or "")


class ApiGatewayJWTUserMiddleware(BaseApiGatewayJWTUserMiddleware):

User = namedtuple("User", ["username", "is_active", "is_authenticated"])

def get_user(self, request, api_name=None, bk_username=None, verified=False, **credentials):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ class PaaSAppPermissionApplyInputSLZ(serializers.Serializer):
def validate(self, data):
if data["grant_dimension"] == GrantDimensionEnum.RESOURCE.value and not data.get("resource_ids"):
raise serializers.ValidationError(
_("申请权限类型为 {grant_dimension} 时,参数 resource_ids 不能为空。").format(grant_dimension=data["grant_dimension"])
_("申请权限类型为 {grant_dimension} 时,参数 resource_ids 不能为空。").format(
grant_dimension=data["grant_dimension"]
)
)

return data
Expand All @@ -144,7 +146,9 @@ def validate_target_app_code(self, value):
request = self.context["request"]
if request.app.app_code != value:
raise serializers.ValidationError(
_("应用【{app_code}】不能为其它应用【{value}】申请访问网关API的权限。").format(app_code=request.app.app_code, value=value)
_("应用【{app_code}】不能为其它应用【{value}】申请访问网关API的权限。").format(
app_code=request.app.app_code, value=value
)
)

return value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ def post(self, request, *args, **kwargs):
try:
docs = parser.parse(slz.validated_data["file"])
except NoResourceDocError:
raise error_codes.INVALID_ARGUMENT.format(_("不存在符合条件的资源文档,请参考使用指南,检查归档文件中资源文档是否正确。"), replace=True)
raise error_codes.INVALID_ARGUMENT.format(
_("不存在符合条件的资源文档,请参考使用指南,检查归档文件中资源文档是否正确。"), replace=True
)
except ResourceDocJinja2TemplateError as err:
raise error_codes.INTERNAL.format(_("导入资源文档失败,{err}。").format(err=err), replace=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class APITestInputSLZ(serializers.Serializer):
query_params = serializers.DictField(child=serializers.CharField(), allow_empty=True, help_text="查询参数")
body = serializers.CharField(allow_blank=True, required=False, help_text="请求体")
use_test_app = serializers.BooleanField(help_text="是否使用测试应用")
use_user_from_cookies = serializers.BooleanField(required=False, default=False, help_text="是否使用 cookies 中的用户信息")
use_user_from_cookies = serializers.BooleanField(
required=False, default=False, help_text="是否使用 cookies 中的用户信息"
)
authorization = AuthorizationSLZ(required=False, allow_null=True, help_text="认证信息")

def validate(self, data: Dict[str, Any]) -> Dict[str, Any]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@


class ComponentSearchInputSLZ(serializers.Serializer):
query = serializers.CharField(required=False, allow_blank=True, help_text="查询关键字,支持模糊匹配组件名称、组件描述、组件系统名称")
query = serializers.CharField(
required=False, allow_blank=True, help_text="查询关键字,支持模糊匹配组件名称、组件描述、组件系统名称"
)


class ComponentSearchOutputSLZ(serializers.Serializer):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@


class SDKListInputSLZ(serializers.Serializer):
language = serializers.ChoiceField(choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python")
language = serializers.ChoiceField(
choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python"
)

class Meta:
ref_name = "apigateway.apis.web.docs.esb.sdk.SDKListInputSLZ"


class SDKRetrieveInputSLZ(serializers.Serializer):
language = serializers.ChoiceField(choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python")
language = serializers.ChoiceField(
choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python"
)


class SDKDocInputSLZ(serializers.Serializer):
language = serializers.ChoiceField(choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python")
language = serializers.ChoiceField(
choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python"
)

class Meta:
ref_name = "apigateway.apis.web.docs.esb.sdk.SDKDocInputSLZ"
Expand All @@ -47,7 +53,9 @@ class Meta:


class SDKUsageExampleInputSLZ(serializers.Serializer):
language = serializers.ChoiceField(choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python")
language = serializers.ChoiceField(
choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python"
)
system_name = serializers.CharField(help_text="组件所属系统名称")
component_name = serializers.CharField(help_text="组件名称")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@


class SDKListInputSLZ(serializers.Serializer):
language = serializers.ChoiceField(choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python")
language = serializers.ChoiceField(
choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python"
)

class Meta:
ref_name = "apigateway.apis.web.docs.gateway.gateway_sdk.SDKListInputSLZ"
Expand Down Expand Up @@ -58,7 +60,9 @@ class StageSDKOutputSLZ(serializers.Serializer):


class SDKUsageExampleInputSLZ(serializers.Serializer):
language = serializers.ChoiceField(choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python")
language = serializers.ChoiceField(
choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python"
)
stage_name = serializers.CharField(help_text="网关环境名称")
resource_name = serializers.CharField(help_text="资源名称")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@


class SDKListInputSLZ(serializers.Serializer):
language = serializers.ChoiceField(choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python")
language = serializers.ChoiceField(
choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python"
)

class Meta:
ref_name = "apigateway.apis.web.docs.gateway.sdk.SDKListInputSLZ"
Expand Down Expand Up @@ -62,7 +64,9 @@ def get_released_stages(self, obj):


class SDKDocInputSLZ(serializers.Serializer):
language = serializers.ChoiceField(choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python")
language = serializers.ChoiceField(
choices=ProgrammingLanguageEnum.get_choices(), help_text="SDK 编程语言,如 python"
)

class Meta:
ref_name = "apigateway.apis.web.docs.gateway.sdk.SDKDocInputSLZ"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
class StageOutputSLZ(serializers.Serializer):
id = serializers.IntegerField(help_text="网关环境 ID")
name = serializers.CharField(help_text="网关环境名称")
description = SerializerTranslatedField(default_field="description_i18n", allow_blank=True, help_text="网关环境描述")
description = SerializerTranslatedField(
default_field="description_i18n", allow_blank=True, help_text="网关环境描述"
)

class Meta:
ref_name = "apigateway.apis.web.docs.gateway.stage.StageOutputSLZ"
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ def __call__(self, value: str):

for prefix in settings.RESERVED_GATEWAY_NAME_PREFIXES:
if value.startswith(prefix):
raise serializers.ValidationError(_("网关名不能以【{prefix}】开头,其为官方保留字。").format(prefix=prefix))
raise serializers.ValidationError(
_("网关名不能以【{prefix}】开头,其为官方保留字。").format(prefix=prefix)
)
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,6 @@ class AlarmStrategySummaryQuerySLZ(serializers.Serializer):
class AlarmRecordSummaryQueryOutputSLZ(serializers.Serializer):
gateway = serializers.DictField(read_only=True, help_text="网关")
alarm_record_count = serializers.IntegerField(read_only=True, help_text="告警记录总数")
strategy_summary = serializers.ListField(child=AlarmStrategySummaryQuerySLZ(), read_only=True, help_text="策略汇总")
strategy_summary = serializers.ListField(
child=AlarmStrategySummaryQuerySLZ(), read_only=True, help_text="策略汇总"
)
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def to_internal_value(self, payload: str) -> str:
for item in loaded_data["rates"].get("specials", []):
bk_app_code = item["bk_app_code"]
if bk_app_code in result["rates"]:
raise ValidationError({"bk_app_code": _(f"蓝鲸应用ID重复: {bk_app_code}").format(bk_app_code=bk_app_code)})
raise ValidationError(
{"bk_app_code": _(f"蓝鲸应用ID重复: {bk_app_code}").format(bk_app_code=bk_app_code)}
)

result["rates"][bk_app_code] = [{"period": item["period"], "tokens": item["tokens"]}]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@


@method_decorator(
name="get", decorator=swagger_auto_schema(tags=["WebAPI.Release"], operation_description="获取环境下可用的资源列表接口(在线调试)")
name="get",
decorator=swagger_auto_schema(
tags=["WebAPI.Release"], operation_description="获取环境下可用的资源列表接口(在线调试)"
),
)
class ReleaseAvailableResourceListApi(generics.ListAPIView):
lookup_field = "stage_id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def list(self, request, gateway_id: int, resource_id: int, *args, **kwargs):
"""获取指定资源的资源文档"""
queryset = self.get_queryset().filter(resource_id=resource_id)

docs = [doc for doc in queryset]
docs = list(queryset)
existed_languages = [doc.language for doc in docs]
missing_languages = set(DocLanguageEnum.get_values()) - set(existed_languages)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class ResourceQueryInputSLZ(serializers.Serializer):
method = serializers.CharField(allow_blank=True, required=False, help_text="资源请求方法,完整匹配")
label_ids = serializers.CharField(allow_blank=True, required=False, help_text="标签 ID,多个以逗号 , 分割")
backend_id = serializers.IntegerField(allow_null=True, required=False, help_text="后端服务 ID")
query = serializers.CharField(allow_blank=True, required=False, help_text="资源筛选条件,支持模糊匹配资源名称,前端请求路径")
query = serializers.CharField(
allow_blank=True, required=False, help_text="资源筛选条件,支持模糊匹配资源名称,前端请求路径"
)
order_by = serializers.ChoiceField(
choices=["-id", "name", "-name", "path", "-path", "updated_time", "-updated_time"],
allow_blank=True,
Expand Down Expand Up @@ -132,9 +134,15 @@ def get_has_updated(self, obj):


class ResourceAuthConfigSLZ(serializers.Serializer):
auth_verified_required = serializers.BooleanField(required=False, help_text="是否需要认证用户,true:需要,false:不需要")
app_verified_required = serializers.BooleanField(required=False, help_text="是否需要认证应用,true:需要,false:不需要")
resource_perm_required = serializers.BooleanField(required=False, help_text="是否需要校验资源权限,true:需要,false:不需要")
auth_verified_required = serializers.BooleanField(
required=False, help_text="是否需要认证用户,true:需要,false:不需要"
)
app_verified_required = serializers.BooleanField(
required=False, help_text="是否需要认证应用,true:需要,false:不需要"
)
resource_perm_required = serializers.BooleanField(
required=False, help_text="是否需要校验资源权限,true:需要,false:不需要"
)


class HttpBackendConfigSLZ(serializers.Serializer):
Expand All @@ -145,7 +153,9 @@ class HttpBackendConfigSLZ(serializers.Serializer):
max_value=MAX_BACKEND_TIMEOUT_IN_SECOND, min_value=0, required=False, help_text="超时时间"
)
# 1.13 版本: 兼容旧版 (api_version=0.1) 资源 yaml 通过 openapi 导入
legacy_upstreams = LegacyUpstreamsSLZ(allow_null=True, required=False, help_text="旧版 upstreams,管理端不需要处理")
legacy_upstreams = LegacyUpstreamsSLZ(
allow_null=True, required=False, help_text="旧版 upstreams,管理端不需要处理"
)
legacy_transform_headers = LegacyTransformHeadersSLZ(
allow_null=True, required=False, help_text="旧版 transform_headers,管理端不需要处理"
)
Expand Down Expand Up @@ -262,7 +272,9 @@ def _validate_method(self, gateway: Gateway, path: str, method: str):

if method == HTTP_METHOD_ANY:
if queryset.exists():
raise serializers.ValidationError(_("当前请求方法为 {method},但相同请求路径下,其它请求方法已存在。").format(method=method))
raise serializers.ValidationError(
_("当前请求方法为 {method},但相同请求路径下,其它请求方法已存在。").format(method=method)
)

elif queryset.filter(method=HTTP_METHOD_ANY).exists():
raise serializers.ValidationError(
Expand All @@ -274,7 +286,9 @@ def _validate_method(self, gateway: Gateway, path: str, method: str):

def _validate_match_subpath(self, data):
if data.get("match_subpath", False) != data["backend"]["config"].get("match_subpath", False):
raise serializers.ValidationError(_("资源前端配置中的【匹配所有子路径】与后端配置中的【追加匹配的子路径】值必需相同。"))
raise serializers.ValidationError(
_("资源前端配置中的【匹配所有子路径】与后端配置中的【追加匹配的子路径】值必需相同。")
)

def _exclude_current_instance(self, queryset):
if self.instance is not None:
Expand Down Expand Up @@ -498,9 +512,15 @@ class SelectedResourceSLZ(serializers.Serializer):
class ResourceImportInputSLZ(serializers.Serializer):
content = serializers.CharField(allow_blank=False, required=True, help_text="导入内容,yaml/json 格式字符串")
selected_resources = serializers.ListField(
child=SelectedResourceSLZ(), allow_empty=False, allow_null=True, required=False, help_text="导入时选中的资源列表"
child=SelectedResourceSLZ(),
allow_empty=False,
allow_null=True,
required=False,
help_text="导入时选中的资源列表",
)
delete = serializers.BooleanField(
default=False, help_text="是否删除未选中的资源,即已存在,但是未在 content 中的资源"
)
delete = serializers.BooleanField(default=False, help_text="是否删除未选中的资源,即已存在,但是未在 content 中的资源")

def validate(self, data):
data["resources"] = self._validate_content(data["content"])
Expand All @@ -513,12 +533,16 @@ def _validate_content(self, content: str):
try:
importer = ResourceSwaggerImporter(content)
except Exception as err:
raise serializers.ValidationError({"content": _("导入内容为无效的 json/yaml 数据,{err}。").format(err=err)})
raise serializers.ValidationError(
{"content": _("导入内容为无效的 json/yaml 数据,{err}。").format(err=err)}
)

try:
importer.validate()
except SchemaValidationError as err:
raise serializers.ValidationError({"content": _("导入内容不符合 swagger 2.0 协议,{err}。").format(err=err)})
raise serializers.ValidationError(
{"content": _("导入内容不符合 swagger 2.0 协议,{err}。").format(err=err)}
)

slz = ResourceDataImportSLZ(
data=importer.get_resources(),
Expand All @@ -533,7 +557,10 @@ def _validate_content(self, content: str):

class ResourceImportCheckInputSLZ(ResourceImportInputSLZ):
doc_language = serializers.ChoiceField(
choices=DocLanguageEnum.get_choices(), allow_blank=True, required=False, help_text="文档语言,en: 英文,zh: 中文"
choices=DocLanguageEnum.get_choices(),
allow_blank=True,
required=False,
help_text="文档语言,en: 英文,zh: 中文",
)


Expand Down Expand Up @@ -562,7 +589,9 @@ class ResourceExportInputSLZ(serializers.Serializer):
help_text="值为 all,不需其它参数;值为 filtered,支持 query/path/method/label_name 参数;值为 selected,支持 resource_ids 参数",
)
file_type = serializers.ChoiceField(
choices=SwaggerFormatEnum.get_choices(), default=SwaggerFormatEnum.YAML.value, help_text="导出的文件类型,如 yaml/json"
choices=SwaggerFormatEnum.get_choices(),
default=SwaggerFormatEnum.YAML.value,
help_text="导出的文件类型,如 yaml/json",
)
resource_filter_condition = ResourceQueryInputSLZ(
required=False, help_text="资源筛选条件,export_type 为 filtered 时,应提供当前的筛选条件"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def __call__(self, attrs):
duplicate_names = get_duplicate_items(var_names)
if duplicate_names:
raise serializers.ValidationError(
_("前端请求路径 {path} 中的路径变量 {var_name} 重复。").format(path=path, var_name=", ".join(duplicate_names))
_("前端请求路径 {path} 中的路径变量 {var_name} 重复。").format(
path=path, var_name=", ".join(duplicate_names)
)
)


Expand Down Expand Up @@ -87,7 +89,9 @@ def _parse_backend_path(self, backend_path: str) -> Tuple[List[str], List[str]]:
stage_path_vars.append(match.group(1))
continue

raise serializers.ValidationError(_("后端请求路径中的路径变量 {var_name} 不符合规则。").format(var_name=var_name))
raise serializers.ValidationError(
_("后端请求路径中的路径变量 {var_name} 不符合规则。").format(var_name=var_name)
)

return normal_path_vars, stage_path_vars

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def create(self, request, *args, **kwargs):
@method_decorator(
name="get",
decorator=swagger_auto_schema(
operation_description="获取指定资源信息", responses={status.HTTP_200_OK: ResourceOutputSLZ()}, tags=["WebAPI.Resource"]
operation_description="获取指定资源信息",
responses={status.HTTP_200_OK: ResourceOutputSLZ()},
tags=["WebAPI.Resource"],
),
)
@method_decorator(
Expand Down
Loading

0 comments on commit ea2f378

Please sign in to comment.