Skip to content

Commit 349ab44

Browse files
committed
initial tests + docstrings
1 parent 7d5b098 commit 349ab44

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

qiita_client/qiita_client.py

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# -----------------------------------------------------------------------------
88

99
import os
10+
import shutil
1011
import time
1112
import requests
1213
import threading
@@ -742,11 +743,53 @@ def _process_files_per_sample_fastq(self, files, prep_info,
742743

743744
return sample_names, prep_info
744745

745-
def fetch_file_from_central(self, filepath):
746+
def fetch_file_from_central(self, filepath, prefix=''):
747+
"""Moves content of a file from Qiita's central base_data_dir to a
748+
local plugin file-system.
749+
750+
By default, this is exactly the same location, i.e. the return
751+
filepath is identical to the requested one and nothing is moved /
752+
copied.
753+
However, for less tight plugin couplings, file content can be
754+
transferred via https for situations where the plugin does not have
755+
native access to Qiita's overall base_data_dir.
756+
757+
Parameters
758+
----------
759+
filepath : str
760+
The filepath in Qiita's central base_data_dir to the requested
761+
file content
762+
prefix : str
763+
Primarily for testing: prefix the target filepath with this
764+
filepath prefix to
765+
a) in 'filesystem' mode: create an actual file copy (for testing)
766+
If prefix='', nothing will be copied/moved
767+
b) in 'https' mode: flexibility to locate files differently in
768+
plugin local file system.
769+
770+
Returns
771+
-------
772+
str : the filepath of the requested file within the local file system
773+
"""
774+
target_filepath = filepath
775+
if prefix != '':
776+
# strip off root
777+
if filepath.startswith(os.path.abspath(os.sep)):
778+
target_filepath = target_filepath[
779+
len(os.path.abspath(os.sep)):]
780+
# prefix filepath with given prefix
781+
target_filepath = os.path.join(prefix, target_filepath)
782+
746783
if self._plugincoupling == 'filesystem':
747-
return filepath
784+
if prefix != '':
785+
# create necessary directory locally
786+
os.makedirs(os.path.dirname(target_filepath), exist_ok=True)
787+
788+
shutil.copyfile(filepath, target_filepath)
789+
790+
return target_filepath
748791

749-
if self._plugincoupling == 'https':
792+
elif self._plugincoupling == 'https':
750793
logger.debug('Requesting file %s from qiita server.' % filepath)
751794

752795
# actual call to Qiita central to obtain file content
@@ -755,23 +798,24 @@ def fetch_file_from_central(self, filepath):
755798
rettype='content')
756799

757800
# create necessary directory locally
758-
os.makedirs(os.path.dirname(filepath), exist_ok=True)
801+
os.makedirs(os.path.dirname(target_filepath), exist_ok=True)
759802

760803
# write retrieved file content
761-
with open(filepath, 'wb') as f:
804+
with open(target_filepath, 'wb') as f:
762805
f.write(content)
763806

764-
return filepath
807+
return target_filepath
765808

766-
raise ValueError(
767-
("File communication protocol '%s' as defined in plugins "
768-
"configuration is NOT defined.") % self._plugincoupling)
809+
else:
810+
raise ValueError(
811+
("File communication protocol '%s' as defined in plugins "
812+
"configuration is NOT defined.") % self._plugincoupling)
769813

770814
def push_file_to_central(self, filepath):
771815
if self._plugincoupling == 'filesystem':
772816
return filepath
773817

774-
if self._plugincoupling == 'https':
818+
elif self._plugincoupling == 'https':
775819
logger.debug('Submitting file %s to qiita server.' % filepath)
776820

777821
self.post(

qiita_client/tests/test_qiita_client.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# -----------------------------------------------------------------------------
88

99
from unittest import TestCase, main
10+
import filecmp
1011
from os import remove, close
1112
from os.path import basename, exists
1213
from tempfile import mkstemp
@@ -389,12 +390,37 @@ def test_fetch_file_from_central(self):
389390
self.tester._plugincoupling = 'filesystem'
390391

391392
ainfo = self.tester.get("/qiita_db/artifacts/%s/" % 1)
392-
print("STEFAN", ainfo)
393+
fp = ainfo['files']['raw_forward_seqs'][0]['filepath']
393394

394-
# fp_query = '/home/runner/work/qiita/qiita/qiita_db/support_files/
395-
# test_data/templates/FASTA_QUAL_preprocessing_sample_template.txt'
396-
# fp_res = fetch_file_from_central('')
397-
pass
395+
# mode: filesystem, prefix='': no copy, directly return given fp
396+
fp_obs = self.tester.fetch_file_from_central(fp)
397+
self.assertEqual(fp, fp_obs)
398+
399+
# mode: filesystem, prefix='/karl': make file copy
400+
self.clean_up_files.append('/karl' + fp)
401+
fp_obs = self.tester.fetch_file_from_central(fp, prefix='/karl')
402+
self.assertEqual('/karl' + fp, fp_obs)
403+
self.assertTrue(filecmp.cmp(fp, fp_obs, shallow=False))
404+
405+
# non existing mode
406+
with self.assertRaises(ValueError):
407+
self.tester._plugincoupling = 'foo'
408+
self.tester.fetch_file_from_central(fp)
409+
410+
def test_push_file_to_central(self):
411+
self.tester._plugincoupling = 'filesystem'
412+
413+
ainfo = self.tester.get("/qiita_db/artifacts/%s/" % 1)
414+
fp = ainfo['files']['raw_forward_seqs'][0]['filepath']
415+
416+
# mode: filesystem
417+
fp_obs = self.tester.push_file_to_central(fp)
418+
self.assertEqual(fp, fp_obs)
419+
420+
# non existing mode
421+
with self.assertRaises(ValueError):
422+
self.tester._plugincoupling = 'foo'
423+
self.tester.push_file_to_central(fp)
398424

399425

400426
if __name__ == '__main__':

0 commit comments

Comments
 (0)