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

Provide string interpolation support for async/dynamic dep attributes in R #1048

Merged
merged 25 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f32833c
:sparkles: async/dynamic support in R pkg deps
Dec 9, 2019
a40e246
:hocho: commented out line
Dec 9, 2019
c332a09
:see_no_evil: fix case errors and parens
Dec 11, 2019
eb18503
:see_no_evil: move async/dynamic to correct pos
Dec 11, 2019
744db9c
:sparkles: async/dynamic support in R pkg deps
Dec 9, 2019
1ca04e4
Merge branch '1042-async-rsupport' of github.com:plotly/dash into 104…
Dec 13, 2019
7643ecc
Merge branch 'dev' into 1042-async-rsupport
rpkyle Dec 13, 2019
85a82ac
:necktie: satisfy linting overlords
Dec 13, 2019
ede3df4
Merge branch '1042-async-rsupport' of github.com:plotly/dash into 104…
Dec 13, 2019
269a1fa
:hocho: remove Authors block
Dec 17, 2019
2221282
:hocho: insert package version number
Dec 17, 2019
814022d
:hammer: use verbose :package: title and desc from YAML
Dec 17, 2019
54103c7
:sparkles: autodetect vignettes
Dec 17, 2019
a615eb2
fix package suggests autodefine
Dec 17, 2019
5a46be2
:see_no_evil: remove pdb
Dec 17, 2019
cc87f2e
:bug: specify title/desc if no yaml
Dec 18, 2019
3ff7607
:hocho: blank line if no VB, use get_async_type
Dec 18, 2019
f2171a7
check for author/maintainer address
Dec 18, 2019
03e7359
:hand: halt processing if fatal errors found
Dec 19, 2019
f53f7dc
autopopulate KeepSource
Dec 19, 2019
52a3394
remove error, auto-escape %
Dec 19, 2019
cb061bd
must auto-escape % in description too
Dec 19, 2019
1d5a112
:hocho: package-lock.json
Dec 20, 2019
45885a2
Merge branch 'dev' into 1042-async-rsupport
rpkyle Jan 7, 2020
b471d35
:hocho: remove examples from docstrings in R
Jan 7, 2020
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
65 changes: 57 additions & 8 deletions dash/development/_r_components_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@
file = "deps"), meta = NULL,
script = {script_name},
stylesheet = {css_name}, head = NULL, attachment = NULL, package = "{rpkgname}",
all_files = FALSE), class = "html_dependency")""" # noqa:E501
all_files = FALSE{async_or_dynamic}), class = "html_dependency")""" # noqa:E501

