Skip to content

Commit

Permalink
Merge pull request #105 from zerasul/develop
Browse files Browse the repository at this point in the history
Updated to v0.1.3
  • Loading branch information
zerasul authored Nov 25, 2019
2 parents f738162 + 74e1fc4 commit 78af429
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 164 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
language: python
addons:
sonarcloud:
token: ${SONAR_TOKEN}
python:
- "3.6"
install:
- "pip install -r requirements.txt"
- "pip install pylint"
script: export PYTHONPATH=$PYTHONPATH:$(pwd) && pytest --cov=Blask --cov-report= && pylint Blask --exit-zero
script:
- export PYTHONPATH=$PYTHONPATH:$(pwd) && pytest --cov=Blask --cov-report=xml && pylint Blask --exit-zero
- sonar-scanner -Dsonar.login=${SONAR_TOKEN} -Dsonar.projectKey=blask-project-key -Dsonar.sources=Blask/ -Dsonar.organization=zerasul-github -Dsonar.python.coverage.reportPaths=coverage.xml -Dsonar.projectName=Blask
after_success:
coveralls
70 changes: 38 additions & 32 deletions Blask/blaskapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class BlaskApp:
:Author: Zerasul <suarez.garcia.victor@gmail.com>
date: 2018-05-05
"""

app = None
blogrenderer = None

Expand All @@ -38,22 +39,18 @@ def __init__(self, **kwargs):
:param kwargs: Dictionary with all the required settings; for more info see :settings
"""
self.settings = BlaskSettings(**kwargs)
self.blogrenderer = BlogRenderer(self.settings['postDir'])
self.app = Flask(__name__,
template_folder=self.settings['templateDir'],
static_folder=self.settings['staticDir'])
self.app.add_url_rule('/', endpoint='index', view_func=self._index,
methods=['GET'])
self.app.add_url_rule('/<filename>', view_func=self._getpage,
methods=['GET'])
self.app.add_url_rule('/tag/<tag>', view_func=self._gettag,
methods=['GET'])
self.app.add_url_rule('/search', view_func=self.searchpages,
methods=['POST'])
self.app.add_url_rule('/category/<category>',
view_func=self._getcategory, methods=['GET'])
self.app.add_url_rule('/author/<author>', view_func=self._getauthor,
methods=['GET'])
self.blogrenderer = BlogRenderer(self.settings["postDir"])
self.app = Flask(
__name__,
template_folder=self.settings["templateDir"],
static_folder=self.settings["staticDir"],
)
self.app.add_url_rule("/", endpoint="index", view_func=self._index, methods=["GET"])
self.app.add_url_rule("/<filename>", view_func=self._getpage, methods=["GET"])
self.app.add_url_rule("/tag/<tag>", view_func=self._gettag, methods=["GET"])
self.app.add_url_rule("/search", view_func=self.searchpages, methods=["POST"])
self.app.add_url_rule("/category/<category>", view_func=self._getcategory, methods=["GET"])
self.app.add_url_rule("/author/<author>", view_func=self._getauthor, methods=["GET"])

def _index(self):
"""
Expand All @@ -63,9 +60,8 @@ def _index(self):
entry = self.blogrenderer.renderfile("index")
template = entry.template
if template is None:
template = self.settings['defaultLayout']
return render_template(template, title=self.settings['title'],
content=entry.content)
template = self.settings["defaultLayout"]
return render_template(template, title=self.settings["title"], content=entry.content)

def _getpage(self, filename):
"""
Expand All @@ -84,10 +80,16 @@ def _getpage(self, filename):
category = entry.category
author = entry.author
if template is None:
template = self.settings['defaultLayout']
return render_template(template, title=self.settings['title'],
content=content, date=date, tags=tags,
category=category, author=author)
template = self.settings["defaultLayout"]
return render_template(
template,
title=self.settings["title"],
content=content,
date=date,
tags=tags,
category=category,
author=author,
)

def _gettag(self, tag):
"""
Expand All @@ -97,18 +99,20 @@ def _gettag(self, tag):
"""
postlist = self.blogrenderer.list_posts([tag])
content = self.blogrenderer.generatetagpage(postlist)
return render_template(self.settings['defaultLayout'],
title=self.settings['title'], content=content)
return render_template(
self.settings["defaultLayout"], title=self.settings["title"], content=content
)

def searchpages(self):
"""
Render the search page. Must Be on Method POST
:return: rendered search Page
"""
postlist = self.blogrenderer.list_posts(search=request.form['search'])
postlist = self.blogrenderer.list_posts(search=request.form["search"])
content = self.blogrenderer.generatetagpage(postlist)
return render_template(self.settings['defaultLayout'],
title=self.settings['title'], content=content)
return render_template(
self.settings["defaultLayout"], title=self.settings["title"], content=content
)

def _getcategory(self, category):
"""
Expand All @@ -118,8 +122,9 @@ def _getcategory(self, category):
"""
postlist = self.blogrenderer.list_posts(category=category)
content = self.blogrenderer.generatetagpage(postlist)
return render_template(self.settings['defaultLayout'],
title=self.settings['title'], content=content)
return render_template(
self.settings["defaultLayout"], title=self.settings["title"], content=content
)

def _getauthor(self, author):
"""
Expand All @@ -129,8 +134,9 @@ def _getauthor(self, author):
"""
postlist = self.blogrenderer.list_posts(author=author)
content = self.blogrenderer.generatetagpage(postlist)
return render_template(self.settings['defaultLayout'],
title=self.settings['title'], content=content)
return render_template(
self.settings["defaultLayout"], title=self.settings["title"], content=content
)

