def check_ontology_path(ontology_path): ''' Check that ontology_path is a valid path Argument: ontology_path Path to serialized ontology file or directory of turtle files Return: Error message string Empty string if there are no errors ''' # If ontology_path is a file, it should be one of the Toolkit's serialized ontology files if os.path.isfile(ontology_path): try: if serializer.get_identifier(ontology_path) != serializer.ONTOLOGY: return '{} is not a serialized ontology file'.format( ontology_path) except serializer.DeserializeError: return '{} is not a serialized ontology file'.format(ontology_path) else: return '' # If ontology_path is a directory, it should contain at least one .ttl file if os.path.isdir(ontology_path): filenames = os.listdir(ontology_path) for filename in filenames: if filename.endswith('.ttl'): return '' return '{} is a directory that does not contain any turtle files'.format( ontology_path) # If it's something else, punt return '{} is not a file or a directory'.format(ontology_path)
def check_data_paths(data_paths): ''' Check that all the data_paths are a valid data path Argument: data_paths List of zero or more paths to json files or serialized casedata objects Return: List of error message strings Empty list if there are no errors ''' # Start with empty list of bad files bad_files = [] # Do for each data_path for path in data_paths: # If path is a file, it should be # one of the Toolkit's serialized json files or a valid json file if os.path.isfile(path): # If it's a Toolkit serialized json file, cool. try: if serializer.get_identifier(path) == serializer.CASEDATA: continue except serializer.DeserializeError: pass # If it's a valid json file, cool. try: json.load(open(path)) continue except (json.decoder.JSONDecodeError, UnicodeDecodeError): pass # If it's neither, add to list of bad files bad_files.append(path) # If it's not a file, add to list of bad files else: bad_files.append(path) # If there are any bad files, construct and return error message if len(bad_files) == 0: return '' if len(bad_files) == 1: return '{} is neither a serialized casedata object nor a valid json file.'.format( bad_files[0]) return 'These are neither a serialized casedata objects nor a valid json files: {}.'.format( bad_files)
def get_ontology(path, verbose=True, **kwargs): ''' If path is a serialized ongology file, deserialize it and return it. If path is a directory that contains turtle files, build Ontology object from scratch. If path is something else, raise exception. Arguments: path Full path to a serialized ontology file or a directory containing turtle files verbose If true and using Ontospy, Ontospy library runs verbosely kwargs Additional arguments passed on to Ontospy Return: An Ontology object Raise: Exception if path is not a serialized ontology file or a directory containing turtle files ''' # Start with empty Ontology object ontology = Ontology() # If path is a serialized ontology file, deserialize it and set context for error messages if os.path.isfile(path) and serializer.get_identifier( path) == serializer.ONTOLOGY: _identifer, metadata, ontology.__dict__ = serializer.deserialize(path) if metadata['version'] != VERSION: print( '{} was serialized with a different version of the toolkit. Use this command to reserialize:' .format(path)) print() print(' serialize {}'.format(metadata['path'])) print() sys.exit(1) # If path is a directory containing turtle files, build Ontology # Note that _read_turtle_files sets the context for error messages elif os.path.isdir(path) and [ filename for filename in os.listdir(path) if filename.endswith('.ttl') ]: ontology.__dict__ = _read_turtle_files(path, verbose, **kwargs) # If path is something else, raise else: raise Exception( '{} is neither a serialized ontology file nor a directory containing turtle files' .format(path)) # Return the ontology object return ontology
def get_ontology(path, verbose=True, **kwargs): ''' If path is a serialized ongology file, deserialize it and return it. If path is a directory that contains turtle files, build Ontology object from scratch. If path is something else, raise exception. Arguments: path Full path to a serialized ontology file or a directory containing turtle files verbose If true and using Ontospy, Ontospy library runs verbosely kwargs Additional arguments passed on to Ontospy Return: An Ontology object Side effect: This function populates namespace_manager.namespace_manager Raise: Exception if path is not a serialized ontology file or a directory containing turtle files ''' # Start with empty Ontology object ontology = Ontology() # If path is a serialized ontology file, deserialize it and populate namespace_manager if os.path.isfile(path) and serializer.get_identifier( path) == serializer.ONTOLOGY: _identifer, _metadata, ontology.__dict__ = serializer.deserialize(path) namespace_manager.populate(ontology.constraints.keys()) # If path is a directory containing turtle files, build Ontology object AND populate namespace_manager # Note: _read_turtle_files() populates the namespace_manager so the ontology errors can use it elif os.path.isdir(path) and [ filename for filename in os.listdir(path) if filename.endswith('.ttl') ]: ontology.__dict__ = _read_turtle_files(path, verbose, **kwargs) # If path is something else, raise else: raise Exception( '{} is neither a serialized ontology file nor a directory containing turtle files' .format(path)) # Populate the namespace from the ontology classes # Return the ontology object return ontology