frame_body_template = """`{project_shortname}` = structure(list(name = "{project_shortname}",
version = "{project_ver}", src = list(href = NULL,
file = "deps"), meta = NULL,
script = {script_name},
stylesheet = {css_name}, head = NULL, attachment = NULL, package = "{rpkgname}",
all_files = FALSE), class = "html_dependency")""" # noqa:E501
all_files = FALSE{async_or_dynamic}), class = "html_dependency")""" # noqa:E501

frame_close_template = """)
return(deps_metadata)
Expand Down Expand Up @@ -81,9 +81,8 @@
"""

description_template = """Package: {package_name}
Title: {package_description}
Title: {package_title}
Version: {package_version}
Authors @R: as.person(c({package_author}))
Description: {package_description}
Depends: R (>= 3.0.2){package_depends}
Imports: {package_imports}
Expand All @@ -93,6 +92,7 @@
BugReports: {package_issues}
Encoding: UTF-8
LazyData: true
{vignette_builder}
Author: {package_author_no_email}
Maintainer: {maintainer}
"""
Expand Down Expand Up @@ -276,18 +276,23 @@ def generate_js_metadata(pkg_data, project_shortname):
# pylint: disable=consider-using-enumerate
if len(alldist) > 1:
for dep in range(len(alldist)):
rpp = alldist[dep]["relative_package_path"]
curr_dep = alldist[dep]
rpp = curr_dep["relative_package_path"]

async_or_dynamic = check_async_type(curr_dep)

if "dash_" in rpp:
dep_name = rpp.split(".")[0]
else:
dep_name = "{}".format(project_shortname)
project_ver = str(dep)

if "css" in rpp:
css_name = "'{}'".format(rpp)
script_name = 'NULL'
else:
script_name = "'{}'".format(rpp)
css_name = 'NULL'

function_frame += [
frame_element_template.format(
dep_name=dep_name,
Expand All @@ -296,23 +301,30 @@ def generate_js_metadata(pkg_data, project_shortname):
project_shortname=project_shortname,
script_name=script_name,
css_name=css_name,
async_or_dynamic=async_or_dynamic,
)
]
function_frame_body = ",\n".join(function_frame)
elif len(alldist) == 1:
rpp = alldist[0]["relative_package_path"]
dep = alldist[0]
Copy link
Contributor

Choose a reason for hiding this comment

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

Not part of this change, but I'm having trouble understanding why we process an array of length one differently from an array of length N

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The code doesn't really make much sense as written, but that's because I had trouble getting it to work with enumerate when I wrote it a year ago. I had only been programming in Python for a week or two, so this code is pretty much a giant pile of 💩. It needs some love and refactoring, no disagreement there.

rpp = dep["relative_package_path"]

async_or_dynamic = check_async_type(dep)

if "css" in rpp:
css_name = "'{}'".format(rpp)
script_name = "NULL"
else:
script_name = "'{}'".format(rpp)
css_name = "NULL"

function_frame_body = frame_body_template.format(
project_shortname=project_shortname,
project_ver=project_ver,
rpkgname=rpkgname,
script_name=script_name,
css_name=css_name,
async_or_dynamic=async_or_dynamic,
)

function_string = "".join(
Expand All @@ -322,6 +334,24 @@ def generate_js_metadata(pkg_data, project_shortname):
return function_string


# determine whether dependency uses async or dynamic flag
# then return the properly formatted string if so, i.e.
# " async = TRUE,". a dependency can have async or
# dynamic elements, neither of these, but never both.
def check_async_type(dep):
Copy link
Contributor

Choose a reason for hiding this comment

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

Would suggest renaming to get_** instead as this doesn't check but rather returns some specific value to use as-is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 3ff7607

async_or_dynamic = ""
for key in dep.keys():
if (key in ['async', 'dynamic']):
keyval = dep[key]
if not isinstance(keyval, bool):
keyval = "'{}'".format(keyval.lower())
else:
keyval = str(keyval).upper()
async_or_dynamic = \
", {} = {}".format(key, keyval)
return async_or_dynamic


# This method wraps code within arbitrary LaTeX-like tags, which are used
# by R's internal help parser for constructing man pages
def wrap(tag, code):
Expand Down Expand Up @@ -509,7 +539,17 @@ def generate_rpkg(

package_name = snake_case_to_camel_case(project_shortname)
lib_name = pkg_data.get("name")
package_description = pkg_data.get("description", "")

if rpkg_data is not None:
if rpkg_data.get("pkg_help_title"):
package_title = rpkg_data.get("pkg_help_title",
pkg_data.get("description",
""))
Marc-Andre-Rivet marked this conversation as resolved.
Show resolved Hide resolved
if rpkg_data.get("pkg_help_description"):
package_description = rpkg_data.get("pkg_help_description",
pkg_data.get("description",
""))

package_version = pkg_data.get("version", "0.0.1")

# remove leading and trailing commas
Expand Down Expand Up @@ -565,6 +605,13 @@ def generate_rpkg(
for rpackage in rpackage_list:
packages_string += "\nimport({})\n".format(rpackage)

if os.path.exists("vignettes"):
vignette_builder = "VignetteBuilder: knitr"
if not "knitr" and "rmarkdown" in package_suggests:
package_suggests += ", knitr, rmarkdown".lstrip(", ")
else:
vignette_builder = ""

pkghelp_stub_path = os.path.join("man", package_name + "-package.Rd")

# generate the internal (not exported to the user) functions which
Expand All @@ -582,6 +629,7 @@ def generate_rpkg(

description_string = description_template.format(
package_name=package_name,
package_title=package_title,
package_description=package_description,
package_version=package_version,
package_author=package_author,
Expand All @@ -591,6 +639,7 @@ def generate_rpkg(
package_license=package_license,
package_url=package_url,
package_issues=package_issues,
vignette_builder=vignette_builder,
package_author_no_email=package_author_no_email,
maintainer=maintainer,
)
Expand Down
Loading