Skip to content

Commit a04beeb

Browse files
committed
add a configurable parameter to define plugincoupling for plugin
1 parent fefc02b commit a04beeb

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

qiita_client/plugin.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def __init__(self, name, description, can_be_submitted_to_ebi,
136136

137137

138138
class BaseQiitaPlugin(object):
139-
def __init__(self, name, version, description, publications=None, plugincoupling='filesystem'):
139+
_ALLOWED_PLUGIN_COUPLINGS = ['filesystem', 'https'] # default must be first element
140+
def __init__(self, name, version, description, publications=None, plugincoupling=_ALLOWED_PLUGIN_COUPLINGS[0]):
140141
logger.debug('Entered BaseQiitaPlugin.__init__()')
141142
self.name = name
142143
self.version = version
@@ -160,6 +161,8 @@ def __init__(self, name, version, description, publications=None, plugincoupling
160161
# Actually, all files need to be decorated with this function. The decision how
161162
# data are transferred is then made within these two functions according to the
162163
# "plugincoupling" setting.
164+
if plugincoupling not in self._ALLOWED_PLUGIN_COUPLINGS:
165+
raise ValueError("valid plugincoupling values are ['%s'], but you provided %s" % ("', '".join(self._ALLOWED_PLUGIN_COUPLINGS), plugincoupling))
163166
self.plugincoupling = plugincoupling
164167

165168
# Will hold the different commands
@@ -170,7 +173,7 @@ def __init__(self, name, version, description, publications=None, plugincoupling
170173
'QIITA_PLUGINS_DIR', join(expanduser('~'), '.qiita_plugins'))
171174
self.conf_fp = join(conf_dir, "%s_%s.conf" % (self.name, self.version))
172175

173-
def generate_config(self, env_script, start_script, server_cert=None):
176+
def generate_config(self, env_script, start_script, server_cert=None, plugin_couling=_ALLOWED_PLUGIN_COUPLINGS[0]):
174177
"""Generates the plugin configuration file
175178
176179
Parameters
@@ -184,6 +187,9 @@ def generate_config(self, env_script, start_script, server_cert=None):
184187
If the Qiita server used does not have a valid certificate, the
185188
path to the Qiita certificate so the plugin can connect over
186189
HTTPS to it
190+
plugin_coupling : str
191+
Type of coupling of plugin to central for file exchange.
192+
Valid values are 'filesystem' and 'https'.
187193
"""
188194
logger.debug('Entered BaseQiitaPlugin.generate_config()')
189195
sr = SystemRandom()
@@ -198,7 +204,7 @@ def generate_config(self, env_script, start_script, server_cert=None):
198204
env_script, start_script,
199205
self._plugin_type, self.publications,
200206
server_cert, client_id, client_secret,
201-
self.plugincoupling))
207+
plugin_couling))
202208

203209
def _register_command(self, command):
204210
"""Registers a command in the plugin
@@ -208,8 +214,7 @@ def _register_command(self, command):
208214
command: QiitaCommand
209215
The command to be added to the plugin
210216
"""
211-
logger.debug(
212-
f'Entered BaseQiitaPlugin._register_command({command.name})')
217+
logger.debug('Entered BaseQiitaPlugin._register_command(%s)' % command.name)
213218
self.task_dict[command.name] = command
214219

215220
def _register(self, qclient):
@@ -271,7 +276,8 @@ def __call__(self, server_url, job_id, output_dir):
271276
# this value will prevent underlying libraries
272277
# from validating the server's cert using
273278
# certifi's pem cache.
274-
ca_cert=config.get('oauth2', 'SERVER_CERT'))
279+
ca_cert=config.get('oauth2', 'SERVER_CERT'),
280+
plugincoupling=config.get('network', 'PLUGINCOUPLING'))
275281

276282
if job_id == 'register':
277283
self._register(qclient)
@@ -335,7 +341,7 @@ class QiitaTypePlugin(BaseQiitaPlugin):
335341

