Skip to content

Commit 3a9c3cf

Browse files
authored
Permit blank strings as rprefix, address #712 (#714)
* 🔨 modify transpiler to allow empty string as rprefix to address #712 * 🐫 improve formatting function logic * 🐛 fix error in help template * ⏪ do not prefix.lower() * ✏️ use format_fn_name + ext * 🔪 format_filename
1 parent 54775af commit 3a9c3cf

File tree

2 files changed

+66
-49
lines changed

2 files changed

+66
-49
lines changed

dash/development/_r_components_generation.py

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# Declaring longer string templates as globals to improve
1717
# readability, make method logic clearer to anyone inspecting
1818
# code below
19-
r_component_string = """{prefix}{name} <- function({default_argtext}{wildcards}) {{
19+
r_component_string = """{funcname} <- function({default_argtext}{wildcards}) {{
2020
{wildcard_declaration}
2121
component <- list(
2222
props = list({default_paramtext}{wildcards}),
@@ -60,9 +60,9 @@
6060
"""
6161

6262
help_string = """% Auto-generated: do not edit by hand
63-
\\name{{{prefix}{name}}}
63+
\\name{{{funcname}}}
6464
65-
\\alias{{{prefix}{name}}}
65+
\\alias{{{funcname}}}
6666
6767
\\title{{{name} component}}
6868
@@ -71,7 +71,7 @@
7171
}}
7272
7373
\\usage{{
74-
{argtext}
74+
{funcname}({default_argtext})
7575
}}
7676
7777
\\arguments{{
@@ -205,18 +205,16 @@ def generate_class_string(name, props, project_shortname, prefix):
205205
for p in prop_keys
206206
)
207207

208-
return r_component_string.format(
209-
prefix=prefix,
210-
name=name,
211-
default_argtext=default_argtext,
212-
wildcards=wildcards,
213-
wildcard_declaration=wildcard_declaration,
214-
default_paramtext=default_paramtext,
215-
project_shortname=project_shortname,
216-
prop_names=prop_names,
217-
wildcard_names=wildcard_names,
218-
package_name=package_name,
219-
)
208+
return r_component_string.format(funcname=format_fn_name(prefix, name),
209+
name=name,
210+
default_argtext=default_argtext,
211+
wildcards=wildcards,
212+
wildcard_declaration=wildcard_declaration,
213+
default_paramtext=default_paramtext,
214+
project_shortname=project_shortname,
215+
prop_names=prop_names,
216+
wildcard_names=wildcard_names,
217+
package_name=package_name)
220218

221219

222220
# pylint: disable=R0914
@@ -319,7 +317,7 @@ def write_help_file(name, props, description, prefix):
319317
writes an R help file to the man directory for the generated R package
320318
321319
"""
322-
file_name = "{}{}.Rd".format(prefix, name)
320+
file_name = format_fn_name(prefix, name) + ".Rd"
323321

324322
default_argtext = ""
325323
item_text = ""
@@ -356,32 +354,23 @@ def write_help_file(name, props, description, prefix):
356354
file_path = os.path.join('man', file_name)
357355
with open(file_path, 'w') as f:
358356
f.write(help_string.format(
359-
prefix=prefix,
357+
funcname=format_fn_name(prefix, name),
360358
name=name,
361-
argtext=textwrap.fill(argtext,
362-
width=80,
363-
break_long_words=False),
359+
default_argtext=textwrap.fill(argtext,
360+
width=80,
361+
break_long_words=False),
364362
item_text=item_text,
365363
description=description.replace('\n', ' ')
366364
))
367365

368366

369-
def write_class_file(name, props, description, project_shortname, prefix=None):
367+
def write_class_file(name,
368+
props,
369+
description,
370+
project_shortname,
371+
prefix=None):
370372
props = reorder_props(props=props)
371373

372-
import_string = "# AUTO GENERATED FILE - DO NOT EDIT\n\n"
373-
class_string = generate_class_string(name,
374-
props,
375-
project_shortname,
376-
prefix)
377-
378-
file_name = "{}{}.R".format(prefix, name)
379-
380-
file_path = os.path.join("R", file_name)
381-
with open(file_path, "w") as f:
382-
f.write(import_string)
383-
f.write(class_string)
384-
385374
# generate the R help pages for each of the Dash components that we
386375
# are transpiling -- this is done to avoid using Roxygen2 syntax,
387376
# we may eventually be able to generate similar documentation using
@@ -394,6 +383,22 @@ def write_class_file(name, props, description, project_shortname, prefix=None):
394383
prefix
395384
)
396385

386+
import_string =\
387+
"# AUTO GENERATED FILE - DO NOT EDIT\n\n"
388+
class_string = generate_class_string(
389+
name,
390+
props,
391+
project_shortname,
392+
prefix
393+
)
394+
395+
file_name = format_fn_name(prefix, name) + ".R"
396+
397+
file_path = os.path.join("R", file_name)
398+
with open(file_path, "w") as f:
399+
f.write(import_string)
400+
f.write(class_string)
401+
397402
print("Generated {}".format(file_name))
398403

399404

@@ -596,6 +601,16 @@ def snake_case_to_camel_case(namestring):
596601
return s[0] + "".join(w.capitalize() for w in s[1:])
597602

598603

604+
# this logic will permit passing blank R prefixes to
605+
# dash-generate-components, while also enforcing
606+
# camelCase for the resulting functions; if a prefix
607+
# is supplied, leave it as-is
608+
def format_fn_name(prefix, name):
609+
if prefix:
610+
return prefix + snake_case_to_camel_case(name)
611+
return snake_case_to_camel_case(name[0].lower() + name[1:])
612+
613+
599614
# pylint: disable=unused-argument
600615
def generate_exports(
601616
project_shortname,

dash/development/component_generator.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def generate_components(
5050

5151
project_shortname = project_shortname.replace("-", "_").rstrip("/\\")
5252

53-
if rprefix:
53+
if rprefix is not None:
5454
prefix = rprefix
5555

5656
is_windows = sys.platform == "win32"
@@ -98,25 +98,27 @@ def generate_components(
9898

9999
generator_methods = [generate_class_file]
100100

101-
if rprefix:
102-
if not os.path.exists("man"):
103-
os.makedirs("man")
104-
if not os.path.exists("R"):
105-
os.makedirs("R")
106-
generator_methods.append(functools.partial(write_class_file,
107-
prefix=prefix))
101+
if rprefix is not None:
102+
if not os.path.exists('man'):
103+
os.makedirs('man')
104+
if not os.path.exists('R'):
105+
os.makedirs('R')
106+
generator_methods.append(
107+
functools.partial(write_class_file, prefix=prefix))
108108

109-
components = generate_classes_files(project_shortname,
110-
metadata,
111-
*generator_methods)
109+
components = generate_classes_files(
110+
project_shortname,
111+
metadata,
112+
*generator_methods
113+
)
112114

113-
with open(os.path.join(project_shortname, "metadata.json"), "w") as f:
115+
with open(os.path.join(project_shortname, 'metadata.json'), 'w') as f:
114116
json.dump(metadata, f, indent=2)
115117

116118
generate_imports(project_shortname, components)
117119

118-
if rprefix:
119-
with open("package.json", "r") as f:
120+
if rprefix is not None:
121+
with open('package.json', 'r') as f:
120122
jsondata_unicode = json.load(f, object_pairs_hook=OrderedDict)
121123
if sys.version_info[0] >= 3:
122124
pkg_data = jsondata_unicode

0 commit comments

Comments
 (0)