Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup demo.py #711

Merged
merged 5 commits into from
Jan 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 38 additions & 85 deletions traitsui/extras/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,59 @@

from __future__ import absolute_import

import sys
import glob
import io
from io import StringIO
import operator
from os import listdir
from os.path import (
abspath,
basename,
dirname,
exists,
isabs,
isdir,
join,
split,
splitext,
)
import sys
import token
import tokenize
import operator
from io import StringIO
import io
import traceback


from configobj import ConfigObj

from traits.api import (
Any,
Bool,
cached_property,
HasTraits,
Code,
Dict,
HasPrivateTraits,
Str,
HasTraits,
HTML,
Instance,
Property,
Any,
Code,
HTML,
Dict,
Str,
)

from traitsui.api import (
TreeEditor,
ObjectTreeNode,
TreeNodeObject,
View,
Item,
VSplit,
Tabbed,
VGroup,
HGroup,
Heading,
Handler,
UIInfo,
InstanceEditor,
Heading,
HGroup,
HTMLEditor,
Include,
InstanceEditor,
Item,
ObjectTreeNode,
spring,
)

from os import listdir

from os.path import (
join,
isdir,
split,
splitext,
dirname,
basename,
abspath,
exists,
isabs,
Tabbed,
TreeEditor,
TreeNodeObject,
UIInfo,
VGroup,
View
)


Expand Down Expand Up @@ -158,8 +157,6 @@ def parse_source(file_name):
except Exception:
# Print an error message instead of failing silently.
# Ideally, the message would be output to the "log" tab.
import traceback

traceback_text = traceback.format_exc()
error_fmt = u"""Sorry, something went wrong.\n\n{}"""
error_msg = error_fmt.format(traceback_text)
Expand Down Expand Up @@ -215,26 +212,13 @@ def init(self, info):
demo = DemoButton(demo=demo)
else:
demo = self._get_object("demo", locals)
# FIXME: If a 'demo' object could not be found, then try to execute
# the file setting __name__ to __main__. A lot of test scripts have
# the actual test running when __name__==__main__ and so we can at
# least run all test examples this way. Use a do_later loop so as to
# finish building the current UI before running the test.
if demo is None:
locals["__name__"] = "__main__"
# do_later(self.execute_test, df, locals)
except Exception as excp:
demo = DemoError(msg=str(excp))

# Clean up sys.path
sys.path.remove(dirname(df.path))
df.demo = demo

def execute_test(self, df, locals):
""" Executes the file in df.path in the namespace of locals."""
with io.open(df.path, "r", encoding="utf-8") as fp:
exec(compile(fp.read(), df.path, "exec"), locals, locals)

def closed(self, info, is_ok):
""" Closes the view.
"""
Expand All @@ -250,7 +234,7 @@ def _get_object(self, name, dic):
if isinstance(type(object), type):
try:
object = object()
except:
except Exception:
pass

if isinstance(object, HasTraits):
Expand Down Expand Up @@ -568,16 +552,6 @@ def _get_init_dic(self):
exec((exec_str + source), init_dic)
return init_dic

# fixme: The following code should work, but doesn't, so we use the
# preceding code instead. Changing any trait in the object in
# this method causes the tree to behave as if the DemoPath object
# had been selected instead of a DemoFile object. May be due to
# an 'anytrait' listener in the TreeEditor?
# if self._init_dic is None:
# self._init_dic = {}
# #exec self.source in self._init_dic
# return self._init_dic.copy()

# -------------------------------------------------------------------------
# Initializes the description and source from the path's '__init__.py'
# file:
Expand Down Expand Up @@ -670,7 +644,7 @@ def get_children_from_config(self):
if exists(self.config_filename):
try:
self.config_dict = ConfigObj(self.config_filename)
except:
except Exception:
pass
if not self.config_dict:
return self.get_children_from_datastructure()
Expand Down Expand Up @@ -761,7 +735,6 @@ def has_py_files(self, path):
)

demo_view = View(
# VSplit(
Tabbed(
Item(
"description",
Expand All @@ -786,19 +759,7 @@ def has_py_files(self, path):
export="DockWindowShell",
id="tabbed",
),
# JDM moving log panel provisionally to its own tab, distracting here.
# VGroup(
# Item( 'log',
# show_label = False,
# style = 'readonly'
# ),
# label = 'Log'
# ),
# export = 'DockWindowShell',
# id = 'vsplit'
# ),
id="traitsui.demos.demo.file_view",
# dock = 'horizontal',
handler=demo_file_handler,
)

Expand Down Expand Up @@ -841,15 +802,7 @@ def default_traits_view(self):
),
title=self.title,
id="traitsui.demos.demo.Demo",
# dock = 'horizontal',
resizable=True,
# JDM: Seems that QT interface does not deal well with these size
# limits.
# With them, we get repeated:
# Object::disconnect: Parentheses expected, signal AdvancedCodeWidget::lostFocus
# But without them, it throws an exception on exit:
# Internal C++ object (_StickyDialog) already deleted.
# No, actually sometimes we get the latter even with them.
width=950,
height=900,
)
Expand Down