Example #1
0
    def generateAndCompileXML( self ) :
        '''Generates XML content for the various snippets and compiles them.'''

        xml = [ '<workflow>' ]
        
        snipID = 1
        inputID = 1


        if self.workflow[0][0].inputNeeded  :
            name = QtGui.QInputDialog.getText( self, 'Enter \'%s\' ...'%self.workflow[0][0].details[0], self.workflow[0][0].details[0] )
            if not name[1] :
                return
            first = str( name[0] )
            if first == None :
                first = ''
        else :
            first = ''
        self.workflow[0][1][0] = first



        for i in self.workflow :
            #print i[1]
            xml = xmlgenerator.getxml( xml, i[0], i[1], snipID, inputID )

            if len( xml ) > 5 and xml[:5] == list( 'ERROR' ) :
                QtGui.QMessageBox.warning( self, 'Error !', ''.join( xml ) + ' \n Say Done again.' )
                return -1

            snipID += 1
            inputID += len( i[1] )


        xml.append( '</workflow>')

        outputFile = str( QtGui.QFileDialog.getSaveFileName( parent = self, caption = "Select where to store workflow ... " ) )
        if not outputFile :
            return
        outputFile = '.'.join( outputFile.split( '.' )[:-1] ) + '.py'

        #print 'XML written'

        xName = str( tempfile.mkstemp( dir=BaseDirectory.xdg_cache_home, suffix='.xml' )[0] )
        xmlFile = open( xName, 'w' )
        xmlFile.write( ''.join( xml ) )
        xmlFile.close()
    
        xmlcompiler.compile( xmlFile.name, outputFile )
        print xName

        #print 'Script generated'
        os.remove( xName )
        sys.exit( 0 )
Example #2
0
def main():
    print """Enter a snippet number to begin.
To end, enter a negative snippet number.
Please try and use absolute paths whereever possible.
Relative paths are relative to the main Shellom directory, from where main.py has been run."""

    s = filter(lambda x: x[0] != "_" and x != "os" and x != "sys" and x != "module", dir(snippets))
    print "-" * 50

    oSnip = zip(range(len(s)), s)
    soSnip = map(str, oSnip)

    print "\n".join(soSnip)

    while 1:
        try:
            choice = int(raw_input("... "))
            break
        except ValueError:
            pass

    snipID = 1
    inputID = 1
    xml = ["<workflow>"]

    if not os.access("tmp", os.W_OK):
        os.mkdir("tmp")

    while choice >= 0:

        currentSnippet = getattr(getattr(snippets, oSnip[choice][1]), oSnip[choice][1])
        inputsToBeGot = currentSnippet.tags
        currentInputs = []

        for i in currentSnippet.packages:
            if os.system("dpkg -l | awk '{ print $2 }' | tail -n +6 | grep %s > /dev/null" % i) != 0:
                print i + " missing. Install the package before using this snippet."
                sys.exit(-1)

        lists = []
        more = 0
        for i in range(len(inputsToBeGot)):
            ioro = "i"
            if i == len(inputsToBeGot) - 1:
                ioro = "o"
            # currentInputs.append(
            newInput = raw_input("%s%d - %s ... " % (ioro, inputID + i + more, currentSnippet.details[i]))

            if len(newInput) > 4 and newInput[:4] == "list":
                lists.append(i)
                newInput = newInput[4:].strip().split(", ")
                newInput[0] = newInput[0][1:].strip()
                newInput[-1] = newInput[-1][:-2].strip()
                more += len(newInput) - 1

            currentInputs.append(newInput)

        if len(lists) > 0:
            lenLists = len(currentInputs[lists[0]])
        else:
            lenLists = 0

        for i in lists:
            if lenLists != len(currentInputs[i]):
                print "Lists incompatible. Exiting ..."
                sys.exit(-1)

        if len(lists) > 0 and lists[-1] != len(inputsToBeGot) - 1:
            print "The last input must be a list. Exiting."
            sys.exit(-1)
        if lenLists != 0:
            notLists = list(set(range(len(inputsToBeGot))).difference(set(lists)))
            notLists.sort()

            thisInput = [0] * len(inputsToBeGot)
            for i in notLists:
                thisInput[i] = currentInputs[i]

            for i in range(lenLists):
                for j in lists:
                    thisInput[j] = currentInputs[j][i]
                xml = xmlgenerator.getxml(xml, currentSnippet, thisInput, snipID, inputID)
                inputID += len(inputsToBeGot)
        #                snipID += 1
        else:
            xml = xmlgenerator.getxml(xml, currentSnippet, currentInputs, snipID, inputID)

        inputID += len(inputsToBeGot) + more
        print (xml)

        if xml[:5] == list("ERROR"):
            print "".join(xml)
            sys.exit(1)

        print "-" * 50
        while 1:
            try:
                choice = int(raw_input("... "))
                snipID += 1
                break
            except ValueError:
                pass
    #        choice=int( raw_input( '... ' ) )
    #        snipID+=1

    xml.append("</workflow>")

    xmlFile = open("tmp/workflow.xml", "w")
    xmlFile.write("".join(xml))
    xmlFile.close()

    xmlcompiler.compile("tmp/workflow.xml", "tmp/workflow.py")
