-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
329 lines (289 loc) · 11.5 KB
/
runner.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from wsgic.thirdparty.bottle import run, app as App, load, _ImportRedirect
from wsgic.helpers.extra import set_global, get_global
from wsgic.helpers import ordered
import sys
import os
root = os.getcwd()
sys.path.append(
os.path.join(root, "apps")
)
def load(target, **namespace):
# Source bottle.py:load
module, target = target.split(":", 1) if ':' in target else (target, None)
if module not in sys.modules: __import__(module)
if not target: return sys.modules[module]
if target.isalnum(): return getattr(sys.modules[module], target)
package_name = module.split('.')[0]
namespace[package_name] = sys.modules[package_name]
return eval('%s.%s' % (module, target), namespace)
def _get(data, index=0, e=None):
try:return data[index] if type(data) in (list, dict, set, tuple) else data
except:return e
class Apprunner:
'''Application interface to initialize and run apps
::params::
:app:
:type: String
Name of module
'''
def __init__(self, app):
try:
app = load(app.replace(".app", "")+".app")
except ModuleNotFoundError:
app = load(app.replace(".app", ""))
self.app = app.__app__
set_global("app", self.app)
def init_app(self):
self.app.setup()
# handlers(self.app)
# def get_middlewares(self, app=None):
# if not app:app=self.app
# middlewares = app.get("middlewares", e=None)
# if middlewares:
# App.push(app)
# mapp = App()
# entries = ["wsgic"]
# for a in get_global("installed_apps"):
# entries.append(a.lower().replace("app", ""))
# for middleware in middlewares:
# for apps in entries:
# try:
# middlewareclass = load(f"{apps}.middleware:{middleware}")
# break
# except:pass
# _args = app.get(f"{middleware}['args']", e=(), raw=True)
# _kwargs = app.get(f"{middleware}['kwargs']", e={}, raw=True)
# mapp = middlewareclass(mapp, *_args, **_kwargs)
# else:mapp = self.app
# return mapp
def run(self, server='wsgiref', host='127.0.0.1', port=8080, interval=1, type="wsgi", reloader=False, quiet=False, plugins=None, debug=False, config=None, **kargs):
app = self.app.wrapped_app(type)
if type == "wsgi":
from werkzeug import run_simple
run_simple(
host, port, app, True, debug
)
# from bottle import run
# run(app, server=server, host=host, port=port, interval=interval, reloder=True, debug=True)
elif type == "asgi":
from uvicorn.main import run
run(
app, host=host, port=port, debug=debug
)
return
# if server == "gevent":
# from gevent import monkey
# monkey.patch_all()
# # run(app=self.get_middlewares(), **kw)
# return run(self.app, server=server, host=host, port=port, debug=debug, interval=interval, reloader=reloader, quiet=quiet, plugins=plugins, config=config, **kargs)
# def url(func=None, method=None, name=None, apply=None, skip=None):
# return (func, method, name, apply, skip)
def _cli_parse(args): # pragma: no coverage
from argparse import ArgumentParser
parser = ArgumentParser(prog="wsgic", usage="python -m %(prog)s [options] package.module:app")
opt = parser.add_argument
opt("--version", action="store_true", help="show version number.")
opt("-b", "--bind", metavar="ADDRESS", help="bind socket to ADDRESS.")
opt("-se", "--server", help="use SERVER as backend.")
opt("-p", "--plugin", action="append", help="install additional plugin/s.")
# opt("-c", "--conf", action="append", metavar="FILE", help="load config values from FILE.")
opt("-C", "--param", action="append", metavar="NAME=VALUE", help="override config values.")
opt("-n", "--new", action="store_true", help="create new wsgi app.")
opt("-s", "--script", action="store_true", help="run script then app.")
opt("-a", "--asgi", action="store_true", help="run the asgi version of an app.")
opt("-d", "--django", action="store_true", help="Run the django cli.")
opt("-env", default="dev", help="set app environment")
opt("--debug", action="store_true", help="start server in debug mode.")
opt("--reload", action="store_true", help="auto-reload on file changes.")
opt('app', help='WSGI app entry point.', nargs='*')
cli_args = parser.parse_args(args[1:])
return cli_args, parser
def parse_args(argv): # pragma: no coverage
args, parser = _cli_parse(argv)
def _cli_error(cli_msg=None):
parser.print_help()
if cli_msg:print('\nError: %s\n' % cli_msg)
sys.exit(1)
if args.version:
print('v1.0.0')
sys.exit(0)
if args.new:
return start_app(args.app[0])
if args.django:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings'%args.app[0])
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
return execute_from_command_line(args.app)
if args.app:
# Initialize global variables
set_global("APPSDIR", root+'/apps')
set_global("APPDIR", root+'/apps/'+args.app[0])
set_global("ROOTDIR", root)
set_global("APPMODULE", args.app[0])
set_global("installed_apps", {})
# Main application runner
runner = Apprunner(str(args.app[0]))
runner.init_app()
set_global("app_runner", runner)
if len(args.app) > 1:
parse_scripts(*args.app)
if not args.script:
return
else:
return _cli_error("No app entry specified.")
if args.param:
for cval in args.param:
if '=' in cval:
k, v = cval.split("=")
runner.app.config.set(k.upper(), v)
else:
runner.app.config.set(cval.upper(), True)
host, port = args.bind or runner.app.config.get("host") or "localhost", runner.app.config.get("port") or 7000
if ':' in host:
host, port = host.split(':')
# for route in runner.app.router.routes.data:
# print('<%s "%s" -> %s [name="%s"]>'%(
# route.method, route.rule, route.callback, route.name
# ))
server = args.server or runner.app.config.get("server", "wsgiref")
debug = args.debug or runner.app.config.get("debug") or True
apptype = "asgi" if args.asgi else "wsgi"
return runner.run(server=server, host=host, port=int(port), debug=debug, type=apptype)
# if args.conf:
# for cfile in args.conf:
# try:
# runner.app.get().clear()
# if cfile.endswith('.json'):
# with open(cfile, 'r') as fp:
# runner.app.get["appConfig"].load_dict(json.loads(fp.read()))
# elif cfile.endswith(".ini"):
# runner.app.get["appConfig"].load_config(cfile)
# else:
# runner.app.get["appConfig"].load_module(cfile)
# except configparser.Error as parse_error:
# _cli_error(parse_error)
# except IOError:
# _cli_error("Unable to read config file %r" % cfile)
# except (UnicodeError, TypeError, ValueError) as error:
# _cli_error("Unable to parse config file %r: %s" % (cfile, error))
# middlewares = app.get("middlewares", e=None)
# if middlewares:
# App.push(app)
# mapp = App()
# entries = ["wsgic"]
# for mw in middlewares:
# if type(mw) is list:
# entries.append(*mw)
# pass
# else:
# for apps in entries:
# try:
# m = load(f"{apps}.middleware")
# miw = getattr(m, mw)
# break
# except:
# pass
# _args = app.get(f"{mw}_args", e=(), raw=True)
# _kwargs = app.get(f"{mw}_kwargs", e={}, raw=True)
# mapp = miw(mapp, *_args, **_kwargs)
# else:mapp = app
# run(app=mapp, server=(args.server or app.get("server", e="gevent")), host=host, port=port, reloader=(args.reload or True), debug=(args.debug or app.get("debug")))
def parse_scripts(app, *args):
app = load(app.replace(".app", "")+".scripts")
appglobals = {x: getattr(app, x) for x in dir(app)}
func = appglobals.get(args[0])
if func:
return func(*args[1:])
else:
for item in appglobals.values():
if isinstance(item, object):
item = item()
func = getattr(item, args[0], None)
if func:
return func(*args[1:])
def start_app(name):
import os
files = {
"__init__": "",
"app": f"""from wsgic import WSGIApp
from wsgic.helpers import config
class {name.title()}App(WSGIApp):
def __init__(self):
super().__init__("{name}.urls:router", config)
__app__ = {name.title()}App()
""",
"views" : """from wsgic.http import request
def index():
return "Hello World from {request.path}"
""",
"urls" : """from wsgic.routing import Router
from .views import *
router = Router()
routes = router.get_routes()
routes.get("*", index)
""",
"models" : """
""",
"controllers" : """
""",
"helpers": """
""",
"services": """
""",
"plugins": """from wsgic.plugins import *
""",
"panels": """from wsgic_admin.helpers import AdminPanel, register
from .models import *
register()
""",
"wsgi": f"""from .app import __app__ as {name.title()}App
application = {name.title()}App.wrapped_app("wsgi")
""",
"asgi": f"""from .app import __app__ as {name.title()}App
application = {name.title()}App.wrapped_app("asgi")
"""
}
cwd = str(os.getcwd())
if not cwd.endswith('apps'):
print('Not currently in apps directory')
y = input('Create in apps directory? [y, n, q]: ')
if y.lower() == 'y':
nm = name
name = os.path.join('apps', name)
elif y.lower() == 'n':
nm = name
else:
print("Terminating operation...")
return
print(f'Creating at {os.path.join(cwd, name)}')
while True:
try:
os.mkdir(name)
break
except:
print()
print(f"Folder Already exists with name {name}")
if str(input("Do you want to override it? [y, n]: ")).lower() != "y":
n = str(input("Enter new app name: "))
if y.lower() == 'y':
nm = n
name = os.path.join('apps', n)
print()
print(f"Creating WSGI App: {nm}")
print()
for m in files:
try:
print(f"Generating File: {os.path.join(name, m)}.py")
with open(f"{os.path.join(name, m)}.py", "x") as file:
file.write(files[m])
except FileExistsError:
print("File {os.path.join(name, m)}.py Already Exists")
pass
print()
print(f"Created WSGI App: {nm}")