refactor: rearrange parser loop in dict
All checks were successful
build / build (pull_request) Successful in 2m30s
build / build (push) Successful in 2m33s

This commit is contained in:
李衍志523370910113 2025-03-04 14:46:05 +08:00
parent 275f9f981c
commit 6bfefd4f7b
2 changed files with 68 additions and 82 deletions

View File

@ -1,8 +1,6 @@
import re import re
import shlex import shlex
from typing import Callable, Dict, List, Set, Tuple from typing import Any, Callable, Dict, List, Set, Tuple
from pydantic import BaseModel
from joj3_config_generator.models import result, task from joj3_config_generator.models import result, task
from joj3_config_generator.models.const import JOJ3_CONFIG_ROOT from joj3_config_generator.models.const import JOJ3_CONFIG_ROOT
@ -32,37 +30,34 @@ def get_conf_stage(
] ]
), ),
) )
keyword_parser = ["clangtidy", "keyword", "cppcheck", "cpplint"] processed_dict = get_processed_dict(task_stage)
dummy_parser = ["dummy", "result-status"] for idx, parser in enumerate(task_stage.parsers):
for parser in task_stage.parsers: if parser in processed_dict or parser.replace("-", "_") in processed_dict:
if parser in keyword_parser: fn, parser_model = processed_dict[parser]
fix_keyword(task_stage, conf_stage, parser) fn(parser_model, conf_stage.parsers[idx])
elif parser in dummy_parser:
fix_dummy(task_stage, conf_stage, parser)
elif parser == "result-detail":
fix_result_detail(task_stage, conf_stage, parser)
elif parser == "file":
fix_file(task_stage, conf_stage, parser)
elif parser == "diff": elif parser == "diff":
fix_diff(task_stage, task_conf, conf_stage, parser) fix_diff(task_stage, task_conf, conf_stage.parsers[idx], conf_stage)
else: else:
continue continue
return conf_stage return conf_stage
# def get_processed_dict(task_stage: task.Stagew) -> Dict[str, Tuple[Callable[[task.Stage, BaseModel], None], BaseModel]]: def get_processed_dict(
# processed_dict: Dict[str, Tuple[Callable[[task.Stage, task.StageDetail], None], BaseModel]] = { task_stage: task.Stage,
# "clang-tidy": (fix_keyword, result.clang-tidy), ) -> Dict[str, Tuple[Callable[[Any, result.ParserConfig], None], Any]]:
# "keyword": (fix_keyword, result.KeywordConfig), processed_dict: Dict[
# "cppcheck": (fix_keyword, result.KeywordConfig), str, Tuple[Callable[[Any, result.ParserConfig], None], Any]
# "cpplint": (fix_keyword, result.KeywordConfig), ] = {
# "result-detail": (fix_result_detail, result.ResultDetailConfig), "clangtidy": (fix_keyword, task_stage.clangtidy),
# "dummy": (fix_dummy, result.DummyConfig), "keyword": (fix_keyword, task_stage.keyword),
# "result-status": (fix_dummy, result.DummyConfig), "cppcheck": (fix_keyword, task_stage.cppcheck),
# "file": (fix_file, result.FileConfig), "cpplint": (fix_keyword, task_stage.cpplint),
# "diff": (fix_diff, result.DiffConfig), "result-detail": (fix_result_detail, task_stage.result_detail),
# } "dummy": (fix_dummy, task_stage.dummy),
# return processed_dict "result-status": (fix_dummy, task_stage.result_status),
"file": (fix_file, task_stage.file),
}
return processed_dict
def get_executor_with(task_stage: task.Stage, cached: Set[str]) -> result.ExecutorWith: def get_executor_with(task_stage: task.Stage, cached: Set[str]) -> result.ExecutorWith:
@ -96,21 +91,17 @@ def get_executor_with(task_stage: task.Stage, cached: Set[str]) -> result.Execut
def fix_keyword( def fix_keyword(
task_stage: task.Stage, conf_stage: result.StageDetail, parser: str keyword_config: task.ParserKeyword, keyword_parser_: result.ParserConfig
) -> result.StageDetail: ) -> None:
keyword_parser_ = next(p for p in conf_stage.parsers if p.name == parser)
keyword_weight: List[result.KeywordConfig] = [] keyword_weight: List[result.KeywordConfig] = []
if parser in task_stage.__dict__: unique_weight = list(set(keyword_config.weight))
unique_weight = list(set(task_stage.__dict__[parser].weight))
for score in unique_weight: for score in unique_weight:
keyword_weight.append(result.KeywordConfig(keywords=[], score=score)) keyword_weight.append(result.KeywordConfig(keywords=[], score=score))
for idx, score in enumerate(unique_weight): for idx, score in enumerate(unique_weight):
for idx_, score_ in enumerate(task_stage.__dict__[parser].weight): for idx_, score_ in enumerate(keyword_config.weight):
if score == score_: if score == score_:
keyword_weight[idx].keywords.append( keyword_weight[idx].keywords.append(keyword_config.keyword[idx_])
task_stage.__dict__[parser].keyword[idx_]
)
else: else:
continue continue
@ -120,64 +111,59 @@ def fix_keyword(
).model_dump(by_alias=True) ).model_dump(by_alias=True)
) )
return conf_stage
def fix_result_detail( def fix_result_detail(
task_stage: task.Stage, conf_stage: result.StageDetail, parser: str result_detail_parser_config: task.ParserResultDetail,
result_detail_parser: result.ParserConfig,
) -> None: ) -> None:
result_detail_parser = next(p for p in conf_stage.parsers if p.name == parser)
show_files = [] show_files = []
if task_stage.result_detail.stdout: if result_detail_parser_config.stdout:
show_files.append("stdout") show_files.append("stdout")
if task_stage.result_detail.stderr: if result_detail_parser_config.stderr:
show_files.append("stderr") show_files.append("stderr")
result_detail_parser.with_.update( result_detail_parser.with_.update(
result.ResultDetailConfig( result.ResultDetailConfig(
score=0, score=0,
comment="", comment="",
show_files=show_files, show_files=show_files,
show_exit_status=task_stage.result_detail.exitstatus, show_exit_status=result_detail_parser_config.exitstatus,
show_runtime=task_stage.result_detail.time, show_runtime=result_detail_parser_config.time,
show_memory=task_stage.result_detail.mem, show_memory=result_detail_parser_config.mem,
).model_dump(by_alias=True) ).model_dump(by_alias=True)
) )
def fix_dummy( def fix_dummy(
task_stage: task.Stage, conf_stage: result.StageDetail, parser: str dummy_parser_config: task.ParserDummy, dummy_parser: result.ParserConfig
) -> None: ) -> None:
dummy_parser_ = next(p for p in conf_stage.parsers if p.name == parser) # we don't use dummy parser in real application
if parser.replace("-", "_") not in task_stage.__dict__: if dummy_parser_config is None:
return return
if task_stage.result_status is None: dummy_parser.with_.update(
return
dummy_parser_.with_.update(
result.DummyConfig( result.DummyConfig(
score=task_stage.result_status.score, score=dummy_parser_config.score,
comment=task_stage.result_status.comment, comment=dummy_parser_config.comment,
force_quit_on_not_accepted=task_stage.result_status.force_quit, force_quit_on_not_accepted=dummy_parser_config.force_quit,
).model_dump(by_alias=True) ).model_dump(by_alias=True)
) )
return return
def fix_file( def fix_file(
task_stage: task.Stage, conf_stage: result.StageDetail, parser: str file_parser_config: task.ParserFile, file_parser: result.ParserConfig
) -> None: ) -> None:
file_parser_ = next(p for p in conf_stage.parsers if p.name == parser) file_parser.with_.update(
file_parser_.with_.update( result.FileConfig(name=file_parser_config.name).model_dump(by_alias=True)
result.FileConfig(name=task_stage.file.name).model_dump(by_alias=True)
) )
def fix_diff( def fix_diff(
task_stage: task.Stage, task_stage: task.Stage,
task_conf: task.Config, task_conf: task.Config,
diff_parser_config: result.ParserConfig,
conf_stage: result.StageDetail, conf_stage: result.StageDetail,
parser: str,
) -> None: ) -> None:
diff_parser = next((p for p in conf_stage.parsers if p.name == parser), None) diff_parser = diff_parser_config
skip = task_stage.skip skip = task_stage.skip
cases = task_stage.cases cases = task_stage.cases
finalized_cases = [case for case in cases if case not in skip] finalized_cases = [case for case in cases if case not in skip]

