py.lib

John Y. 2017-04-09 Parent:f35759c0fdd0

2:5236a9505448 Go to Latest

py.lib/awNet/webapp/win.py

awNet/webapp/tool/url.py awNet/webapp/win.py

History
     1.1 --- a/awNet/webapp/win.py	Sun Apr 09 12:16:55 2017 +0300
     1.2 +++ b/awNet/webapp/win.py	Sun Apr 09 15:04:18 2017 +0300
     1.3 @@ -0,0 +1,72 @@
     1.4 +# -*- coding: utf-8 -*-
     1.5 +# ---
     1.6 +#  Модуль среды приложений для Windows
     1.7 +# ---
     1.8 +
     1.9 +from bottle import Bottle as App, request as Req, response as Ans, SimpleTemplate as BottleTemplate, abort as Abort
    1.10 +from os.path import dirname, abspath as pAbs, split as pSplit, join as pJoin
    1.11 +
    1.12 +# --- CLASSES ---
    1.13 +class ReqEnv:
    1.14 +	def __init__(self):
    1.15 +		self.urlBase = Req.environ.get('SCRIPT_NAME', '')
    1.16 +		
    1.17 +	def getUrl(self, url):
    1.18 +		url = str(url)
    1.19 +		buf = self.urlBase
    1.20 +		buf += url if url.startswith('/') else '/' + url
    1.21 +		return buf
    1.22 +	
    1.23 +	def __getitem__(self, key):
    1.24 +		return Req.environ[key]
    1.25 +		
    1.26 +	def __iter__(self):
    1.27 +		for key in Req.environ.keys():
    1.28 +			yield key
    1.29 +			
    1.30 +	@staticmethod
    1.31 +	def abort(*a, **ka):
    1.32 +		Abort(*a, **ka)
    1.33 +	
    1.34 +	@staticmethod	
    1.35 +	def get(self):
    1.36 +		return Req.GET
    1.37 +		
    1.38 +	@staticmethod	
    1.39 +	def post():
    1.40 +		return Req.POST
    1.41 +		
    1.42 +class Env:
    1.43 +	def __init__(self, scFile):
    1.44 +		self.wd = pSplit(dirname(pAbs(scFile)))[0]
    1.45 +		self.static = pJoin(self.wd, 'static')
    1.46 +		
    1.47 +	def http(self, tplName):
    1.48 +		return BottleTemplate(name=pJoin(self.wd, 'http', '%s.html' % tplName)).render
    1.49 +		
    1.50 +	def db(self):
    1.51 +		""" 
    1.52 +		Путь к базе SQLite, иные типы БД дожны иметь более
    1.53 +		серьёзные средства авторизации
    1.54 +		"""
    1.55 +		return pJoin(self.wd, 'db.sqlite')
    1.56 +	
    1.57 +	@staticmethod
    1.58 +	def httpTpl(buf):
    1.59 +		return BottleTemplate(buf).render
    1.60 +	
    1.61 +	@staticmethod
    1.62 +	def app():
    1.63 +		return App()
    1.64 +		
    1.65 +	@staticmethod
    1.66 +	def appRun(app):
    1.67 +		app.run(server='cgi')
    1.68 +	
    1.69 +# --- PROCS ---
    1.70 +def runApp(app):
    1.71 +	Env.appRun(app)
    1.72 +	
    1.73 +def Template(buf):
    1.74 +	return Env.httpTpl(buf)
    1.75 +	
    1.76 \ No newline at end of file