...
 
Commits (6)
......@@ -13,8 +13,10 @@ Design principles:
| its up to the consumer to know where to find the data.
"""
import os
import json
import copy
import sksurgerycore.utilities.file_utilities as fu
import sksurgerycore.utilities.validate_file as f
......@@ -30,18 +32,37 @@ class ConfigurationManager:
def __init__(self, file_name,
write_on_setter=False
):
""" Constructor. """
f.validate_is_file(file_name)
abs_file = fu.get_absolute_path_of_file(file_name)
f.validate_is_file(abs_file)
if write_on_setter:
f.validate_is_writable_file(file_name)
f.validate_is_writable_file(abs_file)
with open(file_name, "r") as read_file:
with open(abs_file, "r") as read_file:
self.config_data = json.load(read_file)
self.file_name = file_name
self.file_name = abs_file
self.write_on_setter = write_on_setter
def get_file_name(self):
"""
Returns the absolute filename that was used when
the ConfigurationManager was created.
:return: str absolute file name
"""
return self.file_name
def get_dir_name(self):
"""
Returns the directory name of the file that was used when
creating the ConfigurationManager.
:return: str dir name
"""
return os.path.dirname(self.file_name)
def get_copy(self):
""" Returns a copy of the data read from file.
......
......@@ -236,12 +236,14 @@ class TransformManager:
return
for candidate in candidates:
if candidate in list_of_nodes:
continue
else:
list_of_nodes.append(candidate)
self.__get_list(candidate, after, list_of_nodes)
if list_of_nodes[-1] == after:
break
else:
list_of_nodes.pop()
list_of_nodes.append(candidate)
self.__get_list(candidate, after, list_of_nodes)
if list_of_nodes[-1] == after:
break
list_of_nodes.pop()
# -*- coding: utf-8 -*-
""" File processing utils. """
import os
def get_absolute_path_of_file(file_name, dir_name=None):
"""
Filenames in our .json config could be absolute or relative to the
current working dir. This method tries to find the valid, full file path.
:param file_name:
:param dir_name: prefix, for example, the dirname of our .json file.
:return: absolute path name of file if found, otherwise None.
"""
if not file_name:
raise ValueError("Empty file_name.")
file = None
if os.path.isabs(file_name):
if os.path.isfile(file_name):
file = file_name
elif dir_name is not None:
joined = os.path.join(dir_name, file_name)
if os.path.isfile(joined):
file = joined
elif os.getcwd() is not None:
joined = os.path.join(os.getcwd(), file_name)
if os.path.isfile(joined):
file = joined
return file
# -*- coding: utf-8 -*-
import os
import pytest
import sksurgerycore.configuration.configuration_manager as cm
......@@ -20,6 +21,8 @@ def test_constructor_with_valid_file():
manager = cm.ConfigurationManager("tests/data/FordPrefect.json")
assert manager is not None
assert manager.get_file_name() is not None
assert manager.get_dir_name() == os.path.dirname(manager.get_file_name())
def test_setter_getter_loop():
......
# -*- coding: utf-8 -*-
""" File processing utils. """
import os
import pytest
import sksurgerycore.utilities.file_utilities as fu
def test_get_absolute_file():
cwd = os.getcwd()
this_file = "tests/utilities/test_file_utilities.py"
fp = fu.get_absolute_path_of_file(this_file)
assert fp == os.path.join(cwd, fp)
fp2 = fu.get_absolute_path_of_file(fp)
assert fp2 == fp
fp3 = fu.get_absolute_path_of_file(this_file, cwd)
assert fp3 == fp
# content of: tox.ini , put in same dir as setup.py
[tox]
envlist = py27,py36,lint
envlist = py36,lint
skipsdist = True
[travis]
python =
2.7: py27
3.6: py36, docs, lint
[testenv]
......@@ -31,13 +30,6 @@ commands = sphinx-build -M html . build
basepython=python3.6
commands=pyinstaller --onefile sksurgerycore.py --noconfirm --windowed
[testenv:pip2]
basepython=python2.7
changedir=pip_test
skip_install=True
commands = pip install {posargs}
sksurgerycore --help
[testenv:pip3]
basepython=python3.6
changedir=pip_test
......