mirror of
https://github.com/langbot-app/LangBot.git
synced 2025-11-25 03:15:06 +08:00
feat: 标识符生成器模块
This commit is contained in:
43
tests/identifier_test/host_identifier.py
Normal file
43
tests/identifier_test/host_identifier.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
import uuid
|
||||
import json
|
||||
|
||||
# 向 ~/.qchatgpt 写入一个 标识符
|
||||
|
||||
if not os.path.exists(os.path.expanduser('~/.qchatgpt')):
|
||||
os.mkdir(os.path.expanduser('~/.qchatgpt'))
|
||||
|
||||
identifier = {
|
||||
"host_id": "host_"+str(uuid.uuid4()),
|
||||
}
|
||||
|
||||
if not os.path.exists(os.path.expanduser('~/.qchatgpt/host.json')):
|
||||
print('create ~/.qchatgpt/host.json')
|
||||
with open(os.path.expanduser('~/.qchatgpt/host.json'), 'w') as f:
|
||||
json.dump(identifier, f)
|
||||
else:
|
||||
print('load ~/.qchatgpt/host.json')
|
||||
with open(os.path.expanduser('~/.qchatgpt/host.json'), 'r') as f:
|
||||
identifier = json.load(f)
|
||||
|
||||
print(identifier)
|
||||
|
||||
instance_id = {
|
||||
"host_id": identifier['host_id'],
|
||||
"instance_id": "instance_"+str(uuid.uuid4()),
|
||||
}
|
||||
|
||||
# 实例 id
|
||||
if os.path.exists("res/instance_id.json"):
|
||||
with open("res/instance_id.json", 'r') as f:
|
||||
instance_id = json.load(f)
|
||||
|
||||
if instance_id['host_id'] != identifier['host_id']:
|
||||
os.remove("res/instance_id.json")
|
||||
|
||||
if not os.path.exists("res/instance_id.json"):
|
||||
print('create res/instance_id.json')
|
||||
with open("res/instance_id.json", 'w') as f:
|
||||
json.dump(instance_id, f)
|
||||
|
||||
print(instance_id)
|
||||
Reference in New Issue
Block a user