api.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. import zmq
  2. from collections import namedtuple
  3. from . import serial, exc
  4. class Expr(str):
  5. """String rendering of an expr-bound property value, sent over netdebug.
  6. Subclasses str so it formats naturally, but stays isinstance-distinct
  7. from plain str property values."""
  8. pass
  9. Property = namedtuple("Property", [
  10. "name",
  11. "type",
  12. "subtype",
  13. #"defaults",
  14. "ui_name",
  15. "desc",
  16. "is_null_allowed",
  17. "is_expr_allowed",
  18. "array_len",
  19. "min_val",
  20. "max_val",
  21. "enum_items",
  22. "depends"
  23. ])
  24. class Command:
  25. HELLO = 0
  26. ADD_NODE = 1
  27. REMOVE_NODE = 9
  28. RENAME_NODE = 23
  29. SCAN_DANGLING = 24
  30. LOOKUP_NODE_ID = 12
  31. ADD_PROPERTY = 11
  32. LINK_NODE = 2
  33. UNLINK_NODE = 8
  34. GET_INFO = 19
  35. GET_CHILDREN = 4
  36. GET_PARENTS = 5
  37. GET_PROPERTIES = 3
  38. GET_PROPERTY_VALUE = 6
  39. SET_PROPERTY_VALUE = 7
  40. GET_SIGNALS = 14
  41. REGISTER_SLOT = 15
  42. UNREGISTER_SLOT = 16
  43. LOOKUP_SLOT_ID = 17
  44. GET_SLOTS = 18
  45. GET_METHODS = 20
  46. GET_METHOD = 21
  47. CALL_METHOD = 22
  48. class SceneNodeType:
  49. NULL = 0
  50. ROOT = 1
  51. WINDOW = 2
  52. WINDOW_INPUT = 3
  53. KEYBOARD = 4
  54. MOUSE = 5
  55. LAYER = 6
  56. OBJECT = 7
  57. VECTOR_ART = 8
  58. TEXT = 9
  59. TEXTURE = 10
  60. FONTS = 11
  61. FONT = 12
  62. CHAT_VIEW = 13
  63. EDIT = 14
  64. IMAGE = 15
  65. BUTTON = 16
  66. SHORTCUT = 17
  67. GESTURE = 18
  68. EMOJI_PICKER = 19
  69. SETTING = 21
  70. MENU = 22
  71. TOKEN_TABLE = 23
  72. TEXT_SCRAMBLE = 24
  73. PLUGIN_ROOT = 100
  74. PLUGIN = 101
  75. class PropertyType:
  76. NULL = 0
  77. BOOL = 1
  78. UINT32 = 2
  79. FLOAT32 = 3
  80. STR = 4
  81. ENUM = 5
  82. SCENE_NODE_ID = 7
  83. SEXPR = 8
  84. @staticmethod
  85. def to_str(prop_type):
  86. match prop_type:
  87. case PropertyType.NULL:
  88. return "null"
  89. case PropertyType.BOOL:
  90. return "bool"
  91. case PropertyType.UINT32:
  92. return "uint32"
  93. case PropertyType.FLOAT32:
  94. return "float32"
  95. case PropertyType.STR:
  96. return "str"
  97. case PropertyType.ENUM:
  98. return "enum"
  99. case PropertyType.SCENE_NODE_ID:
  100. return "scene_node_id"
  101. case PropertyType.SEXPR:
  102. return "sexpr"
  103. class PropertySubType:
  104. NULL = 0
  105. COLOR = 1
  106. PIXEL = 2
  107. RESOURCE_ID = 3
  108. LOCALE = 4
  109. FLAG = 5
  110. @staticmethod
  111. def to_str(prop_type):
  112. match prop_type:
  113. case PropertySubType.NULL:
  114. return "null"
  115. case PropertySubType.COLOR:
  116. return "color"
  117. case PropertySubType.PIXEL:
  118. return "pixel"
  119. case PropertySubType.RESOURCE_ID:
  120. return "resource_id"
  121. case PropertySubType.LOCALE:
  122. return "locale"
  123. case PropertySubType.FLAG:
  124. return "flag"
  125. class CallArgType:
  126. UINT32 = 0
  127. UINT64 = 1
  128. FLOAT32 = 2
  129. BOOL = 3
  130. STR = 4
  131. HASH = 5
  132. @staticmethod
  133. def to_str(arg_type):
  134. match arg_type:
  135. case CallArgType.UINT32:
  136. return "uint32"
  137. case CallArgType.UINT64:
  138. return "uint64"
  139. case CallArgType.FLOAT32:
  140. return "float32"
  141. case CallArgType.BOOL:
  142. return "bool"
  143. case CallArgType.STR:
  144. return "str"
  145. case CallArgType.HASH:
  146. return "hash"
  147. case _:
  148. return "unknown"
  149. class PropertyStatus:
  150. OK = 0
  151. UNSET = 1
  152. NULL = 2
  153. EXPR = 3
  154. class ErrorCode:
  155. INVALID_SCENE_PATH = 1
  156. NODE_NOT_FOUND = 2
  157. CHILD_NODE_NOT_FOUND = 3
  158. PARENT_NODE_NOT_FOUND = 4
  159. PROPERTY_ALREADY_EXISTS = 5
  160. PROPERTY_NOT_FOUND = 6
  161. PROPERTY_WRONG_TYPE = 7
  162. PROPERTY_WRONG_LEN = 9
  163. PROPERTY_WRONG_INDEX = 10
  164. PROPERTY_OUT_OF_RANGE = 11
  165. PROPERTY_NULL_NOT_ALLOWED = 12
  166. PROPERTY_SEXPR_NOT_ALLOWED = 13
  167. PROPERTY_IS_BOUNDED = 14
  168. PROPERTY_WRONG_ENUM_ITEM = 15
  169. SIGNAL_ALREADY_EXISTS = 16
  170. SIGNAL_NOT_FOUND = 17
  171. SLOT_NOT_FOUND = 18
  172. METHOD_ALREADY_EXISTS = 19
  173. METHOD_NOT_FOUND = 20
  174. NODES_ARE_LINKED = 21
  175. NODES_NOT_LINKED = 22
  176. NODE_HAS_PARENTS = 23
  177. NODE_HAS_CHILDREN = 24
  178. NODE_PARENT_NAME_CONFLICT = 25
  179. NODE_CHILD_NAME_CONFLICT = 26
  180. NODE_SIBLING_NAME_CONFLICT = 27
  181. SEXPR_GLOBAL_NOT_FOUND = 32
  182. PUBLISHER_DESTROYED = 34
  183. CHANNEL_CLOSED = 36
  184. NODES_ARE_SAME = 37
  185. UNEXPECTED_TOKEN = 38
  186. KVDB_ERR = 39
  187. SERVICE_FAILED = 40
  188. GFX_DUPLICATE_TEXTURE_ID = 41
  189. GFX_UNKNOWN_TEXTURE_ID = 42
  190. GFX_DUPLICATE_BUFFER_ID = 43
  191. GFX_UNKNOWN_BUFFER_ID = 44
  192. GFX_DUPLICATE_ANIM_ID = 45
  193. GFX_UNKNOWN_ANIM_ID = 46
  194. CONTACT_NOT_FOUND = 47
  195. SERIAL_ERR = 48
  196. TURSO_ERR = 49
  197. @staticmethod
  198. def to_str(errc):
  199. match errc:
  200. case ErrorCode.INVALID_SCENE_PATH:
  201. return "invalid_scene_path"
  202. case ErrorCode.NODE_NOT_FOUND:
  203. return "node_not_found"
  204. case ErrorCode.CHILD_NODE_NOT_FOUND:
  205. return "child_node_not_found"
  206. case ErrorCode.PARENT_NODE_NOT_FOUND:
  207. return "parent_node_not_found"
  208. case ErrorCode.PROPERTY_ALREADY_EXISTS:
  209. return "property_already_exists"
  210. case ErrorCode.PROPERTY_NOT_FOUND:
  211. return "property_not_found"
  212. case ErrorCode.PROPERTY_WRONG_TYPE:
  213. return "property_wrong_type"
  214. case ErrorCode.PROPERTY_WRONG_LEN:
  215. return "property_wrong_len"
  216. case ErrorCode.PROPERTY_WRONG_INDEX:
  217. return "property_wrong_index"
  218. case ErrorCode.PROPERTY_OUT_OF_RANGE:
  219. return "property_out_of_range"
  220. case ErrorCode.PROPERTY_NULL_NOT_ALLOWED:
  221. return "property_null_not_allowed"
  222. case ErrorCode.PROPERTY_SEXPR_NOT_ALLOWED:
  223. return "property_sexpr_not_allowed"
  224. case ErrorCode.PROPERTY_IS_BOUNDED:
  225. return "property_is_bounded"
  226. case ErrorCode.PROPERTY_WRONG_ENUM_ITEM:
  227. return "property_wrong_enum_item"
  228. case ErrorCode.SIGNAL_ALREADY_EXISTS:
  229. return "signal_already_exists"
  230. case ErrorCode.SIGNAL_NOT_FOUND:
  231. return "signal_not_found"
  232. case ErrorCode.SLOT_NOT_FOUND:
  233. return "slot_not_found"
  234. case ErrorCode.METHOD_ALREADY_EXISTS:
  235. return "method_already_exists"
  236. case ErrorCode.METHOD_NOT_FOUND:
  237. return "method_not_found"
  238. case ErrorCode.NODES_ARE_LINKED:
  239. return "nodes_are_linked"
  240. case ErrorCode.NODES_NOT_LINKED:
  241. return "nodes_not_linked"
  242. case ErrorCode.NODE_HAS_PARENTS:
  243. return "node_has_parents"
  244. case ErrorCode.NODE_HAS_CHILDREN:
  245. return "node_has_children"
  246. case ErrorCode.NODE_PARENT_NAME_CONFLICT:
  247. return "node_parent_name_conflict"
  248. case ErrorCode.NODE_CHILD_NAME_CONFLICT:
  249. return "node_child_name_conflict"
  250. case ErrorCode.NODE_SIBLING_NAME_CONFLICT:
  251. return "node_sibling_name_conflict"
  252. case ErrorCode.SEXPR_GLOBAL_NOT_FOUND:
  253. return "sexpr_global_not_found"
  254. case ErrorCode.PUBLISHER_DESTROYED:
  255. return "publisher_destroyed"
  256. case ErrorCode.CHANNEL_CLOSED:
  257. return "channel_closed"
  258. case ErrorCode.NODES_ARE_SAME:
  259. return "nodes_are_same"
  260. case ErrorCode.UNEXPECTED_TOKEN:
  261. return "unexpected_token"
  262. case ErrorCode.KVDB_ERR:
  263. return "kvdb_err"
  264. case ErrorCode.SERVICE_FAILED:
  265. return "service_failed"
  266. case ErrorCode.GFX_DUPLICATE_TEXTURE_ID:
  267. return "gfx_duplicate_texture_id"
  268. case ErrorCode.GFX_UNKNOWN_TEXTURE_ID:
  269. return "gfx_unknown_texture_id"
  270. case ErrorCode.GFX_DUPLICATE_BUFFER_ID:
  271. return "gfx_duplicate_buffer_id"
  272. case ErrorCode.GFX_UNKNOWN_BUFFER_ID:
  273. return "gfx_unknown_buffer_id"
  274. case ErrorCode.GFX_DUPLICATE_ANIM_ID:
  275. return "gfx_duplicate_anim_id"
  276. case ErrorCode.GFX_UNKNOWN_ANIM_ID:
  277. return "gfx_unknown_anim_id"
  278. case ErrorCode.CONTACT_NOT_FOUND:
  279. return "contact_not_found"
  280. case ErrorCode.SERIAL_ERR:
  281. return "serial_err"
  282. case ErrorCode.TURSO_ERR:
  283. return "turso_err"
  284. case _:
  285. return "unknown"
  286. def vertex(x, y, r, g, b, a, u, v):
  287. buf = bytearray()
  288. serial.write_f32(buf, x)
  289. serial.write_f32(buf, y)
  290. serial.write_f32(buf, r)
  291. serial.write_f32(buf, g)
  292. serial.write_f32(buf, b)
  293. serial.write_f32(buf, a)
  294. serial.write_f32(buf, u)
  295. serial.write_f32(buf, v)
  296. return buf
  297. def face(idx1, idx2, idx3):
  298. buf = bytearray()
  299. serial.write_u32(buf, idx1)
  300. serial.write_u32(buf, idx2)
  301. serial.write_u32(buf, idx3)
  302. return buf
  303. class Api:
  304. def __init__(self, addr="127.0.0.1", port=9484):
  305. context = zmq.Context()
  306. self.socket = context.socket(zmq.REQ)
  307. #self.socket.setsockopt(zmq.IPV6, True)
  308. self.socket.connect(f"tcp://{addr}:{port}")
  309. def _make_request(self, cmd, payload):
  310. req_cmd = bytearray()
  311. serial.write_u8(req_cmd, cmd)
  312. self.socket.send_multipart([req_cmd, payload])
  313. errc, reply = self.socket.recv_multipart()
  314. errc = int.from_bytes(errc, "little")
  315. cursor = serial.Cursor(reply)
  316. match errc:
  317. case 0:
  318. pass
  319. case ErrorCode.INVALID_SCENE_PATH:
  320. raise exc.InvalidScenePath
  321. case ErrorCode.NODE_NOT_FOUND:
  322. raise exc.NodeNotFound
  323. case ErrorCode.CHILD_NODE_NOT_FOUND:
  324. raise exc.ChildNodeNotFound
  325. case ErrorCode.PARENT_NODE_NOT_FOUND:
  326. raise exc.ParentNodeNotFound
  327. case ErrorCode.PROPERTY_ALREADY_EXISTS:
  328. raise exc.PropertyAlreadyExists
  329. case ErrorCode.PROPERTY_NOT_FOUND:
  330. raise exc.PropertyNotFound
  331. case ErrorCode.PROPERTY_WRONG_TYPE:
  332. raise exc.PropertyWrongType
  333. case ErrorCode.PROPERTY_WRONG_LEN:
  334. raise exc.PropertyWrongLen
  335. case ErrorCode.PROPERTY_WRONG_INDEX:
  336. raise exc.PropertyWrongIndex
  337. case ErrorCode.PROPERTY_OUT_OF_RANGE:
  338. raise exc.PropertyOutOfRange
  339. case ErrorCode.PROPERTY_NULL_NOT_ALLOWED:
  340. raise exc.PropertyNullNotAllowed
  341. case ErrorCode.PROPERTY_SEXPR_NOT_ALLOWED:
  342. raise exc.PropertySExprNotAllowed
  343. case ErrorCode.PROPERTY_IS_BOUNDED:
  344. raise exc.PropertyIsBounded
  345. case ErrorCode.PROPERTY_WRONG_ENUM_ITEM:
  346. raise exc.PropertyWrongEnumItem
  347. case ErrorCode.SIGNAL_ALREADY_EXISTS:
  348. raise exc.SignalAlreadyExists
  349. case ErrorCode.SIGNAL_NOT_FOUND:
  350. raise exc.SignalNotFound
  351. case ErrorCode.SLOT_NOT_FOUND:
  352. raise exc.SlotNotFound
  353. case ErrorCode.METHOD_ALREADY_EXISTS:
  354. raise exc.MethodAlreadyExists
  355. case ErrorCode.METHOD_NOT_FOUND:
  356. raise exc.MethodNotFound
  357. case ErrorCode.NODES_ARE_LINKED:
  358. raise exc.NodesAreLinked
  359. case ErrorCode.NODES_NOT_LINKED:
  360. raise exc.NodesNotLinked
  361. case ErrorCode.NODE_HAS_PARENTS:
  362. raise exc.NodeHasParents
  363. case ErrorCode.NODE_HAS_CHILDREN:
  364. raise exc.NodeHasChildren
  365. case ErrorCode.NODE_PARENT_NAME_CONFLICT:
  366. raise exc.NodeParentNameConflict
  367. case ErrorCode.NODE_CHILD_NAME_CONFLICT:
  368. raise exc.NodeChildNameConflict
  369. case ErrorCode.NODE_SIBLING_NAME_CONFLICT:
  370. raise exc.NodeSiblingNameConflict
  371. case ErrorCode.SEXPR_GLOBAL_NOT_FOUND:
  372. raise exc.SExprGlobalNotFound
  373. case ErrorCode.PUBLISHER_DESTROYED:
  374. raise exc.PublisherDestroyed
  375. case ErrorCode.CHANNEL_CLOSED:
  376. raise exc.ChannelClosed
  377. case ErrorCode.NODES_ARE_SAME:
  378. raise exc.NodesAreSame
  379. case ErrorCode.UNEXPECTED_TOKEN:
  380. raise exc.UnexpectedToken
  381. case ErrorCode.KVDB_ERR:
  382. raise exc.KvdbErr
  383. case ErrorCode.SERVICE_FAILED:
  384. raise exc.ServiceFailed
  385. case ErrorCode.GFX_DUPLICATE_TEXTURE_ID:
  386. raise exc.GfxDuplicateTextureID
  387. case ErrorCode.GFX_UNKNOWN_TEXTURE_ID:
  388. raise exc.GfxUnknownTextureID
  389. case ErrorCode.GFX_DUPLICATE_BUFFER_ID:
  390. raise exc.GfxDuplicateBufferID
  391. case ErrorCode.GFX_UNKNOWN_BUFFER_ID:
  392. raise exc.GfxUnknownBufferID
  393. case ErrorCode.GFX_DUPLICATE_ANIM_ID:
  394. raise exc.GfxDuplicateAnimID
  395. case ErrorCode.GFX_UNKNOWN_ANIM_ID:
  396. raise exc.GfxUnknownAnimID
  397. case ErrorCode.CONTACT_NOT_FOUND:
  398. raise exc.ContactNotFound
  399. case ErrorCode.SERIAL_ERR:
  400. raise exc.SerialErr
  401. case ErrorCode.TURSO_ERR:
  402. raise exc.TursoErr
  403. case _:
  404. raise exc.UnknownError(f"unknown error code: {errc}")
  405. return cursor
  406. def hello(self):
  407. response = self._make_request(Command.HELLO, bytearray())
  408. return serial.decode_str(response)
  409. def get_info(self, node_id):
  410. req = bytearray()
  411. serial.write_u32(req, node_id)
  412. cur = self._make_request(Command.GET_INFO, req)
  413. name = serial.decode_str(cur)
  414. type = serial.read_u8(cur)
  415. return (name, type)
  416. def get_children(self, node_path):
  417. req = bytearray()
  418. serial.encode_str(req, node_path)
  419. cur = self._make_request(Command.GET_CHILDREN, req)
  420. children_len = serial.decode_varint(cur)
  421. children = []
  422. for _ in range(children_len):
  423. child_name = serial.decode_str(cur)
  424. child_id = serial.read_u32(cur)
  425. child_type = serial.read_u8(cur)
  426. children.append((child_name, child_id, child_type))
  427. return children
  428. def get_parents(self, node_id):
  429. req = bytearray()
  430. serial.write_u32(req, node_id)
  431. cur = self._make_request(Command.GET_PARENTS, req)
  432. parents_len = serial.decode_varint(cur)
  433. parents = []
  434. for _ in range(parents_len):
  435. parent_name = serial.decode_str(cur)
  436. parent_id = serial.read_u32(cur)
  437. parent_type = serial.read_u8(cur)
  438. parents.append((parent_name, parent_id, parent_type))
  439. return parents
  440. def get_properties(self, node_path):
  441. req = bytearray()
  442. serial.encode_str(req, node_path)
  443. cur = self._make_request(Command.GET_PROPERTIES, req)
  444. props_len = serial.decode_varint(cur)
  445. props = []
  446. enum_read_fn = lambda cur: serial.decode_arr(cur, serial.decode_str)
  447. def depend_read_fn(cur):
  448. i = serial.read_u32(cur)
  449. local_name = serial.decode_str(cur)
  450. return (i, local_name)
  451. for _ in range(props_len):
  452. prop_name = serial.decode_str(cur)
  453. # We need prop_type below
  454. prop_type = serial.read_u8(cur)
  455. prop_read_fn = lambda cur: Api.read_prop_val(cur, prop_type)
  456. prop = Property(
  457. prop_name,
  458. prop_type,
  459. # subtype
  460. serial.read_u8(cur),
  461. # defaults
  462. #serial.decode_arr(cur, prop_read_fn),
  463. # ui_name
  464. serial.decode_str(cur),
  465. # desc
  466. serial.decode_str(cur),
  467. # is_null_allowed
  468. bool(serial.read_u8(cur)),
  469. # is_expr_allowed
  470. bool(serial.read_u8(cur)),
  471. # array_len
  472. serial.read_u32(cur),
  473. # min_val
  474. serial.decode_opt(cur, prop_read_fn),
  475. # max_val
  476. serial.decode_opt(cur, prop_read_fn),
  477. # enum_items
  478. serial.decode_opt(cur, enum_read_fn),
  479. # depends
  480. serial.decode_arr(cur, depend_read_fn)
  481. )
  482. props.append(prop)
  483. return props
  484. @staticmethod
  485. def read_prop_val(cur, prop_type):
  486. match prop_type:
  487. case PropertyType.NULL:
  488. return None
  489. case PropertyType.BOOL:
  490. return bool(serial.read_u8(cur))
  491. case PropertyType.UINT32:
  492. return serial.read_u32(cur)
  493. case PropertyType.FLOAT32:
  494. return serial.read_f32(cur)
  495. case PropertyType.STR:
  496. return serial.decode_str(cur)
  497. case PropertyType.ENUM:
  498. return serial.decode_str(cur)
  499. case PropertyType.SCENE_NODE_ID:
  500. return serial.read_u32(cur)
  501. case _:
  502. raise Exception("unknown property type returned")
  503. def get_property_value(self, node_path, prop_name):
  504. req = bytearray()
  505. serial.encode_str(req, node_path)
  506. serial.encode_str(req, prop_name)
  507. cur = self._make_request(Command.GET_PROPERTY_VALUE, req)
  508. prop_type = serial.read_u8(cur)
  509. def prop_read_fn(cur):
  510. prop_status = serial.read_u8(cur)
  511. match prop_status:
  512. case PropertyStatus.NULL:
  513. return None
  514. case PropertyStatus.EXPR:
  515. return Expr(serial.decode_str(cur))
  516. case PropertyStatus.UNSET | PropertyStatus.OK:
  517. return Api.read_prop_val(cur, prop_type)
  518. vals = serial.decode_arr(cur, prop_read_fn)
  519. return vals
  520. def add_node(self, node_name, node_type):
  521. req = bytearray()
  522. serial.encode_str(req, node_name)
  523. serial.write_u8(req, int(node_type))
  524. cur = self._make_request(Command.ADD_NODE, req)
  525. node_id = serial.read_u32(cur)
  526. return node_id
  527. def remove_node(self, node_id):
  528. req = bytearray()
  529. serial.write_u32(req, node_id)
  530. self._make_request(Command.REMOVE_NODE, req)
  531. def rename_node(self, node_id, node_name):
  532. req = bytearray()
  533. serial.write_u32(req, node_id)
  534. serial.encode_str(req, node_name)
  535. self._make_request(Command.RENAME_NODE, req)
  536. def scan_dangling(self):
  537. cur = self._make_request(Command.SCAN_DANGLING, bytearray())
  538. dangling_len = serial.decode_varint(cur)
  539. dangling = []
  540. for _ in range(dangling_len):
  541. node_id = serial.read_u32(cur)
  542. dangling.append(node_id)
  543. return dangling
  544. def lookup_node_id(self, node_path):
  545. req = bytearray()
  546. serial.encode_str(req, node_path)
  547. try:
  548. cur = self._make_request(Command.LOOKUP_NODE_ID, req)
  549. except exc.NodeNotFound:
  550. return None
  551. return serial.read_u32(cur)
  552. def add_property(self, node_id, prop):
  553. req = bytearray()
  554. serial.write_u32(req, node_id)
  555. serial.encode_str(req, prop.name)
  556. serial.write_u8(req, int(prop.type))
  557. serial.write_u8(req, int(prop.subtype))
  558. serial.write_u32(req, int(prop.array_len))
  559. def write_defaults(by):
  560. assert prop.defaults is not None
  561. defaults_len = len(prop.defaults)
  562. serial.encode_varint(req, defaults_len)
  563. for default in prop.defaults:
  564. match prop.type:
  565. case PropertyType.UINT32:
  566. serial.write_u32(req, default)
  567. case PropertyType.FLOAT32:
  568. serial.write_f32(req, default)
  569. case PropertyType.STR:
  570. serial.encode_str(req, default)
  571. case _:
  572. raise exc.PropertyWrongType
  573. serial.encode_opt(req, prop.defaults, write_defaults)
  574. serial.encode_str(req, prop.ui_name)
  575. serial.encode_str(req, prop.desc)
  576. serial.write_u8(req, int(prop.is_null_allowed))
  577. serial.write_u8(req, int(prop.is_expr_allowed))
  578. def write_mxx(v, by):
  579. assert v is not None
  580. match prop.type:
  581. case PropertyType.UINT32:
  582. serial.write_u32(req, v)
  583. case PropertyType.FLOAT32:
  584. serial.write_f32(req, v)
  585. case _:
  586. raise exc.PropertyWrongType
  587. write_min = lambda by: write_mxx(prop.min_val, by)
  588. write_max = lambda by: write_mxx(prop.max_val, by)
  589. serial.encode_opt(req, prop.min_val, write_min)
  590. serial.encode_opt(req, prop.max_val, write_max)
  591. serial.encode_varint(req, len(prop.enum_items))
  592. for enum_item in prop.enum_items:
  593. if prop.type != PropertyType.ENUM:
  594. raise exc.PropertyWrongType
  595. serial.encode_str(req, enum_item)
  596. self._make_request(Command.ADD_PROPERTY, req)
  597. def link_node(self, child_id, parent_id):
  598. req = bytearray()
  599. serial.write_u32(req, child_id)
  600. serial.write_u32(req, parent_id)
  601. self._make_request(Command.LINK_NODE, req)
  602. def unlink_node(self, child_id, parent_id):
  603. req = bytearray()
  604. serial.write_u32(req, child_id)
  605. serial.write_u32(req, parent_id)
  606. self._make_request(Command.UNLINK_NODE, req)
  607. def set_property_null(self, node_path, prop_name, i):
  608. req = bytearray()
  609. serial.encode_str(req, node_path)
  610. serial.encode_str(req, prop_name)
  611. serial.write_u32(req, i)
  612. serial.write_u8(req, PropertyType.NULL)
  613. self._make_request(Command.SET_PROPERTY_VALUE, req)
  614. def set_property_bool(self, node_path, prop_name, i, val):
  615. req = bytearray()
  616. serial.encode_str(req, node_path)
  617. serial.encode_str(req, prop_name)
  618. serial.write_u32(req, i)
  619. serial.write_u8(req, PropertyType.BOOL)
  620. serial.write_u8(req, int(val))
  621. self._make_request(Command.SET_PROPERTY_VALUE, req)
  622. def set_property_u32(self, node_path, prop_name, i, val):
  623. req = bytearray()
  624. serial.encode_str(req, node_path)
  625. serial.encode_str(req, prop_name)
  626. serial.write_u32(req, i)
  627. serial.write_u8(req, PropertyType.UINT32)
  628. serial.write_u32(req, val)
  629. self._make_request(Command.SET_PROPERTY_VALUE, req)
  630. def set_property_f32(self, node_path, prop_name, i, val):
  631. req = bytearray()
  632. serial.encode_str(req, node_path)
  633. serial.encode_str(req, prop_name)
  634. serial.write_u32(req, i)
  635. serial.write_u8(req, PropertyType.FLOAT32)
  636. serial.write_f32(req, val)
  637. self._make_request(Command.SET_PROPERTY_VALUE, req)
  638. def set_property_str(self, node_path, prop_name, i, val):
  639. req = bytearray()
  640. serial.encode_str(req, node_path)
  641. serial.encode_str(req, prop_name)
  642. serial.write_u32(req, i)
  643. serial.write_u8(req, PropertyType.STR)
  644. serial.encode_str(req, val)
  645. self._make_request(Command.SET_PROPERTY_VALUE, req)
  646. def set_property_enum(self, node_path, prop_name, i, val):
  647. req = bytearray()
  648. serial.encode_str(req, node_path)
  649. serial.encode_str(req, prop_name)
  650. serial.write_u32(req, i)
  651. serial.write_u8(req, PropertyType.ENUM)
  652. serial.encode_str(req, val)
  653. self._make_request(Command.SET_PROPERTY_VALUE, req)
  654. def set_property_expr(self, node_path, prop_name, i, expr_str):
  655. req = bytearray()
  656. serial.encode_str(req, node_path)
  657. serial.encode_str(req, prop_name)
  658. serial.write_u32(req, i)
  659. serial.write_u8(req, PropertyType.SEXPR)
  660. serial.encode_str(req, expr_str)
  661. self._make_request(Command.SET_PROPERTY_VALUE, req)
  662. def get_signals(self, node_path):
  663. req = bytearray()
  664. serial.encode_str(req, node_path)
  665. cur = self._make_request(Command.GET_SIGNALS, req)
  666. sigs_len = serial.decode_varint(cur)
  667. sigs = []
  668. for _ in range(sigs_len):
  669. sigs.append(serial.decode_str(cur))
  670. return sigs
  671. def register_slot(self, node_path, sig_name, slot_name, user_data):
  672. req = bytearray()
  673. serial.encode_str(req, node_path)
  674. serial.encode_str(req, sig_name)
  675. serial.encode_str(req, slot_name)
  676. serial.encode_varint(req, len(user_data))
  677. req += user_data
  678. cur = self._make_request(Command.REGISTER_SLOT, req)
  679. slot_id = serial.read_u32(cur)
  680. return slot_id
  681. def unregister_slot(self, node_id, sig_name, slot_id):
  682. req = bytearray()
  683. serial.write_u32(req, node_id)
  684. serial.encode_str(req, sig_name)
  685. serial.write_u32(req, slot_id)
  686. self._make_request(Command.UNREGISTER_SLOT, req)
  687. def lookup_slot_id(self, node_id, sig_name, slot_name):
  688. req = bytearray()
  689. serial.write_u32(req, node_id)
  690. serial.encode_str(req, sig_name)
  691. serial.encode_str(req, slot_name)
  692. try:
  693. cur = self._make_request(Command.LOOKUP_SLOT_ID, req)
  694. except exc.SlotNotFound:
  695. return None
  696. return serial.read_u32(cur)
  697. def get_slots(self, node_path, sig_name):
  698. req = bytearray()
  699. serial.encode_str(req, node_path)
  700. serial.encode_str(req, sig_name)
  701. cur = self._make_request(Command.GET_SLOTS, req)
  702. def read_slot(cur):
  703. slot_name = serial.decode_str(cur)
  704. slot_id = serial.read_u32(cur)
  705. return (slot_name, slot_id)
  706. slots = serial.decode_arr(cur, read_slot)
  707. return slots
  708. def get_methods(self, node_path):
  709. req = bytearray()
  710. serial.encode_str(req, node_path)
  711. cur = self._make_request(Command.GET_METHODS, req)
  712. def read_method(cur):
  713. method_name = serial.decode_str(cur)
  714. return method_name
  715. methods = serial.decode_arr(cur, read_method)
  716. return methods
  717. def get_method(self, node_path, method_name):
  718. req = bytearray()
  719. serial.encode_str(req, node_path)
  720. serial.encode_str(req, method_name)
  721. cur = self._make_request(Command.GET_METHOD, req)
  722. def read_arg(cur):
  723. arg_name = serial.decode_str(cur)
  724. arg_desc = serial.decode_str(cur)
  725. arg_type = serial.read_u8(cur)
  726. return (arg_name, arg_desc, arg_type)
  727. args = serial.decode_arr(cur, read_arg)
  728. results = serial.decode_arr(cur, read_arg)
  729. return (args, results)
  730. def call_method(self, node_path, method_name, arg_data):
  731. req = bytearray()
  732. serial.encode_str(req, node_path)
  733. serial.encode_str(req, method_name)
  734. serial.encode_buf(req, arg_data)
  735. cur = self._make_request(Command.CALL_METHOD, req)
  736. result = serial.decode_opt(cur, serial.decode_buf)
  737. return result