Ramesh-vani commited on
Commit
7cbdc18
·
verified ·
1 Parent(s): 4c1e6b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -39,9 +39,9 @@ async def generate_file_structure(path='.', encoding='utf-8'):
39
 
40
  return file_structure
41
 
42
- async def rename_item(websocket, project_name, path,new_name, connected):
43
 
44
- old_path = os.path.join(os.getcwd(), 'projects', project_name, path)
45
  new_name = new_name
46
 
47
  try:
@@ -67,10 +67,10 @@ async def rename_item(websocket, project_name, path,new_name, connected):
67
  except Exception as e:
68
  websockets.broadcast(connected,str(e))
69
 
70
- async def delete_item(websocket, project_name, path, connected):
71
  try:
72
 
73
- item_path = os.path.join(os.getcwd(), 'projects', project_name, path)
74
 
75
  if os.path.exists(item_path):
76
  if os.path.isdir(item_path):
@@ -94,8 +94,8 @@ async def delete_item(websocket, project_name, path, connected):
94
  # print(e)
95
  websockets.broadcast(connected,str(e))
96
 
97
- async def create_file(websocket, project_name, path,name, connected):
98
- file_path = os.path.join(os.getcwd(), 'projects', project_name, path,name)
99
  # Create the file
100
  with open(file_path, 'w'):
101
  pass
@@ -106,8 +106,8 @@ async def create_file(websocket, project_name, path,name, connected):
106
  websockets.broadcast(connected,json.dumps(event))
107
 
108
 
109
- async def create_folder(websocket, project_name, path,name, connected):
110
- folder_path = os.path.join(os.getcwd(), 'projects', project_name, path,name)
111
  # Create the folder
112
  os.makedirs(folder_path)
