Skip to content

Commit

Permalink
implement image info buffer
Browse files Browse the repository at this point in the history
Resolves #48
  • Loading branch information
TomasTomecek committed Nov 28, 2015
1 parent 68d152c commit 7d6cc22
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
18 changes: 18 additions & 0 deletions sen/docker_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ def short_id(self):
def display_time_created(self):
return humanize.naturaltime(self.created)

def display_formal_time_created(self):
# http://tools.ietf.org/html/rfc2822.html#section-3.3
return self.created.strftime("%d %b %Y, %H:%M:%S")

def inspect(self):
raise NotImplementedError()

Expand Down Expand Up @@ -233,6 +237,20 @@ def command(self):
return " ".join(cmd)
return ""

@property
def size(self):
"""
Size of all layers in bytes
:return: int
"""
return self.data["VirtualSize"]

@property
def labels(self):
labels = self.data["Labels"]
return labels

@property
def names(self):
if self._names is None:
Expand Down
14 changes: 14 additions & 0 deletions sen/tui/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sen.docker_backend import DockerContainer
from sen.exceptions import NotifyError
from sen.tui.constants import HELP_TEXT
from sen.tui.widgets.info import ImageInfoWidget
from sen.tui.widgets.list.main import MainListBox
from sen.tui.widgets.list.util import get_operation_notify_widget
from sen.tui.widgets.list.common import AsyncScrollableListBox, ScrollableListBox
Expand Down Expand Up @@ -59,6 +60,19 @@ def filter(self, s):
self.widget.set_body(widgets)


class ImageInfoBuffer(Buffer):
tag = "I"

def __init__(self, docker_image, ui):
"""
:param docker_image:
:param ui: ui object so we refresh
"""
self.display_name = docker_image.short_name
self.widget = ImageInfoWidget(docker_image)
super().__init__()


class MainListBuffer(Buffer):
display_name = "Listing"
tag = "M"
Expand Down
6 changes: 5 additions & 1 deletion sen/tui/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from sen.exceptions import NotifyError
from sen.tui.commands import search, filter
from sen.tui.statusbar import Footer
from sen.tui.buffer import LogsBuffer, MainListBuffer, InspectBuffer, HelpBuffer
from sen.tui.buffer import LogsBuffer, MainListBuffer, InspectBuffer, HelpBuffer, ImageInfoBuffer
from sen.tui.constants import PALLETE
from sen.docker_backend import DockerBackend

import urwid

from sen.util import log_traceback

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -159,6 +160,9 @@ def display_and_follow_logs(self, docker_container):
def inspect(self, docker_object):
self.add_and_display_buffer(InspectBuffer(docker_object))

def display_image_info(self, docker_image):
self.add_and_display_buffer(ImageInfoBuffer(docker_image, self))

def refresh_main_buffer(self, refresh_buffer=True):
assert self.main_list_buffer is not None
if refresh_buffer:
Expand Down
56 changes: 56 additions & 0 deletions sen/tui/widgets/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Info widgets:
* display detailed info about an object
"""
import logging

from sen.tui.widgets.list.base import VimMovementListBox

import urwid

from sen.util import humanize_bytes

logger = logging.getLogger(__name__)


class ImageInfoItemWidget(urwid.AttrMap):
def __init__(self, key, value=None):
attr_map = "main_list_dg"
focus_map = "main_list_white"
if value:
w = urwid.Columns([
urwid.AttrMap(urwid.Text(key, align="left", wrap="any"), attr_map, focus_map),
urwid.AttrMap(urwid.Text(value, align="left", wrap="any"), attr_map, focus_map),
])
else:
w = urwid.Text(key, align="left", wrap="any")
super().__init__(w, attr_map, focus_map=focus_map)


class ImageInfoWidget(VimMovementListBox):
"""
display info about image
"""
def __init__(self, docker_image):
self.docker_image = docker_image
l = self._basic_data() + self._labels()
self.walker = urwid.SimpleFocusListWalker(l)
super().__init__(self.walker)

def _basic_data(self):
l = [
ImageInfoItemWidget("Id", self.docker_image.image_id),
ImageInfoItemWidget("Created", "{0}, {1}".format(self.docker_image.display_formal_time_created(),
self.docker_image.display_time_created())),
ImageInfoItemWidget("Size", humanize_bytes(self.docker_image.size)),
]
return l

def _labels(self):
if not self.docker_image.labels:
return []
l = [ImageInfoItemWidget("Labels")]
for label_key, label_value in self.docker_image.labels.items():
l.append(ImageInfoItemWidget(label_key, label_value))

return l
3 changes: 3 additions & 0 deletions sen/tui/widgets/list/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ def do_and_report_on_fail(f, docker_object):
elif key == "f":
self.ui.run_in_background(do_and_report_on_fail, self.ui.display_and_follow_logs, self.focused_docker_object)
return
elif key == "enter":
self.ui.run_in_background(do_and_report_on_fail, self.ui.display_image_info, self.focused_docker_object)
return
elif key == "d":
self.ui.run_in_background(
run_and_report_on_fail,
Expand Down
24 changes: 24 additions & 0 deletions sen/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,27 @@ def setup_dirs():

def get_log_file_path():
return os.path.join(setup_dirs(), LOG_FILE_NAME)


def humanize_bytes(bytesize, precision=2):
"""
Humanize byte size figures
https://gist.github.com/moird/3684595
"""
abbrevs = (
(1 << 50, 'PB'),
(1 << 40, 'TB'),
(1 << 30, 'GB'),
(1 << 20, 'MB'),
(1 << 10, 'kB'),
(1, 'bytes')
)
if bytesize == 1:
return '1 byte'
for factor, suffix in abbrevs:
if bytesize >= factor:
break
if factor == 1:
precision = 0
return '%.*f %s' % (precision, bytesize / float(factor), suffix)

0 comments on commit 7d6cc22

Please sign in to comment.