Skip to content

Commit cb340f4

Browse files
committed
feat: sync when created
1 parent 63fd6d7 commit cb340f4

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

importer.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ async def upload_img(path):
1414
'Authorization': 'Token ' + API_KEY,
1515
}
1616
files = {
17-
'FileUpload': (IMPORT_PATH + path, open(IMPORT_PATH + path, 'rb')),
17+
'FileUpload': (path, open(path, 'rb')),
1818
}
19+
print("已上传:" + path)
1920
return requests.post(LABEL_STUDIO_URL + '/api/projects/' + str(PROJ_ID) + '/import',
2021
headers=headers, files=files)
2122

@@ -31,6 +32,7 @@ async def main():
3132
for root, directory, file in os.walk(IMPORT_PATH):
3233
break
3334
response_table = []
35+
map(lambda str: IMPORT_PATH + str, file)
3436
for uploader in asyncio.as_completed(map(upload_img, file)):
3537
response_table.append((await uploader).json())
3638
[print(item) for item in response_table]

syncer.py

+47-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,47 @@
1-
import json
1+
import time
2+
import asyncio
23

3-
import requests
44
from label_studio_sdk import Client
5+
from watchdog.events import PatternMatchingEventHandler
6+
from watchdog.observers import Observer
57

68
from const import LABEL_STUDIO_URL, API_KEY
9+
from importer import upload_img
10+
11+
watch_patterns = "*.py;*.txt" # 监控文件的模式
12+
ignore_patterns = "" # 设置忽略的文件模式
13+
ignore_directories = False # 是否忽略文件夹变化
14+
case_sensitive = True # 是否对大小写敏感
15+
event_handler = PatternMatchingEventHandler(
16+
watch_patterns, ignore_patterns, ignore_directories, case_sensitive)
17+
18+
19+
def on_created(event):
20+
print(str(event.src_path) + "被创建")
21+
asyncio.run(upload_img(event.src_path))
22+
23+
24+
def on_deleted(event):
25+
print(f"{event.src_path}被删除")
26+
27+
28+
def on_modified(event):
29+
print(f"{event.src_path} 被修改")
30+
31+
32+
def on_moved(event):
33+
print(f"{event.src_path}被移动到{event.dest_path}")
34+
35+
36+
event_handler.on_created = on_created
37+
event_handler.on_deleted = on_deleted
38+
event_handler.on_modified = on_modified
39+
event_handler.on_moved = on_moved
40+
41+
watch_path = "/home/ethanjia/dev/label-studio-pipeline/import" # 监控目录
42+
go_recursively = True # 是否监控子文件夹
43+
my_observer = Observer()
44+
my_observer.schedule(event_handler, watch_path, recursive=go_recursively)
745

846

947
def main():
@@ -14,25 +52,13 @@ def main():
1452
pass
1553
else:
1654
print('Connection Succeeds!')
17-
headers = {
18-
'Authorization': 'Token ' + API_KEY,
19-
'Content-Type': 'application/json',
20-
}
21-
data = {
22-
"path": "/home/ethanjia/dev/label-studio-pipeline/img",
23-
"regex_filter": "",
24-
"use_blob_urls": True,
25-
"title": "test sync",
26-
"description": "test sync",
27-
"last_sync": "2019-08-24T14:15:22Z",
28-
"last_sync_count": 0,
29-
"project": 1
30-
}
31-
# FIXME 这里的id不知道是啥
32-
response = requests.post(
33-
'http://localhost:8080/api/storages/localfiles/1/sync', headers=headers,
34-
data=json.dumps(data, separators=(',', ':')))
35-
print(response.json())
55+
my_observer.start()
56+
try:
57+
while True:
58+
time.sleep(1)
59+
except KeyboardInterrupt:
60+
my_observer.stop()
61+
my_observer.join()
3662

3763

3864
if __name__ == '__main__':

0 commit comments

Comments
 (0)