Skip to content

Commit

Permalink
Several improvements.
Browse files Browse the repository at this point in the history
- Add a link for the user to see the log file.
- Notify the user if there was an internal error.
- If docs are not there, link to the website.
- Improve the appearance of the top menu.
  • Loading branch information
prabhuramachandran committed Mar 16, 2017
1 parent 5c54b9b commit 505b854
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 16 deletions.
10 changes: 6 additions & 4 deletions docs/source/using_vixen.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ left panel to create a new project as seen in the figure below:

In the figure, on the top right is a button to stop the ViXeN application.
Clicking on this will close ViXeN and one may safely close the browser tab.
The top left has two links, "Home" and "Help", the home link brings the user
to the page where one can edit or view the different projects. The help link
takes a user to the documentation. If you are using a binary, this will take
one to a copy of the documentation bundled with the binary.
The top left has three links, "Home", "Help", and "Log" the home link brings
the user to the page where one can edit or view the different projects. The
help link takes a user to the documentation. If you are using a binary, this
will take one to a copy of the documentation bundled with the binary. The
"Log" shows the vixen application logs in a separate tab. This is handy if you
are running into errors and is useful when you are reporting problems.



Expand Down
1 change: 1 addition & 0 deletions vixen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def make_ui():
# relative imports in the entry point script.
from vixen.vixen import VixenUI
ui = VixenUI()
ui.setup_logging_handler()
ui.vixen.load()
return ui

