def build_custom_content_object(self, file_path: str) -> dict: """ Build the custom content object represents a custom content entity instance. For example: integration-HelloWorld.yml downloaded from Demisto. """ file_data, file_ending = get_dict_from_file( file_path) # For example: yml, for integration files file_type = find_type( path=file_path, _dict=file_data, file_type=file_ending) # For example: integration if file_type: file_type = file_type.value file_entity = self.file_type_to_entity( file_data, file_type) # For example: Integrations file_id: str = get_entity_id_by_entity_type(file_data, file_entity) file_name: str = get_entity_name_by_entity_type(file_data, file_entity) custom_content_object: dict = { 'id': file_id, 'name': file_name, 'path': file_path, 'entity': file_entity, 'type': file_type, 'file_ending': file_ending, } file_code_language = get_code_lang(file_data, file_entity) if file_code_language: custom_content_object['code_lang'] = file_code_language return custom_content_object
def get_main_file_details(content_entity: str, entity_instance_path: str) -> tuple: """ Returns the details of the "main" file within an entity instance. For example: In the HelloWorld integration under Packs/HelloWorld, the main file is the yml file. It contains all relevant ids and names for all the files under the HelloWorld integration dir. :param content_entity: The content entity, for example Integrations :param entity_instance_path: For example: ~/.../content/Packs/TestPack/Integrations/HelloWorld :return: The main file id & name """ main_file_data: dict = dict() main_file_path: str = str() # Entities which contain yml files if content_entity in (INTEGRATIONS_DIR, SCRIPTS_DIR, PLAYBOOKS_DIR, TEST_PLAYBOOKS_DIR): if os.path.isdir(entity_instance_path): _, main_file_path = get_yml_paths_in_dir(entity_instance_path) elif os.path.isfile(entity_instance_path): main_file_path = entity_instance_path if main_file_path: main_file_data = get_yaml(main_file_path) # Entities which are json files (md files are ignored - changelog/readme) else: if os.path.isfile(entity_instance_path) and retrieve_file_ending( entity_instance_path) == 'json': main_file_data = get_json(entity_instance_path) main_id = get_entity_id_by_entity_type(main_file_data, content_entity) main_name = get_entity_name_by_entity_type(main_file_data, content_entity) return main_id, main_name
def file_type_to_entity(file_data: dict, file_type: str) -> str: """ Given the file type returns the file entity :param file_data: The file data :param file_type: The file type, for example: integration :return: The file entity, for example: Integrations """ if file_type and file_type == 'playbook': name: str = get_entity_name_by_entity_type(file_data, PLAYBOOKS_DIR) if name and 'test' in name.lower(): return TEST_PLAYBOOKS_DIR return ENTITY_TYPE_TO_DIR.get(file_type, '')
def test_get_entity_name_by_entity_type(self, data, entity): assert get_entity_name_by_entity_type(data, entity) == 'wow'