-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_component
executable file
·46 lines (34 loc) · 1.36 KB
/
add_component
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh
echo "Component Topic (often data, model, ...):"
read topic
echo "[Optionally] Component Type (type for which multiple implementations may exist): "
read component_type
echo "Component Name (Actual name for component variant): "
read component_name
caps_component_type=${component_type^^}
component_class=$(echo $component_name | sed -r 's/_([a-z])/\U\1/gi; s/^([a-z])/\U\1/gi; s/$/Model/')
root_path="src/$topic/$component_type"
[ -z $component_type ] && root_path="src/$topic"
mkdir -p "$root_path"
[ -f "$root_path/__init__.py" ] || echo \
"""from .builder import ${caps_component_type}S, build_$component_type
from .$component_name import $component_class
__all__ = [
'$component_class',
'${caps_component_type}S',
'build_$component_type'
]
""" > "$root_path/__init__.py"
[ -f "$root_path/builder.py" ] || echo \
"""from src.utils.config import Registry, build_from_config
${caps_component_type}S = Registry(\"${component_type}s\")
def build_$component_type(cfg, default_args=None):
return build_from_config(cfg, ${caps_component_type}S, default_args=default_args)
""" > "$root_path/builder.py"
[ -f "$root_path/$component_name.py" ] || echo \
"""from .builder import ${caps_component_type}S
@${caps_component_type}S.register_module()
class $component_class:
def __init__(self):
pass
""" > "$root_path/$component_name.py" && doit configs