Skip to content

Commit 19dfa76

Browse files
committed
🐫 generate_imports
1 parent ef8ac5a commit 19dfa76

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

dash/development/base_component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def generate_class_file(typename, props, description, namespace):
465465
with open(file_path, 'w') as f:
466466
f.write(import_string)
467467
f.write(class_string)
468-
468+
469469
print('Generated {}'.format(file_name))
470470

471471

dash/development/component_generator.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import textwrap
99

10+
from dash.development.component_loader import generate_imports
1011
from .base_component import generate_class_file
1112

1213

@@ -58,20 +59,7 @@ def generate_components(component_src, project_shortname):
5859
with open(os.path.join(project_shortname, 'metadata.json'), 'w') as f:
5960
json.dump(metadata, f)
6061

61-
with open(os.path.join(project_shortname, '_imports_.py'), 'w') as f:
62-
f.write(textwrap.dedent(
63-
'''
64-
{}
65-
66-
__all__ = [
67-
{}
68-
]
69-
'''.format(
70-
'\n'.join(
71-
'from .{0} import {0}'.format(x) for x in components),
72-
',\n'.join(' "{}"'.format(x) for x in components)
73-
)
74-
).lstrip())
62+
generate_imports(project_shortname, components)
7563

7664

7765
def cli():

dash/development/component_loader.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import collections
22
import json
33
import os
4+
import textwrap
5+
46
from .base_component import generate_class
57
from .base_component import generate_class_file
68
from .base_component import ComponentRegistry
@@ -16,6 +18,23 @@ def _get_metadata(metadata_path):
1618
return data
1719

1820

21+
def generate_imports(project_shortname, components):
22+
with open(os.path.join(project_shortname, '_imports_.py'), 'w') as f:
23+
f.write(textwrap.dedent(
24+
'''
25+
{}
26+
27+
__all__ = [
28+
{}
29+
]
30+
'''.format(
31+
'\n'.join(
32+
'from .{0} import {0}'.format(x) for x in components),
33+
',\n'.join(' "{}"'.format(x) for x in components)
34+
)
35+
).lstrip())
36+
37+
1938
def load_components(metadata_path,
2039
namespace='default_namespace'):
2140
"""Load React component metadata into a format Dash can parse.
@@ -81,6 +100,8 @@ def generate_classes(namespace, metadata_path='lib/metadata.json'):
81100
if os.path.exists(imports_path):
82101
os.remove(imports_path)
83102

103+
components = []
104+
84105
# Iterate over each property name (which is a path to the component)
85106
for componentPath in data:
86107
componentData = data[componentPath]
@@ -97,16 +118,7 @@ def generate_classes(namespace, metadata_path='lib/metadata.json'):
97118
componentData['description'],
98119
namespace
99120
)
100-
101-
# Add an import statement for this component
102-
with open(imports_path, 'a') as f:
103-
f.write('from .{0:s} import {0:s}\n'.format(name))
121+
components.append(name)
104122

105123
# Add the __all__ value so we can import * from _imports_
106-
all_imports = [p.split('/').pop().split('.')[0] for p in data]
107-
with open(imports_path, 'a') as f:
108-
array_string = '[\n'
109-
for a in all_imports:
110-
array_string += ' "{:s}",\n'.format(a)
111-
array_string += ']\n'
112-
f.write('\n\n__all__ = {:s}'.format(array_string))
124+
generate_imports(namespace, components)

0 commit comments

Comments
 (0)