-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.py
executable file
·199 lines (155 loc) · 6.24 KB
/
run.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# -*- coding: utf-8 -*-
"""
run.py
~~~~~~
application management
"""
import os, sys
import click
from sqlalchemy import exc
from flask.cli import with_appcontext
from celery.bin.celery import main as celery_main
from application import create_app, TrelloList
from application.extensions import db
from application.utils import SubmissionsTrelloClient
from trello import ResourceUnavailable
app = create_app()
@app.cli.command("initlists")
def init_lists():
"""Populates a Trello board with the default lists."""
client = SubmissionsTrelloClient()
board = client.board
if not board:
click.secho("TRELLO_BOARD must be specified in the app configuration.", fg="red")
sys.exit(1)
# We iterate the list in reverse order because by default the Trello client inserts new lists
# at the beginning. So in a sense, we need to create them from right-to-left.
for default_list_options in reversed(app.config["DEFAULT_TRELLO_LISTS"]):
default_list_name = default_list_options["name"]
default_list_caption = default_list_options["default_caption"]
# Verity that this list is already in our database and that it points to a valid list on
# the trello board.
local_list = None
matching_lists = TrelloList().find(list_symbolic_name=default_list_name).all()
if matching_lists:
local_list = matching_lists[0]
# Verify that this list ID is on the trello board
try:
trello_list = board.get_list(local_list.list_id)
# Reopen the list if it was accidentally closed.
if trello_list.closed:
trello_list.open()
except ResourceUnavailable:
trello_list = None
if trello_list:
# All good.
click.echo(f' * "{default_list_name}" list present in both database and Trello.')
continue
# No entry or a mismatched ID in the database. See if the list already exists on the
# board (with the default caption).
for existing_list in board.open_lists():
if existing_list.name == default_list_caption:
# There it is!
click.echo(f' * Found existing Trello list for "{default_list_name}"...')
trello_list = existing_list
break
else:
# No matching trello list found. Create it.
click.echo(f' * Creating Trello list for "{default_list_name}"...')
trello_list = board.add_list(default_list_caption)
if local_list:
# Just need to update the local entry.
click.echo(f' * Updating list ID in database for "{default_list_name}"...')
local_list.list_id = trello_list.id
TrelloList().save(local_list)
else:
# Need to create a new local entry.
click.echo(f' * Adding list ID to database for "{default_list_name}"...')
TrelloList().create(list_symbolic_name=default_list_name, list_id=trello_list.id)
# all done
click.secho(" * DONE", fg="green")
@app.cli.command("initlabels")
def init_labels():
"""Populates a Trello board with the default labels."""
client = SubmissionsTrelloClient()
board = client.board
all_label_names = set(map(lambda x: x.name, board.get_labels()))
for default_labels_by_category in app.config["DEFAULT_TRELLO_LABELS"].values():
for label in default_labels_by_category.values():
default_caption = label["default_caption"]
if default_caption not in all_label_names:
click.echo(f' * Adding label "{default_caption}"...')
board.add_label(default_caption, label["default_color"])
else:
click.echo(f' * "{default_caption}" label already exists...')
# all done
click.secho(" * DONE", fg="green")
@app.cli.command("initboard")
def init_board():
"""Populates a Trello board with the default lists and labels."""
init_lists()
init_labels()
@app.cli.command("runserver")
@click.option("--reload", "reload", is_flag=True, help="run application with livereload")
def runserver(reload):
"""Shortcut to ``flask run``"""
if reload:
import livereload
server = livereload.Server(app.wsgi_app)
server.watch(".", ignore=lambda x: ("log" in x))
server.serve(port=os.environ.get("PORT", "9999"), host=os.environ.get("HOST", "localhost"))
return
app.run()
@app.cli.command("celeryd")
def celeryd():
celery_args = [
"celery",
"worker",
"-l",
"info",
"-E",
"-c",
"2",
"--without-gossip",
"--without-mingle",
"--without-heartbeat",
]
if os.name == "nt":
# Run "solo" in Windows
celery_args += ["-P", "solo"]
with app.app_context():
return celery_main(celery_args)
@app.cli.command("ishell")
@click.argument("ipython_args", nargs=-1, type=click.UNPROCESSED)
@with_appcontext
def shell(ipython_args):
"""Runs an iPython shell in the app context.
Runs an interactive Python shell in the context of a given Flask application. The application
will populate the default namespace of this shell according to it's configuration. This is
useful for executing small snippets of management code without having to manually configuring
the application.
Stolen from: https://github.com/ei-grad/flask-shell-ipython/blob/master/flask_shell_ipython.py
"""
import IPython
from IPython.terminal.ipapp import load_default_config
from traitlets.config.loader import Config
from flask.globals import _app_ctx_stack
app = _app_ctx_stack.top.app
if "IPYTHON_CONFIG" in app.config:
config = Config(app.config["IPYTHON_CONFIG"])
else:
config = load_default_config()
config.TerminalInteractiveShell.banner1 = """Python %s on %s
IPython: %s
App: %s%s
Instance: %s""" % (
sys.version,
sys.platform,
IPython.__version__,
app.import_name,
app.debug and " [debug]" or "",
app.instance_path,
)
IPython.start_ipython(argv=ipython_args, user_ns=app.make_shell_context(), config=config)
if __name__ == "__main__":
runserver(["--reload"])