Skip to content

Support Pyface Color in TraitsUI toolkit Color traits.#1812

Merged
corranwebster merged 6 commits into
mainfrom
enh/color-trait-improvements
Feb 21, 2022
Merged

Support Pyface Color in TraitsUI toolkit Color traits.#1812
corranwebster merged 6 commits into
mainfrom
enh/color-trait-improvements

Conversation

@corranwebster
Copy link
Copy Markdown
Contributor

@corranwebster corranwebster commented Feb 14, 2022

In addition this PR standardizes the set of recognized color names to those that Pyface Color understands (basically all web colors, which aligns with Enable/Chaco). Previously these were just whatever the backend understood.

This includes tests for the PyQtColor and WxColor traits (which correspond to the traits.api Color trait) and which were previously absent.

Checklist

  • Add a news fragment if this PR is news-worthy for end users. (see docs/releases/README.rst)

Copy link
Copy Markdown
Contributor

@aaronayres35 aaronayres35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM just a few questions

standard_colors = {}
for name in QtGui.QColor.colorNames():
standard_colors[str(name)] = QtGui.QColor(name)
for name, rgba in color_table.items():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is color_table is a super set of the standard SVG color keyword names?
At a quick glance it seems to be but I did not go through all colors

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's basically the set that modern HTML understands, plus a couple of extra "transparent" synonyms.

""" Trait definition for a PyQt-based color.
"""

from ast import literal_eval
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why / when should we prefer literal_eval over eval? / why exactly is literal_eval "safer" - the source is limited to a set of python literal structures but its not immediately obvious to me why that is safer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

literal_eval only allows the evaluation of literal expressions (no operations, function calls, . operators, etc.). This means that all of the sneaky tricks that someone can do to craft an expression that executes arbitrary code are not available. Given that these color names may be parsed from untrusted user input, and are only used to evaluate tuples, literal_eval is the right choice.

For things like enabled_when expressions we have to allow the full power of eval for now.

return value.to_toolkit()

elif isinstance(value, wx.Colour):
return value
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do WxColor not handle spaces (I'm assuming not since there is no wx test analogous to the qt test_name_string_with_space) / if so why can't they?
Can we not just add a value = value.replace(" ", "") in the elif isinstance(value, str): block below?

Similarly I notice we still use eval rather than literal_eval on line 136. Same question here as above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made these updates, and added some tests around this and what happens if eval doesn't give something reasonable in several different ways.

Copy link
Copy Markdown
Contributor

@aaronayres35 aaronayres35 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the updates!

@corranwebster corranwebster merged commit d72934e into main Feb 21, 2022
@corranwebster corranwebster deleted the enh/color-trait-improvements branch February 21, 2022 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants