Skip to content

Commit 0801a4c

Browse files
committed
i think it's working now
Signed-off-by: Nick Mitchell <[email protected]>
1 parent 6f5508f commit 0801a4c

File tree

8 files changed

+164
-38
lines changed

8 files changed

+164
-38
lines changed

pdl-live-react/src-tauri/src/cli/run.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn run_pdl_program(
2727
data: Option<&tauri_plugin_cli::ArgData>,
2828
stream: Option<&tauri_plugin_cli::ArgData>,
2929
) -> Result<(), tauri::Error> {
30-
println!(
30+
eprintln!(
3131
"Running {:#?}",
3232
Path::new(&source_file_path).file_name().unwrap()
3333
);
@@ -78,7 +78,6 @@ pub fn run_pdl_program(
7878
_ => source_file_path.clone(),
7979
};
8080

81-
println!("SRC {} -> {}", source_file_path, updated_source_file_path);
8281
let mut args = vec![
8382
updated_source_file_path.clone(),
8483
dashdash("--trace", trace_file),
@@ -89,9 +88,9 @@ pub fn run_pdl_program(
8988
cmd(bin_path.join("pdl"), &args).run()?;
9089

9190
// TODO how do we do this on all exit paths in rust?
92-
/*if updated_source_file_path != source_file_path {
91+
if updated_source_file_path != source_file_path {
9392
remove_file(updated_source_file_path)?;
94-
}*/
93+
}
9594

9695
Ok(())
9796
}

pdl-live-react/src-tauri/src/interpreter/extract.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use ::std::fs::write;
2-
3-
use tempfile::Builder;
41
use yaml_rust2::Yaml;
52

63
use crate::interpreter::shasum;
@@ -42,25 +39,28 @@ pub fn extract_requirements(program: &Yaml) -> (Vec<String>, Yaml) {
4239
} else {
4340
"".to_string()
4441
};
45-
let code_hash = shasum::sha256sum_str(&code_text.as_str()).unwrap();
42+
//let code_hash = shasum::sha256sum_str(&code_text.as_str()).unwrap();
4643

47-
let tmp = Builder::new()
44+
/*let tmp = Builder::new()
4845
.prefix(&format!("pdl-program-{}", code_hash))
4946
.suffix(".pdl")
5047
.tempfile()
5148
.unwrap(); // TODO tmpfile_in(source dir)
5249
write(&tmp, code_text).unwrap();
53-
let (_, tmp_path) = tmp.keep().unwrap();
50+
let (_, tmp_path) = tmp.keep().unwrap();*/
5451

5552
h.remove(&requirements);
5653
h[&lang] = Yaml::String("command".to_string());
57-
h.insert(Yaml::String("file".to_string()), Yaml::Boolean(true));
58-
h[&code] = Yaml::String(format!(
59-
"\"/Users/nickm/Library/Caches/pdl/venvs/{}/{}/python {}\"",
60-
req_hash,
61-
if cfg!(windows) { "Scripts" } else { "bin" },
62-
tmp_path.display(),
63-
));
54+
//h.insert(&Yaml::String("file".to_string()), Yaml::Boolean(true));
55+
h[&code] = Yaml::Array(vec![
56+
Yaml::String(format!(
57+
"/Users/nickm/Library/Caches/pdl/venvs/{}/{}/python",
58+
req_hash,
59+
if cfg!(windows) { "Scripts" } else { "bin" },
60+
)),
61+
Yaml::String("-c".to_owned()),
62+
Yaml::String(code_text),
63+
]);
6464

6565
Yaml::Hash(h.clone())
6666
}

pdl-live-react/src/pdl_ast.d.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,32 @@ export type Code =
19521952
| ImportBlock
19531953
| ErrorBlock
19541954
| EmptyBlock
1955+
| (
1956+
| boolean
1957+
| number
1958+
| string
1959+
| FunctionBlock
1960+
| CallBlock
1961+
| LitellmModelBlock
1962+
| GraniteioModelBlock
1963+
| CodeBlock
1964+
| GetBlock
1965+
| DataBlock
1966+
| IfBlock
1967+
| MatchBlock
1968+
| RepeatBlock
1969+
| TextBlock
1970+
| LastOfBlock
1971+
| ArrayBlock
1972+
| ObjectBlock
1973+
| MessageBlock
1974+
| ReadBlock
1975+
| IncludeBlock
1976+
| ImportBlock
1977+
| ErrorBlock
1978+
| EmptyBlock
1979+
| null
1980+
)[]
19551981
| null
19561982
/**
19571983
* Name of the variable used to store the result of the execution of the block.

src/pdl/pdl-schema.json

+82
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,88 @@
13991399
{
14001400
"$ref": "#/$defs/EmptyBlock"
14011401
},
1402+
{
1403+
"items": {
1404+
"anyOf": [
1405+
{
1406+
"type": "boolean"
1407+
},
1408+
{
1409+
"type": "integer"
1410+
},
1411+
{
1412+
"type": "number"
1413+
},
1414+
{
1415+
"type": "string"
1416+
},
1417+
{
1418+
"$ref": "#/$defs/FunctionBlock"
1419+
},
1420+
{
1421+
"$ref": "#/$defs/CallBlock"
1422+
},
1423+
{
1424+
"$ref": "#/$defs/LitellmModelBlock"
1425+
},
1426+
{
1427+
"$ref": "#/$defs/GraniteioModelBlock"
1428+
},
1429+
{
1430+
"$ref": "#/$defs/CodeBlock"
1431+
},
1432+
{
1433+
"$ref": "#/$defs/GetBlock"
1434+
},
1435+
{
1436+
"$ref": "#/$defs/DataBlock"
1437+
},
1438+
{
1439+
"$ref": "#/$defs/IfBlock"
1440+
},
1441+
{
1442+
"$ref": "#/$defs/MatchBlock"
1443+
},
1444+
{
1445+
"$ref": "#/$defs/RepeatBlock"
1446+
},
1447+
{
1448+
"$ref": "#/$defs/TextBlock"
1449+
},
1450+
{
1451+
"$ref": "#/$defs/LastOfBlock"
1452+
},
1453+
{
1454+
"$ref": "#/$defs/ArrayBlock"
1455+
},
1456+
{
1457+
"$ref": "#/$defs/ObjectBlock"
1458+
},
1459+
{
1460+
"$ref": "#/$defs/MessageBlock"
1461+
},
1462+
{
1463+
"$ref": "#/$defs/ReadBlock"
1464+
},
1465+
{
1466+
"$ref": "#/$defs/IncludeBlock"
1467+
},
1468+
{
1469+
"$ref": "#/$defs/ImportBlock"
1470+
},
1471+
{
1472+
"$ref": "#/$defs/ErrorBlock"
1473+
},
1474+
{
1475+
"$ref": "#/$defs/EmptyBlock"
1476+
},
1477+
{
1478+
"type": "null"
1479+
}
1480+
]
1481+
},
1482+
"type": "array"
1483+
},
14021484
{
14031485
"type": "null"
14041486
}

src/pdl/pdl_ast.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -472,19 +472,18 @@ class CodeBlock(LeafBlock):
472472
requirements: Optional[str | list[str]] = None
473473
"""Pip requirements.txt
474474
"""
475-
code: "BlockType"
475+
code: "BlockOrBlocksType"
476476
"""Code to execute.
477477
"""
478-
file: Optional[bool] = None
479-
"""Code to execute is a file path.
480-
"""
481478

482479
@model_validator(mode="after")
483480
def lang_is_python(self):
484481
if self.requirements is not None and self.lang != "python":
485482
raise ValueError(
486483
"CodeBlock requirements field provided for non-python block"
487484
)
485+
if isinstance(self.code, list) and self.lang != "command":
486+
raise ValueError("CodeBlock code field is array for non-command block")
488487
return self
489488

490489

src/pdl/pdl_ast_utils.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def iter_block_children(f: Callable[[BlockType], None], block: BlockType) -> Non
4848
if block.pdl__trace is not None:
4949
f(block.pdl__trace)
5050
case CodeBlock():
51-
f(block.code)
51+
if isinstance(block.code, list):
52+
for b in block.code:
53+
f(b)
54+
else:
55+
f(block.code)
5256
case GetBlock():
5357
pass
5458
case DataBlock():
@@ -150,7 +154,10 @@ def map_block_children(f: MappedFunctions, block: BlockType) -> BlockType:
150154
if block.parameters is not None:
151155
block.parameters = f.f_expr(block.parameters)
152156
case CodeBlock():
153-
block.code = f.f_block(block.code)
157+
if isinstance(block.code, list):
158+
block.code = [f.f_block(b) for b in block.code]
159+
else:
160+
block.code = f.f_block(block.code)
154161
case GetBlock():
155162
pass
156163
case DataBlock():

src/pdl/pdl_dumper.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ def block_to_dict( # noqa: C901
135135
d["modelResponse"] = block.modelResponse
136136
case CodeBlock():
137137
d["lang"] = block.lang
138-
d["code"] = block_to_dict(block.code, json_compatible)
138+
if isinstance(block.code, list):
139+
d["code"] = [block_to_dict(b, json_compatible) for b in block.code]
140+
else:
141+
d["code"] = block_to_dict(block.code, json_compatible)
139142
d["requirements"] = block.requirements
140143
case GetBlock():
141144
d["get"] = block.get

src/pdl/pdl_interpreter.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -1431,14 +1431,24 @@ def process_call_code(
14311431
state: InterpreterState, scope: ScopeType, block: CodeBlock, loc: PdlLocationType
14321432
) -> tuple[PdlLazy[Any], LazyMessages, ScopeType, CodeBlock]:
14331433
background: LazyMessages
1434-
code_, _, _, block = process_block_of(
1435-
block,
1436-
"code",
1437-
state.with_yield_result(False).with_yield_background(False),
1438-
scope,
1439-
loc,
1440-
)
1441-
code_s = code_.result()
1434+
code_a = None
1435+
if isinstance(block.code, list):
1436+
code_s = ""
1437+
code_a, _, _, _ = process_block(
1438+
state.with_yield_result(False).with_yield_background(False),
1439+
scope,
1440+
ArrayBlock(array=block.code),
1441+
loc,
1442+
)
1443+
else:
1444+
code_, _, _, block = process_block_of(
1445+
block,
1446+
"code",
1447+
state.with_yield_result(False).with_yield_background(False),
1448+
scope,
1449+
loc,
1450+
)
1451+
code_s = code_.result()
14421452
match block.lang:
14431453
case "python":
14441454
try:
@@ -1456,7 +1466,7 @@ def process_call_code(
14561466
) from exc
14571467
case "command":
14581468
try:
1459-
result = call_command(code_s, block.file == True)
1469+
result = call_command(code_s, code_a)
14601470
background = PdlList(
14611471
[
14621472
PdlDict( # type: ignore
@@ -1530,11 +1540,11 @@ def call_python(code: str, scope: ScopeType) -> PdlLazy[Any]:
15301540
return PdlConst(result)
15311541

15321542

1533-
def call_command(code: str, is_file: bool) -> PdlLazy[str]:
1534-
if is_file:
1535-
with open(code, "r") as f:
1536-
code = f.read()
1537-
args = shlex.split(code)
1543+
def call_command(code: str, code_a: PdlLazy[list[str]] | None) -> PdlLazy[str]:
1544+
if code_a is not None and isinstance(code_a.result(), list):
1545+
args = code_a.result()
1546+
else:
1547+
args = shlex.split(code)
15381548
p = subprocess.run(
15391549
args, capture_output=True, text=True, check=False, shell=False
15401550
) # nosec B603

0 commit comments

Comments
 (0)