336342
def __init__(self, name, version, description, validate_func,
337343
html_generator_func, artifact_types, publications=None,
338-
plugincoupling='filesystem'):
344+
plugincoupling=BaseQiitaPlugin._ALLOWED_PLUGIN_COUPLINGS[0]):
339345
super(QiitaTypePlugin, self).__init__(name, version, description,
340346
publications=publications,
341347
plugincoupling=plugincoupling)
@@ -400,9 +406,12 @@ def register_command(self, command):
400406
START_SCRIPT = %s
401407
PLUGIN_TYPE = %s
402408
PUBLICATIONS = %s
403-
PLUGINCOUPLING = %s
404409
405410
[oauth2]
406411
SERVER_CERT = %s
407412
CLIENT_ID = %s
408-
CLIENT_SECRET = %s"""
413+
CLIENT_SECRET = %s
414+
415+
[network]
416+
PLUGINCOUPLING = %s
417+
"""

qiita_client/qiita_client.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class QiitaClient(object):
183183
get
184184
post
185185
"""
186-
def __init__(self, server_url, client_id, client_secret, ca_cert=None):
186+
def __init__(self, server_url, client_id, client_secret, ca_cert=None, plugincoupling='filesystem'):
187187
self._server_url = server_url
188188
self._session = requests.Session()
189189

@@ -219,6 +219,8 @@ def __init__(self, server_url, client_id, client_secret, ca_cert=None):
219219
self._token = None
220220
self._fetch_token()
221221

222+
self._plugincoupling = plugincoupling
223+
222224
def _fetch_token(self):
223225
"""Retrieves an access token from the Qiita server
224226
@@ -732,24 +734,36 @@ def _process_files_per_sample_fastq(self, files, prep_info,
732734
return sample_names, prep_info
733735

734736
def fetch_file_from_central(self, filepath):
735-
logger.debug('Requesting file %s from qiita server.' % filepath)
737+
if self._plugincoupling == 'filesystem':
738+
return filepath
739+
740+
if self._plugincoupling == 'https':
741+
logger.debug('Requesting file %s from qiita server.' % filepath)
742+
743+
# actual call to Qiita central to obtain file content
744+
content = self.get('/cloud/fetch_file_from_central/' + filepath, rettype='content')
736745

737-
# actual call to Qiita central to obtain file content
738-
content = self.get('/cloud/fetch_file_from_central/' + filepath, rettype='content')
746+
# create necessary directory locally
747+
os.makedirs(os.path.dirname(filepath), exist_ok=True)
739748

740-
# create necessary directory locally
741-
os.makedirs(os.path.dirname(filepath), exist_ok=True)
749+
# write retrieved file content
750+
with open(filepath, 'wb') as f:
751+
f.write(content)
742752

743-
# write retrieved file content
744-
with open(filepath, 'wb') as f:
745-
f.write(content)
753+
return filepath
746754

747-
return filepath
755+
raise ValueError("File communication protocol '%s' as defined in plugins configuration is NOT defined." % self._plugincoupling)
748756

749757
def push_file_to_central(self, filepath):
750-
logger.debug('Submitting file %s to qiita server.' % filepath)
758+
if self._plugincoupling == 'filesystem':
759+
return filepath
760+
761+
if self._plugincoupling == 'https':
762+
logger.debug('Submitting file %s to qiita server.' % filepath)
763+
764+
self.post('/cloud/push_file_to_central/',
765+
files={os.path.dirname(filepath): open(filepath, 'rb')})
751766

752-
self.post('/cloud/push_file_to_central/',
753-
files={os.path.dirname(filepath): open(filepath, 'rb')})
767+
return filepath
754768

755-
return filepath
769+
raise ValueError("File communication protocol '%s' as defined in plugins configuration is NOT defined." % self._plugincoupling)

0 commit comments

Comments
 (0)