yaml dump支持ordered dict,支持config_info

This commit is contained in:
Xuwznln
2025-07-19 01:54:53 +08:00
parent 862f250e49
commit ebe9e1b1f8
9 changed files with 152298 additions and 325 deletions

View File

@@ -9,6 +9,7 @@ from typing import Any, Dict, List
import yaml import yaml
from unilabos.resources.graphio import resource_plr_to_ulab, tree_to_list
from unilabos.ros.msgs.message_converter import msg_converter_manager, ros_action_to_json_schema, String from unilabos.ros.msgs.message_converter import msg_converter_manager, ros_action_to_json_schema, String
from unilabos.utils import logger from unilabos.utils import logger
from unilabos.utils.decorator import singleton from unilabos.utils.decorator import singleton
@@ -65,7 +66,9 @@ class Registry:
}, },
"feedback": {}, "feedback": {},
"result": {"success": "success"}, "result": {"success": "success"},
"schema": ros_action_to_json_schema(self.ResourceCreateFromOuter, '用于创建或更新物料资源,每次传入多个物料信息。'), "schema": ros_action_to_json_schema(
self.ResourceCreateFromOuter, "用于创建或更新物料资源,每次传入多个物料信息。"
),
"goal_default": yaml.safe_load( "goal_default": yaml.safe_load(
io.StringIO(get_yaml_from_goal_type(self.ResourceCreateFromOuter.Goal)) io.StringIO(get_yaml_from_goal_type(self.ResourceCreateFromOuter.Goal))
), ),
@@ -86,7 +89,9 @@ class Registry:
}, },
"feedback": {}, "feedback": {},
"result": {"success": "success"}, "result": {"success": "success"},
"schema": ros_action_to_json_schema(self.ResourceCreateFromOuterEasy, '用于创建或更新物料资源,每次传入一个物料信息。'), "schema": ros_action_to_json_schema(
self.ResourceCreateFromOuterEasy, "用于创建或更新物料资源,每次传入一个物料信息。"
),
"goal_default": yaml.safe_load( "goal_default": yaml.safe_load(
io.StringIO(get_yaml_from_goal_type(self.ResourceCreateFromOuterEasy.Goal)) io.StringIO(get_yaml_from_goal_type(self.ResourceCreateFromOuterEasy.Goal))
), ),
@@ -113,7 +118,9 @@ class Registry:
"goal": {}, "goal": {},
"feedback": {}, "feedback": {},
"result": {"latency_ms": "latency_ms", "time_diff_ms": "time_diff_ms"}, "result": {"latency_ms": "latency_ms", "time_diff_ms": "time_diff_ms"},
"schema": ros_action_to_json_schema(self.EmptyIn, '用于测试延迟的动作,返回延迟时间和时间差。'), "schema": ros_action_to_json_schema(
self.EmptyIn, "用于测试延迟的动作,返回延迟时间和时间差。"
),
"goal_default": {}, "goal_default": {},
"handles": {}, "handles": {},
}, },
@@ -149,6 +156,7 @@ class Registry:
for i, file in enumerate(files): for i, file in enumerate(files):
with open(file, encoding="utf-8", mode="r") as f: with open(file, encoding="utf-8", mode="r") as f:
data = yaml.safe_load(io.StringIO(f.read())) data = yaml.safe_load(io.StringIO(f.read()))
complete_data = {}
if data: if data:
# 为每个资源添加文件路径信息 # 为每个资源添加文件路径信息
for resource_id, resource_info in data.items(): for resource_id, resource_info in data.items():
@@ -172,11 +180,21 @@ class Registry:
if len(class_info) and "module" in class_info: if len(class_info) and "module" in class_info:
if class_info.get("type") == "pylabrobot": if class_info.get("type") == "pylabrobot":
res_class = get_class(class_info["module"]) res_class = get_class(class_info["module"])
if callable(res_class) and not isinstance(res_class, type): # 有的是类,有的是函数,这里暂时只登记函数类的 if callable(res_class) and not isinstance(
res_class, type
): # 有的是类,有的是函数,这里暂时只登记函数类的
res_instance = res_class(res_class.__name__) res_instance = res_class(res_class.__name__)
res_ulr = tree_to_list([resource_plr_to_ulab(res_instance)])
print(res_instance) resource_info["config_info"] = res_ulr
resource_info["registry_type"] = "resource" resource_info["registry_type"] = "resource"
complete_data[resource_id] = copy.deepcopy(dict(sorted(resource_info.items()))) # 稍后dump到文件
complete_data = dict(sorted(complete_data.items()))
complete_data = copy.deepcopy(complete_data)
if complete_registry:
with open(file, "w", encoding="utf-8") as f:
yaml.dump(complete_data, f, allow_unicode=True, default_flow_style=False, Dumper=NoAliasDumper)
self.resource_type_registry.update(data) self.resource_type_registry.update(data)
logger.debug( logger.debug(
f"[UniLab Registry] Resource-{current_resource_number} File-{i+1}/{len(files)} " f"[UniLab Registry] Resource-{current_resource_number} File-{i+1}/{len(files)} "
@@ -462,7 +480,9 @@ class Registry:
# 恢复原有的description信息auto开头的不修改 # 恢复原有的description信息auto开头的不修改
for action_name, description in old_descriptions.items(): for action_name, description in old_descriptions.items():
if action_name in device_config["class"]["action_value_mappings"]: # 有一些会被删除 if action_name in device_config["class"]["action_value_mappings"]: # 有一些会被删除
device_config["class"]["action_value_mappings"][action_name]["schema"]["description"] = description device_config["class"]["action_value_mappings"][action_name]["schema"][
"description"
] = description
device_config["init_param_schema"] = {} device_config["init_param_schema"] = {}
device_config["init_param_schema"]["config"] = self._generate_unilab_json_command_schema( device_config["init_param_schema"]["config"] = self._generate_unilab_json_command_schema(
enhanced_info["init_params"], "__init__" enhanced_info["init_params"], "__init__"

View File

@@ -1,8 +1,17 @@
OTDeck: OTDeck:
description: Opentrons deck category:
class: - deck
class:
module: pylabrobot.resources.opentrons.deck:OTDeck module: pylabrobot.resources.opentrons.deck:OTDeck
type: pylabrobot type: pylabrobot
config_info: []
description: Opentrons deck
file_path: C:/Users/10230/PycharmProjects/Uni-Lab-OS/unilabos/registry/resources/opentrons/deck.yaml
handles: []
icon: ''
init_param_schema: {}
model: model:
mesh: opentrons_liquid_handler
type: device type: device
mesh: opentrons_liquid_handler registry_type: resource
version: 1.0.0

View File

@@ -1,5 +1,39 @@
Opentrons_96_adapter_Vb: Opentrons_96_adapter_Vb:
description: Opentrons 96 adapter Vb category:
class: - plate_adapters
class:
module: pylabrobot.resources.opentrons.plate_adapters:Opentrons_96_adapter_Vb module: pylabrobot.resources.opentrons.plate_adapters:Opentrons_96_adapter_Vb
type: pylabrobot type: pylabrobot
config_info:
- children: []
class: ''
config:
barcode: null
category: plate_adapter
model: Opentrons_96_adapter_Vb
rotation:
type: Rotation
x: 0
y: 0
z: 0
size_x: 127.76
size_y: 85.48
size_z: 18.55
type: PlateAdapter
data: {}
id: Opentrons_96_adapter_Vb
name: Opentrons_96_adapter_Vb
parent: null
position:
x: 0
y: 0
z: 0
sample_id: null
type: container
description: Opentrons 96 adapter Vb
file_path: C:/Users/10230/PycharmProjects/Uni-Lab-OS/unilabos/registry/resources/opentrons/plate_adapters.yaml
handles: []
icon: ''
init_param_schema: {}
registry_type: resource
version: 1.0.0

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,28 +1,35 @@
container: container:
description: regular organic container category:
icon: Flask.webp - container
class: class:
module: unilabos.resources.container:RegularContainer module: unilabos.resources.container:RegularContainer
type: unilabos type: unilabos
config_info: []
description: regular organic container
file_path: C:/Users/10230/PycharmProjects/Uni-Lab-OS/unilabos/registry/resources/organic/container.yaml
handles: handles:
- handler_key: top - data_key: fluid_in
label: top data_source: handle
io_type: target data_type: fluid
data_type: fluid handler_key: top
side: NORTH io_type: target
data_source: handle label: top
data_key: fluid_in side: NORTH
- handler_key: bottom - data_key: fluid_out
label: bottom data_source: handle
io_type: source data_type: fluid
data_type: fluid handler_key: bottom
side: SOUTH io_type: source
data_source: handle label: bottom
data_key: fluid_out side: SOUTH
- handler_key: bind - data_key: mechanical_port
label: bind data_source: handle
io_type: target data_type: mechanical
data_type: mechanical handler_key: bind
side: SOUTH io_type: target
data_source: handle label: bind
data_key: mechanical_port side: SOUTH
icon: Flask.webp
init_param_schema: {}
registry_type: resource
version: 1.0.0

View File

@@ -1,5 +1,6 @@
import collections.abc import collections.abc
import json import json
from collections import OrderedDict
from typing import get_origin, get_args from typing import get_origin, get_args
import yaml import yaml
@@ -29,6 +30,14 @@ class NoAliasDumper(yaml.SafeDumper):
return True return True
# 为NoAliasDumper添加OrderedDict的representation方法
def represent_ordereddict(dumper, data):
return dumper.represent_mapping("tag:yaml.org,2002:map", data.items())
# 注册OrderedDict的representer
NoAliasDumper.add_representer(OrderedDict, represent_ordereddict)
class ResultInfoEncoder(json.JSONEncoder): class ResultInfoEncoder(json.JSONEncoder):
"""专门用于处理任务执行结果信息的JSON编码器""" """专门用于处理任务执行结果信息的JSON编码器"""