From: Oleg Broytman Date: Sun, 1 Apr 2018 19:11:22 +0000 (+0300) Subject: Feat(web): Use CheetahTemplate X-Git-Tag: 0.1.0~24 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=023023391fc401b00827d001b30ee6d7c29fc5ef;p=m_librarian.git Feat(web): Use CheetahTemplate --- diff --git a/m_librarian/web/app.py b/m_librarian/web/app.py index 01b15dd..3a95c8b 100644 --- a/m_librarian/web/app.py +++ b/m_librarian/web/app.py @@ -1,6 +1,7 @@ -from bottle import route +from bottle import route, cheetah_view @route('/') -def hello(): - return "Hello World!" +@cheetah_view('index.tmpl') +def index(): + return {} diff --git a/m_librarian/web/server.py b/m_librarian/web/server.py index bc8e552..49c133f 100644 --- a/m_librarian/web/server.py +++ b/m_librarian/web/server.py @@ -1,9 +1,10 @@ -from bottle import run - +import os +import sys from wsgiref import simple_server from wsgiref.handlers import SimpleHandler from wsgiref.simple_server import WSGIServer -from bottle import route + +from bottle import route, run simple_server.ServerHandler = SimpleHandler # Stop logging to stdout @@ -23,4 +24,7 @@ def quit(): def run_server(host='localhost', port=0): + # Start here so that bottle can find templates + os.chdir(os.path.dirname(__file__)) + sys.path.insert(0, os.getcwd()) # To import Cheetah templates run(host=host, port=port, server_class=QuitWSGIServer, debug=True) diff --git a/m_librarian/web/views/Makefile b/m_librarian/web/views/Makefile new file mode 100644 index 0000000..fc8b7be --- /dev/null +++ b/m_librarian/web/views/Makefile @@ -0,0 +1,14 @@ +.SUFFIXES: # Clear the suffix list +.SUFFIXES: .py .tmpl .html + + +%.py: %.tmpl + cheetah compile --nobackup $< && compyle $@ + + +templates = $(shell echo *.tmpl) +modules = $(patsubst %.tmpl,%.py,$(templates)) + + +.PHONY: all +all: $(modules) diff --git a/m_librarian/web/views/__init__.py b/m_librarian/web/views/__init__.py new file mode 100644 index 0000000..792d600 --- /dev/null +++ b/m_librarian/web/views/__init__.py @@ -0,0 +1 @@ +# diff --git a/m_librarian/web/views/index.py b/m_librarian/web/views/index.py new file mode 100644 index 0000000..9456a5e --- /dev/null +++ b/m_librarian/web/views/index.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python + + + + +################################################## +## DEPENDENCIES +import sys +import os +import os.path +try: + import builtins as builtin +except ImportError: + import __builtin__ as builtin +from os.path import getmtime, exists +import time +import types +from Cheetah.Version import MinCompatibleVersion as RequiredCheetahVersion +from Cheetah.Version import MinCompatibleVersionTuple as RequiredCheetahVersionTuple +from Cheetah.Template import Template +from Cheetah.DummyTransaction import * +from Cheetah.NameMapper import NotFound, valueForName, valueFromSearchList, valueFromFrameOrSearchList +from Cheetah.CacheRegion import CacheRegion +import Cheetah.Filters as Filters +import Cheetah.ErrorCatchers as ErrorCatchers +from Cheetah.compat import unicode +from views.layout import layout + +################################################## +## MODULE CONSTANTS +VFFSL=valueFromFrameOrSearchList +VFSL=valueFromSearchList +VFN=valueForName +currentTime=time.time +__CHEETAH_version__ = '3.1.0' +__CHEETAH_versionTuple__ = (3, 1, 0, 'final', 1) +__CHEETAH_genTime__ = 1522609706.987582 +__CHEETAH_genTimestamp__ = 'Sun Apr 1 22:08:26 2018' +__CHEETAH_src__ = 'index.tmpl' +__CHEETAH_srcLastModified__ = 'Sun Apr 1 22:08:25 2018' +__CHEETAH_docstring__ = 'Autogenerated by Cheetah: The Python-Powered Template Engine' + +if __CHEETAH_versionTuple__ < RequiredCheetahVersionTuple: + raise AssertionError( + 'This template was compiled with Cheetah version' + ' %s. Templates compiled before version %s must be recompiled.'%( + __CHEETAH_version__, RequiredCheetahVersion)) + +################################################## +## CLASSES + +class index(layout): + + ################################################## + ## CHEETAH GENERATED METHODS + + + def __init__(self, *args, **KWs): + + super(index, self).__init__(*args, **KWs) + if not self._CHEETAH__instanceInitialized: + cheetahKWArgs = {} + allowedKWs = 'searchList namespaces filter filtersLib errorCatcher'.split() + for k,v in KWs.items(): + if k in allowedKWs: cheetahKWArgs[k] = v + self._initCheetahInstance(**cheetahKWArgs) + + + def body(self, **KWS): + + + + ## CHEETAH: generated from #def body at line 3, col 1. + trans = KWS.get("trans") + if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)): + trans = self.transaction # is None unless self.awake() was called + if not trans: + trans = DummyTransaction() + _dummyTrans = True + else: _dummyTrans = False + write = trans.response().write + SL = self._CHEETAH__searchList + _filter = self._CHEETAH__currentFilter + + ######################################## + ## START - generated method body + + write(u'''

