def process(self, input_args):
        parser = argparse.ArgumentParser()
        parser.add_argument('--cmd', dest='cmd', help='Command', default=Rest.CMD_LIST, choices=Rest.COMMANDS)
        parser.add_argument('--limit', dest='limit', help='Number of segments', default=3, type=int)
        parser.add_argument('--path', dest='path', help='Path where to read and write data', default='data')
        parser.add_argument('--id', dest='id', help='Id of the segment', type=int)
        parser.add_argument('--name', dest='name', help='Name of the segment')
        parser.add_argument('--definition', dest='definition', help='Definition of the segment', type=argparse.FileType('r'))

        args = parser.parse_args(input_args)

        if args.cmd == Rest.CMD_LIST:
            response = http.get_json(self.api_base, Rest.VERB_LIST, {'config':json.dumps({'sortField': 'name', 'sortDirection': 'asc', 'limit': args.limit})})
            print json.dumps(response, sort_keys=True, indent=2)
        elif args.cmd == Rest.CMD_LIST_STORE:
            app_path = os.path.join(args.path, self.appid, 'segments')
            if not os.path.isdir(app_path):
                os.makedirs(app_path)
            response = self.get_all_segments(args.limit)
            if response != None:
                list_json_path = self.get_list_path(args.path)
                list_json = open(list_json_path, 'w')
                list_json.write(json.dumps(response, sort_keys=True, indent=2))
                list_json.close()
                print "List of segments saved to " + list_json_path
                
        elif args.cmd == Rest.CMD_SEGMENTS_STORE:
            app_path = os.path.join(args.path, self.appid, 'segments')
            list_path = self.get_list_path(args.path)
            list_json = open(list_path, 'r')
            json_content = json.load(list_json)
            list_json.close()
            list_of_segments = json_content["data"]
            for segment in list_of_segments:
                segment_id = segment["id"]
                response = http.get_json(self.api_base, Rest.VERB_SEGMENT, {'id':segment_id})
                segment_json_path = self.get_segment_path(args.path, segment_id)
                list_json = open(segment_json_path, 'w')
                list_json.write(json.dumps(response, sort_keys=True, indent=2))
                list_json.close()
                print "Segment " + str(segment_id) + " saved to " + segment_json_path
                
        elif args.cmd == Rest.CMD_SEGMENT:
            list_path = self.get_list_path(args.path)
            list_json = open(list_path, 'r')
            json_content = json.load(list_json)
            list_json.close()
            list_of_segments = json_content["data"]
            print "Read " + str(len(list_of_segments)) + " segments from " + list_path
            
            filtered_segments = filter(lambda seg: seg["id"] == args.id, list_of_segments)
            if len(filtered_segments) > 0:
                response = http.get_json(self.api_base, Rest.VERB_SEGMENT, {'id':args.id})
                segment_json_path = self.get_segment_path(args.path, args.id)
                list_json = open(segment_json_path, 'w')
                list_json.write(json.dumps(response, sort_keys=True, indent=2))
                list_json.close()
                print "Segment " + str(args.id) + " saved to " + segment_json_path
            else:
                print "Did not find any segment " + str(args.id) + ". Call liststore first."
        elif args.cmd == Rest.CMD_SEGMENT_CREATE:
            json_content = json.load(args.definition)
            segment_name = json_content["name"]
            response = self.create_segment(segment_name)
            if "err_code" in response:
                if response["err_code"] == 400:
                    print "Segment" + segment_name + " already exists"
                    segments = self.get_all_segments(2)
                    segments_found = filter(lambda segment: segment["name"] == segment_name, segments["data"])
                    if len(segments_found) == 1:
                        self.delete_segment(str(segments_found[0]["id"]))   
                        response = self.create_segment(segment_name)
                    else:
                      print "Could not retrieve segment " + segment_name     
                else:
                    print "unhandled error " + response 
            
            if "name" in response and response["name"] == segment_name:
                segment_id = response["id"]
                print "segment created " + str(segment_id)
                segment_criteria = json_content["criteria"]
                for criteria in segment_criteria:
                    response = http.put_json(self.api_base, Rest.VERB_CRITERION_CREATE, {'segmentId':segment_id, 'criterion':json.dumps(criteria)}, {})
                    print "criterion created " + str(response)
            else:
                print "Segment " + segment_name + " not created"
        elif args.cmd == Rest.CMD_SEGMENT_DELETE:
            if args.id != None:
                self.delete_segment(args.id)
                print "segment deleted " + str(args.id)
            elif args.name != None:
                segment_name = args.name
                segments = self.get_all_segments(2)
                segments_found = filter(lambda segment: segment["name"] == segment_name, segments["data"])
                if len(segments_found) == 1:
                    self.delete_segment(str(segments_found[0]["id"]))   
                else:
                    print "Could not delete segment " + segment_name + " because it doesn't exist"     
            else:
                print "Command " + Rest.CMD_SEGMENT_DELETE + " expects --id <segment id> or --name <segment name>"
        else:
            print "unknown command " + args.cmd
 def create_segment(self, name):
     print "Create segment " + name
     return http.put_json(self.api_base, Rest.VERB_SEGMENT_CREATE, {'segment':json.dumps({'name': name})}, {})