Skip to content

Commit

Permalink
1.0.6 - minifed html and multifile
Browse files Browse the repository at this point in the history
  • Loading branch information
mauro-balades committed Oct 4, 2021
1 parent 5e8f1ee commit 0f94ef9
Show file tree
Hide file tree
Showing 23 changed files with 191 additions and 7 deletions.
2 changes: 1 addition & 1 deletion expross/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
"""

version = "1.0.5"
version = "1.0.6"

from expross.main import Expross

Expand Down
24 changes: 24 additions & 0 deletions expross/log.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
"""
The MIT License (MIT)
Copyright (c) 2021 expross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import logging


Expand Down
48 changes: 44 additions & 4 deletions expross/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from __future__ import absolute_import

from expross.routes import Route
from expross.errors import ErrorHandlerExists, ErrorCodeExists
from expross.errors import ErrorHandlerExists, ErrorCodeExists, RouteAlreadyExists
from expross.error import ErrorHandler

from wsgiref.simple_server import make_server
Expand All @@ -35,6 +35,7 @@
from falcon import Request, Response
from falcon import HTTPFound

import minify_html
import falcon
import os

Expand Down Expand Up @@ -111,7 +112,21 @@ def set_templates(self, name: str = "templates"):
file_loader: FileSystemLoader = FileSystemLoader(name)
self.jinja_env: Environment = Environment(loader=file_loader)

def add_middleware(self, middleware):
def use(self, middleware):

if type(middleware) == type(self):
for r in middleware.routes:
nr = Route(route=str(r), methods=r.methods, func=r.function, app=self)
self.app.add_route(str(r), nr)
self.routes.append(nr)

for e in middleware.errors:
handler = ErrorHandler(e.error, e.func, self)
self.app.add_error_handler(e.error, handler.handle)
self.errors.append(handler)

return

self.app.add_middleware(middleware)
self.middlewares.append(middleware)

Expand Down Expand Up @@ -267,25 +282,50 @@ def render(self, data: str, **context: any):
Args:
data (str): string template to be parsed
context (any, optional): additional context to give to the template. Defaults to None.
_minified: (bool, optional): make html, css, js a minified version of themselve. Defaults to False
Returns:
str: a rendered version of the string
"""

tm = Template(data)
return tm.render(**context)
min = context.get("_minified", False)
render = tm.render(**context)

if min:
return minify_html.minify(
render,
minify_js=True,
minify_css=True,
remove_processing_instructions=True,
)

return render

def render_template(self, name: str, **context: any):
"""render a jinja2 template file with some context
Args:
name (str): template file to be parsed
context (any, optional): additional context to give to the template. Defaults to None.
_minified: (bool, optional): make html, css, js a minified version of themselve. Defaults to False
Returns:
str: a rendered version of the template
"""
template = self.jinja_env.get_template(name)
rendered = template.render(**context)

min = context.get("_minified", False)

if min:
return minify_html.minify(
rendered,
minify_js=True,
minify_css=True,
remove_processing_instructions=True,
)

return rendered

def _set_request(self, req: Request):
Expand All @@ -300,7 +340,7 @@ def _set_response(self, res: Response):
Args: res (Response): request to be set
"""
self.req = req
self.req = res

def __repr__(self):
# default_port and default_host_name are being overieded
Expand Down
2 changes: 1 addition & 1 deletion expross/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def is_valid_method(self, method: str):
return method in self.methods

def __repr__(self):
return '<Route route="%s" function=%s methods=[%s]>' % (
return '<Route route="%s" function=%s methods=%s>' % (
self.route,
self.function,
self.methods,
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"hiurlparser",
"Jinja2",
"falcon",
"minify-html",
],
project_urls={
"Documentation": "https://mauro-balades.gitbook.io/expross/",
Expand Down
32 changes: 32 additions & 0 deletions test/test_multifile_route_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
The MIT License (MIT)
Copyright (c) 2021 expross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

from expross import Expross

test_routes = Expross()


@test_routes.get("/test")
def test():
return f"tested from other file!"
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Multifile testing
from tests.test_multifile_route_2 import test_routes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/test_cors.py → tests/test_cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
app = Expross()

cors = CORS(allow_origins_list=["*"])
app.add_middleware(cors.middleware)
app.use(cors.middleware)

app.serve_static()

Expand Down
File renamed without changes.
46 changes: 46 additions & 0 deletions tests/test_multifile_route_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
The MIT License (MIT)
Copyright (c) 2021 expross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

from expross import Expross
from expross import HTTPNotFound
from tests import test_routes

app = Expross()
app.use(test_routes)

app.serve_static()


@app.error(HTTPNotFound)
def err():
return "<h1>ups! 404</h1>"


@app.get("/")
def main():

return "<h1>Hello, world!</h1>"


app.listen()
32 changes: 32 additions & 0 deletions tests/test_multifile_route_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
The MIT License (MIT)
Copyright (c) 2021 expross
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

from expross import Expross

test_routes = Expross()


@test_routes.get("/test")
def test():
return f"this is a multify test!"
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions test/test_template.py → tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ def template():
return app.render_template("test.html", test_string="This is my test string")


@app.get("/min")
def min():
return app.render_template(
"test.html", test_string="This is my test string", _minified=True
)


app.listen()
File renamed without changes.
File renamed without changes.

0 comments on commit 0f94ef9

Please sign in to comment.