View File

@ -222,11 +222,11 @@
} }
}, },
"copyInCached": { "copyInCached": {
"h7/build/compile_commands.json": "h7/build/compile_commands.json", "h7/build/ex2": "h7/build/ex2",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-ubsan": "h7/build/ex2-ubsan", "h7/build/ex2-ubsan": "h7/build/ex2-ubsan",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-msan": "h7/build/ex2-msan", "h7/build/ex2-msan": "h7/build/ex2-msan",
"h7/build/ex2": "h7/build/ex2" "h7/build/compile_commands.json": "h7/build/compile_commands.json"
}, },
"copyInDir": ".", "copyInDir": ".",
"copyOut": [ "copyOut": [
@ -322,11 +322,11 @@
} }
}, },
"copyInCached": { "copyInCached": {
"h7/build/compile_commands.json": "h7/build/compile_commands.json", "h7/build/ex2": "h7/build/ex2",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-ubsan": "h7/build/ex2-ubsan", "h7/build/ex2-ubsan": "h7/build/ex2-ubsan",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-msan": "h7/build/ex2-msan", "h7/build/ex2-msan": "h7/build/ex2-msan",
"h7/build/ex2": "h7/build/ex2" "h7/build/compile_commands.json": "h7/build/compile_commands.json"
}, },
"copyInDir": ".", "copyInDir": ".",
"copyOut": [ "copyOut": [
@ -444,11 +444,11 @@
"cpuSetLimit": "", "cpuSetLimit": "",
"copyIn": {}, "copyIn": {},
"copyInCached": { "copyInCached": {
"h7/build/compile_commands.json": "h7/build/compile_commands.json", "h7/build/ex2": "h7/build/ex2",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-ubsan": "h7/build/ex2-ubsan", "h7/build/ex2-ubsan": "h7/build/ex2-ubsan",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-msan": "h7/build/ex2-msan", "h7/build/ex2-msan": "h7/build/ex2-msan",
"h7/build/ex2": "h7/build/ex2" "h7/build/compile_commands.json": "h7/build/compile_commands.json"
}, },
"copyInDir": ".", "copyInDir": ".",
"copyOut": [ "copyOut": [
@ -587,11 +587,11 @@
"cpuSetLimit": "", "cpuSetLimit": "",
"copyIn": {}, "copyIn": {},
"copyInCached": { "copyInCached": {
"h7/build/compile_commands.json": "h7/build/compile_commands.json", "h7/build/ex2": "h7/build/ex2",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-ubsan": "h7/build/ex2-ubsan", "h7/build/ex2-ubsan": "h7/build/ex2-ubsan",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-msan": "h7/build/ex2-msan", "h7/build/ex2-msan": "h7/build/ex2-msan",
"h7/build/ex2": "h7/build/ex2" "h7/build/compile_commands.json": "h7/build/compile_commands.json"
}, },
"copyInDir": ".", "copyInDir": ".",
"copyOut": [ "copyOut": [
@ -684,11 +684,11 @@
"cpuSetLimit": "", "cpuSetLimit": "",
"copyIn": {}, "copyIn": {},
"copyInCached": { "copyInCached": {
"h7/build/compile_commands.json": "h7/build/compile_commands.json", "h7/build/ex2": "h7/build/ex2",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-ubsan": "h7/build/ex2-ubsan", "h7/build/ex2-ubsan": "h7/build/ex2-ubsan",
"h7/build/ex2-asan": "h7/build/ex2-asan",
"h7/build/ex2-msan": "h7/build/ex2-msan", "h7/build/ex2-msan": "h7/build/ex2-msan",
"h7/build/ex2": "h7/build/ex2" "h7/build/compile_commands.json": "h7/build/compile_commands.json"
}, },
"copyInDir": ".", "copyInDir": ".",
"copyOut": [ "copyOut": [