def run(self, **kwargs):
"""
Expand Down
52 changes: 25 additions & 27 deletions Blask/blaskcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CLIController:
"""
Class that controls all the Command Line interface application
"""

default_template_file = """
<html>
<head>
Expand Down Expand Up @@ -90,34 +91,32 @@ def createdefaultindexfile(self, filepath):
create a new default index file.
:param filepath: file path where the new index file is stored
"""
with open(filepath, 'w') as indexfile:
with open(filepath, "w") as indexfile:
indexfile.write(self.default_index)

def createdefaulttemplatefile(self, filepath):
"""
Create a new default template.
:param filepath: file path where the new template file is stored.
"""
with open(filepath, 'w') as templatefile:
with open(filepath, "w") as templatefile:
templatefile.write(self.default_template_file)



return True

def createsettingsfile(self):
"""
Create a new settings file
"""
with open(path.join(getcwd(), '.env'), 'w') as settingsFile:
with open(path.join(getcwd(), ".env"), "w") as settingsFile:
settingsFile.write(self.settings)

def createnotfoundpage(self, filepath):
"""
Create a new page not found file.
:param filepath: file path where the page not found is stored
"""
with open(path.join(filepath, '404.md'), 'w') as page:
with open(path.join(filepath, "404.md"), "w") as page:
page.write(self.not_found)


Expand All @@ -126,24 +125,21 @@ def createnotfoundpage(self, filepath):
cliController = CLIController()





@click.group()
def blaskcli():
"""
Initialice the command Line Interface Objects.
"""
try:
version = get_distribution('Blask').version
version = get_distribution("Blask").version
except DistributionNotFound:
version = "test_version"
click.echo('Blask (C) version %s' % version)
click.echo("Blask (C) version %s" % version)


@blaskcli.command(help='Run the instance of blask')
@click.option('--debug', default=False, help="Init with de debug flag")
@click.option('--port', default=5000, help='Port where the server is listening')
@blaskcli.command(help="Run the instance of blask")
@click.option("--debug", default=False, help="Init with de debug flag")
@click.option("--port", default=5000, help="Port where the server is listening")
def run(debug, port):
"""
Run the current blask instance
Expand All @@ -153,27 +149,29 @@ def run(debug, port):
blask.run(debug=debug, port=port)


@blaskcli.command(help='Initialize a new Blask Project')
@blaskcli.command(help="Initialize a new Blask Project")
def init():
"""
Inits a new Blask Instance; with the default options.
"""
click.echo('Initializing new Blask Project')
click.echo('Using default Settings')
postdir = path.basename(path.dirname(str(blasksettings.DEFAULT_SETTINGS['postDir']+'/')))
templatedir = path.basename(path.dirname(str(blasksettings.DEFAULT_SETTINGS['templateDir']+'/')))
click.echo("Initializing new Blask Project")
click.echo("Using default Settings")
postdir = path.basename(path.dirname(str(blasksettings.DEFAULT_SETTINGS["postDir"] + "/")))
templatedir = path.basename(
path.dirname(str(blasksettings.DEFAULT_SETTINGS["templateDir"] + "/"))
)
try:
makedirs(postdir)
cliController.createdefaultindexfile(path.join(postdir, 'index.md'))
cliController.createdefaultindexfile(path.join(postdir, "index.md"))
makedirs(templatedir)
cliController.createdefaulttemplatefile(path.join(templatedir, 'template.html'))
cliController.createsettingsfile() # creates a sample settings file
cliController.createnotfoundpage(postdir) # creates a 404 page
click.echo('Created new Blask project on %s' % getcwd())
click.echo('Now you can execute: blaskcli run')
except FileExistsError as e:
cliController.createdefaulttemplatefile(path.join(templatedir, "template.html"))
cliController.createsettingsfile() # creates a sample settings file
cliController.createnotfoundpage(postdir) # creates a 404 page
click.echo("Created new Blask project on %s" % getcwd())
click.echo("Now you can execute: blaskcli run")
except FileExistsError:
click.echo("There is an existing Blask Project")


if __name__ == '__main__':
if __name__ == "__main__":
blaskcli()
20 changes: 10 additions & 10 deletions Blask/blasksettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
from importlib import import_module


BASE_DIR = Path('.').resolve()
BASE_DIR = Path(".").resolve()

DEFAULT_SETTINGS = {
'templateDir': str(BASE_DIR / 'templates'),
'postDir': str(BASE_DIR / 'posts'),
'defaultLayout': str('template.html'),
'staticDir': str(BASE_DIR / 'static'),
'title': 'Blask | A Simple Blog Engine Based on Flask'
"templateDir": str(BASE_DIR / "templates"),
"postDir": str(BASE_DIR / "posts"),
"defaultLayout": str("template.html"),
"staticDir": str(BASE_DIR / "static"),
"title": "Blask | A Simple Blog Engine Based on Flask",
}


Expand All @@ -49,11 +49,11 @@ def __init__(self, *args, **kwargs):
:param kwargs:
"""
# Check environment variable for settings module
if 'BLASK_SETTINGS' in os.environ:
#add current Dir to Path
if "BLASK_SETTINGS" in os.environ:
# add current Dir to Path
path.append(os.getcwd())
# Load settings from the module in environment variable
settings_mod = import_module(os.environ['BLASK_SETTINGS'], os.environ['BLASK_SETTINGS'])
settings_mod = import_module(os.environ["BLASK_SETTINGS"], os.environ["BLASK_SETTINGS"])

self.settings = {}
for key in DEFAULT_SETTINGS.keys():
Expand All @@ -74,4 +74,4 @@ def __getitem__(self, key):
"""
if key in self.settings:
return self.settings[key]
raise KeyError('There is no blask setting called %s' % key)
raise KeyError("There is no blask setting called %s" % key)
Loading

0 comments on commit 78af429

Please sign in to comment.