Skip to content

Commit a67c889

Browse files
Merge pull request #8 from martinkilbinger/vos
added vos support
2 parents a3b35cc + 91c6316 commit a67c889

3 files changed

Lines changed: 227 additions & 1 deletion

File tree

cs_util/canfar.py

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
"""CANFAR TOOLS.
2+
3+
This module defines methods for managing CANFAR specific actions.
4+
5+
:Author: Samuel Farrens <samuel.farrens@cea.fr>
6+
Martin Kilbinger <martin.kilbinger@cea.fr>
7+
8+
"""
9+
10+
import os
11+
import sys
12+
from contextlib import redirect_stdout
13+
from io import StringIO
14+
15+
import vos.commands as vosc
16+
17+
18+
class vosError(Exception):
19+
"""VOS Error.
20+
21+
Generic error that is raised by the vosHandler.
22+
23+
"""
24+
25+
pass
26+
27+
28+
class vosHandler:
29+
"""VOS Handler.
30+
31+
This class manages the use of VOS commands.
32+
33+
Parameters
34+
----------
35+
command : str
36+
VOS command name
37+
38+
"""
39+
40+
def __init__(self, command):
41+
42+
self._check_vos_install()
43+
self._avail_commands = tuple(vosc.__all__)
44+
self.command = command
45+
46+
@staticmethod
47+
def _check_vos_install():
48+
"""Check VOS Install.
49+
50+
Check if VOS is correctly installed.
51+
52+
Raises
53+
------
54+
ImportError
55+
if vos package cannot be imported
56+
57+
"""
58+
if import_fail:
59+
raise ImportError('vos package not found')
60+
61+
@property
62+
def command(self):
63+
"""Set Command.
64+
65+
This method sets the VOS command property.
66+
67+
Raises
68+
------
69+
ValueError
70+
if value is not valid vos command
71+
72+
"""
73+
return self._command
74+
75+
@command.setter
76+
def command(self, value):
77+
78+
if value not in self._avail_commands:
79+
raise ValueError(
80+
f'vos command must be one of {self._avail_commands}'
81+
)
82+
83+
self._command = getattr(vosc, value)
84+
85+
def __call__(self, *args, **kwargs):
86+
"""Call Method.
87+
88+
This method allows class instances to be called as functions.
89+
90+
Raises
91+
------
92+
vosError
93+
if error in vos command occurs
94+
95+
"""
96+
try:
97+
self._command()
98+
99+
except Exception:
100+
raise vosError(
101+
f'Error in VOs command: {self._command.__name__}'
102+
)
103+
104+
105+
def download(source, target, verbose=False):
106+
"""Download.
107+
108+
Download file from vos.
109+
110+
Parameters
111+
----------
112+
source : str
113+
source path on vos
114+
target : str
115+
target path
116+
verbose : bool, optional, default=False
117+
verbose output if True
118+
119+
Returns
120+
-------
121+
status : bool
122+
status, True/False or success/failure
123+
124+
"""
125+
cmd = 'vcp'
126+
127+
if not os.path.exists(target):
128+
sys.argv = [cmd, source, target]
129+
if verbose:
130+
print(f'Downloading file {source} to {target}...')
131+
vcp = vosHandler(cmd)
132+
133+
vcp()
134+
if verbose:
135+
print('Download finished.')
136+
else:
137+
if verbose:
138+
print(f'Target file {target} exists, skipping download.')
139+
140+
141+
def dir_list(path, verbose=False):
142+
"""Set Directory List.
143+
144+
List content of path on vos.
145+
146+
Parameters
147+
----------
148+
path : str
149+
path on vos, starts with 'vos:cfis/...'
150+
verbose : bool, optional, default=False
151+
verbose output if True
152+
153+
Raises
154+
------
155+
HTTPError, KeyError
156+
if error occurs during vos command
157+
158+
Returns
159+
-------
160+
list
161+
file or directory at path, type is str
162+
163+
"""
164+
cmd = 'vls'
165+
sys.argv = [cmd, path]
166+
vls = vosHandler(cmd)
167+
168+
if verbose:
169+
print('Getting vos directory content from vls...')
170+
171+
f = StringIO()
172+
173+
try:
174+
with redirect_stdout(f):
175+
vls()
176+
except Exception:
177+
print('Error during vls command')
178+
raise
179+
180+
vls_out = f.getvalue()
181+
182+
return vls_out.split('\n')

cs_util/tests/test_canfar.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""UNIT TESTS FOR CANFAR SUBPACKAGE.
2+
3+
This module contains unit tests for the canfar subpackage.
4+
5+
"""
6+
7+
from unittest import TestCase
8+
9+
from numpy import testing as npt
10+
import os
11+
import sys
12+
13+
from cs_util import canfar
14+
15+
16+
class CanfarTestCase(TestCase):
17+
"""Test case for the ``canfar`` module."""
18+
19+
def setUp(self):
20+
"""Set test parameter values."""
21+
self._cmd_ok = 'vcp'
22+
self._cmd_nok = 'vxx'
23+
24+
def tearDown(self):
25+
"""Unset test parameter values."""
26+
self._cmd_ok = None
27+
self._cmd_nok = None
28+
29+
def test_init_vos(self):
30+
"""Test ``cs_util.canfar.vosHandler()`` with
31+
vos command strings.
32+
33+
"""
34+
# Test whether command is set
35+
vos = canfar.vosHandler(self._cmd_ok)
36+
npt.assert_equal(
37+
vos.command.__name__,
38+
self._cmd_ok,
39+
'vos command not set',
40+
)
41+
42+
# Test error for invalid command
43+
with self.assertRaises(ValueError) as context:
44+
vos = canfar.vosHandler(self._cmd_nok)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
test = pytest
33

44
[metadata]
5-
description-file = README.md
5+
description_file = README.md
66

77
[darglint]
88
docstring_style = numpy

0 commit comments

Comments
 (0)