|
1 | 1 | import logging |
| 2 | +from datetime import datetime |
2 | 3 | from contextlib import contextmanager |
| 4 | +from pathlib import Path |
3 | 5 | from ..common import Object |
| 6 | +from ..objects.uri import unquote |
4 | 7 | from . import common, exceptions |
5 | 8 |
|
6 | 9 |
|
|
10 | 13 | class EdgePath(common.BasePath): |
11 | 14 | """Path for CTERA Edge Filer""" |
12 | 15 |
|
| 16 | + def __init__(self, scope, reference): |
| 17 | + """ |
| 18 | + Initialize a CTERA Edge Filer Path. |
| 19 | +
|
| 20 | + :param str scope: Scope. |
| 21 | + :param str reference: Reference. |
| 22 | + """ |
| 23 | + if isinstance(reference, Object): |
| 24 | + super().__init__(scope, reference.path) |
| 25 | + elif isinstance(reference, str): |
| 26 | + super().__init__(scope, reference) |
| 27 | + else: |
| 28 | + message = 'Path validation failed: ensure the path exists and is correctly formatted.' |
| 29 | + logger.error(message) |
| 30 | + raise ValueError(message) |
| 31 | + |
13 | 32 | @staticmethod |
14 | 33 | def instance(scope, reference): |
15 | 34 | return EdgePath(scope, reference) |
16 | 35 |
|
17 | 36 |
|
18 | | -@contextmanager |
19 | | -def listdir(path): |
20 | | - param = Object() |
21 | | - param.path = path |
22 | | - logger.info('Listing directory: %s', path) |
23 | | - yield param |
| 37 | +def fetch_reference(href): |
| 38 | + namespace = 'localFiles/' |
| 39 | + return unquote(href[href.index(namespace)+len(namespace):]) |
| 40 | + |
| 41 | + |
| 42 | +def format_listdir_response(parent, response): |
| 43 | + entries = [] |
| 44 | + for e in response: |
| 45 | + path = fetch_reference(e.href) |
| 46 | + if parent != path: |
| 47 | + is_dir = e.getcontenttype == 'httpd/unix-directory' |
| 48 | + param = Object( |
| 49 | + path=path, |
| 50 | + name=Path(path).name, |
| 51 | + is_dir=is_dir, |
| 52 | + is_file=not is_dir, |
| 53 | + created_at=e.creationdate, |
| 54 | + last_modified=datetime.strptime(e.getlastmodified, "%a, %d %b %Y %H:%M:%S GMT").isoformat(), |
| 55 | + size=e.getcontentlength |
| 56 | + ) |
| 57 | + entries.append(param) |
| 58 | + return entries |
24 | 59 |
|
25 | 60 |
|
26 | 61 | @contextmanager |
|
0 commit comments