77# -----------------------------------------------------------------------------
88
99import os
10+ import shutil
1011import time
1112import requests
1213import 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 (
0 commit comments