Skip to content

Commit

Permalink
Rewrite default view using blocks
Browse files Browse the repository at this point in the history
Print blocks of session and static applications when `-a`; Fix #23
  • Loading branch information
FrostyX committed Dec 24, 2014
1 parent 370f424 commit d56fb70
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 24 deletions.
2 changes: 2 additions & 0 deletions tracer/lang/en.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"you_should_restart" : "You should restart:",
"restart_using_helpers" : "* Some applications using:",
"restart_manually" : "* These applications manually:",
"restart_session" : "* These applications restarting your session:",
"restart_rebooting" : "* These applications rebooting your computer:",
"for_more_informations": "For more information run:",
"nothing_to_restart" : "Nothing needs to be restarted",
"locked_database" : "Package database is locked by another process",
Expand Down
36 changes: 36 additions & 0 deletions tracer/views/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from . import View
import sys

print_ = sys.stdout.write


class BlocksView(View):

"""
Assign ``blocks`` with this list
[
{
"title": "...",
"content": "some text",
},
...
]
"""

def render(self):
for index, block in enumerate(self.args.blocks):
if block["content"]:
if "title" in block:
print block["title"]
print_(block["content"])

if self.next(index):
print ""

def next(self, index):
"""Return True if there is any nonempty block after given index"""
for block in self.args.blocks[index+1:]:
if block["content"]:
return True
return False
67 changes: 43 additions & 24 deletions tracer/views/default.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,64 @@
from . import View
from tracer.resources.lang import _
from tracer.resources.applications import Applications
from tracer.views.note_for_hidden import NoteForHiddenView
from tracer.views.blocks import BlocksView
import sys
import StringIO
import re


class DefaultView(View):
def render(self):

with_helpers = self.args.applications.with_helpers()
without_helpers = self.args.applications.without_helpers()
note = self.args.session_count or self.args.static_count

if with_helpers or without_helpers:
print _("you_should_restart")

if len(with_helpers):
print " " + _("restart_using_helpers")
for application in with_helpers.sorted("helper"):
def with_helpers_content():
content = ""
types = [Applications.TYPES["SESSION"], Applications.TYPES["STATIC"]]
applications = self.args.applications.with_helpers().exclude_types(types).sorted("helper")
for application in applications:
helpers = "; ".join(application.helpers)
if application.helper_contains_formating and not application.helper_contains_name:
helpers += " # {}".format(application.name)
print " " + helpers

if without_helpers:
print ""

if len(without_helpers):
print " " + _("restart_manually")
for application in without_helpers.sorted("name"):
print " " + application.name
content += " " + helpers + "\n"
return content

if note:
print ""
def without_helpers_content():
content = ""
for application in self.args.applications.without_helpers().sorted("name"):
content += " " + application.name + "\n"
return content

if with_helpers and not without_helpers and note:
print ""
def unrestartable_content(app_type):
content = ""
applications = self.args.applications.with_helpers().filter_types([app_type]).sorted("name")
for application in applications:
content += " " + application.name + "\n"
return content

if not self.args.args.all:
def note_content():
content = StringIO.StringIO()
sys.stdout = content
view = NoteForHiddenView()
view.assign("args", self.args.args)
view.assign("total_count", self.args.total_count)
view.assign("session_count", self.args.session_count)
view.assign("static_count", self.args.static_count)
view.render()
sys.stdout = sys.__stdout__
return content.getvalue()

blocks = [
{"title": " " + _("restart_using_helpers"), "content": with_helpers_content()},
{"title": " " + _("restart_manually"), "content": without_helpers_content()},
]

if self.args.args.all:
blocks.append({"title": " " + _("restart_session"), "content": unrestartable_content(Applications.TYPES["SESSION"])})
blocks.append({"title": " " + _("restart_rebooting"), "content": unrestartable_content(Applications.TYPES["STATIC"])})
else:
blocks.append({"content": note_content()})

print _("you_should_restart")
view = BlocksView()
view.assign("blocks", blocks)
view.render()

0 comments on commit d56fb70

Please sign in to comment.