def BuildUniverse( use_simplified_universe: bool = False, modified_types_filepath: str = None, ) -> presubmit_validate_types_lib.ConfigUniverse: """Generates the ontology universe. Args: use_simplified_universe: boolean to quick load minimal universe instead of full ontology modified_types_filepath: filepath to the modified ontology types Returns: Generated universe object. """ if use_simplified_universe: yaml_files = None universe = universe_helper.create_simplified_universe() elif modified_types_filepath: modified_ontology_exists = path.exists(modified_types_filepath) if not modified_ontology_exists: print(f'Specified filepath [{modified_types_filepath}] ' 'modified ontology does not exist') return None modified_types_filepath = path.expanduser(modified_types_filepath) external_file_lib.Validate(filter_text=None, changed_directory=modified_types_filepath, original_directory=constants.ONTOLOGY_ROOT, interactive=False) yaml_files = external_file_lib.RecursiveDirWalk( modified_types_filepath) else: default_ontology_exists = path.exists(constants.ONTOLOGY_ROOT) if not default_ontology_exists: print(f'Specified filepath [{constants.ONTOLOGY_ROOT}] ' 'for default ontology does not exist') return None # use default location for ontology files yaml_files = external_file_lib.RecursiveDirWalk( constants.ONTOLOGY_ROOT) if yaml_files: config = presubmit_validate_types_lib.SeparateConfigFiles(yaml_files) universe = presubmit_validate_types_lib.BuildUniverse(config) namespace_validation = namespace_validator.NamespaceValidator( universe.GetEntityTypeNamespaces()) if not namespace_validation.IsValid(): print('Universe is not valid') return None return universe
def BuildUniverse( modified_types_filepath: str = None ) -> presubmit_validate_types_lib.ConfigUniverse: """Generates the ontology universe. Args: modified_types_filepath: filepath to the modified ontology types Returns: Generated universe object. """ if modified_types_filepath: modified_ontology_exists = path.exists(modified_types_filepath) if not modified_ontology_exists: print('Specified filepath [{0}] modified ontology does not exist'. format(modified_types_filepath)) return None modified_types_filepath = path.expanduser(modified_types_filepath) external_file_lib.Validate(filter_text=None, changed_directory=modified_types_filepath, original_directory=constants.ONTOLOGY_ROOT, interactive=False) yaml_files = external_file_lib.RecursiveDirWalk( modified_types_filepath) else: default_ontology_exists = path.exists(constants.ONTOLOGY_ROOT) if not default_ontology_exists: print( 'Specified filepath [{0}] for default ontology does not exist'. format(constants.ONTOLOGY_ROOT)) return None # use default location for ontology files yaml_files = external_file_lib.RecursiveDirWalk( constants.ONTOLOGY_ROOT) config = presubmit_validate_types_lib.SeparateConfigFiles(yaml_files) universe = presubmit_validate_types_lib.BuildUniverse(config) namespace_validation = namespace_validator.NamespaceValidator( universe.GetEntityTypeNamespaces()) if not namespace_validation.IsValid(): print('Universe is not valid') return None return universe
def BuildUniverse(): """Generates the ontology universe. Returns: Generated universe object. """ ontology_validator_exists = path.exists( path.join('..', 'ontology_validator')) ontology_exists = path.exists(path.join('..', '..', '..', 'ontology')) if not (ontology_validator_exists and ontology_exists): print('ERROR: ontology validator or ontology have changed locations') return None yaml_files = external_file_lib.RecursiveDirWalk( path.join('..', '..', '..', 'ontology', 'yaml', 'resources')) config = presubmit_validate_types_lib.SeparateConfigFiles(yaml_files) universe = presubmit_validate_types_lib.BuildUniverse(config) namespace_validation = namespace_validator.NamespaceValidator( universe.GetEntityTypeNamespaces()) if not namespace_validation.IsValid(): print('Universe is not valid') return None return universe
def test_RecursiveDirWalk_multiLevel(self): path_parts = external_file_lib.RecursiveDirWalk(DIR_MULTI_DIR) path_parts.sort(key=lambda x: x.relative_path.replace('\\', '/')) self.assertLen(path_parts, 4) path_part_modified_client_fan = path_parts[0] self.assertEqual(path_part_modified_client_fan.root, DIR_MULTI_DIR) self.assertEqual(path_part_modified_client_fan.relative_path, FAN_2) path_part_modified_client_fan = path_parts[1] self.assertEqual(path_part_modified_client_fan.root, DIR_MULTI_DIR) self.assertEqual(path_part_modified_client_fan.relative_path, path.join('entity_types', FAN)) path_part_modified_client_fan = path_parts[2] self.assertEqual(path_part_modified_client_fan.root, DIR_MULTI_DIR) self.assertEqual( path_part_modified_client_fan.relative_path, path.join('entity_types', 'another_entity_types', FAN)) path_part_modified_client_fan = path_parts[3] self.assertEqual(path_part_modified_client_fan.root, DIR_MULTI_DIR) self.assertEqual( path_part_modified_client_fan.relative_path, path.join('entity_types', 'another_entity_types2', FAN))
def BuildUniverse(modified_types_filepath=None): """Generates the ontology universe. Args: modified_types_filepath: filepath to the modified ontology types Returns: Generated universe object. """ if modified_types_filepath: modified_ontology_exists = path.exists(modified_types_filepath) if not modified_ontology_exists: print('Specified filepath for modified ontology does not exist') return None modified_types_filepath = path.expanduser(modified_types_filepath) external_file_lib.Validate(filter_text=None, changed_directory=modified_types_filepath, original_directory=path.join( '..', '..', '..', 'ontology', 'yaml', 'resources'), interactive=False) yaml_files = external_file_lib.RecursiveDirWalk( modified_types_filepath) else: # use default location for ontology files yaml_files = external_file_lib.RecursiveDirWalk( path.join('..', '..', '..', 'ontology', 'yaml', 'resources')) config = presubmit_validate_types_lib.SeparateConfigFiles(yaml_files) universe = presubmit_validate_types_lib.BuildUniverse(config) namespace_validation = namespace_validator.NamespaceValidator( universe.GetEntityTypeNamespaces()) if not namespace_validation.IsValid(): print('Universe is not valid') return None return universe
def test_RecursiveDirWalk_oneLevel(self): path_parts = external_file_lib.RecursiveDirWalk(DIR_ONE_LEVEL) path_parts.sort() self.assertLen(path_parts, 2) path_part_modified_base = path_parts[0] self.assertNotEmpty(path_part_modified_base) self.assertEqual(path_part_modified_base.root, DIR_ONE_LEVEL) self.assertEqual(path_part_modified_base.relative_path, 'entity_types/' + FAN) path_part_modified_base = path_parts[1] self.assertEqual(path_part_modified_base.root, DIR_ONE_LEVEL) self.assertEqual(path_part_modified_base.relative_path, 'entity_types/' + FAN_2)
def Build(ontology_path: str) -> OntologyWrapper: """A constructor for the ontology explorer. Args: ontology_path: A path for an alternative ontology extended from DBO. Returns: ontology: An instance of the OntologyWrapper class. """ if ontology_path is not None: yaml_file_path = ontology_path else: yaml_file_path = constants.ONTOLOGY_ROOT yaml_files = external_file_lib.RecursiveDirWalk(yaml_file_path) config = presubmit_validate_types_lib.SeparateConfigFiles(yaml_files) universe = presubmit_validate_types_lib.BuildUniverse(config) nv.NamespaceValidator(universe.GetEntityTypeNamespaces()) ontology = OntologyWrapper(universe) return ontology
def test_RecursiveDirWalk_oneLevel(self): path_parts = external_file_lib.RecursiveDirWalk(DIR_ONE_LEVEL) # In ASCII code order, '/' < '[0-9A-Z]' < '\', so sorting path names # yields different results on different operating systems unless # we sort on a standardized version of the path names. path_parts.sort(key=lambda x: x.relative_path.replace('\\', '/')) self.assertLen(path_parts, 2) path_part_modified_base = path_parts[0] self.assertNotEmpty(path_part_modified_base) self.assertEqual(path_part_modified_base.root, DIR_ONE_LEVEL) self.assertEqual(path_part_modified_base.relative_path, path.join('entity_types', FAN)) path_part_modified_base = path_parts[1] self.assertEqual(path_part_modified_base.root, DIR_ONE_LEVEL) self.assertEqual(path_part_modified_base.relative_path, path.join('entity_types', FAN_2))
def test_RecursiveDirWalk_multiLevel(self): path_parts = external_file_lib.RecursiveDirWalk(DIR_MULTI_DIR) path_parts.sort() self.assertLen(path_parts, 4) path_part_modified_client_fan = path_parts[0] self.assertEqual(path_part_modified_client_fan.root, DIR_MULTI_DIR) self.assertEqual(path_part_modified_client_fan.relative_path, FAN_2) path_part_modified_client_fan = path_parts[1] self.assertEqual(path_part_modified_client_fan.root, DIR_MULTI_DIR) self.assertEqual(path_part_modified_client_fan.relative_path, 'entity_types/' + FAN) path_part_modified_client_fan = path_parts[2] self.assertEqual(path_part_modified_client_fan.root, DIR_MULTI_DIR) self.assertEqual(path_part_modified_client_fan.relative_path, 'entity_types/another_entity_types/' + FAN) path_part_modified_client_fan = path_parts[3] self.assertEqual(path_part_modified_client_fan.root, DIR_MULTI_DIR) self.assertEqual(path_part_modified_client_fan.relative_path, 'entity_types/another_entity_types2/' + FAN)