diff --git a/pypiper/AttributeDict.py b/pypiper/AttributeDict.py deleted file mode 100644 index 2861dbbd..00000000 --- a/pypiper/AttributeDict.py +++ /dev/null @@ -1,56 +0,0 @@ - -import os - -class AttributeDict(object): - """ - A class to convert a nested Dictionary into an object with key-values - accessibly using attribute notation (AttributeDict.attribute) instead of - key notation (Dict["key"]). This class recursively sets Dicts to objects, - allowing you to recurse down nested dicts (like: AttributeDict.attr.attr) - """ - def __init__(self, entries, default=False): - """ - :param entries: A dictionary (key-value pairs) to add as attributes. - :param default: Should this AttributeDict object return Default - values for attributes that it does not have? If True, then - AttributeDict.java would return "java" instead of raising an error, - if no .java attribute were found. - """ - self.add_entries(entries, default) - self.return_defaults = default - - def add_entries(self, entries, default=False): - for key, value in entries.items(): - if type(value) is dict: - self.__dict__[key] = AttributeDict(value, default) - else: - try: - # try expandvars() to allow the yaml to use - # shell variables. - self.__dict__[key] = os.path.expandvars(value) # value - except TypeError: - # if value is an int, expandvars() will fail; if - # expandvars() fails, just use the raw value - self.__dict__[key] = value - - def __getitem__(self, key): - """ - Provides dict-style access to attributes - """ - return getattr(self, key) - - def __repr__(self): - return str(self.__dict__) - - def __getattr__(self, name): - if name in self.__dict__.keys(): - return self.name - else: - if self.return_defaults and not \ - (name.startswith("__") and name.endswith("__")): - # If this object has default mode on, then we should - # simply return the name of the requested attribute as - # a default, if no attribute with that name exists. - return name - else: - raise AttributeError("No attribute " + name) diff --git a/pypiper/__init__.py b/pypiper/__init__.py index ad2f07fc..86bcf9b9 100644 --- a/pypiper/__init__.py +++ b/pypiper/__init__.py @@ -1,7 +1,6 @@ from ._version import __version__ from .manager import * from .ngstk import * -from .AttributeDict import * from .utils import * from .pipeline import * from .exceptions import * diff --git a/pypiper/manager.py b/pypiper/manager.py index aa311bbc..3642bd63 100644 --- a/pypiper/manager.py +++ b/pypiper/manager.py @@ -21,7 +21,7 @@ import sys import time -from .AttributeDict import AttributeDict +from attmap import AttMapEcho from .exceptions import PipelineHalt, SubprocessError from .flags import * from .utils import \ @@ -286,7 +286,7 @@ def __init__( # later to pass to, for example, toolkits import yaml config = yaml.load(conf) - self.config = AttributeDict(config, default=True) + self.config = AttMapEcho(config) else: print("No config file") self.config = None diff --git a/pypiper/ngstk.py b/pypiper/ngstk.py index a6e4de91..ac54c0d6 100755 --- a/pypiper/ngstk.py +++ b/pypiper/ngstk.py @@ -4,17 +4,17 @@ import re import subprocess import errno -from .AttributeDict import AttributeDict as _AttributeDict +from attmap import AttMapEcho from .exceptions import UnsupportedFiletypeException from .utils import is_fastq, is_gzipped_fastq, is_sam_or_bam -class NGSTk(_AttributeDict): +class NGSTk(AttMapEcho): """ Class to hold functions to build command strings used during pipeline runs. Object can be instantiated with a string of a path to a yaml `pipeline config file`. - Since NGSTk inherits from `AttributeDict`, the passed config file and its elements + Since NGSTk inherits from `AttMapEcho`, the passed config file and its elements will be accessible through the NGSTk object as attributes under `config` (e.g. `NGSTk.tools.java`). In case no `config_file` argument is passed, all commands will be returned assuming the tool is in the user's $PATH. @@ -46,29 +46,29 @@ def __init__(self, config_file=None, pm=None): # self.add_entries(**config) if config_file is None: - super(NGSTk, self).__init__({}, default=True) + super(NGSTk, self).__init__() else: import yaml with open(config_file, 'r') as config_file: config = yaml.load(config_file) - super(NGSTk, self).__init__(config, default=True) + super(NGSTk, self).__init__(config) # Keep a link to the pipeline manager, if one is provided. - # if None is provided, instantiate "tools" and "parameters" with empty AttributeDicts + # if None is provided, instantiate "tools" and "parameters" with empty AttMaps # this allows the usage of the same code for a command with and without using a pipeline manager if pm is not None: self.pm = pm if hasattr(pm.config, "tools"): self.tools = self.pm.config.tools else: - self.tools = _AttributeDict(dict(), default=True) + self.tools = AttMapEcho() if hasattr(pm.config, "parameters"): self.parameters = self.pm.config.parameters else: - self.parameters = _AttributeDict(dict(), default=True) + self.parameters = AttMapEcho() else: - self.tools = _AttributeDict(dict(), default=True) - self.parameters = _AttributeDict(dict(), default=True) + self.tools = AttMapEcho() + self.parameters = AttMapEcho() # If pigz is available, use that. Otherwise, default to gzip. if hasattr(self.pm, "cores") and self.pm.cores > 1 and self.check_command("pigz"): diff --git a/requirements/reqs-pypiper.txt b/requirements/reqs-pypiper.txt index 33e5c900..6f94f43b 100644 --- a/requirements/reqs-pypiper.txt +++ b/requirements/reqs-pypiper.txt @@ -1,2 +1,3 @@ +attmap pyyaml psutil diff --git a/tests/test_attribute_dict.py b/tests/test_attribute_dict.py deleted file mode 100644 index 273a5543..00000000 --- a/tests/test_attribute_dict.py +++ /dev/null @@ -1,16 +0,0 @@ -""" Tests for pypiper implementation of AttributeDict """ - -import pytest -from pypiper import AttributeDict as AD - - -@pytest.mark.parametrize("base", ["random", "irrelevant", "arbitrary"]) -@pytest.mark.parametrize("protect", [False, True]) -def test_echo_respects_protected(base, protect): - """ Regardless of AttributeDict behavior, protected member isn't echoed. """ - ad = AD({}, default=True) - if protect: - with pytest.raises(AttributeError): - ad.__getattr__("__{}__".format(base)) - else: - assert base == ad.__getattr__(base)