api.py 23 KB

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