Skip to content

Commit be01d01

Browse files
committed
Add file listing API
1 parent 9576e37 commit be01d01

File tree

10 files changed

+157
-41
lines changed

10 files changed

+157
-41
lines changed

src/pyrdfrules/api/workspace/workspace_http_api.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,28 @@ def __init__(self, context: HTTPRDFRulesApiContext) -> None:
1111
super().__init__(context)
1212
pass
1313

14-
async def get_all_files(self):
14+
async def get_all_files(self) -> dict:
15+
"""Get all files and directories recursively from the workspace directory.
16+
"""
1517

1618
response = self.context.get_http_client().get(WORKSPACE_URL)
1719

18-
print(response.text)
19-
20-
"""Get all files and directories recursively from the workspace directory.
21-
"""
22-
pass
20+
return response.json()
2321

2422
async def get_file(self, path: str):
2523
"""Get the file content.
2624
"""
25+
2726
pass
2827

2928
async def delete_file(self, path: str):
3029
"""Delete a file.
3130
"""
31+
3232
pass
3333

3434
async def upload_file(self, path: str, content: bytes):
3535
"""Upload a file.
3636
"""
37+
3738
pass

src/pyrdfrules/application.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,4 @@ async def stop(self) -> None:
4242
"""Stops the application.
4343
"""
4444

45-
await self.__rdfrules.engine.stop()
46-
47-
def check(self):
48-
"""Checks the liveliness state of the application.
49-
"""
45+
await self.__rdfrules.engine.stop()
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
from pathlib import Path
2-
from pydantic import BaseModel
32

4-
from pyrdfrules.common.file.workspace_file import WorkspaceFile
3+
from pyrdfrules.api.workspace.workspace_api import WorkspaceApi
4+
from pyrdfrules.common.file.workspace_tree import WorkspaceTree
55

6-
class Workspace(BaseModel):
6+
class Workspace():
77
"""
88
Path in which RDFRules can find datasets and store results.
99
"""
1010

11-
"""
12-
Base path to the workspace root directory.
13-
"""
14-
base_path: Path
11+
api: WorkspaceApi
1512

16-
def open(self, path: str) -> WorkspaceFile:
17-
"""Creates a file reference if the specified path exists.
18-
19-
Args:
20-
path (str): Relative path from the workspace root.
13+
def __init__(self, api: WorkspaceApi) -> None:
14+
self.api = api
15+
pass
16+
17+
async def get_files(self) -> WorkspaceTree:
18+
"""Returns a list of all files in the workspace.
2119
2220
Returns:
23-
WorkspaceFile: Workspace file abstraction.
21+
WorkspaceTree: List of workspace files.
2422
"""
2523

26-
# todo - check if the file exists
27-
return WorkspaceFile(path, self)
24+
tree = WorkspaceTree()
25+
tree.create_from_dict(await self.api.get_all_files())
26+
27+
return tree
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from pathlib import Path
2+
from typing import Optional
3+
4+
from pyrdfrules.common.file.workspace_item import WorkspaceItem
5+
6+
class WorkspaceDirectory(WorkspaceItem):
7+
"""Represents a directory in the workspace.
8+
"""
9+
10+
name: str
11+
"""Name of the workspace diretory.
12+
"""
13+
14+
size: int
15+
"""Size of the directory in bytes.
16+
"""
17+
18+
path: Optional[Path]
19+
"""Full path to the file.
20+
"""
21+
22+
writable: bool
23+
"""If the file is writable.
24+
"""
25+
26+
files: list[WorkspaceItem] = []
27+
28+
def add(self, item: WorkspaceItem) -> None:
29+
"""Adds a file or directory to the directory.
30+
31+
Args:
32+
item (WorkspaceItem): File or directory to add.
33+
"""
34+
35+
self.files.append(item)
36+
pass
37+
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
from pathlib import Path
2+
from typing import List
23
from pydantic import BaseModel
34

4-
from pyrdfrules.common.file.workspace import Workspace
5+
from pyrdfrules.common.file.workspace_item import WorkspaceItem
56

6-
class WorkspaceFile(BaseModel):
7-
"""
8-
Represents a file in the workspace.
7+
class WorkspaceFile(WorkspaceItem):
8+
"""Represents a file in the workspace.
99
"""
1010