Example #3
0
    def submitInputs( self ) :
        '''This function adds the current snippet to the XML.'''

        #if self.snippetSelected == 0  or self.curSnip == '' :
        #    return -2

        # Make sure that a snippet is selected
        selection = str( self.inputsList.topLevelItem( self.inputsList.topLevelItemCount() - 1 ).text( 0 ) )
        snippet = getattr( getattr( snippets, selection ), selection )

        currentInputs, lists, toBeShown = self.getInputs()


        if len( lists ) > 0 :
            lenLists = len( currentInputs[ lists[0] ] )
        else :
            lenLists = 0
        
        # Make sure that if there are lists, they are all of the same length
        for i in lists :
            if lenLists != len( currentInputs[i] ) :
                QtGui.QMessageBox.warning( self,
                        'Error', 'Lists entered are of different lengths. Try correcting them.' )
                return -2
        
        #If there are lists in the inputs :
        if lenLists != 0 :
            notLists = list( set( range( len( toBeShown[0] ) ) ).difference( set( lists ) ) )
            notLists.sort()
        
            thisInput = [0] * len( toBeShown[0] )
            for i in notLists :
                thisInput[i] = currentInputs[i]

            # Make a copy of the XML so that if an input from a list is wrong,
            # we can reject all previous inputs from the list.
            copy = []
            copy.extend( self.xml )
        
            for i in range( lenLists ) :
                for j in lists :
                    thisInput[j] = currentInputs[j][i]
                tmp = xmlgenerator.getxml( copy, snippet, thisInput, self.snipID, self.inputID )

                # I still don't know the cause of this bug :
                # *  thisInput contained a list of lists of inputs formed from the
                #    lists. But when appended to toBeShown, they just contained
                #    duplicates of the last list.append
                # *  SOLUTION - I use a copy of thisInput called debug
                debug = []
                debug.extend( thisInput )
                toBeShown.append( debug )
                
                # The input was invalid
                if tmp[:5] == list( 'ERROR' ) :
                    QtGui.QMessageBox.warning( self,
                            'Error', ''.join( tmp ) + '. Try rechecking your input. Inputs that preceeded this one in the list have not been processed.' )

                    #Revert to proper IDs
                    self.snipID = self.oldSnipID
                    self.inputID = self.oldInputID
                    return -2
                # _This_ set of inputs from the lists is valid
                else :
                    copy = tmp
                    self.snipID += 1
                    self.inputID += len( toBeShown[0] )

            self.setTreeProperties( toBeShown )
            self.xml = copy

            # Make necessary changes to old IDs
            self.oldSnipID = self.snipID + 1
            self.oldInputID = self.inputID + 1

        # There is a single input :
        else :
            copy = []
            copy.extend( self.xml )
            tmp = xmlgenerator.getxml( copy, snippet, currentInputs, self.snipID, self.inputID )
            if tmp[:5] == list( 'ERROR' ) :
                QtGui.QMessageBox.warning( self, 'Error !', ''.join( tmp ) + '. Try rechecking your inputs' )
                #self.clear()
                return -2
            else :
                self.xml = tmp
                self.snipID += 1
                self.inputID += len( currentInputs )
                self.setTreeProperties( [currentInputs] )

            self.oldSnipID = self.snipID + 1
            self.oldInputID = self.inputID + 1



        print ''.join( self.xml ), '\n\n\n\n\n\n'