Expand Down
3 changes: 3 additions & 0 deletions vixen/html/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ p.loading {
.processed {
color: green;
}
a.menu {
margin-right: 10px;
}
label {
line-height: 1.2;
}
Expand Down
8 changes: 5 additions & 3 deletions vixen/html/vixen_ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<link href="$HTML_ROOT/css/style.css" rel="stylesheet">
<!-- Use an empty icon for now until we have a logo. -->
<link rel="icon" href="data:,">
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
</head>

<body>
Expand All @@ -40,8 +40,10 @@ <h2 style="text-align:center; padding: 0px; margin: 0px;">
</div>

<!-- Menu on the top -->
<div style="position: absolute; top: 5px;" v-on:click="ui.home()">
<a href="#">Home</a> <a v-bind:href="ui.docs" target="_blank">Help</a>
<div style="position: absolute; top: 5px; margin-left: 5px;">
<a href="#" v-on:click="ui.home()" class="menu">Home</a>
<a v-bind:href="ui.docs" target="_blank" class="menu">Help</a>
<a v-bind:href="$ROOTui.log_file" target="_blank" class="menu">Log</a>
</div>
<div style="position: absolute; top: 5px; right: 5px;">
<button v-on:click="halt()" id="halt">Stop ViXeN</button>
Expand Down
70 changes: 69 additions & 1 deletion vixen/tests/test_vixen.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
import logging
import os
import mock
import unittest

import vixen
from vixen.processor import PythonFunctionFactory
from vixen.project import Project
from vixen.vixen import VixenUI, Vixen
from vixen.vixen import VixenUI, Vixen, UIErrorHandler
from vixen.vixen_ui import get_html, get_html_file

from vixen.tests.test_project import TestProjectBase


class MockRecord():
def __init__(self, name, message):
self.name = name
self.message = message


class TestUIErrorHandler(unittest.TestCase):
def setUp(self):
self.mock_ui = mock.MagicMock()
self.h = UIErrorHandler(self.mock_ui)

def test_emit_catches_general_error(self):
# Given
record = MockRecord(name='name', message='favicon.ico')
# When
self.h.emit(record)

# Then
self.assertTrue(self.mock_ui.notify_user.call_count, 1)

def test_emit_catches_access_error_non_favicon(self):
# Given
record = MockRecord(name='tornado.access', message='hello')
# When
self.h.emit(record)

# Then
self.assertTrue(self.mock_ui.notify_user.call_count, 1)

def test_emit_skips_favicon_errors(self):
# Given
record = MockRecord(name='tornado.access',
message='hello I have favicon.ico')
# When
self.h.emit(record)

# Then
self.mock_ui.notify_user.assert_not_called()

# Given
record = MockRecord(name='tornado.application',
message='hello I have favicon.ico')
# When
self.h.emit(record)

# Then
self.mock_ui.notify_user.assert_not_called()


class TestVixenBase(TestProjectBase):
def setUp(self):
super(TestVixenBase, self).setUp()
Expand Down Expand Up @@ -288,6 +338,24 @@ def test_vixen_ui_log(self, logger):
logger.error.assert_called_with('Unknown message kind: %s', 'blah')
logger.info.assert_called_with('err')

def test_logging_handler_is_setup_correctly(self):
# Given
ui = VixenUI()

# When
m = mock.MagicMock()
with mock.patch('vixen.vixen.logging.getLogger', return_value=m) as p:
ui.setup_logging_handler()

# Then
p.assert_called_once_with()
self.assertEqual(m.addHandler.call_count, 1)
args = m.addHandler.call_args[0]
obj = args[0]
self.assertTrue(isinstance(obj, UIErrorHandler))
self.assertEqual(obj.level, logging.ERROR)
self.assertEqual(obj.ui, ui)

def test_add_remove_project_works(self):
# Given
ui = VixenUI()
Expand Down
53 changes: 45 additions & 8 deletions vixen/vixen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import json
import logging
from logging import Handler
from os.path import dirname, exists, join, isdir
import os
import subprocess
Expand All @@ -21,6 +22,25 @@
logger = logging.getLogger(__name__)


class UIErrorHandler(Handler):
"""Simple logging handler to let the user know if there was
an internal error.
"""
def __init__(self, ui):
super(UIErrorHandler, self).__init__()
self.ui = ui

def emit(self, record):
"""Handle a logging record.
"""
exclude = ('tornado.access', 'tornado.application')
if not ((record.name in exclude) and
('favicon.ico' in record.message)):
msg = ('An error was encountered, please send the\n'
'log file to the developers. Thank you!')
self.ui.notify_user(msg, 'error')


class Vixen(HasTraits):

projects = List(Project)
Expand Down Expand Up @@ -473,13 +493,21 @@ class VixenUI(HasTraits):

docs = Property(Str)

log_file = Str

version = Str

message = Tuple()

# Private trait to generate message counts.
_message_count = Int

def setup_logging_handler(self):
handler = UIErrorHandler(self)
handler.setLevel(logging.ERROR)
root = logging.getLogger()
root.addHandler(handler)

def get_context(self):
return dict(
ui=self, vixen=self.vixen, editor=self.editor, viewer=self.viewer
Expand All @@ -488,14 +516,18 @@ def get_context(self):
def home(self):
self.mode = 'edit'

def error(self, msg):
def notify_user(self, message, kind):
"""Meant to just notify the user from the Python side.
"""
mid = self._get_message_id()
self.message = msg, "error", mid
self.message = message, kind, mid

def error(self, msg):
self.notify_user(msg, 'error')
logger.info("ERROR: %s", msg)

def info(self, msg):
mid = self._get_message_id()
self.message = msg, "info", mid
self.notify_user(msg, 'info')
logger.info("INFO: %s", msg)

def log(self, msg, kind='info'):
Expand All @@ -510,8 +542,7 @@ def log(self, msg, kind='info'):
logger.info(msg)

def success(self, msg):
mid = self._get_message_id()
self.message = msg, "success", mid
self.notify_user(msg, 'success')
logger.info("SUCCESS: %s", msg)

def edit(self, project):
Expand Down Expand Up @@ -584,10 +615,16 @@ def _get_docs(self):
bundled = join(
dirname(mydir), 'vixen_data', 'docs', 'html', 'index.html'
)
root = "'/' +" if sys.platform.startswith('win') else ''
if exists(bundled):
return bundled
return root + bundled
elif exists(build):
return root + build
else:
return build
return 'http://vixen.readthedocs.io'

def _log_file_default(self):
return join(get_project_dir(), 'vixen.log')

def _vixen_default(self):
v = Vixen()
Expand Down

0 comments on commit 505b854

Please sign in to comment.