11+
name: str
12+
"""Name of the workspace file.
1113
"""
12-
Path relative to the workspace root.
13-
"""
14-
path: str
1514

15+
size: int
16+
"""Size of the file in bytes.
1617
"""
17-
Workspace the file is in.
18-
"""
19-
workspace: Workspace
2018

21-
def get_absolute_path(self) -> str:
22-
return ''
19+
path: Path
20+
"""Full path to the file.
21+
"""
22+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pathlib import Path
2+
from pydantic import BaseModel
3+
4+
class WorkspaceItem(BaseModel):
5+
"""Represents a directory in the workspace.
6+
"""
7+
8+
name: str
9+
"""Name of the workspace diretory.
10+
"""
11+
12+
path: Path
13+
"""Full path to the file.
14+
"""
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from pathlib import Path
2+
from typing import List
3+
from pydantic import BaseModel
4+
5+
from pyrdfrules.common.file.workspace_directory import WorkspaceDirectory
6+
from pyrdfrules.common.file.workspace_file import WorkspaceFile
7+
8+
class WorkspaceTree(BaseModel):
9+
10+
root: WorkspaceDirectory|None = None
11+
12+
def create_from_dict(self, data: dict) -> None:
13+
"""Creates a workspace tree from a dictionary response.
14+
15+
Args:
16+
data (dict): Dictionary with workspace items.
17+
"""
18+
19+
def create_folder(subdata: dict, base_path: Path) -> WorkspaceDirectory:
20+
21+
directory = WorkspaceDirectory(
22+
name=subdata['name'],
23+
path=base_path,
24+
size=0,
25+
writable=subdata['writable']
26+
)
27+
28+
if 'subfiles' in subdata:
29+
for file in subdata['subfiles']:
30+
if "subfiles" in file:
31+
directory.add(
32+
create_folder(file, base_path / Path(file['name']))
33+
)
34+
else:
35+
directory.add(
36+
WorkspaceFile(
37+
name=file['name'],
38+
path=base_path / Path(file['name']),
39+
size=file['size']
40+
)
41+
)
42+
43+
return directory
44+
45+
46+
self.root = create_folder(data, Path('/'))
47+
pass
48+
49+
50+
def get_root(self) -> WorkspaceDirectory:
51+
"""Returns the root directory of the workspace tree.
52+
53+
Returns:
54+
WorkspaceDirectory: Root directory.
55+
"""
56+
57+
return self.root

src/pyrdfrules/common/http/http_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Awaitable
12
from pydantic_core import Url
23
from requests import Response, Session
34

@@ -29,7 +30,7 @@ def set_base_url(self, url: str | Url) -> None:
2930

3031
self.base_url = str(url)
3132

32-
def get(self, url: str | Url) -> Response:
33+
def get(self, url: str | Url) -> Awaitable[Response]:
3334
"""Sends a GET request.
3435
3536
Args:

src/pyrdfrules/rdfrules/rdfrules.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
from typing import Awaitable
1+
from typing import Any, Awaitable
22

3+
from pyrdfrules.api.workspace.workspace_api import WorkspaceApi
4+
from pyrdfrules.common.file.workspace import Workspace
35
from pyrdfrules.engine.engine import Engine
46

57
class RDFRules():
68

79
engine: Engine
810

11+
workspace: Workspace
12+
913
def __init__(self, engine: Engine) -> None:
1014
self.engine = engine
1115
pass
1216

17+
def __getattr__(self, name: str) -> Any:
18+
if name == 'workspace':
19+
return Workspace(self.engine.api.workspace)
20+
21+
pass
22+
1323
async def start(self) -> Awaitable:
1424
"""
1525
Starts the engine.

src/tests/test_remote_application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def test_runs_workspace(self):
1919
)
2020

2121
self.assertIsNotNone(rdfrules, "Should not be None")
22-
await rdfrules.engine.check()
22+
await rdfrules.workspace.get_files()
2323

2424
await app.stop()
2525

0 commit comments

Comments
 (0)