|
| 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._avail_commands = tuple(vosc.__all__) |
| 43 | + self.command = command |
| 44 | + |
| 45 | + @property |
| 46 | + def command(self): |
| 47 | + """Set Command. |
| 48 | +
|
| 49 | + This method sets the VOS command property. |
| 50 | +
|
| 51 | + Raises |
| 52 | + ------ |
| 53 | + ValueError |
| 54 | + if value is not valid vos command |
| 55 | +
|
| 56 | + """ |
| 57 | + return self._command |
| 58 | + |
| 59 | + @command.setter |
| 60 | + def command(self, value): |
| 61 | + |
| 62 | + if value not in self._avail_commands: |
| 63 | + raise ValueError( |
| 64 | + f'vos command must be one of {self._avail_commands}' |
| 65 | + ) |
| 66 | + |
| 67 | + self._command = getattr(vosc, value) |
| 68 | + |
| 69 | + def __call__(self, *args, **kwargs): |
| 70 | + """Call Method. |
| 71 | +
|
| 72 | + This method allows class instances to be called as functions. |
| 73 | +
|
| 74 | + Raises |
| 75 | + ------ |
| 76 | + vosError |
| 77 | + if error in vos command occurs |
| 78 | +
|
| 79 | + """ |
| 80 | + try: |
| 81 | + self._command() |
| 82 | + |
| 83 | + except Exception: |
| 84 | + raise vosError( |
| 85 | + f'Error in VOs command: {self._command.__name__}' |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def download(source, target, verbose=False): |
| 90 | + """Download. |
| 91 | +
|
| 92 | + Download file from vos. |
| 93 | +
|
| 94 | + Parameters |
| 95 | + ---------- |
| 96 | + source : str |
| 97 | + source path on vos |
| 98 | + target : str |
| 99 | + target path |
| 100 | + verbose : bool, optional, default=False |
| 101 | + verbose output if True |
| 102 | +
|
| 103 | + Returns |
| 104 | + ------- |
| 105 | + status : bool |
| 106 | + status, True/False or success/failure |
| 107 | +
|
| 108 | + """ |
| 109 | + cmd = 'vcp' |
| 110 | + |
| 111 | + if not os.path.exists(target): |
| 112 | + sys.argv = [cmd, source, target] |
| 113 | + if verbose: |
| 114 | + print(f'Downloading file {source} to {target}...') |
| 115 | + vcp = vosHandler(cmd) |
| 116 | + |
| 117 | + vcp() |
| 118 | + if verbose: |
| 119 | + print('Download finished.') |
| 120 | + else: |
| 121 | + if verbose: |
| 122 | + print(f'Target file {target} exists, skipping download.') |
| 123 | + |
| 124 | + |
| 125 | +def dir_list(path, verbose=False): |
| 126 | + """Set Directory List. |
| 127 | +
|
| 128 | + List content of path on vos. |
| 129 | +
|
| 130 | + Parameters |
| 131 | + ---------- |
| 132 | + path : str |
| 133 | + path on vos, starts with 'vos:cfis/...' |
| 134 | + verbose : bool, optional, default=False |
| 135 | + verbose output if True |
| 136 | +
|
| 137 | + Raises |
| 138 | + ------ |
| 139 | + HTTPError, KeyError |
| 140 | + if error occurs during vos command |
| 141 | +
|
| 142 | + Returns |
| 143 | + ------- |
| 144 | + list |
| 145 | + file or directory at path, type is str |
| 146 | +
|
| 147 | + """ |
| 148 | + cmd = 'vls' |
| 149 | + sys.argv = [cmd, path] |
| 150 | + vls = vosHandler(cmd) |
| 151 | + |
| 152 | + if verbose: |
| 153 | + print('Getting vos directory content from vls...') |
| 154 | + |
| 155 | + f = StringIO() |
| 156 | + |
| 157 | + try: |
| 158 | + with redirect_stdout(f): |
| 159 | + vls() |
| 160 | + except Exception: |
| 161 | + print('Error during vls command') |
| 162 | + raise |
| 163 | + |
| 164 | + vls_out = f.getvalue() |
| 165 | + |
| 166 | + return vls_out.split('\n') |
0 commit comments