nkparser 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nkparser
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Parse, query, and serialize Foundry Nuke .nk script files
5
5
  Author-email: Virgil Sisoe <28490646+sisoe24@users.noreply.github.com>
6
6
  Requires-Python: >=3.10
@@ -240,43 +240,19 @@ def cmd_nodes(args: argparse.Namespace) -> int:
240
240
  )
241
241
  col_w[col] = min(_MAX_W, max(len(col), max_val))
242
242
 
243
- if getattr(args, "format", "text") == "json":
244
- def _node_dict(node: Node) -> dict[str, Any]:
245
- d: dict[str, Any] = {
246
- "class": node.class_name,
247
- "name": node.name,
248
- "xpos": int(node.xpos) if node.xpos is not None else None,
249
- "ypos": int(node.ypos) if node.ypos is not None else None,
250
- }
251
- if extra_cols:
252
- d["knobs"] = {c: node.get(c)
253
- for c in extra_cols if node.knob(c)}
254
- return d
243
+ knobs_arg = extra_cols if extra_cols else None
255
244
 
245
+ if getattr(args, "format", "text") == "json":
256
246
  out: dict[str, Any] = {
257
247
  "file": args.file,
258
- "nodes": [_node_dict(n) for n in nodes],
248
+ "nodes": [n.to_dict(knobs=knobs_arg) for n in nodes],
259
249
  "count": len(nodes),
260
250
  }
261
251
  print(json.dumps(out, indent=2))
262
252
  return _exit_code(script)
263
253
 
264
254
  if getattr(args, "format", "text") == "csv":
265
- buf = io.StringIO()
266
- headers = ["class", "name", "xpos", "ypos"] + extra_cols
267
- writer = csv.DictWriter(buf, fieldnames=headers)
268
- writer.writeheader()
269
- for node in nodes:
270
- row: dict[str, Any] = {
271
- "class": node.class_name,
272
- "name": node.name,
273
- "xpos": int(node.xpos) if node.xpos is not None else "",
274
- "ypos": int(node.ypos) if node.ypos is not None else "",
275
- }
276
- for c in extra_cols:
277
- row[c] = node.get(c)
278
- writer.writerow(row)
279
- print(buf.getvalue(), end="")
255
+ print(script.to_csv(nodes=nodes, knobs=knobs_arg), end="")
280
256
  return _exit_code(script)
281
257
 
282
258
  parts = [
@@ -216,6 +216,29 @@ class Node:
216
216
 
217
217
  return Serializer._serialize_node(self)
218
218
 
219
+ def to_dict(self, knobs: Optional[list[str]] = None) -> dict:
220
+ """Return a dict representation of this node.
221
+
222
+ Parameters
223
+ ----------
224
+ knobs:
225
+ ``None`` — omit extra knobs (default).
226
+ ``["*"]`` — include all knobs as a nested dict.
227
+ ``[...]`` — include only the named knobs.
228
+ """
229
+ d: dict = {
230
+ "class": self.class_name,
231
+ "name": self.name,
232
+ "xpos": int(self.xpos) if self.xpos is not None else None,
233
+ "ypos": int(self.ypos) if self.ypos is not None else None,
234
+ }
235
+ if knobs is not None:
236
+ cols = (
237
+ [k.name for k in self.knobs] if knobs == ["*"] else knobs
238
+ )
239
+ d["knobs"] = {c: self.get(c) for c in cols if self.knob(c)}
240
+ return d
241
+
219
242
  def __repr__(self) -> str:
220
243
  return f"<Node {self.class_name!r} name={self.name!r}>"
221
244
 
@@ -398,6 +421,77 @@ class Script:
398
421
  with open(path, "w", encoding="utf-8") as fh:
399
422
  fh.write(self.to_nk())
400
423
 
424
+ def to_json(
425
+ self,
426
+ nodes: Optional[list[Node]] = None,
427
+ knobs: Optional[list[str]] = None,
428
+ ) -> str:
429
+ """Return a JSON array string of nodes.
430
+
431
+ Parameters
432
+ ----------
433
+ nodes:
434
+ Subset of nodes to include; defaults to ``all_nodes()``.
435
+ knobs:
436
+ ``None`` — omit extra knobs (default).
437
+ ``["*"]`` — include all knobs per node as a nested dict.
438
+ ``[...]`` — include only the named knobs.
439
+ """
440
+ import json
441
+
442
+ target = nodes if nodes is not None else list(self.all_nodes())
443
+ return json.dumps([n.to_dict(knobs=knobs) for n in target], indent=2)
444
+
445
+ def to_csv(
446
+ self,
447
+ nodes: Optional[list[Node]] = None,
448
+ knobs: Optional[list[str]] = None,
449
+ ) -> str:
450
+ """Return a CSV string of nodes.
451
+
452
+ Parameters
453
+ ----------
454
+ nodes:
455
+ Subset of nodes to include; defaults to ``all_nodes()``.
456
+ knobs:
457
+ ``None`` — only class/name/xpos/ypos columns (default).
458
+ ``["*"]`` — add a column for every knob found across all nodes.
459
+ ``[...]`` — add columns for the named knobs only.
460
+ """
461
+ import csv
462
+ import io
463
+
464
+ _FIXED = {"name", "xpos", "ypos"}
465
+ target = nodes if nodes is not None else list(self.all_nodes())
466
+
467
+ extra_cols: list[str] = []
468
+ if knobs is not None:
469
+ if knobs == ["*"]:
470
+ seen: dict[str, None] = {}
471
+ for node in target:
472
+ for k in node.knobs:
473
+ if k.name not in _FIXED and k.name not in seen:
474
+ seen[k.name] = None
475
+ extra_cols = list(seen)
476
+ else:
477
+ extra_cols = knobs
478
+
479
+ buf = io.StringIO()
480
+ headers = ["class", "name", "xpos", "ypos"] + extra_cols
481
+ writer = csv.DictWriter(buf, fieldnames=headers)
482
+ writer.writeheader()
483
+ for node in target:
484
+ row: dict = {
485
+ "class": node.class_name,
486
+ "name": node.name,
487
+ "xpos": int(node.xpos) if node.xpos is not None else "",
488
+ "ypos": int(node.ypos) if node.ypos is not None else "",
489
+ }
490
+ for c in extra_cols:
491
+ row[c] = node.get(c)
492
+ writer.writerow(row)
493
+ return buf.getvalue()
494
+
401
495
  # ------------------------------------------------------------------
402
496
  # Navigation
403
497
  # ------------------------------------------------------------------
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nkparser"
3
- version = "0.2.0"
3
+ version = "0.3.0"
4
4
  description = "Parse, query, and serialize Foundry Nuke .nk script files"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"
@@ -301,7 +301,7 @@ wheels = [
301
301
 
302
302
  [[package]]
303
303
  name = "nkparser"
304
- version = "0.2.0"
304
+ version = "0.3.0"
305
305
  source = { editable = "." }
306
306
 
307
307
  [package.optional-dependencies]
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes