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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py CHANGED
@@ -16,6 +16,29 @@ JOIN = {}
16
 
17
  WATCH = {}
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  async def rename_item(websocket, project_name, path,new_name, connected):
20
 
21
  old_path = os.path.join(os.getcwd(), 'projects', project_name, path)
 
16
 
17
  WATCH = {}
18
 
19
+ async def generate_file_structure(path='.', encoding='utf-8'):
20
+ file_structure = {'name': os.path.basename(path), 'type': 'folder', 'path': path, 'children': []}
21
+
22
+ try:
23
+ entries = os.listdir(path)
24
+ except FileNotFoundError:
25
+ return file_structure # Return an empty structure for non-existing directories
26
+
27
+ for entry in entries:
28
+ entry_path = os.path.join(path, entry)
29
+ if os.path.isdir(entry_path):
30
+ child_structure = generate_file_structure(entry_path, encoding)
31
+ file_structure['children'].append(child_structure)
32
+ elif os.path.isfile(entry_path):
33
+ try:
34
+ with open(entry_path, 'r', encoding=encoding) as file:
35
+ content = file.read()
36
+ except UnicodeDecodeError:
37
+ content = 'Unable to read content'
38
+ file_structure['children'].append({'name': entry, 'type': 'file', 'path': entry_path, 'content': content})
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)