Commit 970f9604 authored by André Anjos's avatar André Anjos 💬

More python fixes after gigantic move

parent db8a44fd
Pipeline #19595 failed with stages
in 6 minutes and 31 seconds
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from jinja2 import Environment, PackageLoader
ENV = Environment(loader=PackageLoader(__name__, 'templates'))
"""Jinja2 environment for loading our templates"""
def generate_database(views=None):
"""Generates a BEAT database template
Parameters:
views (:py:class:`list`, Optional): A list of strings that represents the
views for the database
Returns:
str: The rendered template as a string
"""
views = views or ['View']
template = env.get_template('database.jinja2')
s = template.render(views=views)
return s
def generate_library(uses=None):
"""Parameters:
- uses : A dict of other libraries that the library uses.
Keys are the value to reference the library, values are the library being referenced.
"""
uses = uses or {}
template = env.get_template('library.jinja2')
s = template.render(uses=uses)
return s
def generate_algorithm(has_parameters=False, uses={}):
"""Parameters:
- has_parameters: Whether the algorithm has parameters or not (default: False)
- uses : A dict of libraries that the algorithm uses.
Keys are the value to reference the library, values are the library being referenced.
"""
template = env.get_template('algorithm.jinja2')
s = template.render(uses=uses, has_parameters=has_parameters)
return s
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import jinja2
import logging
logger = logging.getLogger(__name__)
ENV = jinja2.Environment(loader=jinja2.PackageLoader(__name__, 'templates'))
"""Jinja2 environment for loading our templates"""
def generate_database(views=None):
"""Generates a valid BEAT database from our stored template
Parameters:
views (:py:class:`list`, Optional): A list of strings that represents the
views for the database
Returns:
str: The rendered template as a string
"""
views = views or ['View']
template = env.get_template('database.jinja2')
return template.render(views=views)
def generate_library(uses=None):
"""Generates a valid BEAT library from our stored template
Parameters:
uses (:py:class:`dict`, Optional): A dict of other libraries that the
library uses. Keys are the value to reference the library, values are
the library being referenced.
Returns:
str: The rendered template as a string
"""
uses = uses or {}
template = env.get_template('library.jinja2')
return template.render(uses=uses)
def generate_algorithm(has_parameters=False, uses=None):
"""Generates a valid BEAT algorithm from our stored template
Parameters:
has_parameters (:py:class:`bool`, Optional): Whether the algorithm has
parameters or not (default: False)
uses (:py:class:`dict`, Optional): A dict of libraries that the algorithm
uses. Keys are the value to reference the library, values are the
library being referenced.
Returns:
str: The rendered template as a string
"""
uses = uses or {}
template = env.get_template('algorithm.jinja2')
return template.render(uses=uses, has_parameters=has_parameters)
TEMPLATE_FUNCTION = dict(
database = generate_database,
library = generate_library,
algorithm = generate_algorithm,
)
"""Functions for template instantiation within beat.editor"""
def generate_python_template(entity, name, **kwargs):
"""Generates a template for a BEAT entity with the given named arguments
Parameters:
entity (str): A valid BEAT entity (valid values are
"""
s = TEMPLATE_FUNCTION[entity](**kwargs)
resource_path = os.path.join(get_prefix(), entity)
file_path = os.path.join(resource_path, name) + '.py'
with open(file_path, 'w') as f: f.write(s)
return s
def get_user_conf():
"""Reads & returns the user configuration in a dict"""
user_conf = {}
if os.path.isfile('./user_conf.json'):
with open('./user_conf.json') as f:
try:
user_conf = json.load(f)
except json.JSONDecodeError:
logger.critical('user_conf.json is invalid!')
raise SystemExit
else:
with open('./user_conf.json', 'w') as f:
user_conf = {
'prefix': '~'
}
json.dump(user_conf, f)
if 'prefix' not in user_conf:
raise Exception('Invalid user_conf: Needs "prefix" key!')
return user_conf
def get_prefix():
"""Returns an absolute path to the user-defined BEAT prefix location"""
return os.path.expanduser(get_user_conf()['prefix'])
def setup_logger(name, verbosity):
'''Sets up the logging of a script
Parameters:
name (str): The name of the logger to setup
verbosity (int): The verbosity level to operate with. A value of ``0``
(zero) means only errors, ``1``, errors and warnings; ``2``, errors,
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")
_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
_debug_info = logging.StreamHandler(sys.stdout)
_debug_info.setFormatter(formatter)
_debug_info.setLevel(logging.DEBUG)
_debug_info.addFilter(_InfoFilter())
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)
return logger
{% set name = 'beat.editor' %}
{% set project_dir = environ.get('RECIPE_DIR') + '/..' %}
{% set nodejs = '8.9.3' %}
package:
name: {{ name }}
......@@ -8,7 +7,7 @@ package:
build:
entry_points:
- beatedit = beat.editor.scripts.server:main
- beateditor = beat.editor.scripts.server:main
number: {{ environ.get('BOB_BUILD_NUMBER', 0) }}
run_exports:
- {{ pin_subpackage(name) }}
......@@ -32,7 +31,8 @@ requirements:
- flask
- flask-cors
- flask-restful
- enum34 # [py<34]
- docopt
- beat.cmdline
test:
source_files:
......@@ -52,6 +52,7 @@ test:
- {{ name }}
commands:
- beateditor --help
- nosetests --with-coverage --cover-package={{ name }} -sv {{ name }}
- sphinx-build -aEW {{ project_dir }}/doc {{ project_dir }}/sphinx
- sphinx-build -aEb doctest {{ project_dir }}/doc sphinx
......
......@@ -51,7 +51,7 @@ setup(
install_requires=load_requirements('requirements.txt'),
entry_points={
'console_scripts': [
'beatedit = beat.editor.scripts.server:main',
'beateditor = beat.editor.scripts.server:main',
],
},
......
Markdown is supported
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