forked from quokkaproject/quokka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·129 lines (107 loc) · 3.5 KB
/
manage.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import click
from quokka import create_app
from quokka.ext.blueprints import blueprint_commands
from quokka.core.db import db
app = create_app()
if app.config.get("LOGGER_ENABLED"):
logging.basicConfig(
level=getattr(logging, app.config.get("LOGGER_LEVEL", "DEBUG")),
format=app.config.get(
"LOGGER_FORMAT",
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s'),
datefmt=app.config.get("LOGGER_DATE_FORMAT", '%d.%m %H:%M:%S')
)
@click.group()
def core_cmd():
""" Core commands """
pass
@core_cmd.command()
@click.option('--ipython/--no-ipython', default=True)
def shell(ipython):
"""Runs a Python shell with Quokka context"""
import code
import readline
import rlcompleter
_vars = globals()
_vars.update(locals())
_vars.update(dict(app=app, db=db))
readline.set_completer(rlcompleter.Completer(_vars).complete)
readline.parse_and_bind("tab: complete")
try:
if ipython is True:
from IPython import start_ipython
start_ipython(argv=[], user_ns=_vars)
else:
raise ImportError
except ImportError:
shell = code.InteractiveConsole(_vars)
shell.interact()
@core_cmd.command()
def check():
"""Prints app status"""
from pprint import pprint
print("Extensions.")
pprint(app.extensions)
print("Modules.")
pprint(app.blueprints)
print("App.")
return app
@core_cmd.command()
@click.option(
'-f',
'--filename',
help='Fixtures JSON path',
default='./etc/fixtures/initial_data.json')
@click.option('-b', '--baseurl', help='base url to use', default=None)
def populate(filename, baseurl=None):
"""Populate the database with sample data"""
from quokka.utils.populate import Populate
Populate(db, filepath=filename, baseurl=baseurl, app=app)()
@core_cmd.command()
@click.option(
'-f',
'--filename',
help='Fixtures JSON path',
default='./etc/fixtures/initial_data.json')
@click.option('-b', '--baseurl', help='base url to use', default=None)
def populate_reset(filename, baseurl=None):
"""De-Populate the database with sample data"""
from quokka.utils.populate import Populate
Populate(db, filepath=filename, baseurl=baseurl, app=app).reset()
@core_cmd.command()
def showconfig():
"""Print all config variables"""
from pprint import pprint
print("Config.")
pprint(dict(app.config.store))
@core_cmd.command()
@click.option('--reloader/--no-reloader', default=True)
@click.option('--debug/--no-debug', default=True)
@click.option('--host', default='127.0.0.1')
@click.option('--port', default=5000)
def runserver(reloader, debug, host, port):
"""Run the Flask development server i.e. app.run()"""
app.run(use_reloader=reloader, debug=debug, host=host, port=port)
help_text = """
Subcommands are loaded from the module/commands folder dynamically.
The module name and command file inside commands folder will be
used for compose the command name.
(For a file in the path 'mymodule/commands/sayhi.py' the command
name will be 'mymodule_sayhi')
The click command function must be named 'cli'.
Example:
\b
import click
@click.command()
def cli():
click.echo("Do whatever you want")
"""
manager = click.CommandCollection(help=help_text)
manager.add_source(core_cmd)
manager.add_source(blueprint_commands(app))
if __name__ == '__main__':
with app.app_context():
manager()