1import sys
2import u2py_internals
3import serializer
4
5setattr( sys.modules[__name__], 'releaseContext', u2py_internals.releaseContext )
6
7def initContext( workingDirectory ) :
8    return u2py_internals.initContext( serializer.getUnixAbsPath( workingDirectory ) )
9
10class Scheme( u2py_internals.Scheme ) :
11    def __init__( self, *args ) :
12        argCount = len( args )
13        if 1 == argCount :
14            schemeFilePath = serializer.getUnixAbsPath( args[0] )
15            super( Scheme, self ).__init__( unicode( schemeFilePath ) )
16        elif 1 < argCount :
17            algorithmName = args[0]
18            inputData = self._serializer.bioListToString( args[1], True )
19            outputData = self._serializer.createTmpFileAndGetItsPath( )
20            if 3 == argCount :
21                outputData = serializer.getUnixAbsPath( args[2] )
22            elif 3 < argCount :
23                raise TypeError( 'expected 3 or less arguments' )
24            super( Scheme, self ).__init__( unicode( algorithmName ), unicode( inputData ), unicode( outputData ) )
25        else :
26            super( Scheme, self ).__init__( )
27
28    def addReader( self, readerName, inputData ) :
29        """
30        Add to the computational scheme a new read element of the supplied type,
31        set its input data and get the element's name
32        """
33        inputString = self._serializer.bioListToString( inputData, True )
34        return super( Scheme, self ).addReader( unicode( readerName ), inputString )
35
36    def addWriter( self, writerName, outputPath ) :
37        """
38        Add to the computational scheme a new write element of the supplied type,
39        set its output file path and get the element's name
40        """
41        absPath = serializer.getUnixAbsPath( outputPath )
42        return super( Scheme, self ).addWriter( unicode( writerName ), absPath )
43
44    def setElementAttribute( self, elementName, attributeName, attributeValue ) :
45        """"
46        Specify the given attribute of the supplied element
47        """
48        treatStringsAsPaths = True
49        if not ( 'url-in' == attributeName or 'url-out' == attributeName ) :
50            treatStringsAsPaths = False
51        stringValue = self._serializer.bioListToString( attributeValue, treatStringsAsPaths )
52        super( Scheme, self ).setElementAttribute( unicode( elementName ), unicode( attributeName ), stringValue )
53
54    def getElementAttribute( self, elementName, attributeName ) :
55        """"
56        Get the value of the given element's attribute
57        """
58        multivalueAttributeDelimeter = ';'
59        attributeValue = super( Scheme, self ).getElementAttribute( unicode( elementName ), unicode( attributeName ) )
60        if multivalueAttributeDelimeter in attributeValue :
61            valuesList = attributeValue.split( multivalueAttributeDelimeter )
62            for index, value in enumerate( valuesList ) :
63                if value in self._serializer.createdFiles :
64                    valuesList[index] = self._serializer.createdFiles[value]
65            return valuesList
66        elif attributeValue in self._serializer.createdFiles :
67            return self._serializer.createdFiles[attributeValue]
68        else :
69            return attributeValue
70
71    def launch( self ) :
72        """
73        Run the scheme synchronously by the Workflow Designer
74        """
75        resultList = super( Scheme, self ).launch( )
76        return self._serializer.stringListToBioList( resultList )
77
78    @staticmethod
79    def launchSas( *args ) :
80        """
81        Create and run the Single Algorithm Scheme synchronously by the Workflow Designer
82        """
83        tempSerializer = serializer.Serializer( )
84        argumentCount = len( args )
85        if not 2 <= argumentCount <= 3 :
86            raise RuntimeError( 'expected 2 or 3 arguments' )
87        algorithmName = args[0]
88        outputString = unicode( '' )
89        if 2 == argumentCount :
90            outputString = tempSerializer.createTmpFileAndGetItsPath( )
91        elif 3 == argumentCount :
92            outputString = serializer.getUnixAbsPath( args[2] )
93        inputString = tempSerializer.bioListToString( args[1], True )
94        resultList = u2py_internals.Scheme.launchSas( unicode( algorithmName ), inputString, outputString )
95        if 2 == argumentCount :
96            resultList = tempSerializer.stringListToBioList( resultList )
97        tempSerializer.cleanUp( )
98        return resultList
99
100    def cleanUp( self ) :
101        """
102        Removes all temporary files created during the scheme execution
103        """
104        self._serializer.cleanUp( )
105
106    _serializer = serializer.Serializer( )