-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CONVERSION_FIELDS.py
154 lines (114 loc) · 6.75 KB
/
CONVERSION_FIELDS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
116
117
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
import configparser
import argparse
from LIST_FIELDS import LIST_FIELDS
from UPDATE_FIELD import UPDATE_FIELD
def CONVERSION_FIELDS_HUMAN_TO_MACHINE(app_token=None, table_id=None, view_id=None, page_token=None, page_size=None, config_file=None):
if config_file is None:
config_file = 'feishu-config.ini'
# 读取配置文件
config = configparser.ConfigParser()
config.read(config_file, encoding='utf-8')
# 如果参数为空,则使用配置文件中的默认值
if not app_token:
app_token = config.get('TOKEN', 'app_token')
if not table_id:
table_id = config.get('ID', 'table_id')
field_type = 1
fields_map = dict(config.items('FIELD_MAP'))
# 获取当前的字段
current_fields = LIST_FIELDS(app_token=app_token, table_id=table_id, view_id=view_id, page_token=page_token,
page_size=page_size, config_file=config_file)
if 'data' in current_fields and isinstance(current_fields['data'], dict):
for field in current_fields['data']['items']:
# 检查当前字段是否在映射中
if field['field_name'] in fields_map:
field_id = field['field_id'] # 获取字段的ID
field_name = fields_map[field['field_name']] # 获取字段的映射名称
# 如果在,则更新字段名
UPDATE_FIELD(app_token=app_token, table_id=table_id, field_id=field_id, field_name=field_name,
field_type=field_type)
# 打印处理后的数据
print(f"Field '{field['field_name']}' converted to '{field_name}'")
else:
# 当没有有效数据时的处理逻辑
print("没有获取到有效的字段数据")
def CONVERSION_FIELDS_MACHINE_TO_HUMAN_V1(app_token=None, table_id=None, view_id=None, page_token=None, page_size=None, config_file=None):
if config_file is None:
config_file = 'feishu-config.ini'
# 读取配置文件
config = configparser.ConfigParser()
config.read(config_file, encoding='utf-8')
# 如果参数为空,则使用配置文件中的默认值
if not app_token:
app_token = config.get('TOKEN', 'app_token')
if not table_id:
table_id = config.get('ID', 'table_id')
fields_map = dict(config.items('FIELD_MAP'))
# 获取当前的字段
current_fields = LIST_FIELDS(app_token=app_token, table_id=table_id, view_id=view_id, page_token=page_token, page_size=page_size, config_file=config_file)
# 反转字典映射
reversed_fields_map = {v: k for k, v in fields_map.items()}
field_type=1
# 遍历当前的字段
for field in current_fields['data']['items']:
# 检查当前字段是否在反转映射中
if field['field_name'] in reversed_fields_map:
# 如果在,则更新字段名
#UPDATE_FIELD(app_token=app_token, table_id=table_id, field_id=field['field_id'], field_name=reversed_fields_map[field['field_name']], field_type=1)
field_id = field['field_id'] # 获取字段的ID
field_name = reversed_fields_map[field['field_name']] # 获取字段的映射名称
UPDATE_FIELD(app_token=app_token, table_id=table_id, field_id=field_id, field_name=field_name, field_type=field_type)
# 打印处理后的数据
print(f"Field '{field['field_name']}' converted to '{field_name}'")
def CONVERSION_FIELDS_MACHINE_TO_HUMAN(app_token=None, table_id=None, view_id=None, page_token=None, page_size=None, config_file=None):
if config_file is None:
config_file = 'feishu-config.ini'
# 读取配置文件
config = configparser.ConfigParser()
config.read(config_file, encoding='utf-8')
# 如果参数为空,则使用配置文件中的默认值
if not app_token:
app_token = config.get('TOKEN', 'app_token')
if not table_id:
table_id = config.get('ID', 'table_id')
fields_map = dict(config.items('FIELD_MAP'))
# 获取当前的字段
current_fields = LIST_FIELDS(app_token=app_token, table_id=table_id, view_id=view_id, page_token=page_token, page_size=page_size, config_file=config_file)
# 反转字典映射
reversed_fields_map = {v: k for k, v in fields_map.items()}
field_type=1
if 'data' in current_fields and isinstance(current_fields['data'], dict):
for field in current_fields['data']['items']:
# 检查当前字段是否在映射中
if field['field_name'] in reversed_fields_map:
# 如果在,则更新字段名
#UPDATE_FIELD(app_token=app_token, table_id=table_id, field_id=field['field_id'], field_name=reversed_fields_map[field['field_name']], field_type=1)
field_id = field['field_id'] # 获取字段的ID
field_name = reversed_fields_map[field['field_name']] # 获取字段的映射名称
UPDATE_FIELD(app_token=app_token, table_id=table_id, field_id=field_id, field_name=field_name, field_type=field_type)
# 打印处理后的数据
print(f"Field '{field['field_name']}' converted to '{field_name}'")
else:
# 当没有有效数据时的处理逻辑
print("没有获取到有效的字段数据")
def CONVERSION_FIELDS_CMD():
parser = argparse.ArgumentParser(description='Human-to-Machine and Machine-to-Human Field Name Conversion')
parser.add_argument('-c', '--convert_to_machine', action='store_true', help='Convert human field names to machine field names')
parser.add_argument('-b', '--convert_to_human', action='store_true', help='Convert machine field names to human field names')
parser.add_argument('--app_token', default=None, help='App Token')
parser.add_argument('--table_id', default=None, help='Table ID')
parser.add_argument('--view_id', default=None, help='View ID')
parser.add_argument('--page_token', default=None, help='Page Token')
parser.add_argument('--page_size', default=None, help='Page Size')
parser.add_argument('--config_file', default='feishu-config.ini', help='Config file path')
args = parser.parse_args()
if args.convert_to_machine:
CONVERSION_FIELDS_HUMAN_TO_MACHINE(app_token=args.app_token, table_id=args.table_id, view_id=args.view_id,
page_token=args.page_token, page_size=args.page_size, config_file=args.config_file)
elif args.convert_to_human:
CONVERSION_FIELDS_MACHINE_TO_HUMAN(app_token=args.app_token, table_id=args.table_id, view_id=args.view_id,
page_token=args.page_token, page_size=args.page_size, config_file=args.config_file)
else:
print("Please specify either -c or -b option for field name conversion.")
if __name__ == '__main__':
CONVERSION_FIELDS_CMD()