py.lib

Yohn Y. 2022-02-23 Parent:cab7fedf8432 Child:84b54a8a6d4c

23:1668cc57225b 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
9 # --- CLASSES ---
10 class ReqEnv:
11 def __init__(self):
12 self.urlBase = Req.environ.get('SCRIPT_NAME', '')
14 def getUrl(self, url):
15 url = str(url)
16 buf = self.urlBase
17 buf += url if url.startswith('/') else '/' + url
18 return buf
20 def __getitem__(self, key):
21 return Req.environ[key]
23 def __iter__(self):
24 for key in Req.environ.keys():
25 yield key
27 @staticmethod
28 def abort(*a, **ka):
29 Abort(*a, **ka)
31 @staticmethod
32 def get(self):
33 return Req.GET
35 @staticmethod
36 def post():
37 return Req.POST
39 class Env:
40 def __init__(self, scFile):
41 self.wd = pSplit(dirname(pAbs(scFile)))[0]
42 self.static = pJoin(self.wd, 'static')
44 def http(self, tplName):
45 return BottleTemplate(name=pJoin(self.wd, 'http', '%s.html' % tplName)).render
47 def db(self):
48 """
49 Путь к базе SQLite, иные типы БД дожны иметь более
50 серьёзные средства авторизации
51 """
52 return pJoin(self.wd, 'db.sqlite')
54 @staticmethod
55 def httpTpl(buf):
56 return BottleTemplate(buf).render
58 @staticmethod
59 def app():
60 return App()
62 @staticmethod
63 def appRun(app):
64 app.run(server='cgi')
66 # --- PROCS ---
67 def runApp(app):
68 Env.appRun(app)
70 def Template(buf):
71 return Env.httpTpl(buf)