Skip to content

Commit 890d00e

Browse files
committed
added piccolo shell run command
1 parent a949760 commit 890d00e

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

piccolo/apps/shell/__init__.py

Whitespace-only changes.

piccolo/apps/shell/commands/__init__.py

Whitespace-only changes.

piccolo/apps/shell/commands/run.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
import typing as t
3+
4+
from piccolo.conf.apps import AppRegistry, AppConfig
5+
from piccolo.table import Table
6+
7+
8+
def start_ipython_shell(**tables: t.Dict[str, t.Type[Table]]):
9+
try:
10+
import IPython
11+
except ImportError:
12+
print(
13+
"Install iPython using `pip install ipython==7.6.1` to use this "
14+
"feature."
15+
)
16+
sys.exit(1)
17+
18+
existing_global_names = globals().keys()
19+
for table_class_name, table_class in tables.items():
20+
if table_class_name not in existing_global_names:
21+
globals()[table_class_name] = table_class
22+
23+
IPython.embed()
24+
25+
26+
def run():
27+
"""
28+
Runs an iPython shell, and automatically imports all of the Table classes
29+
from your project.
30+
"""
31+
try:
32+
import piccolo_conf
33+
except ImportError:
34+
print("Can't find piccolo_conf")
35+
sys.exit(1)
36+
37+
app_registry: AppRegistry = piccolo_conf.APP_REGISTRY
38+
39+
tables = {}
40+
spacer = "-------"
41+
42+
if app_registry.app_configs:
43+
print(spacer)
44+
45+
for app_name, app_config in app_registry.app_configs.items():
46+
app_config: AppConfig = app_config
47+
print(f"Importing {app_name} tables:")
48+
if app_config.table_classes:
49+
for table_class in app_config.table_classes:
50+
table_class_name = table_class.__name__
51+
print(f"- {table_class_name}")
52+
tables[table_class_name] = table_class
53+
else:
54+
print("- None")
55+
56+
print(spacer)
57+
58+
start_ipython_shell(**tables)

piccolo/apps/shell/piccolo_app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from piccolo.conf.apps import AppConfig
2+
from .commands.run import run
3+
4+
5+
APP_CONFIG = AppConfig(
6+
app_name="shell", migrations_folder_path="", commands=[run]
7+
)

piccolo/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from piccolo.apps.migrations.piccolo_app import APP_CONFIG as migrations_config
1111
from piccolo.apps.playground.piccolo_app import APP_CONFIG as playground_config
1212
from piccolo.apps.project.piccolo_app import APP_CONFIG as project_config
13+
from piccolo.apps.shell.piccolo_app import APP_CONFIG as shell_config
1314
from piccolo.apps.user.piccolo_app import APP_CONFIG as user_config
1415

1516

@@ -29,6 +30,7 @@ def main():
2930
migrations_config,
3031
playground_config,
3132
project_config,
33+
shell_config,
3234
user_config,
3335
]:
3436
for command in _app_config.commands:

0 commit comments

Comments
 (0)