Skip to content

datatable

数据表相关 CLI 命令(本地操作,无需 Dimine 运行)。

cmd_datatable_add_record(fmt, file, values)

向 .dmd 数据表添加记录并保存(本地操作)。

Source code in dimine_python_sdk\cli\commands\datatable.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
def cmd_datatable_add_record(fmt: str, file: str, values: str) -> None:
    """向 .dmd 数据表添加记录并保存(本地操作)。"""
    err = require_local_sdk()
    if err:
        output_error(err, fmt)

    file_path = resolve_path(file)

    try:
        records = parse_key_values(values)
    except ValueError as e:
        output_error(str(e), fmt)

    from dimine_python_sdk.lib.io.dmt_file import DmtFile

    try:
        dmt = DmtFile(file_path)

        added_count = 0
        for record_data in records:
            if not isinstance(record_data, dict):
                output_error(f"记录必须是键值对格式,实际类型: {type(record_data).__name__}", fmt)
            new_index = dmt.add_record()
            for field_name, value in record_data.items():
                dmt.set_value(new_index, field_name, value)
            added_count += 1

        dmt.save(file_path)

        output_result({
            "success": True,
            "file": file_path,
            "added_count": added_count,
            "total_records": len(dmt),
            "message": f"已添加 {added_count} 条记录并保存",
        }, fmt)
    except SystemExit:
        raise
    except Exception as e:
        output_error(f"添加记录失败: {e}", fmt)

cmd_datatable_info(fmt, file, column=None, to_json_output=False)

加载并检查 .dmd 数据表文件(本地操作)。

Source code in dimine_python_sdk\cli\commands\datatable.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def cmd_datatable_info(fmt: str, file: str, column: str | None = None,
                       to_json_output: bool = False) -> None:
    """加载并检查 .dmd 数据表文件(本地操作)。"""
    err = require_local_sdk()
    if err:
        output_error(err, fmt)

    file_path = resolve_path(file)

    from dimine_python_sdk.lib.io.dmt_file import DmtFile

    try:
        dmt = DmtFile(file_path)

        field_names = dmt.columns
        record_count = len(dmt)

        if column:
            if column not in dmt:
                output_error(f"字段 '{column}' 不存在。可用字段: {field_names}", fmt)
            col_values = dmt[column]
            output_result({"column": column, "count": len(col_values), "values": col_values}, fmt)
            return

        result = {
            "file": file_path,
            "field_count": len(field_names),
            "record_count": record_count,
            "fields": field_names,
        }

        if to_json_output:
            records = list(dmt)
            result["records"] = records

        output_result(result, fmt)
    except Exception as e:
        output_error(f"加载数据表失败: {e}", fmt)

cmd_datatable_save(fmt, file)

保存 .dmd 数据表文件(本地操作)。

Source code in dimine_python_sdk\cli\commands\datatable.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def cmd_datatable_save(fmt: str, file: str) -> None:
    """保存 .dmd 数据表文件(本地操作)。"""
    err = require_local_sdk()
    if err:
        output_error(err, fmt)

    file_path = resolve_path(file)

    from dimine_python_sdk.lib.io.dmt_file import DmtFile

    try:
        dmt = DmtFile(file_path)
        dmt.save(file_path)
        output_result({
            "success": True,
            "file": file_path,
            "message": f"数据表已保存: {file_path}",
        }, fmt)
    except Exception as e:
        output_error(f"保存数据表失败: {e}", fmt)

cmd_datatable_set(fmt, file, index, field, value)

设置 .dmd 数据表字段值并保存(本地操作)。

Source code in dimine_python_sdk\cli\commands\datatable.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def cmd_datatable_set(fmt: str, file: str, index: int, field: str, value: str) -> None:
    """设置 .dmd 数据表字段值并保存(本地操作)。"""
    err = require_local_sdk()
    if err:
        output_error(err, fmt)

    file_path = resolve_path(file)
    index = safe_int(index)

    from dimine_python_sdk.lib.io.dmt_file import DmtFile

    try:
        dmt = DmtFile(file_path)
        field_names = dmt.columns
        record_count = len(dmt)

        if index < 0 or index >= record_count:
            output_error(f"记录索引 {index} 超出范围 [0, {record_count})", fmt)
        if field not in field_names:
            output_error(f"字段 '{field}' 不存在。可用字段: {field_names}", fmt)

        typed_value = _infer_value_type(value)
        dmt.set_value(index, field, typed_value)

        dmt.save(file_path)

        output_result({
            "success": True,
            "file": file_path,
            "index": index,
            "field": field,
            "value": typed_value,
            "total_records": record_count,
            "message": f"已更新记录[{index}].{field} = {typed_value}",
        }, fmt)
    except SystemExit:
        raise
    except Exception as e:
        output_error(f"设置字段值失败: {e}", fmt)

register_commands(subparsers, format_parent)

向 subparsers 注册数据表相关子命令。

Source code in dimine_python_sdk\cli\commands\datatable.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def register_commands(subparsers, format_parent) -> None:
    """向 subparsers 注册数据表相关子命令。"""
    p = subparsers.add_parser("datatable-info", parents=[format_parent], help="加载并检查 .dmd 数据表文件")
    p.add_argument("--file", required=True, help=".dmd 数据表文件路径")
    p.add_argument("--column", default=None, help="提取指定列的值")
    p.add_argument("--to-json", action="store_true", dest="to_json_output", default=False,
                   help="同时输出全部记录数据")

    p = subparsers.add_parser("datatable-save", parents=[format_parent], help="保存 .dmd 数据表文件")
    p.add_argument("--file", required=True, help=".dmd 数据表文件路径")

    p = subparsers.add_parser("datatable-add-record", parents=[format_parent], help="向 .dmd 数据表添加记录并保存")
    p.add_argument("--file", required=True, help=".dmd 数据表文件路径")
    p.add_argument("--values", required=True, help="JSON 记录数组 [{\"key\":\"val\",...},...],如 '[{\"HoleID\":\"ZK001\",\"Depth\":500.0}]'")

    p = subparsers.add_parser("datatable-set", parents=[format_parent], help="设置 .dmd 数据表字段值并保存")
    p.add_argument("--file", required=True, help=".dmd 数据表文件路径")
    p.add_argument("--index", type=int, required=True, help="记录索引(从0开始)")
    p.add_argument("--field", required=True, help="字段名")
    p.add_argument("--value", required=True, help="新值")