Example #1
0
    def convert( self , value , acceptedMobyleType , detectedMobyleType = None , paramFile= False ):
        """
        Do the generals control and cast the value in the right type.
        if a control or the casting fail a MobyleError is raised

        @param value: the value provided by the User for this parameter
        @type value: String
        @param acceptedMobyleType: the MobyleType  accepted by this service parameter 
        @type acceptedMobyleType: L{MobyleType} instance
        @param detectedMobyleType: the MobyleType discribing this data
        @type detectedMobyleType: L{MobyleType} instance
        @return: the fileName ( basename ) of the binary file
        """
        if value is None:
            return None
        elif len( value ) == 5 :
            data , dest , destFileName , src , srcFileName = value
        else:
            raise MobyleError ,"value must be a tuple of 5 elements: ( data , dest , destFileName , src , srcFileName )"
        if destFileName is None:
            return None
        if dest is None:
            raise MobyleError, "the destination is mandatory"
        if  data and src :
            raise MobyleError, "you cannot specify data and src at the same time"
        if not data and ( not dest or not src ) :
            raise MobyleError , "if data is not specified, dest and src must be defined"
        if src and not srcFileName :
            raise MobyleError , "if src is specified, srcFileName must be also specified"
        #safeFileName return a basename
        fileName = safeFileName( destFileName )
        mt = MobyleType( self )
        return ( fileName , mt )
Example #2
0
 def validate( self, param ):
     value = param.getValue()
     if value is None :
         return True
     safefileName = safeFileName( value )
     if safefileName != value:
         msg = "invalid value: %s : the followings characters \:/ ;` {} are not allowed" %( value )
         raise UserValueError( parameter = param , msg = msg )
     else:
         return True
Example #3
0
 def convert( self , value , acceptedMobyleType , detectedMobyleType = None , paramFile= False ):  
     """
     @param acceptedMobyleType: the MobyleType  accepted by this service parameter 
     @type acceptedMobyleType: L{MobyleType} instance
     @param detectedMobyleType: the MobyleType discribing this data
     @type detectedMobyleType: L{MobyleType} instance
     """  
     if value is None:
         return ( None , acceptedMobyleType )
         #raise UserValueError( parameter = param , msg= " this parameter must be a String" )
     fileName = safeFileName( value )
     return ( fileName , acceptedMobyleType )
Example #4
0
 def toFile( self , data , dest , destFileName , src , srcFileName ):
     """
     Write file (of user data) in the working directory .
     @param fileName:
     @type fileName: string
     @param content: the content of the file
     @type content: string
     @return: the name ( absolute path ) of the created file ( could be different from the arg fileName )
     @rtype: string
     @call: L{MobyleJob._fillEvaluator}
     """
     try:
         destSafeFileName = safeFileName( destFileName )
     except UserValueError, err:
         raise UserValueError( msg = "this value : %s is not allowed for a file name, please change it" % destFileName )
Example #5
0
 def toFile( self , data  , dest , destFileName , src , srcFileName ):
     """
     Write file (of user data) in dest directory .
     @param fileName:
     @type fileName: string
     @param data: the content of the file
     @type data: string
     @param dest: the object in which the data will be copied
     @type dest: Job, Session object
     @param src: the object where the data can be found
     @type src: Job, Session object
     @param srcFileName: the file namae of the data in the src ( basename )
     @type srcFileName: string
     @return: the name of the created file (the basename)
     @rtype: string
     @raise: L{UserValueError} when filename is not allowed (for security reason)
     @raise: L{MobyleError} if an error occured during the file creation
     """
     try:
         destSafeFileName = safeFileName( destFileName )            
     except UserValueError, err:
         raise UserValueError( msg = "this value : %s is not allowed for a file name, please change it" % destFileName )
Example #6
0
    def convert( self , value ,acceptedMobyleType , detectedMobyleType = None,  paramFile= False):
        """
        convert the sequence contain in the file fileName in the rigth format
        throws an UnsupportedFormatError if the output format is not supported
        or a MobyleError if something goes wrong during the conversion.

        @param value: is a tuple ( src , srcFileName) 
          - srcfilename is the name of the file to convert in the src
          - src must be a L{Job} instance the conversion are perform only by jobs (not session) .
        @type value: ( L{Job} instance dest, L{Job} instance, src)
        @return: the fileName ( basename ) of the  sequence file and the effective MobyleType associated to this 
        value
        @rtype: ( string fileName , MobyleType instance )
        @raise UnSupportedFormatError: if the data cannot be converted in any suitable format
        """
        if value is None:
            return None
        if len( value ) == 2 :
            src , srcFileName = value
        else:
            raise MobyleError ,"value must be a tuple of 2 elements: ( Job/MobyleJob/JobState or Session instance , Job/MobyleJob/JobState or Session instance , srcFileName )"
        if src and not srcFileName :
            raise MobyleError , "if src is specified, srcFileName must be also specified"
        absFileName = os.path.join( src.getDir() , srcFileName )
        all_converters = _cfg.dataconverter( self.__class__.__name__[:-8] )
        in_format = detectedMobyleType.getDataFormat()
        
        if not all_converters:
            fileName = safeFileName( absFileName )
            if detectedMobyleType :
                mt = detectedMobyleType
            else:
                mt = MobyleType( self )
            #filename is a basename
            return ( fileName , mt )
        else:
            def unknow_converter( in_format , converters ):
                result = None 
                for converter in converters:
                    try:
                        result = fixed_converter( in_format , converter )
                    except UnSupportedFormatError:
                        continue
                    if result is not None:
                        break
                #if I cannot find any suitable converter result is None    
                return result
                
            def fixed_converter( in_format ,  converter ):
                allowed_conversions = detector_used.convertedFormat()
                for out_format , force in acceptedMobyleType.getAcceptedFormats():
                    if ( in_format , out_format ) in allowed_conversions:
                        try:
                            outFileName = converter.convert( absFileName , out_format , inputFormat = in_format )
                            outFileName = os.path.basename( outFileName )
                        except UnSupportedFormatError, err:
                            raise UserValueError( msg = "a problem occurred during the data conversion : "+str( err ) ) 
                        converted_mt = MobyleType( self  , dataFormat = out_format , format_program = detector_used.program_name )
                        return ( outFileName , converted_mt )
                #it's not a suitable converter
                return None
            
            if detectedMobyleType.format_program is None:
                result = unknow_converter( in_format , all_converters )
            else:
                for converter in all_converters:
                    if converter.program_name == detectedMobyleType.format_program :
                        detector_used = converter
                        break
                if detector_used is None:
                    raise MobyleError( "unable to find %s converter" % detectedMobyleType.format_program )
                result  = fixed_converter( in_format , detector_used )
                if result is None:# the detector_used is not suitable for the conversion
                    result= unknow_converter( in_format , [ c for c in all_converters if c != detector_used ] )
                    
            if result is None:
                raise UnSupportedFormatError( "unable to convert %s in %s sequence format" %( in_format , 
                                                                                              [ f[0] for f in acceptedMobyleType.getAcceptedFormats() ] 
                                                                                               ) )
            else:
                return result #( outFileName , converted_mt )