Skip to content

Commit

Permalink
Add dash length list to dash property validator #1136
Browse files Browse the repository at this point in the history
Fixes #1107
  • Loading branch information
jonmmease authored Aug 29, 2018
1 parent d42b2ac commit d8095de
Show file tree
Hide file tree
Showing 25 changed files with 211 additions and 66 deletions.
76 changes: 76 additions & 0 deletions _plotly_utils/basevalidators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,82 @@ def validate_coerce(self, v):
return v


class DashValidator(EnumeratedValidator):
"""
Special case validator for handling dash properties that may be specified
as lists of dash lengths. These are not currently specified in the
schema.
"dash": {
"valType": "string",
"values": [
"solid",
"dot",
"dash",
"longdash",
"dashdot",
"longdashdot"
],
"dflt": "solid",
"role": "style",
"editType": "style",
"description": "Sets the dash style of lines. Set to a dash type
string (*solid*, *dot*, *dash*, *longdash*, *dashdot*, or
*longdashdot*) or a dash length list in px (eg *5px,10px,2px,2px*)."
},
"""
def __init__(self,
plotly_name,
parent_name,
values,
**kwargs):

# Add regex to handle dash length lists
dash_list_regex = \
r"/^\d+(\.\d+)?(px|%)?((,|\s)\s*\d+(\.\d+)?(px|%)?)*$/"

values = values + [dash_list_regex]

# Call EnumeratedValidator superclass
super(DashValidator, self).__init__(
plotly_name=plotly_name,
parent_name=parent_name,
values=values, **kwargs)

def description(self):

# Separate regular values from regular expressions
enum_vals = []
enum_regexs = []
for v, regex in zip(self.values, self.val_regexs):
if regex is not None:
enum_regexs.append(regex.pattern)
else:
enum_vals.append(v)
desc = ("""\
The '{name}' property is an enumeration that may be specified as:"""
.format(name=self.plotly_name))

if enum_vals:
enum_vals_str = '\n'.join(
textwrap.wrap(
repr(enum_vals),
initial_indent=' ' * 12,
subsequent_indent=' ' * 12,
break_on_hyphens=False,
width=80))

desc = desc + """
- One of the following dash styles:
{enum_vals_str}""".format(enum_vals_str=enum_vals_str)

desc = desc + """
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
"""
return desc


class ImageUriValidator(BaseValidator):
_PIL = None

Expand Down
53 changes: 53 additions & 0 deletions _plotly_utils/tests/validators/test_dash_validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest
from _plotly_utils.basevalidators import DashValidator


# Constants
# ---------
dash_types = ['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']


# Fixtures
# --------
@pytest.fixture()
def validator():
return DashValidator('prop', 'parent', dash_types)


# Acceptance
# ----------
@pytest.mark.parametrize('val',
dash_types)
def test_acceptance_dash_types(val, validator):
# Values should be accepted and returned unchanged
assert validator.validate_coerce(val) == val


@pytest.mark.parametrize('val',
['2', '2.2', '2.002', '1 2 002',
'1,2,3', '1, 2, 3',
'1px 2px 3px', '1.5px, 2px, 3.9px',
'23% 18% 13px', '200% 3px'])
def test_acceptance_dash_lists(val, validator):
# Values should be accepted and returned unchanged
assert validator.validate_coerce(val) == val


# Rejection
# ---------
# ### Value Rejection ###
@pytest.mark.parametrize('val', ['bogus', 'not-a-dash'])
def test_rejection_by_bad_dash_type(val, validator):
with pytest.raises(ValueError) as validation_failure:
validator.validate_coerce(val)

assert 'Invalid value' in str(validation_failure.value)


@pytest.mark.parametrize('val',
['', '1,,3,4', '2 3 C', '2pxx 3 4'])
def test_rejection_by_bad_dash_list(val, validator):
with pytest.raises(ValueError) as validation_failure:
validator.validate_coerce(val)

assert 'Invalid value' in str(validation_failure.value)
16 changes: 16 additions & 0 deletions codegen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ def write_init_py(pkg_root, path_parts, import_pairs):
'frame.layout': 'plotly.validators.LayoutValidator'
}

# Add custom dash validators
CUSTOM_VALIDATOR_DATATYPES.update(
{prop: '_plotly_utils.basevalidators.DashValidator'
for prop in [
'scatter.line.dash',
'histogram2dcontour.line.dash',
'scattergeo.line.dash',
'scatterpolar.line.dash',
'ohlc.line.dash',
'ohlc.decreasing.line.dash',
'ohlc.increasing.line.dash',
'contourcarpet.line.dash',
'contour.line.dash',
'scatterternary.line.dash',
'scattercarpet.line.dash']})

# Mapping from property string (as found in plot-schema.json) to a custom
# class name. If not included here, names are converted to TitleCase and
# underscores are removed.
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/contour/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/contourcarpet/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/histogram2dcontour/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/ohlc/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def dash(self):
set per direction via `increasing.line.dash` and
`decreasing.line.dash`.
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/ohlc/decreasing/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/ohlc/increasing/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/scatter/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/scattercarpet/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/scattergeo/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/scatterpolar/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
10 changes: 5 additions & 5 deletions plotly/graph_objs/scatterternary/_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def dash(self):
*longdashdot*) or a dash length list in px (eg
*5px,10px,2px,2px*).
The 'dash' property is a string and must be specified as:
- One of the following strings:
['solid', 'dot', 'dash', 'longdash', 'dashdot',
'longdashdot']
- A number that will be converted to a string
The 'dash' property is an enumeration that may be specified as:
- One of the following dash styles:
['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot']
- A string containing a dash length list in pixels or percentages
(e.g. '5px 10px 2px 2px', '5, 10, 2, 2', '10% 20% 40%', etc.)
Returns
-------
Expand Down
2 changes: 1 addition & 1 deletion plotly/validators/contour/line/_dash.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _plotly_utils.basevalidators


class DashValidator(_plotly_utils.basevalidators.StringValidator):
class DashValidator(_plotly_utils.basevalidators.DashValidator):

def __init__(
self, plotly_name='dash', parent_name='contour.line', **kwargs
Expand Down
2 changes: 1 addition & 1 deletion plotly/validators/contourcarpet/line/_dash.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _plotly_utils.basevalidators


class DashValidator(_plotly_utils.basevalidators.StringValidator):
class DashValidator(_plotly_utils.basevalidators.DashValidator):

def __init__(
self, plotly_name='dash', parent_name='contourcarpet.line', **kwargs
Expand Down
2 changes: 1 addition & 1 deletion plotly/validators/histogram2dcontour/line/_dash.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _plotly_utils.basevalidators


class DashValidator(_plotly_utils.basevalidators.StringValidator):
class DashValidator(_plotly_utils.basevalidators.DashValidator):

def __init__(
self,
Expand Down
2 changes: 1 addition & 1 deletion plotly/validators/ohlc/decreasing/line/_dash.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _plotly_utils.basevalidators


class DashValidator(_plotly_utils.basevalidators.StringValidator):
class DashValidator(_plotly_utils.basevalidators.DashValidator):

def __init__(
self, plotly_name='dash', parent_name='ohlc.decreasing.line', **kwargs
Expand Down
2 changes: 1 addition & 1 deletion plotly/validators/ohlc/increasing/line/_dash.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _plotly_utils.basevalidators


class DashValidator(_plotly_utils.basevalidators.StringValidator):
class DashValidator(_plotly_utils.basevalidators.DashValidator):

def __init__(
self, plotly_name='dash', parent_name='ohlc.increasing.line', **kwargs
Expand Down
Loading

0 comments on commit d8095de

Please sign in to comment.