Example #1
0
 def test_parse_csv_to_dict_with_numeric(self):
     parsed_list = parse_csv.parse_csv_to_dict(
         f'{self.dirpath}/test_numeric.csv')
     self.assertTrue(type(parsed_list[0]['number']) == int)
     self.assertTrue(type(parsed_list[0]['string']) == str)
     self.assertTrue(type(parsed_list[1]['number']) == int)
     self.assertTrue(type(parsed_list[1]['string']) == str)
     self.assertTrue(type(parsed_list[2]['number']) == int)
     self.assertTrue(type(parsed_list[2]['string']) == str)
Example #2
0
 def test_parse_json_with_bool_positive(self):
     parsed_object_list = None
     with open(f'{self.dirpath}/test_json_wbool.json',
               encoding='utf-8-sig') as jsonfile:
         parsed_object_list = json.load(jsonfile)
     parse_json.parse_json_to_csv(f'{self.dirpath}/test_json_wbool.json',
                                  f'{self.dirpath}/outputfile.csv')
     parsed_csv_list = parse_csv.parse_csv_to_dict(
         f'{self.dirpath}/outputfile.csv')
     self.assertEqual(parsed_object_list, parsed_csv_list)
Example #3
0
 def test_parse_csv_to_dict2(self):
     #my_num(int), my_num_2.nested, val[0], val[1], vala[0].dog, valb[0][1].chicken
     # 1, 45, horse, pig, cocker spaniel, rooster
     #5, 20, goat, rat, cockapoo, rooster
     parsed_list = parse_csv.parse_csv_to_dict(
         f'{self.dirpath}/test_basic.csv')
     answer_list = [{'my_num': 1, 'my_num_2': {'nested': '45'}, 'val': ['horse', 'pig'], 'vala':[{'dog': 'cocker spaniel'}], 'valb': [[None, {'chicken': 'rooster'}]]},
                    {'my_num': 5, 'my_num_2': {'nested': '20'}, 'val': ['goat', 'rat'], 'vala':[
                        {'dog': 'cockapoo'}], 'valb': [[None, {'chicken': 'rooster'}]]}
                    ]
     self.assertEqual(parsed_list, answer_list)
Example #4
0
 def test_parse_json_to_csv_positive(self):
     # parse_json_to_csv(json_file_in, csv_file_out)
     # load raw json file
     parsed_object_list = None
     with open(f'{self.dirpath}/sample_json.json',
               encoding='utf-8-sig') as jsonfile:
         parsed_object_list = json.load(jsonfile)
     parse_json.parse_json_to_csv(f'{self.dirpath}/sample_json.json',
                                  f'{self.dirpath}/outputfile.csv')
     parsed_csv_list = parse_csv.parse_csv_to_dict(
         f'{self.dirpath}/outputfile.csv')
     self.assertEqual(parsed_object_list, parsed_csv_list)
Example #5
0
 def test_parse_csv_to_dict(self):
     # parse_csv_to_dict(csv_file)
     parsed_list = parse_csv.parse_csv_to_dict(
         f'{self.dirpath}/sample_network_object.csv')
     answer_list = [{'name': 'host1',
                     'description': 'description1',
                     'subType': 'HOST',
                     'value': '1.1.1.1',
                     'type': 'networkObject'},
                    {
         'name': 'host2',
         'description': 'description 2',
         'subType': 'HOST',
         'value': '2.2.2.2',
         'type': 'networkObject'}
     ]
     self.assertEqual(parsed_list, answer_list)
Example #6
0
    def bulk_import(self,
                    file_list,
                    input_format='JSON',
                    id_list=None,
                    type_list=None,
                    name_list=None,
                    filter_local=False):
        """
        This method will import a list of files in the given format
        
        Parameters:
        
        file_list -- A Python list of files to import
        input_format -- enum (JSON | CSV | YAML)
        id_list -- IDs to exclude from the import package
        type_list -- Types to exclude from the import package
        name_list -- Names to exclude from the import package
        local_filter -- This determines if the name, type, id filters will be applied locally or on the remote side (passed to the server)
        
        This will return a bool indicating success
        """
        IMPORT_SUCCESS = 'Successfully completed import'
        IMPORT_FAIL = 'Unable to complete import'
        return_result = False
        entity_filter_list = self._create_entity_filter(id_list=id_list,
                                                        type_list=type_list,
                                                        name_list=name_list)
        object_list = []
        if input_format == 'CSV':
            logging.info('Importing in CSV mode')
            # need to loop  through files and convert to JSON and merge into a single list
            for inputfile in file_list:
                object_list.extend(parse_csv.parse_csv_to_dict(inputfile))

        elif input_format == 'JSON':
            logging.info('Importing in JSON mode')
            #JSON case just merge the JSON docs
            for input_file in file_list:
                object_list.extend(
                    json.loads(read_string_from_file(input_file)))

        elif input_format == 'YAML':
            logging.info('Importing in YAML mode')
            #JSON case just merge the JSON docs
            for input_file in file_list:
                object_list.extend(read_yaml_to_dict(input_file))

        if filter_local:
            # If filter local is set true the objects will be removed client side
            # instead of server side.  This works around some of the issues with server
            # side filtering.
            object_list = self._filter_object_list(object_list,
                                                   id_list=id_list,
                                                   name_list=name_list,
                                                   type_list=type_list)
        if self._do_upload_import_dict_list(
                object_list,
                entity_filter_list=(entity_filter_list
                                    if not filter_local else None)):
            logging.info(IMPORT_SUCCESS)
            return_result = True
        else:
            logging.error(IMPORT_FAIL)
        return return_result