From 19120efe595f6d25b44b3282a260f3282bf9fa36 Mon Sep 17 00:00:00 2001 From: Luca Venturini Date: Fri, 10 Jun 2022 10:34:59 +0100 Subject: [PATCH] Issue #433 - changing the order of operations in printing out the file solves the issue. No idea of why it is triggered by the new versions of marshmallow; the creation and loading of the dictionaries functions as before. --- Mikado/configuration/__init__.py | 4 ++++ Mikado/subprograms/configure.py | 28 ++++++++++++++++++++-------- Mikado/tests/test_configurators.py | 16 +++++++++++++++- requirements.txt | 4 ++-- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Mikado/configuration/__init__.py b/Mikado/configuration/__init__.py index 4efced753..0f1e6cd98 100644 --- a/Mikado/configuration/__init__.py +++ b/Mikado/configuration/__init__.py @@ -7,6 +7,7 @@ import os from marshmallow import fields from .configuration import MikadoConfiguration +from .configurator import load_and_validate_config from .daijin_configuration import DaijinConfiguration from . import configurator import itertools @@ -55,6 +56,9 @@ def print_config(config: Union[MikadoConfiguration, DaijinConfiguration], out, if not full: config_dict = filter_config(config_dict, config) + assert isinstance(config_dict, dict) + # load_and_validate_config(config_dict) + if output_format == "toml": print_toml_config(config_dict, config, out) elif output_format == "yaml": diff --git a/Mikado/subprograms/configure.py b/Mikado/subprograms/configure.py index 748f0ea54..cbb5b7185 100644 --- a/Mikado/subprograms/configure.py +++ b/Mikado/subprograms/configure.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 """Stub of pre-configurer for Mikado""" +import shutil + import yaml import os import re @@ -186,14 +188,6 @@ def create_config(args): config.check() # Check that the configuration file is correct - with tempfile.NamedTemporaryFile("wt", suffix=".json", delete=True) as tempcheck: - print_config(config, tempcheck, full=args.full, output_format="json") - tempcheck.flush() - try: - load_and_validate_config(tempcheck.name) - except InvalidConfiguration as exc: - raise InvalidConfiguration("Created an invalid configuration file! Error:\n{}".format(exc)) - # Print out the final configuration file if args.json is True or args.out.name.endswith("json"): format_name = "json" @@ -202,7 +196,25 @@ def create_config(args): else: format_name = "toml" + with tempfile.NamedTemporaryFile("wt", suffix=f".{format_name}", delete=False, + dir=os.path.dirname(os.path.abspath(args.out.name))) as tempcheck: + print_config(config, tempcheck, full=args.full, output_format=format_name, no_files=args.no_files) + tempcheck.flush() + assert os.path.exists(tempcheck.name) + try: + load_and_validate_config(tempcheck.name) + os.remove(tempcheck.name) + except InvalidConfiguration as exc: + raise InvalidConfiguration(f"Created an invalid configuration file! Error:\n{exc}") + + config = config.copy() print_config(config, args.out, output_format=format_name, no_files=args.no_files, full=args.full) + args.out.close() + try: + load_and_validate_config(args.out.name) + except InvalidConfiguration as exc: + os.remove(args.out.name) + raise InvalidConfiguration("Created an invalid configuration file! Error:\n{}".format(exc)) def configure_parser(): diff --git a/Mikado/tests/test_configurators.py b/Mikado/tests/test_configurators.py index aa5fc230e..1948963ec 100644 --- a/Mikado/tests/test_configurators.py +++ b/Mikado/tests/test_configurators.py @@ -3,11 +3,12 @@ import marshmallow import tempfile import pkg_resources -from ..configuration import configurator +from ..configuration import configurator, print_config from .._transcripts.scoring_configuration import SizeFilter, TargetScore from ..configuration.configuration import * from ..loci.abstractlocus import Abstractlocus import pickle +import io class TestConfigurator(unittest.TestCase): @@ -119,3 +120,16 @@ def test_load_invalid_scoring(self): pkg_resources.resource_filename("Mikado.configuration", os.path.join("scoring_files", "plant.yaml"))) os.chdir(current) + + def test_create_config(self): + config = MikadoConfiguration() + self.assertIsInstance(config, MikadoConfiguration) + self.assertIsInstance(config.pick, PickConfiguration) + config.check() + buffer = io.StringIO() + print_config(config, buffer, output_format="json") + reading = buffer.getvalue() + self.assertGreater(len(reading), 0) + reloaded = json.loads(reading) + reloaded_config = MikadoConfiguration.Schema().load(reloaded) + self.assertEqual(reloaded_config, config) diff --git a/requirements.txt b/requirements.txt index 96c851f53..86e24bfbb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,6 +19,6 @@ python-rapidjson>=1.0.0 toml>=0.10.0 pyfaidx>=0.5.8 dataclasses; python_version < '3.7' -marshmallow==3.14.1 -marshmallow-dataclass==8.5.3 +marshmallow>=3.14.1 +marshmallow-dataclass>=8.5.3 typeguard # Necessary for mashmallow apparently