113
  event = {
@@ -117,9 +117,9 @@ async def create_folder(websocket, project_name, path,name, connected):
117
  websockets.broadcast(connected,json.dumps(event))
118
 
119
 
120
- async def wirte_file(websocket, project_name, path, content, connected):
121
  try:
122
- file_path = os.path.join(os.getcwd(), 'projects', project_name, path)
123
  file_content = content
124
  with open(file_path, 'w', encoding='utf-8') as file:
125
  file.write(file_content)
@@ -138,13 +138,14 @@ async def wirte_file(websocket, project_name, path, content, connected):
138
 
139
 
140
 
141
- async def execute_command(websocket, project_name, command, connected):
142
- base_path = os.path.join(os.getcwd(), 'projects', project_name)
 
143
 
144
  try:
145
  process = await asyncio.create_subprocess_shell(
146
- command,
147
- cwd=base_path,
148
  stdin=asyncio.subprocess.PIPE,
149
  stdout=asyncio.subprocess.PIPE,
150
  stderr=asyncio.subprocess.PIPE,
@@ -270,7 +271,7 @@ async def play(websocket, game, player, connected):
270
  }
271
  websockets.broadcast(connected, json.dumps(event))
272
 
273
- async def exe(websocket,connected):
274
  """
275
  Receive and process moves from a player.
276
 
@@ -285,23 +286,23 @@ async def exe(websocket,connected):
285
 
286
  try:
287
  if event["command"]["type"]=="shell":
288
- await execute_command(websocket, event["project_name"], event["command"]["command"], connected)
289
  elif event["command"]["type"]=="write":
290
 
291
- await wirte_file(websocket, event["project_name"], event["path"], event["content"], connected)
292
  elif event["command"]["type"]=="create":
293
  if event["item"]=="folder":
294
- await create_folder(websocket, event["project_name"], event["path"],event["name"], connected)
295
  else:
296
- await create_file(websocket, event["project_name"], event["path"],event["name"], connected)
297
 
298
 
299
  elif event["command"]["type"]=="delete":
300
 
301
- await delete_item(websocket, event["project_name"], event["path"], connected)
302
  elif event["command"]["type"]=="rename":
303
 
304
- await rename_item(websocket, event["project_name"], event["path"], event["name"], connected)
305
  elif event["command"]["type"]=="collabration":
306
 
307
  event = {
@@ -366,18 +367,18 @@ async def start(websocket,events):
366
 
367
  js = json.loads(events)
368
  assert js["type"] == "init"
369
- base_path = os.path.join(os.getcwd(), 'projects', js["project_name"])
370
  data=json.loads(js["file_structure"])
371
  # Receive and process moves from the first player.
372
  # await play(websocket, game, PLAYER1, connected)
373
  await create_file_structure(websocket,data, base_path=base_path)
374
- await exe(websocket,connected)
375
  finally:
376
  del JOIN[join_key]
377
  del WATCH[watch_key]
378
 
379
 
380
- async def join(websocket, join_key):
381
  """
382
  Handle a connection from the second player: join an existing game.
383
 
@@ -386,16 +387,22 @@ async def join(websocket, join_key):
386
  try:
387
  game, connected = JOIN[join_key]
388
  except KeyError:
389
- await error(websocket, "Game not found.")
390
  return
391
 
392
  # Register to receive moves from this game.
393
  connected.add(websocket)
394
  try:
 
395
  # Send the first move, in case the first player already played it.
396
- await replay(websocket, game)
397
  # Receive and process moves from the second player.
398
- await play(websocket, game, PLAYER2, connected)
 
 
 
 
 
399
  finally:
400
  connected.remove(websocket)
401
 
 
39
 
40
  return file_structure
41
 
42
+ async def rename_item(websocket, key,project_name, path,new_name, connected):
43
 
44
+ old_path = os.path.join(os.getcwd(),'projects', key,project_name, path)
45
  new_name = new_name
46
 
47
  try:
 
67
  except Exception as e:
68
  websockets.broadcast(connected,str(e))
69
 
70
+ async def delete_item(websocket, key,project_name, path, connected):
71
  try:
72
 
73
+ item_path = os.path.join(os.getcwd(), 'projects', key,project_name, path)
74
 
75
  if os.path.exists(item_path):
76
  if os.path.isdir(item_path):
 
94
  # print(e)
95
  websockets.broadcast(connected,str(e))
96
 
97
+ async def create_file(websocket, key,project_name, path,name, connected):
98
+ file_path = os.path.join(os.getcwd(), 'projects', ,keyproject_name, path,name)
99
  # Create the file
100
  with open(file_path, 'w'):
101
  pass
 
106
  websockets.broadcast(connected,json.dumps(event))
107
 
108
 
109
+ async def create_folder(websocket, key,project_name, path,name, connected):
110
+ folder_path = os.path.join(os.getcwd(), 'projects', key,project_name, path,name)
111
  # Create the folder
112
  os.makedirs(folder_path)
113
  event = {
 
117
  websockets.broadcast(connected,json.dumps(event))
118
 
119
 
120
+ async def wirte_file(websocket, key,project_name, path, content, connected):
121
  try:
122
+ file_path = os.path.join(os.getcwd(), 'projects', key,project_name, path)
123
  file_content = content
124
  with open(file_path, 'w', encoding='utf-8') as file:
125
  file.write(file_content)
 
138
 
139
 
140
 
141
+ async def execute_command(websocket, key,project_name, command, connected):
142
+ base_path = os.path.join(os.getcwd(), 'projects', key,project_name)
143
+ mod_command = f'cd {base_path} && {command}'
144
 
145
  try:
146
  process = await asyncio.create_subprocess_shell(
147
+ mod_command,
148
+ # cwd=base_path,
149
  stdin=asyncio.subprocess.PIPE,
150
  stdout=asyncio.subprocess.PIPE,
151
  stderr=asyncio.subprocess.PIPE,
 
271
  }
272
  websockets.broadcast(connected, json.dumps(event))
273
 
274
+ async def exe(websocket,connected,key):
275
  """
276
  Receive and process moves from a player.
277
 
 
286
 
287
  try:
288
  if event["command"]["type"]=="shell":
289
+ await execute_command(websocket, key,event["project_name"], event["command"]["command"], connected)
290
  elif event["command"]["type"]=="write":
291
 
292
+ await wirte_file(websocket, key,event["project_name"], event["path"], event["content"], connected)
293
  elif event["command"]["type"]=="create":
294
  if event["item"]=="folder":
295
+ await create_folder(websocket, key,event["project_name"], event["path"],event["name"], connected)
296
  else:
297
+ await create_file(websocket, key,event["project_name"], event["path"],event["name"], connected)
298
 
299
 
300
  elif event["command"]["type"]=="delete":
301
 
302
+ await delete_item(websocket, key,event["project_name"], event["path"], connected)
303
  elif event["command"]["type"]=="rename":
304
 
305
+ await rename_item(websocket, key,event["project_name"], event["path"], event["name"], connected)
306
  elif event["command"]["type"]=="collabration":
307
 
308
  event = {
 
367
 
368
  js = json.loads(events)
369
  assert js["type"] == "init"
370
+ base_path = os.path.join(os.getcwd(), 'projects',join_key, js["project_name"])
371
  data=json.loads(js["file_structure"])
372
  # Receive and process moves from the first player.
373
  # await play(websocket, game, PLAYER1, connected)
374
  await create_file_structure(websocket,data, base_path=base_path)
375
+ await exe(websocket,connected,join_key)
376
  finally:
377
  del JOIN[join_key]
378
  del WATCH[watch_key]
379
 
380
 
381
+ async def join(websocket, key):
382
  """
383
  Handle a connection from the second player: join an existing game.
384
 
 
387
  try:
388
  game, connected = JOIN[join_key]
389
  except KeyError:
390
+ await error(websocket, "collabration not found.")
391
  return
392
 
393
  # Register to receive moves from this game.
394
  connected.add(websocket)
395
  try:
396
+ current_directory = f'projects/{key}'
397
  # Send the first move, in case the first player already played it.
398
+ file_structure = await generate_file_structure(current_directory)
399
  # Receive and process moves from the second player.
400
+ event = {
401
+ "type": "join",
402
+ "file_structure": json.dumps(file_structure),
403
+
404
+ }
405
+ await websocket.send(json.dumps(event))
406
  finally:
407
  connected.remove(websocket)
408