Skip to content

Commit

Permalink
Save stats and add reload command (#3)
Browse files Browse the repository at this point in the history
* save stats and enable to relaod

* fix bug in load

* update pgcs version
  • Loading branch information
Asugawara authored Mar 15, 2024
1 parent 6e2777f commit e82d0f2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
29 changes: 11 additions & 18 deletions pgcs/custom_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@
SELECTED_CLASS = "class:selected"


@lru_cache
def get_file_info(file_path: str, preview: bool = False) -> str:
content = ""
if preview:
content = gfs.read_block(file_path, 0, 50, delimiter=b"\n").decode("utf-8")
file_stats = gfs.stat(file_path)
file_createdat = f"created_at: {file_stats['timeCreated']}"
file_updatedat = f"updated_at: {file_stats['updated']}"
return "\n".join((file_createdat, file_updatedat, content))


class CustomFormattedTextControl(FormattedTextControl):
def __init__(
self,
Expand Down Expand Up @@ -139,6 +128,13 @@ def _(event: KeyPressEvent) -> None:
entry.path(), ".", recursive=isinstance(entry, (Bucket, Directory))
)

@bindings.add(Keys.ControlR)
def _(event: KeyPressEvent) -> None:
entry_name = to_plain_text(self.get_pointed_at()).strip()
entry = self._choices[entry_name]
if entry and isinstance(entry, (Directory, Bucket)):
entry.load(force=True)

@bindings.add(Keys.Enter)
def _(event: KeyPressEvent) -> None:
content = self.get_pointed_at()
Expand All @@ -154,6 +150,7 @@ def _(event: KeyPressEvent) -> None:
def custom_select(
choices: Dict[str, Entry], max_preview_height: int = 10, **kwargs: Any
) -> str:
print(choices)
text_area = TextArea(prompt="QUERY> ", multiline=False)

def filter_candidates(choices: List[str]) -> List[Tuple[str, str]]:
Expand All @@ -177,7 +174,7 @@ def get_entry_info() -> str:
return ""
content = ""
if isinstance(entry, File):
content = get_file_info(entry.path())
content = "\n".join(entry.stat())
elif isinstance(entry, (Directory, Bucket)):
content = "\n".join(map(os.path.basename, entry.ls()[:10]))
return content
Expand Down Expand Up @@ -226,10 +223,6 @@ def traverse_gcs(choices: Dict[str, Entry]) -> File:
entry = choices[result]
if isinstance(entry, File):
return entry
if not entry.children: # type: ignore
for _, dirnames, filenames in gfs.walk(entry.path(), maxdepth=1):
for dirname in dirnames:
entry.add(Directory(dirname, entry))
for filename in filenames:
entry.add(File(filename, entry))
elif isinstance(entry, (Directory, Bucket)):
entry.load()
return traverse_gcs(entry.children) # type: ignore
43 changes: 40 additions & 3 deletions pgcs/file_system/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

import os
import pickle
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Tuple

import gcsfs

from pgcs.file_system.base import Entry

gfs = gcsfs.GCSFileSystem()


class File(Entry):
def __init__(self, name: str, parent: Entry) -> None:
super().__init__(name)
self._parent = parent
self._created_at: str = ""
self._updated_at: str = ""

@property
def parent(self) -> Entry:
Expand All @@ -22,6 +28,14 @@ def path(self) -> str:
def add(self, entry: Entry) -> None:
raise NotImplementedError

def stat(self) -> Tuple[str, str]:
if self._created_at and self._updated_at:
return (self._created_at, self._updated_at)
file_stats = gfs.stat(self.path())
self._created_at = file_stats.get("timeCreated", "")
self._updated_at = file_stats.get("updated", "")
return (self._created_at, self._updated_at)


class Directory(Entry):
def __init__(self, name: str, parent: Entry) -> None:
Expand All @@ -44,10 +58,21 @@ def get(self, entry_name: str, default: Optional[Entry] = None) -> Optional[Entr
return self._children.get(entry_name, default)

def add(self, entry: Entry) -> None:
if entry.path().startswith(self.path()):
if entry.name and entry.path().startswith(self.path()):
if entry.name not in self._children:
self._children[entry.name] = entry

def load(self, force: bool = False) -> None:
if force:
self._children = {}
if not self._children:
for _, dirnames, filenames in gfs.walk(self.path(), maxdepth=1):
print(f"{dirnames=}, {filenames=}")
for dirname in dirnames:
self.add(Directory(dirname, self))
for filename in filenames:
self.add(File(filename, self))

def ls(self) -> List[str]:
return [entry.path() for entry in self._children.values()]

Expand All @@ -73,10 +98,22 @@ def get(self, entry_name: str, default: Optional[Entry] = None) -> Optional[Entr
return self._children.get(entry_name, default)

def add(self, entry: Entry) -> None:
if entry.path().startswith(self.path()):
if entry.name and entry.path().startswith(self.path()):
if entry.name not in self._children:
self._children[entry.name] = entry

def load(self, force: bool = False) -> None:
if force:
self._children = {}
if not self._children:
for _, dirnames, filenames in gfs.walk(self.path(), maxdepth=1):
for dirname in dirnames:
if dirname:
self.add(Directory(dirname, self))
for filename in filenames:
if filename:
self.add(File(filename, self))

def ls(self) -> List[str]:
return [entry.path() for entry in self._children.values()]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pgcs"
version = "0.1.1"
version = "0.1.2"
description = "Pgcs is an intuitive TUI tool designed to simplify your interaction with Google Cloud Storage. Stay in your coding zone by navigating directories, searching files (with case-insensitive support), and previewing files all from your terminal. Easily save paths to clipboard or download files with straightforward keyboard shortcuts. Experience a seamless Cloud Storage interaction right from your terminal; no more swapping to a browser. Stimulate your productivity with Pgcs."
authors = ["Asugawara <asgasw@gmail.com>"]
readme = "README.md"
Expand Down

0 comments on commit e82d0f2

Please sign in to comment.