py.lib

John Y. 2017-04-09 Parent:8dcee5f5df4f Child:f3ecca8e4adc

2:5236a9505448 Browse Files

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

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

     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/awNet/webapp/tool/__init__.py	Sun Apr 09 15:04:18 2017 +0300
     1.3 @@ -0,0 +1,4 @@
     1.4 +# -*- coding: utf-8 -*-
     1.5 +# ---
     1.6 +#  
     1.7 +# ---
     1.8 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/awNet/webapp/tool/url.py	Sun Apr 09 15:04:18 2017 +0300
     2.3 @@ -0,0 +1,12 @@
     2.4 +# -*- coding: utf-8 -*-
     2.5 +# ---
     2.6 +#  Инструменты для работы с URL
     2.7 +# ---
     2.8 +from base64 import urlsafe_b64encode as _b64e, urlsafe_b64decode as _b64d
     2.9 +from urllib.parse import quote as urlquote
    2.10 +
    2.11 +def encode(buf):
    2.12 +  return _b64e(buf.encode('UTF-8')).decode('UTF-8')
    2.13 +  
    2.14 +def decode(buf):
    2.15 +  return _b64d(buf.encode('UTF-8')).decode('UTF-8')
     3.1 --- a/awNet/webapp/win.py	Sun Apr 09 12:16:55 2017 +0300
     3.2 +++ b/awNet/webapp/win.py	Sun Apr 09 15:04:18 2017 +0300
     3.3 @@ -0,0 +1,72 @@
     3.4 +# -*- coding: utf-8 -*-
     3.5 +# ---
     3.6 +#  Модуль среды приложений для Windows
     3.7 +# ---
     3.8 +
     3.9 +from bottle import Bottle as App, request as Req, response as Ans, SimpleTemplate as BottleTemplate, abort as Abort
    3.10 +from os.path import dirname, abspath as pAbs, split as pSplit, join as pJoin
    3.11 +
    3.12 +# --- CLASSES ---
    3.13 +class ReqEnv:
    3.14 +	def __init__(self):
    3.15 +		self.urlBase = Req.environ.get('SCRIPT_NAME', '')
    3.16 +		
    3.17 +	def getUrl(self, url):
    3.18 +		url = str(url)
    3.19 +		buf = self.urlBase
    3.20 +		buf += url if url.startswith('/') else '/' + url
    3.21 +		return buf
    3.22 +	
    3.23 +	def __getitem__(self, key):
    3.24 +		return Req.environ[key]
    3.25 +		
    3.26 +	def __iter__(self):
    3.27 +		for key in Req.environ.keys():
    3.28 +			yield key
    3.29 +			
    3.30 +	@staticmethod
    3.31 +	def abort(*a, **ka):
    3.32 +		Abort(*a, **ka)
    3.33 +	
    3.34 +	@staticmethod	
    3.35 +	def get(self):
    3.36 +		return Req.GET
    3.37 +		
    3.38 +	@staticmethod	
    3.39 +	def post():
    3.40 +		return Req.POST
    3.41 +		
    3.42 +class Env:
    3.43 +	def __init__(self, scFile):
    3.44 +		self.wd = pSplit(dirname(pAbs(scFile)))[0]
    3.45 +		self.static = pJoin(self.wd, 'static')
    3.46 +		
    3.47 +	def http(self, tplName):
    3.48 +		return BottleTemplate(name=pJoin(self.wd, 'http', '%s.html' % tplName)).render
    3.49 +		
    3.50 +	def db(self):
    3.51 +		""" 
    3.52 +		Путь к базе SQLite, иные типы БД дожны иметь более
    3.53 +		серьёзные средства авторизации
    3.54 +		"""
    3.55 +		return pJoin(self.wd, 'db.sqlite')
    3.56 +	
    3.57 +	@staticmethod
    3.58 +	def httpTpl(buf):
    3.59 +		return BottleTemplate(buf).render
    3.60 +	
    3.61 +	@staticmethod
    3.62 +	def app():
    3.63 +		return App()
    3.64 +		
    3.65 +	@staticmethod
    3.66 +	def appRun(app):
    3.67 +		app.run(server='cgi')
    3.68 +	
    3.69 +# --- PROCS ---
    3.70 +def runApp(app):
    3.71 +	Env.appRun(app)
    3.72 +	
    3.73 +def Template(buf):
    3.74 +	return Env.httpTpl(buf)
    3.75 +	
    3.76 \ No newline at end of file