Exemplo n.º 1
0
    def handle(self, *args, **options):

        # Collect parameters
        self.traceback = bool(options.get("traceback", False) ) 
        self.verbosity = int(options.get('verbosity', 1))
        filename       = options.get('filename','-')

        # dataset's (coverages') ids
        rt_list = args

        #----------------------------------------------------------------------
        # load and parse the input data  

        try :  
            
            if filename == "-" : 

                # standard input 
                rts = json.load( sys.stdin ) 

            else : 

                # file input 
                with open(filename,"r") as fin : 
                    rts = json.load( fin ) 

        except IOError as e : 

            # print stack trace if required 
            if self.traceback : 
                self.print_msg(traceback.format_exc())

            raise CommandError( "Failed to open the input file '%s' ! "
                                    "REASON: %s " % ( filename , str(e) ) ) 
            
        #----------------------------------------------------------------------
        # insert the range types to DB 

        success_count = 0 # success counter - counts finished syncs

        for i,rt in enumerate(rts) : 

            # extract RT name 

            rt_name = rt.get('name',None) 

            if not ( isinstance(rt_name, basestring) and rt_name ) : 

                self.print_err( "Range type #%d rejected as it has no valid"
                                " name."%(i+1) ) 
                continue 
            
            if isRangeTypeName( rt_name ): 

                self.print_err( "The name '%s' is already used by another "
                "range type! Import of range type #%d aborted!" \
                        %( rt_name , (i+1) ) )

                continue 

            #------------------------------------------------------------------

            try : 

                # create rangetype record 
                setRangeType( rt ) 
        
                success_count += 1 # increment success counter 

            except Exception as e: 

                # print stack trace if required 
                if self.traceback : 
                    self.print_msg(traceback.format_exc())

                self._error( rt['name'], "%s: %s"%(type(e).__name__, str(e)) )

                continue # continue by next dataset 


            self.print_msg( "Range type '%s' loaded."%rt['name']) 

        #----------------------------------------------------------------------
        # print the final info 
        
        count = len(rts) 
        error_count = count - success_count

        if ( error_count > 0 ) : 
            self.print_msg( "Failed to load %d range types." % (
                error_count ) , 1 )  

        if ( success_count > 0 ) : 
            self.print_msg( "Successfully loaded %d of %s range types." % (
                success_count , count ) , 1 )
        else : 
            self.print_msg( "No range type loaded." ) 
Exemplo n.º 2
0
 def __checkRangeType( rt ) :
     rv = isRangeTypeName( rt )
     if not rv :
         self.print_err( "Invalid range-type identifier '%s' !"%rt )
     return rv
Exemplo n.º 3
0
    def handle(self, *args, **options):
        filename = options.get('filename', '-')

        # load and parse the input data
        try:
            if filename == "-":
                rts = json.load(sys.stdin)
            else:
                with open(filename, "r") as fin:
                    rts = json.load(fin)

        except IOError as exc:
            if self.traceback:
                self.print_msg(traceback.format_exc())
            raise CommandError("Failed to open the input file '%s' ! "
                                    "REASON: %s " % (filename, str(exc)))

        # allow single range-type objects
        if isinstance(rts, dict):
            rts = [rts]

        # insert the range types to DB
        success_count = 0 # success counter - counts finished syncs
        for i, rt in enumerate(rts):
            rt_name = rt.get('name', None)
            if not (isinstance(rt_name, basestring) and rt_name):
                self.print_err("Range type #%d rejected as it has no valid"
                                " name."%(i+1))
                continue

            if isRangeTypeName(rt_name):
                self.print_err("The name '%s' is already used by another "
                "range type! Import of range type #%d aborted!" \
                        %(rt_name, (i+1)))
                continue

            try:
                setRangeType(rt) # create rangetype record
                success_count += 1 # increment success counter

            except Exception as exc:
                if self.traceback:
                    self.print_msg(traceback.format_exc())
                self._error(rt['name'], "%s: %s"%(type(exc).__name__, str(exc)))
                continue # continue by next dataset


            self.print_msg("Range type '%s' loaded."%rt['name'])

        # print the final info

        count = len(rts)
        error_count = count - success_count

        if error_count > 0:
            self.print_msg("Failed to load %d range types." % (
                error_count), 1)

        if success_count > 0:
            self.print_msg("Successfully loaded %d of %s range types." % (
                success_count, count), 1)
        else:
            self.print_msg("No range type loaded.")