Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -66,23 +66,19 @@ async def send_html_chunks(websocket, soup):
|
|
66 |
|
67 |
await websocket.send(json.dumps({"type": "end",}))
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
# await ws.send(chunk_message.encode())
|
83 |
-
|
84 |
-
# # Wait for a short interval before sending the next chunk
|
85 |
-
# await asyncio.sleep(0.1) # Adjust as needed
|
86 |
|
87 |
def auto_detect_mode(content):
|
88 |
if isinstance(content, str):
|
@@ -282,7 +278,7 @@ async def get_file_content(websocket, key,project_name, path,rpath,name,connecte
|
|
282 |
'rfilePath':rpath,
|
283 |
'filePath':path,
|
284 |
}
|
285 |
-
await websocket
|
286 |
except UnicodeDecodeError:
|
287 |
with open(file_path, 'rb') as file:
|
288 |
content = file.read()
|
@@ -293,13 +289,13 @@ async def get_file_content(websocket, key,project_name, path,rpath,name,connecte
|
|
293 |
'rfilePath':rpath,
|
294 |
'filePath':path,
|
295 |
}
|
296 |
-
await websocket
|
297 |
except Exception as e:
|
298 |
event = {
|
299 |
"type": "error",
|
300 |
"message": f"Failed to read file content: {str(e)}",
|
301 |
}
|
302 |
-
await websocket
|
303 |
|
304 |
|
305 |
|
@@ -371,14 +367,14 @@ async def read_process_output(process, websocket):
|
|
371 |
"data": word.decode('utf-8'),
|
372 |
}
|
373 |
print('sending data',1)
|
374 |
-
await websocket
|
375 |
word = b''
|
376 |
print("line is completed")
|
377 |
event = {
|
378 |
"type": "terminal-newline",
|
379 |
"data": "",
|
380 |
}
|
381 |
-
await websocket
|
382 |
elif char == b' ':
|
383 |
if word:
|
384 |
print(word.decode('utf-8'),2)
|
@@ -387,7 +383,7 @@ async def read_process_output(process, websocket):
|
|
387 |
"data": word.decode('utf-8'),
|
388 |
}
|
389 |
print('sending data',2)
|
390 |
-
await websocket
|
391 |
word = b''
|
392 |
print("space is occurred")
|
393 |
event = {
|
@@ -395,7 +391,7 @@ async def read_process_output(process, websocket):
|
|
395 |
"data": " ",
|
396 |
}
|
397 |
print('sending data',3)
|
398 |
-
await websocket
|
399 |
elif char == b'':
|
400 |
if word:
|
401 |
print(word.decode('utf-8'),3)
|
@@ -404,7 +400,7 @@ async def read_process_output(process, websocket):
|
|
404 |
"data": word.decode('utf-8'),
|
405 |
}
|
406 |
print('sending data',4)
|
407 |
-
await websocket
|
408 |
break
|
409 |
else:
|
410 |
word += char
|
@@ -418,14 +414,14 @@ async def read_process_output(process, websocket):
|
|
418 |
"type": "terminal-error",
|
419 |
"data": word.decode('utf-8'),
|
420 |
}
|
421 |
-
await websocket
|
422 |
word = b''
|
423 |
print("line is completed")
|
424 |
event = {
|
425 |
"type": "terminal-newline",
|
426 |
"data": "",
|
427 |
}
|
428 |
-
await websocket
|
429 |
elif char == b' ':
|
430 |
if word:
|
431 |
print(word.decode('utf-8'))
|
@@ -433,14 +429,14 @@ async def read_process_output(process, websocket):
|
|
433 |
"type": "terminal-error",
|
434 |
"data": word.decode('utf-8'),
|
435 |
}
|
436 |
-
await websocket
|
437 |
word = b''
|
438 |
print("space is occurred")
|
439 |
event = {
|
440 |
"type": "terminal-error",
|
441 |
"data": " ",
|
442 |
}
|
443 |
-
await websocket
|
444 |
elif char == b'':
|
445 |
if word:
|
446 |
print(word.decode('utf-8'))
|
@@ -454,7 +450,7 @@ async def read_process_output(process, websocket):
|
|
454 |
# "type": "terminal-data",
|
455 |
# "data": line.strip().decode('utf-8'),
|
456 |
# }
|
457 |
-
# await websocket
|
458 |
|
459 |
|
460 |
# async for line in process.stderr:
|
@@ -463,7 +459,7 @@ async def read_process_output(process, websocket):
|
|
463 |
# "type": "terminal-error",
|
464 |
# "data": line.strip().decode('utf-8'),
|
465 |
# }
|
466 |
-
# await websocket
|
467 |
|
468 |
async def handle_user_input(websocket,key, process, connected,process_ids):
|
469 |
while True:
|
@@ -529,14 +525,14 @@ async def handle_user_input(websocket,key, process, connected,process_ids):
|
|
529 |
"type": "terminal-data-end",
|
530 |
|
531 |
}
|
532 |
-
await websocket
|
533 |
|
534 |
else:
|
535 |
event = {
|
536 |
"type": "terminal-data-end",
|
537 |
|
538 |
}
|
539 |
-
await websocket
|
540 |
|
541 |
process_ids.remove(id)
|
542 |
|
@@ -848,7 +844,7 @@ async def create_file_structure(websocket, data, base_path='.'):
|
|
848 |
"type": "msg",
|
849 |
"message": "project created",
|
850 |
}
|
851 |
-
await websocket
|
852 |
|
853 |
|
854 |
async def error(websocket, message):
|
@@ -860,7 +856,7 @@ async def error(websocket, message):
|
|
860 |
"type": "error",
|
861 |
"message": message,
|
862 |
}
|
863 |
-
await websocket
|
864 |
|
865 |
|
866 |
async def exe(websocket,connected,key,process_ids):
|
@@ -869,332 +865,341 @@ async def exe(websocket,connected,key,process_ids):
|
|
869 |
|
870 |
"""
|
871 |
# print('in exe')
|
|
|
872 |
async for message in websocket:
|
873 |
# Parse a "play" event from the UI.
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
-
|
895 |
-
|
896 |
-
|
897 |
-
|
|
|
898 |
try:
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
-
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
909 |
|
910 |
-
|
911 |
-
|
912 |
-
|
913 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
914 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
915 |
|
|
|
|
|
|
|
|
|
|
|
|
|
916 |
|
917 |
-
|
918 |
-
|
919 |
-
|
920 |
-
|
|
|
|
|
|
|
|
|
|
|
921 |
|
922 |
-
|
923 |
-
|
924 |
-
|
925 |
-
|
926 |
-
|
927 |
-
|
928 |
-
await websocket.send(json.dumps(event))
|
929 |
-
|
930 |
-
else:
|
931 |
-
event = {
|
932 |
-
"type": "terminal-data-end",
|
933 |
-
|
934 |
-
}
|
935 |
-
await websocket.send(json.dumps(event))
|
936 |
-
process_ids.remove(id)
|
937 |
|
938 |
-
|
939 |
-
|
940 |
-
|
941 |
-
|
942 |
-
|
943 |
-
|
944 |
-
|
945 |
-
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
952 |
-
|
953 |
-
|
954 |
-
|
955 |
-
|
956 |
-
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
-
|
961 |
-
|
962 |
-
|
963 |
-
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
972 |
|
973 |
-
|
974 |
-
|
975 |
-
|
976 |
-
|
977 |
-
|
978 |
-
|
979 |
-
|
980 |
-
|
981 |
-
|
982 |
-
|
983 |
-
|
984 |
-
|
985 |
-
|
986 |
-
|
987 |
-
|
988 |
-
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
-
|
993 |
-
|
994 |
-
|
995 |
-
|
996 |
-
|
|
|
997 |
|
998 |
-
|
999 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1000 |
|
1001 |
-
|
1002 |
-
|
1003 |
-
|
1004 |
-
|
1005 |
-
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
-
elif event["command"]["type"]=="create":
|
1010 |
-
if event["item"]=="folder":
|
1011 |
-
await create_folder(websocket, key,event["project_name"], event["path"],event["name"],event['root'],event['targetElementData'],event["rpath"], connected)
|
1012 |
-
else:
|
1013 |
-
await create_file(websocket, key,event["project_name"], event["path"],event["name"],event['root'],event['targetElementData'],event["rpath"], connected)
|
1014 |
-
|
1015 |
-
|
1016 |
-
elif event["command"]["type"]=="delete":
|
1017 |
-
|
1018 |
-
await delete_item(websocket, key,event["project_name"], event["path"],event['rpath'],event['targetElementData'], connected)
|
1019 |
-
elif event["command"]["type"]=="get_content":
|
1020 |
-
|
1021 |
-
await get_file_content(websocket, key,event["project_name"],event["filePath"], event["rfilePath"],event["fileName"] ,connected)
|
1022 |
-
elif event["command"]["type"]=="rename":
|
1023 |
-
|
1024 |
-
await rename_item(websocket, key,event["project_name"], event["path"],event['rpath'], event["name"], event["root"], connected)
|
1025 |
-
elif event["command"]["type"]=="join":
|
1026 |
-
await join(websocket, event["join"])
|
1027 |
-
elif event["command"]["type"]=="sendDir":
|
1028 |
-
data=json.loads(event["file_structure"])
|
1029 |
-
event = {
|
1030 |
-
"type": "createDir",
|
1031 |
-
|
1032 |
-
"path": data,
|
1033 |
-
"root":event['root'],
|
1034 |
-
|
1035 |
-
}
|
1036 |
-
# websockets.broadcast(connected,json.dumps(event))
|
1037 |
-
websockets.broadcast(connected, json.dumps(event))
|
1038 |
-
elif event["command"]["type"]=="createItemUI":
|
1039 |
-
|
1040 |
-
event = {
|
1041 |
-
"type": "createItemUI",
|
1042 |
-
|
1043 |
-
'targetElementData':event['targetElementData'],
|
1044 |
-
'data':event['data']
|
1045 |
-
|
1046 |
-
}
|
1047 |
-
# websockets.broadcast(connected,json.dumps(event))
|
1048 |
-
websockets.broadcast(connected, json.dumps(event))
|
1049 |
-
|
1050 |
-
elif event["command"]["type"]=="renameItemInUI":
|
1051 |
-
|
1052 |
-
event = {
|
1053 |
-
"type": "renameItemInUI",
|
1054 |
-
|
1055 |
-
'path':event['path'],
|
1056 |
-
'new_path':event['new_path'],
|
1057 |
-
'name':event['name'],
|
1058 |
-
'new_rpath':event['new_rpath'],
|
1059 |
-
|
1060 |
-
}
|
1061 |
-
# websockets.broadcast(connected,json.dumps(event))
|
1062 |
-
websockets.broadcast(connected, json.dumps(event))
|
1063 |
-
elif event["command"]["type"]=="createFolderUI":
|
1064 |
-
|
1065 |
-
event = {
|
1066 |
-
"type": "createFolderUI",
|
1067 |
-
|
1068 |
-
'targetElementData':event['targetElementData'],
|
1069 |
-
'data':event['data']
|
1070 |
-
|
1071 |
-
}
|
1072 |
-
# websockets.broadcast(connected,json.dumps(event))
|
1073 |
-
websockets.broadcast(connected, json.dumps(event))
|
1074 |
-
|
1075 |
-
elif event["command"]["type"]=="removeItemFromUI":
|
1076 |
-
|
1077 |
-
event = {
|
1078 |
-
"type": "removeItemFromUI",
|
1079 |
-
|
1080 |
-
'targetElementData':event['targetElementData'],
|
1081 |
-
'path':event['path']
|
1082 |
-
|
1083 |
-
}
|
1084 |
-
# websockets.broadcast(connected,json.dumps(event))
|
1085 |
-
websockets.broadcast(connected, json.dumps(event))
|
1086 |
-
|
1087 |
-
elif event["command"]["type"]=="project":
|
1088 |
-
base_path = os.path.join(os.getcwd(), 'projects',key, event["project_name"])
|
1089 |
-
data=json.loads(event["file_structure"])
|
1090 |
-
|
1091 |
-
await create_file_structure(websocket,data, base_path=base_path)
|
1092 |
-
# await rename_item(websocket, key,event["project_name"], event["path"], event["name"], connected)
|
1093 |
-
elif event["command"]["type"]=="collabration":
|
1094 |
-
|
1095 |
-
event = {
|
1096 |
-
"type": "collabration",
|
1097 |
-
'name': event["name"],
|
1098 |
-
'line': event["cursorPos-line"],
|
1099 |
-
'ch': event["cursorPos-ch"],
|
1100 |
-
'file':event["file"],
|
1101 |
-
'content': event["content"],
|
1102 |
-
'color':event["color"]
|
1103 |
-
}
|
1104 |
-
websockets.broadcast(connected, json.dumps(event))
|
1105 |
-
elif event["command"]["type"]=="cursor":
|
1106 |
-
|
1107 |
-
event = {
|
1108 |
-
"type": "cursor",
|
1109 |
-
'offset': event["offset"],
|
1110 |
-
'userid': event["userid"],
|
1111 |
-
'userlabel': event["userlabel"],
|
1112 |
-
'usercolor': event["usercolor"],
|
1113 |
-
'fileName':event["fileName"],
|
1114 |
|
1115 |
-
|
1116 |
-
websockets.broadcast(connected, json.dumps(event))
|
1117 |
-
elif event["command"]["type"]=="select":
|
1118 |
-
|
1119 |
-
event = {
|
1120 |
-
"type": "select",
|
1121 |
-
'id': event["id"],
|
1122 |
-
'startOffset': event["startOffset"],
|
1123 |
-
'endOffset': event["endOffset"],
|
1124 |
-
'userid': event["userid"],
|
1125 |
-
'userlabel': event["userlabel"],
|
1126 |
-
'usercolor': event["usercolor"],
|
1127 |
-
'fileName':event["fileName"],
|
1128 |
-
|
1129 |
-
}
|
1130 |
-
websockets.broadcast(connected, json.dumps(event))
|
1131 |
-
elif event["command"]["type"]=="addselection":
|
1132 |
-
|
1133 |
-
event = {
|
1134 |
-
"type": "addselection",
|
1135 |
-
|
1136 |
-
'userid': event["userid"],
|
1137 |
-
'userlabel': event["userlabel"],
|
1138 |
-
'usercolor': event["usercolor"],
|
1139 |
-
|
1140 |
|
1141 |
-
|
1142 |
-
|
1143 |
-
|
1144 |
-
|
1145 |
-
event
|
1146 |
-
|
1147 |
-
|
1148 |
-
'text': event["text"],
|
1149 |
-
'userid': event["userid"],
|
1150 |
-
'userlabel': event["userlabel"],
|
1151 |
-
'usercolor': event["usercolor"],
|
1152 |
-
'fileName':event["fileName"],
|
1153 |
-
'tabStatus':event['tabStatus'],
|
1154 |
-
|
1155 |
-
}
|
1156 |
-
websockets.broadcast(connected, json.dumps(event))
|
1157 |
-
elif event["command"]["type"]=="replace":
|
1158 |
-
|
1159 |
-
event = {
|
1160 |
-
"type": "replace",
|
1161 |
-
'index': event["index"],
|
1162 |
-
'length': event["length"],
|
1163 |
-
'text': event["text"],
|
1164 |
-
'userid': event["userid"],
|
1165 |
-
'userlabel': event["userlabel"],
|
1166 |
-
'usercolor': event["usercolor"],
|
1167 |
-
'fileName':event["fileName"],
|
1168 |
-
'tabStatus':event['tabStatus'],
|
1169 |
-
}
|
1170 |
-
websockets.broadcast(connected, json.dumps(event))
|
1171 |
-
elif event["command"]["type"]=="edelelte":
|
1172 |
-
|
1173 |
-
event = {
|
1174 |
-
"type": "edelelte",
|
1175 |
-
'index': event["index"],
|
1176 |
-
'length': event["length"],
|
1177 |
-
'userid': event["userid"],
|
1178 |
-
'userlabel': event["userlabel"],
|
1179 |
-
'usercolor': event["usercolor"],
|
1180 |
-
'fileName':event["fileName"],
|
1181 |
-
|
1182 |
-
|
1183 |
-
}
|
1184 |
-
websockets.broadcast(connected, json.dumps(event))
|
1185 |
-
elif event["command"]["type"]=="keep-alive":
|
1186 |
-
event = {
|
1187 |
-
"type": "keep-alive",
|
1188 |
-
|
1189 |
-
}
|
1190 |
-
|
1191 |
-
else:
|
1192 |
-
# First player starts a new game.
|
1193 |
-
pass
|
1194 |
-
except Exception as exc:
|
1195 |
-
# Send an "error" event if the move was illegal.
|
1196 |
-
await error(websocket, str(exc))
|
1197 |
-
continue
|
1198 |
|
1199 |
|
1200 |
async def start(websocket,events):
|
@@ -1215,7 +1220,7 @@ async def start(websocket,events):
|
|
1215 |
"join": join_key,
|
1216 |
|
1217 |
}
|
1218 |
-
await websocket
|
1219 |
|
1220 |
await exe(websocket,connected,join_key,process_ids)
|
1221 |
finally:
|
|
|
66 |
|
67 |
await websocket.send(json.dumps({"type": "end",}))
|
68 |
|
69 |
+
async def send_message_chunks(websocket, message):
|
70 |
+
msg_id = str(uuid.uuid4())
|
71 |
+
message_json = json.dumps(message)
|
72 |
+
chunks = [message_json[i:i+10] for i in range(0, len(message_json), 10)]
|
73 |
+
|
74 |
+
for i, chunk in enumerate(chunks):
|
75 |
+
chunk_data = {
|
76 |
+
'msg_id': msg_id,
|
77 |
+
'part': chunk,
|
78 |
+
'end': i == len(chunks) - 1
|
79 |
+
}
|
80 |
+
await websocket.send(json.dumps(chunk_data))
|
81 |
+
print(f"Sent chunk: {chunk_data}")
|
|
|
|
|
|
|
|
|
82 |
|
83 |
def auto_detect_mode(content):
|
84 |
if isinstance(content, str):
|
|
|
278 |
'rfilePath':rpath,
|
279 |
'filePath':path,
|
280 |
}
|
281 |
+
await send_message_chunks(websocket,event)
|
282 |
except UnicodeDecodeError:
|
283 |
with open(file_path, 'rb') as file:
|
284 |
content = file.read()
|
|
|
289 |
'rfilePath':rpath,
|
290 |
'filePath':path,
|
291 |
}
|
292 |
+
await await send_message_chunks(websocket,event)
|
293 |
except Exception as e:
|
294 |
event = {
|
295 |
"type": "error",
|
296 |
"message": f"Failed to read file content: {str(e)}",
|
297 |
}
|
298 |
+
await await send_message_chunks(websocket,event)
|
299 |
|
300 |
|
301 |
|
|
|
367 |
"data": word.decode('utf-8'),
|
368 |
}
|
369 |
print('sending data',1)
|
370 |
+
await send_message_chunks(websocket,event)
|
371 |
word = b''
|
372 |
print("line is completed")
|
373 |
event = {
|
374 |
"type": "terminal-newline",
|
375 |
"data": "",
|
376 |
}
|
377 |
+
await send_message_chunks(websocket,event)
|
378 |
elif char == b' ':
|
379 |
if word:
|
380 |
print(word.decode('utf-8'),2)
|
|
|
383 |
"data": word.decode('utf-8'),
|
384 |
}
|
385 |
print('sending data',2)
|
386 |
+
await send_message_chunks(websocket,event)
|
387 |
word = b''
|
388 |
print("space is occurred")
|
389 |
event = {
|
|
|
391 |
"data": " ",
|
392 |
}
|
393 |
print('sending data',3)
|
394 |
+
await send_message_chunks(websocket,event)
|
395 |
elif char == b'':
|
396 |
if word:
|
397 |
print(word.decode('utf-8'),3)
|
|
|
400 |
"data": word.decode('utf-8'),
|
401 |
}
|
402 |
print('sending data',4)
|
403 |
+
await send_message_chunks(websocket,event)
|
404 |
break
|
405 |
else:
|
406 |
word += char
|
|
|
414 |
"type": "terminal-error",
|
415 |
"data": word.decode('utf-8'),
|
416 |
}
|
417 |
+
await send_message_chunks(websocket,event)
|
418 |
word = b''
|
419 |
print("line is completed")
|
420 |
event = {
|
421 |
"type": "terminal-newline",
|
422 |
"data": "",
|
423 |
}
|
424 |
+
await send_message_chunks(websocket,event)
|
425 |
elif char == b' ':
|
426 |
if word:
|
427 |
print(word.decode('utf-8'))
|
|
|
429 |
"type": "terminal-error",
|
430 |
"data": word.decode('utf-8'),
|
431 |
}
|
432 |
+
await send_message_chunks(websocket,event)
|
433 |
word = b''
|
434 |
print("space is occurred")
|
435 |
event = {
|
436 |
"type": "terminal-error",
|
437 |
"data": " ",
|
438 |
}
|
439 |
+
await send_message_chunks(websocket,event)
|
440 |
elif char == b'':
|
441 |
if word:
|
442 |
print(word.decode('utf-8'))
|
|
|
450 |
# "type": "terminal-data",
|
451 |
# "data": line.strip().decode('utf-8'),
|
452 |
# }
|
453 |
+
# await send_message_chunks(websocket,event)
|
454 |
|
455 |
|
456 |
# async for line in process.stderr:
|
|
|
459 |
# "type": "terminal-error",
|
460 |
# "data": line.strip().decode('utf-8'),
|
461 |
# }
|
462 |
+
# await send_message_chunks(websocket,event)
|
463 |
|
464 |
async def handle_user_input(websocket,key, process, connected,process_ids):
|
465 |
while True:
|
|
|
525 |
"type": "terminal-data-end",
|
526 |
|
527 |
}
|
528 |
+
await send_message_chunks(websocket,event)
|
529 |
|
530 |
else:
|
531 |
event = {
|
532 |
"type": "terminal-data-end",
|
533 |
|
534 |
}
|
535 |
+
await send_message_chunks(websocket,event)
|
536 |
|
537 |
process_ids.remove(id)
|
538 |
|
|
|
844 |
"type": "msg",
|
845 |
"message": "project created",
|
846 |
}
|
847 |
+
await send_message_chunks(websocket,event)
|
848 |
|
849 |
|
850 |
async def error(websocket, message):
|
|
|
856 |
"type": "error",
|
857 |
"message": message,
|
858 |
}
|
859 |
+
await send_message_chunks(websocket,event)
|
860 |
|
861 |
|
862 |
async def exe(websocket,connected,key,process_ids):
|
|
|
865 |
|
866 |
"""
|
867 |
# print('in exe')
|
868 |
+
message_parts = {}
|
869 |
async for message in websocket:
|
870 |
# Parse a "play" event from the UI.
|
871 |
+
data = json.loads(message)
|
872 |
+
msg_id = data['msg_id']
|
873 |
+
part = data['part']
|
874 |
+
if msg_id not in message_parts:
|
875 |
+
message_parts[msg_id] = []
|
876 |
+
message_parts[msg_id].append(part)
|
877 |
+
if data['end']:
|
878 |
+
full_message = ''.join(message_parts[msg_id])
|
879 |
+
print(f"Received full message: {full_message}")
|
880 |
+
|
881 |
+
event = json.loads(full_message)
|
882 |
+
del message_parts[msg_id]
|
883 |
+
assert event["type"] == "cmd"
|
884 |
+
# command = event["command"]
|
885 |
+
print(event,2)
|
886 |
+
|
887 |
+
try:
|
888 |
+
if event["command"]["type"]=="shell":
|
889 |
+
base_path = os.path.join(os.getcwd(), 'projects', key,event["project_name"])
|
890 |
+
directory_path = base_path
|
891 |
+
event_handler = FileHandler(websocket,connected)
|
892 |
+
observer = Observer()
|
893 |
+
observer.schedule(event_handler, path=directory_path, recursive=True)
|
894 |
+
observer.start()
|
895 |
+
|
896 |
try:
|
897 |
+
# await execute_command(websocket, key,event["project_name"], event["command"]["command"], connected)
|
898 |
+
# base_path = os.path.join(os.getcwd(), 'projects', key,event["project_name"])
|
899 |
+
print(base_path)
|
900 |
+
mod_command = f'cd {base_path} && {event["command"]["command"]}'
|
901 |
+
print(mod_command)
|
902 |
+
|
903 |
+
try:
|
904 |
+
process = await asyncio.create_subprocess_shell(
|
905 |
+
mod_command,
|
906 |
+
# cwd=base_path,
|
907 |
+
stdin=asyncio.subprocess.PIPE,
|
908 |
+
stdout=asyncio.subprocess.PIPE,
|
909 |
+
stderr=asyncio.subprocess.PIPE,
|
910 |
+
# text=True,
|
911 |
+
)
|
912 |
+
id=process.pid
|
913 |
+
process_ids.append(id)
|
914 |
+
|
915 |
+
async def send_message(message):
|
916 |
+
# print('sending msg')
|
917 |
+
# await websocket.send(f'data: {message}')
|
918 |
+
websockets.broadcast(connected,json.dumps(message) )
|
919 |
+
|
920 |
+
|
921 |
+
|
922 |
+
await asyncio.gather(
|
923 |
+
handle_user_input(websocket,key, process, connected,process_ids),
|
924 |
+
read_process_output(process, websocket)
|
925 |
+
)
|
926 |
+
|
927 |
+
return_code = await process.wait()
|
928 |
+
if return_code == 0:
|
929 |
+
event = {
|
930 |
+
"type": "terminal-data-end",
|
931 |
+
|
932 |
+
}
|
933 |
+
await send_message_chunks(websocket,event)
|
934 |
+
|
935 |
+
else:
|
936 |
+
event = {
|
937 |
+
"type": "terminal-data-end",
|
938 |
+
|
939 |
+
}
|
940 |
+
await send_message_chunks(websocket,event)
|
941 |
+
process_ids.remove(id)
|
942 |
+
|
943 |
+
except Exception as e:
|
944 |
+
await error(websocket, str(e))
|
945 |
+
except KeyboardInterrupt:
|
946 |
+
pass # Handle KeyboardInterrupt to gracefully stop the observer
|
947 |
+
|
948 |
+
observer.stop()
|
949 |
+
observer.join()
|
950 |
+
|
951 |
+
elif event["command"]["type"]=="write":
|
952 |
|
953 |
+
await wirte_file(websocket, key,event["project_name"], event["path"], event["content"], connected)
|
954 |
+
elif event["command"]["type"] == "curl":
|
955 |
+
method = event.get("method", "GET")
|
956 |
+
url = event["url"]
|
957 |
+
body = event.get("data", "")
|
958 |
+
headers = event.get("headers", {})
|
959 |
+
|
960 |
+
if method.upper() == "GET":
|
961 |
+
response = requests.get(url, headers=headers)
|
962 |
+
elif method.upper() == "POST":
|
963 |
+
response = requests.post(url, data=body, headers=headers)
|
964 |
+
else:
|
965 |
+
response = {"error": "Unsupported method"}
|
966 |
+
|
967 |
+
if isinstance(response, dict):
|
968 |
+
response_data = {
|
969 |
+
"type": "error",
|
970 |
+
"data": response,
|
971 |
+
}
|
972 |
+
await websocket.send(json.dumps(response_data))
|
973 |
+
return
|
974 |
+
|
975 |
+
# Parse HTML content
|
976 |
+
souptmp = BeautifulSoup(response.text, "html.parser")
|
977 |
|
978 |
+
# Step 1: Define the initial HTML string
|
979 |
+
html_str = """
|
980 |
+
<!DOCTYPE html>
|
981 |
+
<html lang="en">
|
982 |
+
<head>
|
983 |
+
<meta charset="UTF-8">
|
984 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
985 |
+
|
986 |
+
</head>
|
987 |
+
<body>
|
988 |
+
<h1>Hello, World!</h1>
|
989 |
+
</body>
|
990 |
+
</html>
|
991 |
+
"""
|
992 |
+
|
993 |
+
# Step 2: Parse the initial HTML content
|
994 |
+
soup = BeautifulSoup(html_str, 'html.parser')
|
995 |
|
996 |
+
# Step 3: Replace the existing body content with the new content from souptmp
|
997 |
+
if soup.body:
|
998 |
+
soup.body.clear()
|
999 |
+
soup.body.append(souptmp)
|
1000 |
+
else:
|
1001 |
+
print("No <body> tag found in the HTML")
|
1002 |
|
1003 |
+
# Inline CSS and JavaScript
|
1004 |
+
soup = inline_resources(soup, url)
|
1005 |
+
|
1006 |
+
response_data = {
|
1007 |
+
"type": "web-data",
|
1008 |
+
"data": str(soup),
|
1009 |
+
}
|
1010 |
+
# await websocket.send(json.dumps(response_data))
|
1011 |
+
await send_html_chunks(websocket, soup)
|
1012 |
|
1013 |
+
|
1014 |
+
elif event["command"]["type"]=="create":
|
1015 |
+
if event["item"]=="folder":
|
1016 |
+
await create_folder(websocket, key,event["project_name"], event["path"],event["name"],event['root'],event['targetElementData'],event["rpath"], connected)
|
1017 |
+
else:
|
1018 |
+
await create_file(websocket, key,event["project_name"], event["path"],event["name"],event['root'],event['targetElementData'],event["rpath"], connected)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1019 |
|
1020 |
+
|
1021 |
+
elif event["command"]["type"]=="delete":
|
1022 |
+
|
1023 |
+
await delete_item(websocket, key,event["project_name"], event["path"],event['rpath'],event['targetElementData'], connected)
|
1024 |
+
elif event["command"]["type"]=="get_content":
|
1025 |
+
|
1026 |
+
await get_file_content(websocket, key,event["project_name"],event["filePath"], event["rfilePath"],event["fileName"] ,connected)
|
1027 |
+
elif event["command"]["type"]=="rename":
|
1028 |
+
|
1029 |
+
await rename_item(websocket, key,event["project_name"], event["path"],event['rpath'], event["name"], event["root"], connected)
|
1030 |
+
elif event["command"]["type"]=="join":
|
1031 |
+
await join(websocket, event["join"])
|
1032 |
+
elif event["command"]["type"]=="sendDir":
|
1033 |
+
data=json.loads(event["file_structure"])
|
1034 |
+
event = {
|
1035 |
+
"type": "createDir",
|
1036 |
+
|
1037 |
+
"path": data,
|
1038 |
+
"root":event['root'],
|
1039 |
+
|
1040 |
+
}
|
1041 |
+
# websockets.broadcast(connected,json.dumps(event))
|
1042 |
+
websockets.broadcast(connected, json.dumps(event))
|
1043 |
+
elif event["command"]["type"]=="createItemUI":
|
1044 |
+
|
1045 |
+
event = {
|
1046 |
+
"type": "createItemUI",
|
1047 |
+
|
1048 |
+
'targetElementData':event['targetElementData'],
|
1049 |
+
'data':event['data']
|
1050 |
+
|
1051 |
+
}
|
1052 |
+
# websockets.broadcast(connected,json.dumps(event))
|
1053 |
+
websockets.broadcast(connected, json.dumps(event))
|
1054 |
+
|
1055 |
+
elif event["command"]["type"]=="renameItemInUI":
|
1056 |
+
|
1057 |
+
event = {
|
1058 |
+
"type": "renameItemInUI",
|
1059 |
+
|
1060 |
+
'path':event['path'],
|
1061 |
+
'new_path':event['new_path'],
|
1062 |
+
'name':event['name'],
|
1063 |
+
'new_rpath':event['new_rpath'],
|
1064 |
+
|
1065 |
+
}
|
1066 |
+
# websockets.broadcast(connected,json.dumps(event))
|
1067 |
+
websockets.broadcast(connected, json.dumps(event))
|
1068 |
+
elif event["command"]["type"]=="createFolderUI":
|
1069 |
+
|
1070 |
+
event = {
|
1071 |
+
"type": "createFolderUI",
|
1072 |
+
|
1073 |
+
'targetElementData':event['targetElementData'],
|
1074 |
+
'data':event['data']
|
1075 |
+
|
1076 |
+
}
|
1077 |
+
# websockets.broadcast(connected,json.dumps(event))
|
1078 |
+
websockets.broadcast(connected, json.dumps(event))
|
1079 |
+
|
1080 |
+
elif event["command"]["type"]=="removeItemFromUI":
|
1081 |
+
|
1082 |
+
event = {
|
1083 |
+
"type": "removeItemFromUI",
|
1084 |
+
|
1085 |
+
'targetElementData':event['targetElementData'],
|
1086 |
+
'path':event['path']
|
1087 |
+
|
1088 |
+
}
|
1089 |
+
# websockets.broadcast(connected,json.dumps(event))
|
1090 |
+
websockets.broadcast(connected, json.dumps(event))
|
1091 |
+
|
1092 |
+
elif event["command"]["type"]=="project":
|
1093 |
+
base_path = os.path.join(os.getcwd(), 'projects',key, event["project_name"])
|
1094 |
+
data=json.loads(event["file_structure"])
|
1095 |
+
|
1096 |
+
await create_file_structure(websocket,data, base_path=base_path)
|
1097 |
+
# await rename_item(websocket, key,event["project_name"], event["path"], event["name"], connected)
|
1098 |
+
elif event["command"]["type"]=="collabration":
|
1099 |
+
|
1100 |
+
event = {
|
1101 |
+
"type": "collabration",
|
1102 |
+
'name': event["name"],
|
1103 |
+
'line': event["cursorPos-line"],
|
1104 |
+
'ch': event["cursorPos-ch"],
|
1105 |
+
'file':event["file"],
|
1106 |
+
'content': event["content"],
|
1107 |
+
'color':event["color"]
|
1108 |
+
}
|
1109 |
+
websockets.broadcast(connected, json.dumps(event))
|
1110 |
+
elif event["command"]["type"]=="cursor":
|
1111 |
+
|
1112 |
+
event = {
|
1113 |
+
"type": "cursor",
|
1114 |
+
'offset': event["offset"],
|
1115 |
+
'userid': event["userid"],
|
1116 |
+
'userlabel': event["userlabel"],
|
1117 |
+
'usercolor': event["usercolor"],
|
1118 |
+
'fileName':event["fileName"],
|
1119 |
|
1120 |
+
}
|
1121 |
+
websockets.broadcast(connected, json.dumps(event))
|
1122 |
+
elif event["command"]["type"]=="select":
|
1123 |
+
|
1124 |
+
event = {
|
1125 |
+
"type": "select",
|
1126 |
+
'id': event["id"],
|
1127 |
+
'startOffset': event["startOffset"],
|
1128 |
+
'endOffset': event["endOffset"],
|
1129 |
+
'userid': event["userid"],
|
1130 |
+
'userlabel': event["userlabel"],
|
1131 |
+
'usercolor': event["usercolor"],
|
1132 |
+
'fileName':event["fileName"],
|
1133 |
+
|
1134 |
+
}
|
1135 |
+
websockets.broadcast(connected, json.dumps(event))
|
1136 |
+
elif event["command"]["type"]=="addselection":
|
1137 |
+
|
1138 |
+
event = {
|
1139 |
+
"type": "addselection",
|
1140 |
+
|
1141 |
+
'userid': event["userid"],
|
1142 |
+
'userlabel': event["userlabel"],
|
1143 |
+
'usercolor': event["usercolor"],
|
1144 |
+
|
1145 |
|
1146 |
+
}
|
1147 |
+
websockets.broadcast(connected, json.dumps(event))
|
1148 |
+
elif event["command"]["type"]=="insert":
|
1149 |
+
|
1150 |
+
event = {
|
1151 |
+
"type": "insert",
|
1152 |
+
'index': event["index"],
|
1153 |
+
'text': event["text"],
|
1154 |
+
'userid': event["userid"],
|
1155 |
+
'userlabel': event["userlabel"],
|
1156 |
+
'usercolor': event["usercolor"],
|
1157 |
+
'fileName':event["fileName"],
|
1158 |
+
'tabStatus':event['tabStatus'],
|
1159 |
+
|
1160 |
+
}
|
1161 |
+
websockets.broadcast(connected, json.dumps(event))
|
1162 |
+
elif event["command"]["type"]=="replace":
|
1163 |
+
|
1164 |
+
event = {
|
1165 |
+
"type": "replace",
|
1166 |
+
'index': event["index"],
|
1167 |
+
'length': event["length"],
|
1168 |
+
'text': event["text"],
|
1169 |
+
'userid': event["userid"],
|
1170 |
+
'userlabel': event["userlabel"],
|
1171 |
+
'usercolor': event["usercolor"],
|
1172 |
+
'fileName':event["fileName"],
|
1173 |
+
'tabStatus':event['tabStatus'],
|
1174 |
+
}
|
1175 |
+
websockets.broadcast(connected, json.dumps(event))
|
1176 |
+
elif event["command"]["type"]=="edelelte":
|
1177 |
+
|
1178 |
+
event = {
|
1179 |
+
"type": "edelelte",
|
1180 |
+
'index': event["index"],
|
1181 |
+
'length': event["length"],
|
1182 |
+
'userid': event["userid"],
|
1183 |
+
'userlabel': event["userlabel"],
|
1184 |
+
'usercolor': event["usercolor"],
|
1185 |
+
'fileName':event["fileName"],
|
1186 |
|
1187 |
+
|
1188 |
+
}
|
1189 |
+
websockets.broadcast(connected, json.dumps(event))
|
1190 |
+
elif event["command"]["type"]=="keep-alive":
|
1191 |
+
event = {
|
1192 |
+
"type": "keep-alive",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1193 |
|
1194 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1195 |
|
1196 |
+
else:
|
1197 |
+
# First player starts a new game.
|
1198 |
+
pass
|
1199 |
+
except Exception as exc:
|
1200 |
+
# Send an "error" event if the move was illegal.
|
1201 |
+
await error(websocket, str(exc))
|
1202 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1203 |
|
1204 |
|
1205 |
async def start(websocket,events):
|
|
|
1220 |
"join": join_key,
|
1221 |
|
1222 |
}
|
1223 |
+
await send_message_chunks(websocket,event)
|
1224 |
|
1225 |
await exe(websocket,connected,join_key,process_ids)
|
1226 |
finally:
|