Skip to content

Commit

Permalink
Move version functions from utils to __version__ module
Browse files Browse the repository at this point in the history
Also stop using AugurException in node file parsing. It doesn't add any
information over RuntimeError.
  • Loading branch information
elebow committed Aug 12, 2020
1 parent 673ccd1 commit f04f357
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
22 changes: 22 additions & 0 deletions augur/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
__version__ = '9.0.0'


def is_augur_version_compatible(version):
"""
Checks if the provided **version** is the same major version
as the currently running version of augur.
Parameters
----------
version : str
version to check against the current version
Returns
-------
Bool
"""
import packaging.version

current_version = packaging.version.parse(__version__)
this_version = packaging.version.parse(version)
return this_version.release[0] == current_version.release[0]
15 changes: 7 additions & 8 deletions augur/util_support/node_data_file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json

from augur.utils import AugurException # TODO use something more meaningful
from augur.utils import get_augur_version
from augur.utils import is_augur_version_compatible
from augur.__version__ import __version__
from augur.__version__ import is_augur_version_compatible
from augur.validate import validate_json, ValidateError, load_json_schema


Expand Down Expand Up @@ -59,21 +58,21 @@ def validate(self):
self.fname,
)
except ValidateError as err:
raise AugurException(
raise RuntimeError(
f"{self.fname} contains an `annotations` attribute of an invalid JSON format. Was it "
"produced by different version of augur the one you are currently using "
f" ({get_augur_version()})? Please check the program that produced that JSON file."
f" ({__version__})? Please check the program that produced that JSON file."
) from err

if not isinstance(self.nodes, dict):
raise AugurException(
raise RuntimeError(
f"`nodes` value in {self.fname} is not a dictionary. Please check the formatting of this JSON!"
)

if self.is_generated_by_incompatible_augur:
raise AugurException(
raise RuntimeError(
f"Augur version incompatibility detected: the JSON {self.fname} was generated by "
f"{self.generated_by}, which is incompatible with the current augur version "
f"({get_augur_version()}). We suggest you rerun the pipeline using the current version of "
f"({__version__}). We suggest you rerun the pipeline using the current version of "
"augur."
)
26 changes: 2 additions & 24 deletions augur/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
from pkg_resources import resource_stream
from io import TextIOWrapper
from .__version__ import __version__
import packaging.version as packaging_version

from augur.util_support.color_parser import ColorParser
from augur.util_support.date_disambiguator import DateDisambiguator
from augur.util_support.metadata_file import MetadataFile
from augur.util_support.node_data_reader import NodeDataReader
from augur.util_support.shell_command_runner import ShellCommandRunner


Expand Down Expand Up @@ -156,11 +156,8 @@ def read_tree(fname, min_terminals=3):
return T



def read_node_data(fnames, tree=None):
import augur
return augur.util_support.node_data_reader.NodeDataReader(fnames, tree).read()
#return NodeDataReader(fnames, tree).read()
return NodeDataReader(fnames, tree).read()


def write_json(data, file_name, indent=(None if os.environ.get("AUGUR_MINIFY_JSON") else 2), include_version=True):
Expand Down Expand Up @@ -572,25 +569,6 @@ def get_augur_version():
return __version__


def is_augur_version_compatible(version):
"""
Checks if the provided **version** is the same major version
as the currently running version of augur.
Parameters
----------
version : str
version to check against the current version
Returns
-------
Bool
"""
current_version = packaging_version.parse(get_augur_version())
this_version = packaging_version.parse(version)
return this_version.release[0] == current_version.release[0]

def read_bed_file(bed_file):
"""Read a BED file and return a list of excluded sites.
Expand Down
20 changes: 10 additions & 10 deletions tests/util_support/test_node_data_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from augur.utils import AugurException
from augur.__version__ import __version__
from augur.util_support.node_data_file import NodeDataFile

import pytest
Expand Down Expand Up @@ -35,17 +35,17 @@ def test_items(self, build_node_data_file):
def test_validate_valid(self, build_node_data_file):
# Implied assertion that no exceptions are raised
build_node_data_file(
"""
{
"annotations": { "a": { "start": 5 } },
"generated_by": { "program": "augur", "version": "9.0.0" },
"nodes": { "a": 5 }
}
f"""
{{
"annotations": {{ "a": {{ "start": 5 }} }},
"generated_by": {{ "program": "augur", "version": "{__version__}" }},
"nodes": {{ "a": 5 }}
}}
"""
)

def test_validate_invalid_annotation(self, build_node_data_file):
with pytest.raises(AugurException, match="annotations.*invalid JSON format"):
with pytest.raises(RuntimeError, match="annotations.*invalid JSON format"):
build_node_data_file(
"""
{
Expand All @@ -56,11 +56,11 @@ def test_validate_invalid_annotation(self, build_node_data_file):
)

def test_validate_nodes_not_dict(self, build_node_data_file):
with pytest.raises(AugurException, match="is not a dictionary"):
with pytest.raises(RuntimeError, match="is not a dictionary"):
build_node_data_file('{ "nodes": "hhh" }')

def test_validate_incompatible_augur_version(self, build_node_data_file):
with pytest.raises(AugurException, match="incompatibility detected"):
with pytest.raises(RuntimeError, match="incompatibility detected"):
build_node_data_file(
"""
{
Expand Down

0 comments on commit f04f357

Please sign in to comment.