-
Notifications
You must be signed in to change notification settings - Fork 124
/
manage.py
119 lines (91 loc) · 3.07 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
# -*- coding: utf-8 -*-
'''
Created on 2016-12-08
@author: hustcc
'''
from __future__ import absolute_import
from flask_script import Manager, Command, Server as _Server, Option
from app import SQLAlchemyDB as db, app, __version__
import os
manager = Manager(app)
class Server(_Server):
help = description = 'Runs the Redis-monitor web server'
def get_options(self):
options = (
Option('-h', '--host',
dest='host',
default='0.0.0.0'),
Option('-p', '--port',
dest='port',
type=int,
default=9527),
Option('-d', '--debug',
action='store_true',
dest='use_debugger',
help=('enable the Werkzeug debugger (DO NOT use in '
'production code)'),
default=self.use_debugger),
Option('-D', '--no-debug',
action='store_false',
dest='use_debugger',
help='disable the Werkzeug debugger',
default=self.use_debugger)
)
return options
def __call__(self, app, host, port, use_debugger):
if use_debugger is None:
use_debugger = app.debug
if use_debugger is None:
# default is False
use_debugger = False
app.run(host, port, debug=use_debugger, threaded=True)
manager.add_command("start", Server())
CONFIG_TEMP = """# -*- coding: utf-8 -*-
'''
Created on 2016-12-07
@author: hustcc
'''
# for sqlite
SQLALCHEMY_DATABASE_URI = 'sqlite:///%s'
# for mysql
# SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://dev:dev@127.0.0.1/redis_monitor'
"""
class Init(Command):
"""Generates new configuration file into user Home dir."""
name = 'config'
capture_all_args = True
def run(self, argv):
dir = os.path.join(os.path.expanduser('~'), '.redis-monitor')
if not os.path.exists(dir):
os.makedirs(dir)
if os.path.isdir(dir):
path = os.path.join(dir, 'redis_monitor_config.py')
if os.path.exists(path):
print('Fail: the configuration file exist in `%s`.' % path)
else:
sqlite_file = os.path.join(dir, 'redis_monitor.db') \
.replace('\\', '/')
with open(path, 'w') as f:
f.write(CONFIG_TEMP % sqlite_file)
print('OK: init configuration file into `%s`.' % path)
db.create_all()
print('OK: database is initialed into `%s`.' % sqlite_file)
else:
print('Fail: %s should be directory.' % dir)
manager.add_command("init", Init())
@manager.command
def createdb(drop_first=False):
"""Creates the database."""
if drop_first:
db.drop_all()
db.create_all()
print('OK: database is initialed.')
@manager.command
def version():
"Shows the version"
print(__version__)
# script entry
def run():
manager.run()
if __name__ == '__main__':
manager.run()