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

Commit e557a3e

Browse files
committed
Refactor some IO to io mod #69
1 parent 5c66146 commit e557a3e

File tree

5 files changed

+67
-21
lines changed

5 files changed

+67
-21
lines changed

tests/io/test_helpers.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
""" Tests for ``yatsm.io.helpers``
2+
"""
3+
import pytest
4+
5+
from yatsm.io import helpers
6+
7+
8+
def test_mkdir_p_success(tmpdir):
9+
helpers.mkdir_p(tmpdir.join('test').strpath)
10+
11+
12+
def test_mkdir_p_succcess_exists(tmpdir):
13+
helpers.mkdir_p(tmpdir.join('test').strpath)
14+
helpers.mkdir_p(tmpdir.join('test').strpath)
15+
16+
17+
def test_mkdir_p_failure_permission(tmpdir):
18+
with pytest.raises(OSError):
19+
helpers.mkdir_p('/asdf')

yatsm/cli/line.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from yatsm.cli import options
1111
from yatsm.config_parser import parse_config_file
1212
from yatsm.errors import TSLengthException
13+
from yatsm.io.helpers import mkdir_p
1314
from yatsm.utils import (distribute_jobs, get_output_name, get_image_IDs,
1415
csvfile_to_dataframe)
1516
from yatsm.reader import get_image_attribute, read_line
@@ -52,20 +53,13 @@ def line(ctx, config, job_number, total_jobs,
5253
# Make sure output directory exists and is writable
5354
output_dir = cfg['dataset']['output']
5455
try:
55-
os.makedirs(output_dir)
56-
except OSError as e:
57-
# File exists
58-
if e.errno == 17:
59-
pass
60-
elif e.errno == 13:
61-
click.secho('Cannot create output directory %s' % output_dir,
62-
fg='red')
63-
raise click.Abort()
64-
56+
mkdir_p(output_dir)
57+
except OSError as err:
58+
raise click.ClickException('Cannot create output directory %s (%s)' %
59+
(output_dir, str(err)))
6560
if not os.access(output_dir, os.W_OK):
66-
click.secho('Cannot write to output directory %s' % output_dir,
67-
fg='red')
68-
raise click.Abort()
61+
raise click.ClickException('Cannot write to output directory %s' %
62+
output_dir)
6963

7064
# Test existence of cache directory
7165
read_cache, write_cache = test_cache(cfg['dataset'])

yatsm/io/__init__.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
""" YATSM IO module
2+
3+
TODO: include result file IO abstraction (issue #69)
4+
5+
Contents:
6+
7+
* ``helpers``: Collection of helper functions that ease common filesystem
8+
operations
9+
"""

yatsm/io/helpers.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
""" Collection of helper functions that ease common filesystem operations
2+
"""
3+
import errno
4+
import os
5+
6+
7+
def mkdir_p(d):
8+
""" Make a directory, ignoring error if it exists (i.e., ``mkdir -p``)
9+
10+
Args:
11+
d (str): directory path to create
12+
13+
Raises:
14+
OSError: Raise OSError if cannot create directory for reasons other
15+
than it existing already (errno 13 "EEXIST")
16+
"""
17+
try:
18+
os.makedirs(d)
19+
except OSError as err:
20+
# File exists
21+
if err.errno == errno.EEXIST:
22+
pass
23+
else:
24+
raise err

yatsm/utils.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ def distribute_jobs(job_number, total_jobs, n, interlaced=True):
2121
""" Assign `job_number` out of `total_jobs` a subset of `n` tasks
2222
2323
Args:
24-
job_number (int): 0-indexed processor to distribute jobs to
25-
total_jobs (int): total number of processors running jobs
26-
n (int): number of tasks (e.g., lines in image, regions in segment)
27-
interlaced (bool, optional): interlace job assignment (default: True)
24+
job_number (int): 0-indexed processor to distribute jobs to
25+
total_jobs (int): total number of processors running jobs
26+
n (int): number of tasks (e.g., lines in image, regions in segment)
27+
interlaced (bool, optional): interlace job assignment (default: True)
2828
2929
Returns:
30-
np.ndarray: np.ndarray of task IDs to be processed
30+
np.ndarray: np.ndarray of task IDs to be processed
3131
3232
Raises:
33-
ValueError: raise error if `job_number` and `total_jobs` specified
34-
result in no jobs being assinged (happens if `job_number` and
35-
`total_jobs` are both 1)
33+
ValueError: raise error if `job_number` and `total_jobs` specified
34+
result in no jobs being assinged (happens if `job_number` and
35+
`total_jobs` are both 1)
3636
3737
"""
3838
if interlaced:

0 commit comments

Comments
 (0)