Commit d0b2494f authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[utils] Removed unused methods

parent 3a9492ef
......@@ -29,46 +29,11 @@
Test the utils.py file
"""
import os
import pkg_resources
from PyQt5 import QtCore
from .. import utils
DATA_PATH = pkg_resources.resource_filename("beat.editor.test", "reference_data")
def compare_with_reference(generated, reference_file_name):
""" Compare the given generated code with the content of the reference file
"""
with open(os.path.join(DATA_PATH, reference_file_name)) as reference_file:
reference = reference_file.read()
return generated == reference
def test_generate_empty_database():
database = utils.generate_database()
assert compare_with_reference(database, "empty_database.py")
def test_generate_empty_algorithm():
alg = {
"name": "user/alg/1",
"contents": {"splittable": True, "groups": [], "uses": {}},
}
algorithm = utils.generate_algorithm(alg["contents"])
assert compare_with_reference(algorithm, "empty_algorithm.py")
def test_generate_empty_library():
library = utils.generate_library()
assert compare_with_reference(library, "empty_library.py")
def test_Qt_version_equal():
assert utils.is_Qt_equal_or_higher(QtCore.QT_VERSION_STR)
......
......@@ -35,8 +35,6 @@ import logging
import simplejson as json
import pkg_resources
import jinja2
from functools import wraps
from packaging import version
from PyQt5 import QtCore
......@@ -44,139 +42,6 @@ from PyQt5 import QtCore
logger = logging.getLogger(__name__)
# Jinja2 environment for loading our templates
ENV = jinja2.Environment(
loader=jinja2.PackageLoader(__name__, "templates"),
autoescape=True,
keep_trailing_newline=True,
)
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(contents):
"""Generates a valid BEAT algorithm from our stored template
Parameters:
contents (:py:class:`dict`): The algorithm's JSON metadata
Returns:
str: The rendered template as a string
"""
template = ENV.get_template("algorithm.jinja2")
return template.render(contents=contents)
def generate_plotter(uses):
"""Generates a valid BEAT plotter from our stored template
Parameters:
contents (:py:class:`dict`): The plotter's JSON metadata
Returns:
str: The rendered template as a string
"""
uses = uses or {}
template = ENV.get_template("plotter.jinja2")
return template.render(uses=uses)
TEMPLATE_FUNCTION = dict(
databases=generate_database,
libraries=generate_library,
algorithms=generate_algorithm,
plotters=generate_plotter,
)
class PythonFileAlreadyExistsError(Exception):
pass
# Functions for template instantiation within beat.editor
def generate_python_template(entity, name, confirm, config, **kwargs):
"""Generates a template for a BEAT entity with the given named arguments
Parameters:
entity (str): A valid BEAT entity
name (str): The name of the object to have a python file generated for
confirm (:py:class:`boolean`): Whether to override the Python file if
one is found at the desired location
"""
resource_path = os.path.join(config.path, entity)
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)
return s
def setup_logger(name, verbosity):
"""Sets up the logging of a script
......
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