Skip to content

Commit

Permalink
Re-enable python-dotenv support, replaced Flask-HTMLmin with htmlmin
Browse files Browse the repository at this point in the history
  • Loading branch information
tocsinde committed Jun 27, 2018
1 parent d25cde9 commit 0d98418
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
10 changes: 3 additions & 7 deletions newsic/.env.sample
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
YOUTUBE_API_KEY=
VIMEO_TOKEN=
VIMEO_KEY=
VIMEO_SECRET=
#You'll need to restart newsic for changes to take effect

CACHE=False
CACHE_TIMEOUT=600
CACHE_TYPE=filesystem
CACHE_DIR =
CACHE_DIR=
CACHE_MAX_AGE=604800
SNIPPETLENGTH=30
DEBUG=False
SNIPPETLENGTH=30
12 changes: 3 additions & 9 deletions newsic/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ Both "Local" and "Server" classes are based upon this
TODO: add link to deployment repo and a short explanation on how to use env variables instead of this file
"""

class General(object):

'''
Usage of python-dotenv
# This file can be overwritten by installing python-dotenv and using an .env file

python-dotenv is optional and it uses the .env file to overwrite specific settings of this config file
'''

CONFIG_DOTENV = False
class General(object):

'''
YouTube API configuration
Expand All @@ -38,7 +32,7 @@ class General(object):
SNIPPETLENGTH = 30

'''
Performance/caching (with Flask Caching, Flask Compress and Flask-HTMLmin)
Performance/caching (with Flask Caching, Flask Compress and htmlmin)

Installable with pip install -r requirements-performance.txt
'''
Expand Down
43 changes: 29 additions & 14 deletions newsic/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
)
from time import perf_counter
from click import echo as click_echo

from flask_babel import Babel, gettext

"""
Expand All @@ -17,23 +16,30 @@
try:
from flask_caching import Cache
from flask_compress import Compress
from flask_htmlmin import HTMLMIN
#from dotenv import load_dotenv, find_dotenv
from htmlmin import Minifier
from dotenv import load_dotenv, find_dotenv
except ImportError:
pass

# for python-dotenv
#from os.path import join, dirname
#from os import getenv
from os import getenv

app.config.from_object('newsic.config.Local')
load_dotenv(find_dotenv(), override=True)

def read_config(value):

"""
Import config from config.py
NOTE: .env support might be removed
Settings can be overwritten by .env file
"""

app.config.from_object('newsic.config.Local')
if getenv(value):
if getenv(value) == "True":
return True
if getenv(value) == "False":
return False
return getenv(value)
return app.config[value]

@app.errorhandler(404)
Expand Down Expand Up @@ -97,8 +103,7 @@ def cache():

if read_config("CACHE"):
return CACHE.cached(timeout=read_config("CACHE_TIMEOUT"))
else:
return lambda x: x
return lambda x: x

@app.cli.command()
def flushcache():
Expand Down Expand Up @@ -126,15 +131,25 @@ def flushcache():


"""
Flask-HTMLmin
htmlmin
"""

if read_config("MINIFY_PAGE"):
debug("Minify activated")
HTMLMIN(app, remove_comments=read_config("REMOVE_COMMENTS"),
reduce_empty_attributes=read_config("REDUCE_EMPTY_ATTRIBUTES"),
remove_optional_attribute_quotes=read_config("REMOVE_OPTIONAL_ATTRIBUTE_QUOTES")
)
minify = Minifier(remove_comments=read_config("REMOVE_COMMENTS"),
reduce_empty_attributes=read_config("REDUCE_EMPTY_ATTRIBUTES"),
remove_empty_space=read_config("REMOVE_EMPTY_SPACE"),
remove_all_empty_space=read_config("REMOVE_ALL_EMPTY_SPACE"),
remove_optional_attribute_quotes=read_config("REMOVE_OPTIONAL_ATTRIBUTE_QUOTES"))

@app.after_request
def minify_the_page(response):
if response.content_type == u'text/html; charset=utf-8':
response.direct_passthrough = False
response.set_data(
minify.minify(response.get_data(as_text=True)))
return response
return response

if read_config("CACHE_MAX_AGE"):
@app.after_request
Expand Down
2 changes: 1 addition & 1 deletion requirements-performance.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask-Caching
Flask-Compress
Flask-HTMLmin >= 1.3.1
htmlmin

0 comments on commit 0d98418

Please sign in to comment.