|  | 
| 10 | 10 | from pathlib import Path | 
| 11 | 11 | import stat | 
| 12 | 12 | import time | 
| 13 |  | -from typing import Self | 
|  | 13 | +from typing import Self, Any | 
| 14 | 14 | 
 | 
| 15 | 15 | from tornado.web import RequestHandler, HTTPError | 
| 16 | 16 | from rest_tools.server import RestServer | 
| @@ -70,6 +70,17 @@ async def get(self, path): | 
| 70 | 70 |         self.render('details.html', path=path, data=data) | 
| 71 | 71 | 
 | 
| 72 | 72 | 
 | 
|  | 73 | +class API(BaseHandler): | 
|  | 74 | +    async def get(self, path): | 
|  | 75 | +        for fs in self.filesystems: | 
|  | 76 | +            if path.startswith(fs): | 
|  | 77 | +                data = await self.filesystems[fs].dir_entry(path[len(fs):]) | 
|  | 78 | +                break | 
|  | 79 | +        else: | 
|  | 80 | +            raise HTTPError(400, reason='bad path') | 
|  | 81 | +        self.write(data.to_dict()) | 
|  | 82 | + | 
|  | 83 | + | 
| 73 | 84 | async def call(*args, shell=False): | 
| 74 | 85 |     if shell: | 
| 75 | 86 |         ret = await asyncio.create_subprocess_shell(' '.join(args), stdout=asyncio.subprocess.PIPE) | 
| @@ -108,6 +119,11 @@ def from_entry(cls, e: Entry) -> Self: | 
| 108 | 119 |             raise Exception('is not a directory!') | 
| 109 | 120 |         return cls(e.name, e.path, e.size, e.nfiles, []) | 
| 110 | 121 | 
 | 
|  | 122 | +    def to_dict(self) -> dict[str, Any]: | 
|  | 123 | +        ret = vars(self) | 
|  | 124 | +        ret['children'] = [vars(c) for c in self.children] | 
|  | 125 | +        return ret | 
|  | 126 | + | 
| 111 | 127 |     def child_summary(self, threshold: float = .5) -> list[Entry]: | 
| 112 | 128 |         """ | 
| 113 | 129 |         Summarize children | 
| @@ -256,6 +272,7 @@ def __init__(self, s3_override=None): | 
| 256 | 272 |             server.add_route(cwd, Main, kwargs) | 
| 257 | 273 |         else: | 
| 258 | 274 |             server.add_route('/data', Main, kwargs) | 
|  | 275 | +        server.add_route(r'/api(.*)', API, kwargs) | 
| 259 | 276 |         server.add_route('/healthz', Health, kwargs) | 
| 260 | 277 |         server.add_route(r'(.*)', Details, kwargs) | 
| 261 | 278 | 
 | 
|  | 
0 commit comments