Commit 9a171983 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[utils] Code cleanup based on black and flake8

parent c0a40af4
......@@ -32,10 +32,11 @@ import sys
import jinja2
import logging
logger = logging.getLogger(__name__)
ENV = jinja2.Environment(loader=jinja2.PackageLoader(__name__, 'templates'))
ENV = jinja2.Environment(loader=jinja2.PackageLoader(__name__, "templates"))
"""Jinja2 environment for loading our templates"""
......@@ -55,8 +56,8 @@ def generate_database(views=None):
"""
views = views or ['View']
template = ENV.get_template('database.jinja2')
views = views or ["View"]
template = ENV.get_template("database.jinja2")
return template.render(views=views)
......@@ -78,7 +79,7 @@ def generate_library(uses=None):
"""
uses = uses or {}
template = ENV.get_template('library.jinja2')
template = ENV.get_template("library.jinja2")
return template.render(uses=uses)
......@@ -97,7 +98,7 @@ def generate_algorithm(contents):
"""
template = ENV.get_template('algorithm.jinja2')
template = ENV.get_template("algorithm.jinja2")
return template.render(contents=contents)
......@@ -117,7 +118,7 @@ def generate_plotter(uses):
"""
uses = uses or {}
template = ENV.get_template('plotter.jinja2')
template = ENV.get_template("plotter.jinja2")
return template.render(uses=uses)
......@@ -130,9 +131,11 @@ TEMPLATE_FUNCTION = dict(
"""Functions for template instantiation within beat.editor"""
class PythonFileAlreadyExistsError(Exception):
pass
def generate_python_template(entity, name, confirm, config, **kwargs):
"""Generates a template for a BEAT entity with the given named arguments
......@@ -148,20 +151,21 @@ def generate_python_template(entity, name, confirm, config, **kwargs):
"""
resource_path = os.path.join(config.path, entity)
file_path = os.path.join(resource_path, name) + '.py'
file_path = os.path.join(resource_path, name) + ".py"
if not confirm and os.path.isfile(file_path):
# python file already exists
raise PythonFileAlreadyExistsError
s = TEMPLATE_FUNCTION[entity](**kwargs)
with open(file_path, 'w') as f: f.write(s)
with open(file_path, "w") as f:
f.write(s)
return s
def setup_logger(name, verbosity):
'''Sets up the logging of a script
"""Sets up the logging of a script
Parameters:
......@@ -173,18 +177,20 @@ def setup_logger(name, verbosity):
warnings and informational messages and, finally, ``3``, all types of
messages including debugging ones.
'''
"""
logger = logging.getLogger(name)
formatter = logging.Formatter("%(name)s@%(asctime)s -- %(levelname)s: " \
"%(message)s")
formatter = logging.Formatter(
"%(name)s@%(asctime)s -- %(levelname)s: " "%(message)s"
)
_warn_err = logging.StreamHandler(sys.stderr)
_warn_err.setFormatter(formatter)
_warn_err.setLevel(logging.WARNING)
class _InfoFilter:
def filter(self, record): return record.levelno <= logging.INFO
def filter(self, record):
return record.levelno <= logging.INFO
_debug_info = logging.StreamHandler(sys.stdout)
_debug_info.setFormatter(formatter)
......@@ -194,10 +200,12 @@ def setup_logger(name, verbosity):
logger.addHandler(_debug_info)
logger.addHandler(_warn_err)
logger.setLevel(logging.ERROR)
if verbosity == 1: logger.setLevel(logging.WARNING)
elif verbosity == 2: logger.setLevel(logging.INFO)
elif verbosity >= 3: logger.setLevel(logging.DEBUG)
if verbosity == 1:
logger.setLevel(logging.WARNING)
elif verbosity == 2:
logger.setLevel(logging.INFO)
elif verbosity >= 3:
logger.setLevel(logging.DEBUG)
return logger
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment