保存环境变量表格的更改 Args: data: Dataframe数据,可能是pandas DataFrame对象 Returns: str: 操作状态信息,包含HTML格式的状态消息
(data)
| 813 | |
| 814 | |
| 815 | def save_env_table_changes(data): |
| 816 | """保存环境变量表格的更改 |
| 817 | |
| 818 | Args: |
| 819 | data: Dataframe数据,可能是pandas DataFrame对象 |
| 820 | |
| 821 | Returns: |
| 822 | str: 操作状态信息,包含HTML格式的状态消息 |
| 823 | """ |
| 824 | try: |
| 825 | logging.info( |
| 826 | f"Starting to process environment variable table data, type: {type(data)}" |
| 827 | ) |
| 828 | |
| 829 | # 获取当前所有环境变量 |
| 830 | current_env_vars = load_env_vars() |
| 831 | processed_keys = set() # 记录已处理的键,用于检测删除的变量 |
| 832 | |
| 833 | # 处理pandas DataFrame对象 |
| 834 | import pandas as pd |
| 835 | |
| 836 | if isinstance(data, pd.DataFrame): |
| 837 | # 获取列名信息 |
| 838 | columns = data.columns.tolist() |
| 839 | logging.info(f"DataFrame column names: {columns}") |
| 840 | |
| 841 | # 遍历DataFrame的每一行 |
| 842 | for index, row in data.iterrows(): |
| 843 | # 使用列名访问数据 |
| 844 | if len(columns) >= 3: |
| 845 | # 获取变量名和值 (第0列是变量名,第1列是值) |
| 846 | key = row[0] if isinstance(row, pd.Series) else row.iloc[0] |
| 847 | value = row[1] if isinstance(row, pd.Series) else row.iloc[1] |
| 848 | |
| 849 | # 检查是否为空行或已删除的变量 |
| 850 | if key and str(key).strip(): # 如果键名不为空,则添加或更新 |
| 851 | logging.info(f"Processing environment variable: {key} = {value}") |
| 852 | add_env_var(key, str(value)) |
| 853 | processed_keys.add(key) |
| 854 | # 处理其他格式 |
| 855 | elif isinstance(data, dict): |
| 856 | logging.info(f"Dictionary format data keys: {list(data.keys())}") |
| 857 | # 如果是字典格式,尝试不同的键 |
| 858 | if "data" in data: |
| 859 | rows = data["data"] |
| 860 | elif "values" in data: |
| 861 | rows = data["values"] |
| 862 | elif "value" in data: |
| 863 | rows = data["value"] |
| 864 | else: |
| 865 | # 尝试直接使用字典作为行数据 |
| 866 | rows = [] |
| 867 | for key, value in data.items(): |
| 868 | if key not in ["headers", "types", "columns"]: |
| 869 | rows.append([key, value]) |
| 870 | |
| 871 | if isinstance(rows, list): |
| 872 | for row in rows: |
nothing calls this directly
no test coverage detected