Skip to content

Commit

Permalink
feat: improve startup time, added user config
Browse files Browse the repository at this point in the history
  • Loading branch information
pommee committed Jun 5, 2024
1 parent 6f8b10d commit a7a8f58
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 47 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dev:
textual run --dev application.main:UI
poetry shell && textual run --dev application.main:UI
35 changes: 35 additions & 0 deletions application/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
import yaml
from pydantic import BaseModel


CONFIG_PATH = "config.yaml"


class Config(BaseModel):
log_tail: int
start_wrap: bool
start_fullscreen: bool
start_scroll: bool

def __repr__(self) -> str:
return super().__repr__(self.start_wrap)


def create_default_config():
default_config = {
"log_tail": 2000,
"start_wrap": False,
"start_fullscreen": False,
"start_scroll": False,
}
with open(CONFIG_PATH, "w") as file:
yaml.dump(default_config, file)


def load_config():
if not os.path.exists(CONFIG_PATH):
create_default_config()
with open(CONFIG_PATH, "r") as file:
config_dict = yaml.safe_load(file)
return Config(**config_dict)
26 changes: 12 additions & 14 deletions application/docker_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
from textual.widgets import RichLog
from textual.logging import TextualHandler

from application.config import Config

logging.basicConfig(
level="INFO",
handlers=[TextualHandler()],
)


class DockerManager:
def __init__(self) -> None:
def __init__(self, config: Config) -> None:
self.client = docker.from_env()
self.containers: ContainerCollection = self.client.containers.list(all=True)
self.containers: ContainerCollection = self.client.containers.list(
all=True, sparse=True
)
self.images: ImageCollection = self.client.images.list(all=True)
self.selected_container = self.containers[0].name
self.selected_container = self.containers[0].attrs["Names"][0].replace("/", "")
self.config = config

def container(self, container_name: str) -> Container:
return self.client.containers.get(container_name)
Expand All @@ -27,12 +32,10 @@ def curr_container(self):
return self.container(self.selected_container)

def logs(self):
return (
self.container(self.selected_container)
.logs(follow=False)
.decode("utf-8")
.strip()
logs: bytes = self.curr_container().logs(
tail=self.config.log_tail, follow=False, stream=False
)
return logs.decode("utf-8").strip()

def attributes(self):
return self.container(self.selected_container).attrs
Expand All @@ -54,12 +57,7 @@ def live_container_logs(self, logs: RichLog, stop_event: Event):
if len(logs.lines) == 0:
last_fetch = time.time()
# If logs are empty, write initial logs
logs.write(
self.container(self.selected_container)
.logs()
.decode("utf-8")
.strip()
)
logs.write(self.logs())

new_logs = (
self.container(self.selected_container)
Expand Down
23 changes: 20 additions & 3 deletions application/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
from rich.segment import Segment
from textual.logging import TextualHandler

from application.config import Config, load_config

#### REFERENCES ####
logs = None
docker_manager = DockerManager()
log_task_stop_event = Event()

config: Config = None
docker_manager: DockerManager = None

logging.basicConfig(
level="INFO",
Expand Down Expand Up @@ -82,8 +84,9 @@ def compose(self) -> ComposeResult:
status = "running"
else:
status = "down"
container_name = container.attrs["Names"][0].replace("/", "")
listview_container = ListItem(
Label(container.name), id=container.name, classes=status
Label(container_name), id=container_name, classes=status
)
yield listview_container
with Horizontal(id="startstopbuttons"):
Expand Down Expand Up @@ -270,6 +273,11 @@ class UI(App):
pocker_images: PockerImages

def compose(self) -> ComposeResult:
global config, docker_manager

config = load_config()
docker_manager = DockerManager(config)

header = Header()
containers_and_images = Vertical(id="containers-and-images")
self.pocker_containers = PockerContainers(id="PockerContainers")
Expand All @@ -288,7 +296,16 @@ def compose(self) -> ComposeResult:
def on_mount(self) -> None:
self._run_threads()
self.title = "Pocker"

if config.start_fullscreen:
self.action_toggle_content_full_screen()
if config.start_wrap:
self.action_wrap_text()
if not config.start_scroll:
self.action_toggle_auto_scroll()

self.set_header()

# self.notify(
# title="New version available!",
# message="Update to v1.2.1 by running: 'pocker update'",
Expand Down
4 changes: 4 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
log_tail: 2000
start_fullscreen: false
start_scroll: true
start_wrap: false
91 changes: 63 additions & 28 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ readme = "README.md"
packages = [{ include = "application" }]

[tool.poetry.dependencies]
python = "^3.10.9"
python = "^3.11.9"
docker = "^7.0.0"
textual = "^0.59.0"
black = "^24.4.2"
textual-dev = "^1.5.1"
pyyaml = "^6.0.1"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit a7a8f58

Please sign in to comment.