Exemplo n.º 1
0
    def handle(self, *args, **options):
        print_json = bool(options.get('json_dump', False))
        filename = options.get('filename', '-')
        rt_list = args # list of range-type identifiers

        if not rt_list:
            # if no range-type name specified get all of them
            rt_list = getAllRangeTypeNames()

        else:
            # filter existing range-type names
            def __checkRangeType(rt):
                rv = isRangeTypeName(rt)
                if not rv:
                    self.print_err("Invalid range-type identifier '%s' !"%rt)
                return rv
            rt_list = [rt for rt in rt_list if __checkRangeType(rt)]

        # select the right output format
        if print_json:
            output = OutputJSON
        else:
            output = OutputBrief

        def _write_out(fout):
            """ Write the output."""
            fout.write(output.lead())
            for i, rt_name in enumerate(rt_list):
                if i > 0:
                    fout.write(output.separator())
                fout.write(output.object(rt_name))
            fout.write(output.trail())

        try:
            if filename == "-":
                _write_out(sys.stdout)
            else:
                with open(filename, "w") as fout:
                    _write_out(fout)

        except IOError as exc:
            raise CommandError("Failed to write to file '%s'!"
                                " REASON: %s" % (filename, str(exc)))
Exemplo n.º 2
0
    def handle(self, *args, **options):

        # Collect parameters

        self.verbosity  = int(options.get('verbosity', 1))

        print_details   = bool(options.get('details',False))

        print_json      = bool(options.get('json_dump',False))

        filename        = options.get('filename','-')

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

        #----------------------------------------------------------------------
        # check the input rangetype names

        if not rt_list :

            # if no IDs specified get all identifiers

            rt_list = getAllRangeTypeNames()

        else :

            # filter existing range-type names

            def __checkRangeType( rt ) :
                rv = isRangeTypeName( rt )
                if not rv :
                    self.print_err( "Invalid range-type identifier '%s' !"%rt )
                return rv

            rt_list = filter( __checkRangeType , rt_list )

        #----------------------------------------------------------------------
        # output

        # select the right output driver 

        if print_json :         output = OutputJSON
        elif print_details :    output = OutputDetailed 
        else :                  output = OutputBrief 


        # write the output 

        def _write_out( fout ) : 
            fout.write( output.lead() ) 
            for i,rt_name in enumerate(rt_list) :
                if i > 0 : fout.write( output.separator() )
                fout.write( output.object( rt_name ) ) 
            fout.write( output.trail() ) 

        # output file 
        try :  

            if filename == "-" : 

                # write to stdout 
                _write_out( sys.stdout ) 

            else : 
                
                # write to a file 
                with open(filename,"w") as fout :
                    _write_out( fout )

        except IOError as e : 

            raise CommandError( "Failed to open the output file '%s' ! "
                    "REASON: %s" % ( filename , str(e) ) )