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

Replace docstring \r\n with \n when generating classes from windows. #431

Merged
merged 2 commits into from
Oct 18, 2018

Conversation

T4rk1n
Copy link
Contributor

@T4rk1n T4rk1n commented Oct 18, 2018

Resolves #366

@T4rk1n T4rk1n requested a review from rmarren1 October 18, 2018 21:05
@rmarren1
Copy link
Contributor

Would just changing this part work?

return (
"""A {name} component.\n{description}
Keyword arguments:\n{args}
Available events: {events}"""
).format(

I think git will auto-magically put \r\n instead of \n in this code block, which we can avoid by explicitly putting \n
e.g. return ("A {name} component.\n{description}\n\nKeyword arguments:\n{args}\n\nAvailable events: {events}").format(...)

@T4rk1n
Copy link
Contributor Author

T4rk1n commented Oct 18, 2018

No it wouldn't work, it's not those that are explicit the problems (\n), it's the whitespace newlines that get auto \r\n by python not git. By replacing the output of the docstring, we can keep a sane docstring.

Copy link
Contributor

@rmarren1 rmarren1 left a comment

Choose a reason for hiding this comment

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

Okay that makes sense, didn't know python was the one doing that.

@rmarren1
Copy link
Contributor

💃

Do we also need to do that replacement here?

"""
Dynamically generate class strings to have nicely formatted docstrings,
keyword arguments, and repr
Inspired by http://jameso.be/2013/08/06/namedtuple.html
Parameters
----------
typename
props
description
namespace
Returns
-------
string
"""
# TODO _prop_names, _type, _namespace, available_events,
# and available_properties
# can be modified by a Dash JS developer via setattr
# TODO - Tab out the repr for the repr of these components to make it
# look more like a hierarchical tree
# TODO - Include "description" "defaultValue" in the repr and docstring
#
# TODO - Handle "required"
#
# TODO - How to handle user-given `null` values? I want to include
# an expanded docstring like Dropdown(value=None, id=None)
# but by templating in those None values, I have no way of knowing
# whether a property is None because the user explicitly wanted
# it to be `null` or whether that was just the default value.
# The solution might be to deal with default values better although
# not all component authors will supply those.
c = '''class {typename}(Component):
"""{docstring}"""
@_explicitize_args
def __init__(self, {default_argtext}):
self._prop_names = {list_of_valid_keys}
self._type = '{typename}'
self._namespace = '{namespace}'
self._valid_wildcard_attributes =\
{list_of_valid_wildcard_attr_prefixes}
self.available_events = {events}
self.available_properties = {list_of_valid_keys}
self.available_wildcard_properties =\
{list_of_valid_wildcard_attr_prefixes}
_explicit_args = kwargs.pop('_explicit_args')
_locals = locals()
_locals.update(kwargs) # For wildcard attrs
args = {{k: _locals[k] for k in _explicit_args if k != 'children'}}
for k in {required_args}:
if k not in args:
raise TypeError(
'Required argument `' + k + '` was not specified.')
super({typename}, self).__init__({argtext})
def __repr__(self):
if(any(getattr(self, c, None) is not None
for c in self._prop_names
if c is not self._prop_names[0])
or any(getattr(self, c, None) is not None
for c in self.__dict__.keys()
if any(c.startswith(wc_attr)
for wc_attr in self._valid_wildcard_attributes))):
props_string = ', '.join([c+'='+repr(getattr(self, c, None))
for c in self._prop_names
if getattr(self, c, None) is not None])
wilds_string = ', '.join([c+'='+repr(getattr(self, c, None))
for c in self.__dict__.keys()
if any([c.startswith(wc_attr)
for wc_attr in
self._valid_wildcard_attributes])])
return ('{typename}(' + props_string +
(', ' + wilds_string if wilds_string != '' else '') + ')')
else:
return (
'{typename}(' +
repr(getattr(self, self._prop_names[0], None)) + ')')
'''

@T4rk1n
Copy link
Contributor Author

T4rk1n commented Oct 18, 2018

No, I tested it and it works with just that change.

@T4rk1n T4rk1n force-pushed the generate-classes-endline branch from 0cb4c5d to d134bb9 Compare October 18, 2018 21:58
@T4rk1n T4rk1n merged commit a6a1ea9 into master Oct 18, 2018
@T4rk1n T4rk1n deleted the generate-classes-endline branch October 18, 2018 22:01
HammadTheOne pushed a commit to HammadTheOne/dash that referenced this pull request May 28, 2021
HammadTheOne pushed a commit that referenced this pull request Jul 23, 2021
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