Index

+ +

Hello, World!

+ +

Quit

+''') + + ######################################## + ## END - generated method body + + return _dummyTrans and trans.response().getvalue() or "" + + + def writeBody(self, **KWS): + + + + ## CHEETAH: main method generated for this template + trans = KWS.get("trans") + if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)): + trans = self.transaction # is None unless self.awake() was called + if not trans: + trans = DummyTransaction() + _dummyTrans = True + else: _dummyTrans = False + write = trans.response().write + SL = self._CHEETAH__searchList + _filter = self._CHEETAH__currentFilter + + ######################################## + ## START - generated method body + + + ######################################## + ## END - generated method body + + return _dummyTrans and trans.response().getvalue() or "" + + ################################################## + ## CHEETAH GENERATED ATTRIBUTES + + + _CHEETAH__instanceInitialized = False + + _CHEETAH_version = __CHEETAH_version__ + + _CHEETAH_versionTuple = __CHEETAH_versionTuple__ + + _CHEETAH_genTime = __CHEETAH_genTime__ + + _CHEETAH_genTimestamp = __CHEETAH_genTimestamp__ + + _CHEETAH_src = __CHEETAH_src__ + + _CHEETAH_srcLastModified = __CHEETAH_srcLastModified__ + + title = 'Index' + + _mainCheetahMethod_for_index = 'writeBody' + +## END CLASS DEFINITION + +if not hasattr(index, '_initCheetahAttributes'): + templateAPIClass = getattr(index, + '_CHEETAH_templateClass', + Template) + templateAPIClass._addCheetahPlumbingCodeToClass(index) + + +# CHEETAH was developed by Tavis Rudd and Mike Orr +# with code, advice and input from many other volunteers. +# For more information visit http://cheetahtemplate.org/ + +################################################## +## if run from command line: +if __name__ == '__main__': + from Cheetah.TemplateCmdLineIface import CmdLineIface + CmdLineIface(templateObj=index()).run() + + diff --git a/m_librarian/web/views/index.tmpl b/m_librarian/web/views/index.tmpl new file mode 100644 index 0000000..9832186 --- /dev/null +++ b/m_librarian/web/views/index.tmpl @@ -0,0 +1,9 @@ +#extends views.layout +#attr $title = 'Index' +#def body +

Index

+ +

Hello, World!

+ +

Quit

+#end def diff --git a/m_librarian/web/views/layout.py b/m_librarian/web/views/layout.py new file mode 100644 index 0000000..f8ba1b2 --- /dev/null +++ b/m_librarian/web/views/layout.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python + + + + +################################################## +## DEPENDENCIES +import sys +import os +import os.path +try: + import builtins as builtin +except ImportError: + import __builtin__ as builtin +from os.path import getmtime, exists +import time +import types +from Cheetah.Version import MinCompatibleVersion as RequiredCheetahVersion +from Cheetah.Version import MinCompatibleVersionTuple as RequiredCheetahVersionTuple +from Cheetah.Template import Template +from Cheetah.DummyTransaction import * +from Cheetah.NameMapper import NotFound, valueForName, valueFromSearchList, valueFromFrameOrSearchList +from Cheetah.CacheRegion import CacheRegion +import Cheetah.Filters as Filters +import Cheetah.ErrorCatchers as ErrorCatchers +from Cheetah.compat import unicode + +################################################## +## MODULE CONSTANTS +VFFSL=valueFromFrameOrSearchList +VFSL=valueFromSearchList +VFN=valueForName +currentTime=time.time +__CHEETAH_version__ = '3.1.0' +__CHEETAH_versionTuple__ = (3, 1, 0, 'final', 1) +__CHEETAH_genTime__ = 1522609587.049776 +__CHEETAH_genTimestamp__ = 'Sun Apr 1 22:06:27 2018' +__CHEETAH_src__ = 'layout.tmpl' +__CHEETAH_srcLastModified__ = 'Sun Apr 1 22:06:06 2018' +__CHEETAH_docstring__ = 'Autogenerated by Cheetah: The Python-Powered Template Engine' + +if __CHEETAH_versionTuple__ < RequiredCheetahVersionTuple: + raise AssertionError( + 'This template was compiled with Cheetah version' + ' %s. Templates compiled before version %s must be recompiled.'%( + __CHEETAH_version__, RequiredCheetahVersion)) + +################################################## +## CLASSES + +class layout(Template): + + ################################################## + ## CHEETAH GENERATED METHODS + + + def __init__(self, *args, **KWs): + + super(layout, self).__init__(*args, **KWs) + if not self._CHEETAH__instanceInitialized: + cheetahKWArgs = {} + allowedKWs = 'searchList namespaces filter filtersLib errorCatcher'.split() + for k,v in KWs.items(): + if k in allowedKWs: cheetahKWArgs[k] = v + self._initCheetahInstance(**cheetahKWArgs) + + + def respond(self, trans=None): + + + + ## CHEETAH: main method generated for this template + if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)): + trans = self.transaction # is None unless self.awake() was called + if not trans: + trans = DummyTransaction() + _dummyTrans = True + else: _dummyTrans = False + write = trans.response().write + SL = self._CHEETAH__searchList + _filter = self._CHEETAH__currentFilter + + ######################################## + ## START - generated method body + + write(u''' + + + + +''') + _v = VFFSL(SL,"title",True) # u'$title' on line 8, col 8 + if _v is not None: write(_filter(_v, rawExpr=u'$title')) # from line 8, col 8. + write(u''' + + + + + + + +''') + _v = VFFSL(SL,"body",True) # u'$body' on line 17, col 1 + if _v is not None: write(_filter(_v, rawExpr=u'$body')) # from line 17, col 1. + write(u''' + + +''') + + ######################################## + ## END - generated method body + + return _dummyTrans and trans.response().getvalue() or "" + + ################################################## + ## CHEETAH GENERATED ATTRIBUTES + + + _CHEETAH__instanceInitialized = False + + _CHEETAH_version = __CHEETAH_version__ + + _CHEETAH_versionTuple = __CHEETAH_versionTuple__ + + _CHEETAH_genTime = __CHEETAH_genTime__ + + _CHEETAH_genTimestamp = __CHEETAH_genTimestamp__ + + _CHEETAH_src = __CHEETAH_src__ + + _CHEETAH_srcLastModified = __CHEETAH_srcLastModified__ + + title = '' + + _mainCheetahMethod_for_layout = 'respond' + +## END CLASS DEFINITION + +if not hasattr(layout, '_initCheetahAttributes'): + templateAPIClass = getattr(layout, + '_CHEETAH_templateClass', + Template) + templateAPIClass._addCheetahPlumbingCodeToClass(layout) + + +# CHEETAH was developed by Tavis Rudd and Mike Orr +# with code, advice and input from many other volunteers. +# For more information visit http://cheetahtemplate.org/ + +################################################## +## if run from command line: +if __name__ == '__main__': + from Cheetah.TemplateCmdLineIface import CmdLineIface + CmdLineIface(templateObj=layout()).run() + + diff --git a/m_librarian/web/views/layout.tmpl b/m_librarian/web/views/layout.tmpl new file mode 100644 index 0000000..60da30e --- /dev/null +++ b/m_librarian/web/views/layout.tmpl @@ -0,0 +1,19 @@ +#attr $title = '' + + + + + +$title + + + + + + + +$body + + diff --git a/setup.cfg b/setup.cfg index 48b6211..39c17d2 100644 --- a/setup.cfg +++ b/setup.cfg @@ -11,5 +11,5 @@ tag_date = 0 tag_svn_revision = 0 [flake8] -exclude = .git,.tox,docs/conf.py,docs-ru/conf.py +exclude = .git,.tox,docs/conf.py,docs-ru/conf.py,m_librarian/web/views diff --git a/setup.py b/setup.py index d342061..3ba6882 100755 --- a/setup.py +++ b/setup.py @@ -59,9 +59,7 @@ setup(name='m_librarian', 'm_lib.defenc>=1.0', ], extras_require={ - 'm_lib': ['m_lib>=3.1'], 'pbar': ['m_lib>=3.1'], - 'web': ['bottle'], - 'bottle': ['bottle'], + 'web': ['bottle', 'Cheetah3'], }, )