1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import os |
3
|
|
|
import sys |
4
|
|
|
|
5
|
|
|
from gearbox.command import Command |
6
|
|
|
from paste.deploy import loadapp |
7
|
|
|
from webtest import TestApp |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class BaseCommand(Command): |
11
|
|
|
pass |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class AppContextCommand(BaseCommand): |
15
|
|
|
""" |
16
|
|
|
Command who initialize app context at beginning of take_action method. |
17
|
|
|
""" |
18
|
|
|
def __init__(self, *args, **kwargs): |
19
|
|
|
super(AppContextCommand, self).__init__(*args, **kwargs) |
20
|
|
|
self._wsgi_app = None |
21
|
|
|
self._test_app = None |
22
|
|
|
|
23
|
|
|
@staticmethod |
24
|
|
|
def _get_initialized_app_context(parsed_args): |
25
|
|
|
""" |
26
|
|
|
:param parsed_args: parsed args (eg. from take_action) |
27
|
|
|
:return: (wsgi_app, test_app) |
28
|
|
|
""" |
29
|
|
|
config_file = parsed_args.config_file |
30
|
|
|
config_name = 'config:%s' % config_file |
31
|
|
|
here_dir = os.getcwd() |
32
|
|
|
|
33
|
|
|
# Load locals and populate with objects for use in shell |
34
|
|
|
sys.path.insert(0, here_dir) |
35
|
|
|
|
36
|
|
|
# Load the wsgi app first so that everything is initialized right |
37
|
|
|
wsgi_app = loadapp(config_name, relative_to=here_dir) |
38
|
|
|
test_app = TestApp(wsgi_app) |
39
|
|
|
|
40
|
|
|
# Make available the tg.request and other global variables |
41
|
|
|
tresponse = test_app.get('/_test_vars') |
42
|
|
|
|
43
|
|
|
return wsgi_app, test_app |
44
|
|
|
|
45
|
|
|
def take_action(self, parsed_args): |
46
|
|
|
super(AppContextCommand, self).take_action(parsed_args) |
47
|
|
|
wsgi_app, test_app = self._get_initialized_app_context(parsed_args) |
48
|
|
|
self._wsgi_app = wsgi_app |
49
|
|
|
self._test_app = test_app |
50
|
|
|
|
51
|
|
|
def get_parser(self, prog_name): |
52
|
|
|
parser = super(AppContextCommand, self).get_parser(prog_name) |
53
|
|
|
|
54
|
|
|
parser.add_argument("-c", "--config", |
55
|
|
|
help='application config file to read (default: development.ini)', |
56
|
|
|
dest='config_file', default="development.ini") |
57
|
|
|
return parser |
58
|
|
|
|