mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2026-09-02 21:03:34 +03:00
Add defaults to calculations for ToolIO (#7289)
# Description of Changes Currently when calculating the output file type for some tools, the system will get it wrong because it doesn't know about what the default parameters in tools are, so if it doesn't have a value for some key, it'll just bail out and say "it might not be compatible". This PR adds logic to `ToolIO` to read the default values set for the parameters if the tool has `ToolIOCase`s and takes them into account when figuring out the output type. I've built it with horrible Java reflection magic to avoid having to specify the default for params twice, which will make it impossible for the defaults to disagree with each other. This just runs once at startup so there's negligible performance impact. The change is easily tested with Change Parameters, which is just `add-password` behind the scenes but with the password params omitted (so Change Password is always PDF->PDF, never encrypted like Add Password). Also (somewhat hackily) fixes a bug I noticed where saving a Change Permissions step then leaving and returning to the pipeline will cause the step to be reloaded as Add Password. I've added a system to disambiguate tools which share the same endpoint (which is only these two currently). ## Currently <img width="455" height="135" alt="image" src="https://github.com/user-attachments/assets/c8867d6a-599b-4f21-a2db-4a1b6ac22d73" /> ## Now <img width="415" height="122" alt="image" src="https://github.com/user-attachments/assets/bd8d1bab-f00a-421b-8c91-5af0d2ad5335" />
This commit is contained in:
@@ -65,6 +65,8 @@ class ToolIOWhen(ApiModel):
|
||||
|
||||
param: str
|
||||
matches: list[str]
|
||||
# The value the endpoint uses when this parameter is absent; None when it has none.
|
||||
default: str | None = None
|
||||
|
||||
|
||||
class ToolIOCase(ApiModel):
|
||||
@@ -378,7 +380,10 @@ def collect_tool_io(spec: dict[str, Any]) -> dict[str, dict[str, Any]]:
|
||||
|
||||
|
||||
def _render_when(condition: dict[str, Any]) -> str:
|
||||
return f"ToolIOWhen(param={json.dumps(condition['param'])}, matches={json.dumps(condition['matches'])})"
|
||||
parts = [f"param={json.dumps(condition['param'])}", f"matches={json.dumps(condition['matches'])}"]
|
||||
if "default" in condition:
|
||||
parts.append(f"default={json.dumps(condition['default'])}")
|
||||
return f"ToolIOWhen({', '.join(parts)})"
|
||||
|
||||
|
||||
def _render_case(case: dict[str, Any]) -> str:
|
||||
|
||||
@@ -58,6 +58,8 @@ class ToolIOWhen(ApiModel):
|
||||
|
||||
param: str
|
||||
matches: list[str]
|
||||
# The value the endpoint uses when this parameter is absent; None when it has none.
|
||||
default: str | None = None
|
||||
|
||||
|
||||
class ToolIOCase(ApiModel):
|
||||
@@ -101,7 +103,7 @@ TOOL_IO: dict[ToolEndpoint, ToolIOSpec] = {
|
||||
arity=ToolArity.SIMO,
|
||||
cases=[
|
||||
ToolIOCase(
|
||||
when=[ToolIOWhen(param="singleOrMultiple", matches=["single"])],
|
||||
when=[ToolIOWhen(param="singleOrMultiple", matches=["single"], default="multiple")],
|
||||
produces=ToolFormat.IMAGE,
|
||||
arity=ToolArity.SISO,
|
||||
)
|
||||
@@ -130,15 +132,19 @@ TOOL_IO: dict[ToolEndpoint, ToolIOSpec] = {
|
||||
arity=ToolArity.SISO,
|
||||
cases=[
|
||||
ToolIOCase(
|
||||
when=[ToolIOWhen(param="outputFormat", matches=["ps"])],
|
||||
when=[ToolIOWhen(param="outputFormat", matches=["ps"], default="eps")],
|
||||
produces=ToolFormat.POSTSCRIPT,
|
||||
arity=ToolArity.SISO,
|
||||
),
|
||||
ToolIOCase(
|
||||
when=[ToolIOWhen(param="outputFormat", matches=["pcl"])], produces=ToolFormat.PCL, arity=ToolArity.SISO
|
||||
when=[ToolIOWhen(param="outputFormat", matches=["pcl"], default="eps")],
|
||||
produces=ToolFormat.PCL,
|
||||
arity=ToolArity.SISO,
|
||||
),
|
||||
ToolIOCase(
|
||||
when=[ToolIOWhen(param="outputFormat", matches=["xps"])], produces=ToolFormat.XPS, arity=ToolArity.SISO
|
||||
when=[ToolIOWhen(param="outputFormat", matches=["xps"], default="eps")],
|
||||
produces=ToolFormat.XPS,
|
||||
arity=ToolArity.SISO,
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -151,7 +157,7 @@ TOOL_IO: dict[ToolEndpoint, ToolIOSpec] = {
|
||||
arity=ToolArity.MIMO,
|
||||
cases=[
|
||||
ToolIOCase(
|
||||
when=[ToolIOWhen(param="combineIntoSinglePdf", matches=["true"])],
|
||||
when=[ToolIOWhen(param="combineIntoSinglePdf", matches=["true"], default="false")],
|
||||
produces=ToolFormat.PDF,
|
||||
arity=ToolArity.MISO,
|
||||
)
|
||||
@@ -202,7 +208,9 @@ TOOL_IO: dict[ToolEndpoint, ToolIOSpec] = {
|
||||
arity=ToolArity.SISO,
|
||||
cases=[
|
||||
ToolIOCase(
|
||||
when=[ToolIOWhen(param="dryRun", matches=["true"])], produces=ToolFormat.JSON, arity=ToolArity.SISO
|
||||
when=[ToolIOWhen(param="dryRun", matches=["true"], default="false")],
|
||||
produces=ToolFormat.JSON,
|
||||
arity=ToolArity.SISO,
|
||||
)
|
||||
],
|
||||
),
|
||||
@@ -223,7 +231,9 @@ TOOL_IO: dict[ToolEndpoint, ToolIOSpec] = {
|
||||
arity=ToolArity.SISO,
|
||||
cases=[
|
||||
ToolIOCase(
|
||||
when=[ToolIOWhen(param="sidecar", matches=["true"])], produces=ToolFormat.ZIP, arity=ToolArity.SISO
|
||||
when=[ToolIOWhen(param="sidecar", matches=["true"], default="false")],
|
||||
produces=ToolFormat.ZIP,
|
||||
arity=ToolArity.SISO,
|
||||
)
|
||||
],
|
||||
),
|
||||
@@ -242,7 +252,10 @@ TOOL_IO: dict[ToolEndpoint, ToolIOSpec] = {
|
||||
arity=ToolArity.SISO,
|
||||
cases=[
|
||||
ToolIOCase(
|
||||
when=[ToolIOWhen(param="password", matches=[""]), ToolIOWhen(param="ownerPassword", matches=[""])],
|
||||
when=[
|
||||
ToolIOWhen(param="password", matches=[""], default=""),
|
||||
ToolIOWhen(param="ownerPassword", matches=[""], default=""),
|
||||
],
|
||||
produces=ToolFormat.PDF,
|
||||
arity=ToolArity.SISO,
|
||||
)
|
||||
|
||||
@@ -89,11 +89,16 @@ def resolve_output(spec: ToolIOSpec, parameters: dict[str, object] | None) -> Re
|
||||
for rule in spec.cases:
|
||||
all_hold = True
|
||||
for condition in rule.when:
|
||||
if parameters is None or condition.param not in parameters:
|
||||
if parameters is not None and condition.param in parameters:
|
||||
raw: object = parameters[condition.param]
|
||||
elif condition.default is not None:
|
||||
# The caller omitted it, so it takes the endpoint's default.
|
||||
raw = condition.default
|
||||
else:
|
||||
saw_unknown_param = True
|
||||
all_hold = False
|
||||
continue
|
||||
normalised = _normalise(parameters[condition.param])
|
||||
normalised = _normalise(raw)
|
||||
all_hold = all_hold and any(_normalise(m) == normalised for m in condition.matches)
|
||||
if all_hold:
|
||||
return ResolvedOutput(format=rule.produces, arity=rule.arity, certain=True)
|
||||
|
||||
Reference in New Issue
Block a user