py.lib

Yohn Y. 2022-08-19 Parent:84b54a8a6d4c

37:ae0107755941 Go to Latest

py.lib/webapp/win.py

. Исправление ошибок и рефакторинг

History
1 # -*- coding: utf-8 -*-
2 # ---
3 # Модуль среды приложений для Windows
4 # ---
6 from bottle import Bottle as App, request as Req, response as Ans, SimpleTemplate as BottleTemplate, abort as Abort
7 from os.path import dirname, abspath as pAbs, split as pSplit, join as pJoin
10 # --- CLASSES ---
11 class ReqEnv:
12 def __init__(self):
13 self.urlBase = Req.environ.get('SCRIPT_NAME', '')
15 def getUrl(self, url):
16 url = str(url)
17 buf = self.urlBase
18 buf += url if url.startswith('/') else '/' + url
19 return buf
21 def __getitem__(self, key):
22 return Req.environ[key]
24 def __iter__(self):
25 for key in Req.environ.keys():
26 yield key
28 @staticmethod
29 def abort(*a, **ka):
30 Abort(*a, **ka)
32 @staticmethod
33 def get(self):
34 return Req.GET
36 @staticmethod
37 def post():
38 return Req.POST
41 class Env:
42 def __init__(self, scFile):
43 self.wd = pSplit(dirname(pAbs(scFile)))[0]
44 self.static = pJoin(self.wd, 'static')
46 def http(self, tplName):
47 return BottleTemplate(name=pJoin(self.wd, 'http', '%s.html' % tplName)).render
49 def db(self):
50 """
51 Путь к базе SQLite, иные типы БД дожны иметь более
52 серьёзные средства авторизации
53 """
54 return pJoin(self.wd, 'db.sqlite')
56 @staticmethod
57 def httpTpl(buf):
58 return BottleTemplate(buf).render
60 @staticmethod
61 def app():
62 return App()
64 @staticmethod
65 def appRun(app):
66 app.run(server='cgi')
69 # --- PROCS ---
70 def runApp(app):
71 Env.appRun(app)
74 def Template(buf):
75 return Env.httpTpl(buf)