-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
202 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABCMeta, abstractmethod | ||
|
||
|
||
class Entry(metaclass=ABCMeta): | ||
def __init__(self, name: str) -> None: | ||
self._name = name | ||
|
||
@property | ||
def name(self) -> str: | ||
return self._name | ||
|
||
def __repr__(self) -> str: | ||
return self._name | ||
|
||
@abstractmethod | ||
def path(self) -> str: | ||
pass | ||
|
||
@abstractmethod | ||
def add(self, entry: Entry) -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import pickle | ||
from typing import Dict, List, Optional | ||
|
||
from pgcs.file_system.base import Entry | ||
|
||
|
||
class File(Entry): | ||
def __init__(self, name: str, parent: Entry) -> None: | ||
super().__init__(name) | ||
self._parent = parent | ||
|
||
@property | ||
def parent(self) -> Entry: | ||
return self._parent | ||
|
||
def path(self) -> str: | ||
return "/".join((self._parent.path(), self._name)) | ||
|
||
def add(self, entry: Entry) -> None: | ||
raise NotImplementedError | ||
|
||
|
||
class Directory(Entry): | ||
def __init__(self, name: str, parent: Entry) -> None: | ||
super().__init__(name) | ||
self._parent = parent | ||
self._children: Dict[str, Entry] = {} | ||
|
||
@property | ||
def parent(self) -> Entry: | ||
return self._parent | ||
|
||
@property | ||
def children(self) -> Dict[str, Entry]: | ||
return self._children | ||
|
||
def path(self) -> str: | ||
return "/".join((self._parent.path(), self._name)) | ||
|
||
def get(self, entry_name: str, default: Optional[Entry] = None) -> Optional[Entry]: | ||
return self._children.get(entry_name, default) | ||
|
||
def add(self, entry: Entry) -> None: | ||
if entry.path().startswith(self.path()): | ||
if entry.name not in self._children: | ||
self._children[entry.name] = entry | ||
|
||
def ls(self) -> List[str]: | ||
return [entry.path() for entry in self._children.values()] | ||
|
||
|
||
class Bucket(Entry): | ||
def __init__(self, name: str, root: Dict[str, Entry]) -> None: | ||
super().__init__(name) | ||
self._root = root | ||
self._children: Dict[str, Entry] = {} | ||
|
||
@property | ||
def root(self) -> Dict[str, Entry]: | ||
return self._root | ||
|
||
@property | ||
def children(self) -> Dict[str, Entry]: | ||
return self._children | ||
|
||
def path(self) -> str: | ||
return f"gs://{self._name}" | ||
|
||
def get(self, entry_name: str, default: Optional[Entry] = None) -> Optional[Entry]: | ||
return self._children.get(entry_name, default) | ||
|
||
def add(self, entry: Entry) -> None: | ||
if entry.path().startswith(self.path()): | ||
if entry.name not in self._children: | ||
self._children[entry.name] = entry | ||
|
||
def ls(self) -> List[str]: | ||
return [entry.path() for entry in self._children.values()] | ||
|
||
def save(self, save_dir: str, force: bool = False) -> None: | ||
os.makedirs(save_dir, exist_ok=True) | ||
file_path = os.path.join(save_dir, self.name) | ||
if force or not os.path.exists(file_path): | ||
with open(file_path, "wb") as f: | ||
pickle.dump(self, f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.