Skip to content

Commit

Permalink
Merge pull request #434 from EI-CoreBioinformatics/issue-433-marshmallow
Browse files Browse the repository at this point in the history
Issue #433 - changing the order of operations in printing out the fil…
  • Loading branch information
gemygk authored Feb 28, 2023
2 parents 2b80d2f + 19120ef commit c8f6b7c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Mikado/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand Down
28 changes: 20 additions & 8 deletions Mikado/subprograms/configure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python3

"""Stub of pre-configurer for Mikado"""
import shutil

import yaml
import os
import re
Expand Down Expand Up @@ -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"
Expand All @@ -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():
Expand Down
16 changes: 15 additions & 1 deletion Mikado/tests/test_configurators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c8f6b7c

Please sign in to comment.