Source code for hylite.Generic_wrapper

#!/usr/bin/env python3

#    (c) Copyright 2013-2018 Murray Cox, Wandrille Duchemin, Pierre-Yves Dupont.
#
#
#    This file is part of HyLiTE.
#
#    HyLiTE is a free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3 as published by
#    the Free Software Foundation.
#
#    HyLiTE is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with HyLiTE.  If not, see <http://www.gnu.org/licenses/>


#===============================#
# author: Wandrille Duchemin    #
#         Murray Cox            #
# last modified: 9 January 2018 #
#===============================#


import os
import sys
from subprocess import PIPE, Popen
import shlex
from subprocess import getoutput
from .Parameters import Parameters

[docs]class Generic_wrapper: '''This abstract class represent a wrapper Attributes: - path (str): the path of the software - basename (dict): keys are of the software function, values are the corresponding command - option (dict): keys are the options name, values are their usage in the command line ''' def __init__(self, path, basename, options): ''' This abstract class represent a wrapper Args: - path (str): a string containing the path to the software - basename (dict): a dictionnary of the different commands used (key is command name, value is the executable name) - options (dict): a dictionnary containing the different options (key are option name, value are option) ''' self.path = path self.basename = basename self.options = options return
[docs] def build_command_line(self, basename, options_value, order): '''Build the command line that the *run* method will use. Args: - basename (str):the name of command he want to use - options_value (dict): a dictionnary containing the options he want to use: the key are the option name, the value are the one associated with the option - order (list): a list containing the ordered name of options (order of appearance in the command line) Example: If we consider a class grep_wrapper inheriting from this class. basename would comprise only {'grep':'grep'} and option could be something like {'inverse':'-v','count':'-c','pattern':'','input':''}. To build a grep command, the user would have to provide the basename 'grep' and a dictionnary of option as follow: DICT RESULTING GREP COMMAND {'pattern':'"^>"','input':'file1.fasta'} grep "^>" file1.fasta {'count':True,'pattern':'"^>"','input':'file1.fasta'} grep -c "^>" file1.fasta {'inverse':False,'count':True,'pattern':'"^>"','input':'file1.fasta'} grep -c "^>" file1.fasta ''' commandline = [self.path, self.basename[basename]] for opt in order: if options_value[opt] is False: continue elif options_value[opt] is True: commandline.append(str(self.options[opt])) else: commandline.append(str(self.options[opt])) commandline.append(str(options_value[opt])) commandline=' '.join(commandline) if os.name=='nt': return commandline else: #we have to cut it to pieces (otherwise Popen won't work) return shlex.split(commandline)
[docs] def run(self): ''' Execute the commandline NB: as this is an abstract class, it does not contain an attribute command line and will thus raise an error if directly executed. Any inheriting class have to define a commandline attribute Returns: - Popen. Popen of the PIPED command Raises: - AttributeError Exception. if you haven't defined a command line before ''' if Parameters().get_param('HIGH_LEVEL_VERBOSE'): try: s = inspect.stack()[1] except NameError: import inspect s = inspect.stack()[1] print("***Called by: %s (line %s) function: %s" % (os.path.basename(s[1]), s[2], s[3])) print(" ".join(self.commandline)) try : # print self.commandline p = Popen(self.commandline, stdout=PIPE, stderr=PIPE, stdin=PIPE) return p except AttributeError as e: sys.stderr.write(e+'\n')
[docs] def print_command_line(self): '''Print the current command line Raises: - AttributeError Exception. if you haven't defined a command line before ''' try: if os.name=='nt': sys.stdout.write(self.commandline+'\n') else: sys.stdout.write(' '.join(self.commandline)+'\n') except AttributeError as e: sys.stderr.write(e+'\n') return