Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

Commit

Permalink
Refactor some IO to io mod #69
Browse files Browse the repository at this point in the history
  • Loading branch information
ceholden committed Dec 21, 2015
1 parent 5c66146 commit e557a3e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
19 changes: 19 additions & 0 deletions tests/io/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
""" Tests for ``yatsm.io.helpers``
"""
import pytest

from yatsm.io import helpers


def test_mkdir_p_success(tmpdir):
helpers.mkdir_p(tmpdir.join('test').strpath)


def test_mkdir_p_succcess_exists(tmpdir):
helpers.mkdir_p(tmpdir.join('test').strpath)
helpers.mkdir_p(tmpdir.join('test').strpath)


def test_mkdir_p_failure_permission(tmpdir):
with pytest.raises(OSError):
helpers.mkdir_p('/asdf')
20 changes: 7 additions & 13 deletions yatsm/cli/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from yatsm.cli import options
from yatsm.config_parser import parse_config_file
from yatsm.errors import TSLengthException
from yatsm.io.helpers import mkdir_p
from yatsm.utils import (distribute_jobs, get_output_name, get_image_IDs,
csvfile_to_dataframe)
from yatsm.reader import get_image_attribute, read_line
Expand Down Expand Up @@ -52,20 +53,13 @@ def line(ctx, config, job_number, total_jobs,
# Make sure output directory exists and is writable
output_dir = cfg['dataset']['output']
try:
os.makedirs(output_dir)
except OSError as e:
# File exists
if e.errno == 17:
pass
elif e.errno == 13:
click.secho('Cannot create output directory %s' % output_dir,
fg='red')
raise click.Abort()

mkdir_p(output_dir)
except OSError as err:
raise click.ClickException('Cannot create output directory %s (%s)' %
(output_dir, str(err)))
if not os.access(output_dir, os.W_OK):
click.secho('Cannot write to output directory %s' % output_dir,
fg='red')
raise click.Abort()
raise click.ClickException('Cannot write to output directory %s' %
output_dir)

# Test existence of cache directory
read_cache, write_cache = test_cache(cfg['dataset'])
Expand Down
9 changes: 9 additions & 0 deletions yatsm/io/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
""" YATSM IO module
TODO: include result file IO abstraction (issue #69)
Contents:
* ``helpers``: Collection of helper functions that ease common filesystem
operations
"""
24 changes: 24 additions & 0 deletions yatsm/io/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
""" Collection of helper functions that ease common filesystem operations
"""
import errno
import os


def mkdir_p(d):
""" Make a directory, ignoring error if it exists (i.e., ``mkdir -p``)
Args:
d (str): directory path to create
Raises:
OSError: Raise OSError if cannot create directory for reasons other
than it existing already (errno 13 "EEXIST")
"""
try:
os.makedirs(d)
except OSError as err:
# File exists
if err.errno == errno.EEXIST:
pass
else:
raise err
16 changes: 8 additions & 8 deletions yatsm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ def distribute_jobs(job_number, total_jobs, n, interlaced=True):
""" Assign `job_number` out of `total_jobs` a subset of `n` tasks
Args:
job_number (int): 0-indexed processor to distribute jobs to
total_jobs (int): total number of processors running jobs
n (int): number of tasks (e.g., lines in image, regions in segment)
interlaced (bool, optional): interlace job assignment (default: True)
job_number (int): 0-indexed processor to distribute jobs to
total_jobs (int): total number of processors running jobs
n (int): number of tasks (e.g., lines in image, regions in segment)
interlaced (bool, optional): interlace job assignment (default: True)
Returns:
np.ndarray: np.ndarray of task IDs to be processed
np.ndarray: np.ndarray of task IDs to be processed
Raises:
ValueError: raise error if `job_number` and `total_jobs` specified
result in no jobs being assinged (happens if `job_number` and
`total_jobs` are both 1)
ValueError: raise error if `job_number` and `total_jobs` specified
result in no jobs being assinged (happens if `job_number` and
`total_jobs` are both 1)
"""
if interlaced:
Expand Down

0 comments on commit e557a3e

Please sign in to comment.