Utils#
linkml.utils.converter module#
linkml.utils.datautils module#
- linkml.utils.datautils.infer_index_slot(sv: SchemaView, root_class: ClassDefinitionName) SlotDefinitionName | None [source]#
- linkml.utils.datautils.infer_root_class(sv: SchemaView) ClassDefinitionName | None [source]#
Infer the class that should be at the root of the object tree
(Note this is distinct from the root of the class hierarchy)
If a class is explicitly designated with tree_root, use this. Otherwise use the class that is not referenced as a range in any other class.
linkml.utils.datavalidator module#
- class linkml.utils.datavalidator.DataValidator(schema: str | SchemaDefinition | None = None)[source]#
Bases:
object
Base class for all validators
- schema: str | SchemaDefinition = None#
LinkML Schema to validate against
linkml.utils.execute_tutorial module#
- class linkml.utils.execute_tutorial.Block(category: str = None, title: str = None, content: str = None, output: str = None, error: str = None, expected_fail: bool = None, prior_lines: List[str] = None, annotations: List[str] = None)[source]#
Bases:
object
- linkml.utils.execute_tutorial.execute_blocks(directory: str, blocks: List[Block]) List[str] [source]#
Execute the code blocks embedded in a tutorial
- Parameters:
directory –
blocks –
- Returns:
errors
linkml.utils.generator module#
Base class for all generators
Developer Notes#
Subclasses of this class implement specific generators. Each generator is in one of two styles:
schemaloader-based, using a Visitor pattern (older)
schemaview-based, no visitor pattern (newer)
New generators should always using the latter approach
- class linkml.utils.generator.Generator(schema: str | ~typing.TextIO | ~linkml_runtime.linkml_model.meta.SchemaDefinition | ~linkml.utils.generator.Generator | ~pathlib.Path, schemaview: ~linkml_runtime.utils.schemaview.SchemaView | None = None, format: str | None = None, metadata: bool = True, useuris: bool | None = None, log_level: int | None = 30, mergeimports: bool | None = True, source_file_date: str | None = None, source_file_size: int | None = None, logger: ~logging.Logger | None = None, verbose: bool | None = None, output: str | None = None, namespaces: ~linkml_runtime.utils.namespaces.Namespaces | None = None, directory_output: bool = False, base_dir: str | None = None, metamodel_name_map: ~typing.Dict[str, str] | None = None, importmap: str | ~typing.Mapping[str, str] | None = None, emit_prefixes: ~typing.Set[str] = <factory>, metamodel: ~linkml.utils.schemaloader.SchemaLoader | None = None, stacktrace: bool = False, include: str | ~pathlib.Path | ~linkml_runtime.linkml_model.meta.SchemaDefinition | None = None, **_kwargs)[source]#
Bases:
object
Base class for generators
For usage Generator Docs
- add_mappings(defn: Definition) None [source]#
Process any mappings in defn, adding all of the mappings prefixes to the namespace map :param defn: Class or Slot Definition
- add_prefix(ncname: str) None [source]#
Add a prefix to the list of prefixes to emit
@param ncname: name to add
- aliased_slot_name(slot: SlotDefinitionName | SlotDefinition) SlotDefinitionName [source]#
Return the overloaded slot name – the alias if one exists otherwise the actual name
@param slot: either a slot name or a definition @return: overloaded name
- all_slots(cls: ClassDefinitionName | ClassDefinition, *, cls_slots_first: bool = False, seen: Set[ClassDefinitionName] | None = None) List[SlotDefinition] [source]#
Return all slots that are part of the class definition. This includes all is_a, mixin and apply_to slots but does NOT include slot_usage targets. If class B has a slot_usage entry for slot “s”, only the slot definition for the redefined slot will be included, not its base. Slots are added in the order they appear in classes, with recursive is_a’s being added first followed by mixins and finally apply_tos
@param cls: class definition or class definition name @param cls_slots_first: True means return own slots at the top of the list @param seen: List of slots already recorded. Used for internal recursion @return: ordered list of slots in the class with slot usages removed
- ancestors(element: ClassDefinition | SlotDefinition) List[ElementName] [source]#
Return an ordered list of ancestor names for the supplied slot or class
@param element: Slot or class name or definition @return: Ordered list of ancestor names
- base_dir: str = None#
Working directory or base URL of sources. Setting this is necessary for correct retrieval of relative imports.
- class_identifier(def_or_name: str | ClassDefinition | TypeDefinition) SlotDefinitionName | None [source]#
Return the class identifier if any
- Parameters:
def_or_name – class name or definition
- Returns:
name of class key (or identifier) if one exists
- class_identifier_path(cls_or_clsname: str | ClassDefinition, force_non_key: bool) List[str] [source]#
Return the path closure to a class identifier if the class has a key and force_non_key is false otherwise return a dictionary closure.
- Parameters:
cls_or_clsname – class definition
force_non_key – True means inlined even if the class has a key
- Returns:
path
- class_or_type_for(name: str) Element | None [source]#
Return the corresponding class or type for name
- class_or_type_name(name: str) str [source]#
Return the camelcase representation of clsname if it is a valid class or type. Prepend “Unknown” if the name isn’t valid
- default_prefix() str | None [source]#
Return the default prefix for the schema
@return: URI or NCNAME of default prefix
- domain_slots(cls: ClassDefinition) List[SlotDefinition] [source]#
Return all slots in the class definition that are owned by the class
- end_class(cls: ClassDefinition) str | None [source]#
Visited after visit_class_slots (if visit_class returned true)
@param cls: class being visited
- end_schema(**kwargs) str | None [source]#
Visited once at the end of generation
@param kwargs: Arguments passed through from CLI – implementation dependent
- static enum_identifier_path(enum_or_enumname: str | EnumDefinition) List[str] [source]#
Return an enum_identifier path
- formatted_element_name(el_or_elname: ElementName | Element, is_range_name: bool = False) str | None [source]#
Return the default format for the name or the referenced element. Slots are under_scored, all others are CamelCased. Slot names are the alias, not the actual name
- Parameters:
el_or_elname – element or name to map
is_range_name – True means that we’re looking for a class or type. False means Slot or Subset. Only applies if el_or_elname is an ElementName (otherwise we know what we’ve got
- Returns:
Formatted name if type can be known else None
- generatorname: ClassVar[str] = None#
Name of the generator. Override with os.path.basename(__file__)
- generatorversion: ClassVar[str] = None#
Version of the generator. Consider deprecating and instead use overall linkml version
- get_metamodel_slot_name(slot_name: str) str [source]#
Allows for localization of some generators, such as markdown generator
The client can control how metamodel elements are displayed; e.g. mapping the metamodel slot “slot” to “field”
This method also takes care of pluralization; e.g. if “slot” is mapped to “field”, then “slots” will be mapped to “fields”
- Parameters:
slot_name –
- Returns:
- importmap: str | Mapping[str, str] | None = None#
File name of import mapping file – maps import name/uri to target
- include: str | Path | SchemaDefinition | None = None#
If set, include extra schema outside of the imports mechanism
- static is_class_unconstrained(cls: ClassDefinition)[source]#
Determine if the class is mapped to typing.Any, i.e the unconstrained class
- Parameters:
cls – class definition
- Returns:
true if the class is unconstrained
- mergeimports: bool | None = True#
True means merge non-linkml sources into importing package. False means separate packages
- metadata: bool = True#
True means include date, generator, etc. information in source header if appropriate
- metamodel: SchemaLoader = None#
A SchemaLoader instance that points to the LinkML metamodel (meta.yaml)
- metamodel_name_map: Dict[str, str] = None#
Allows mapping of names of metamodel elements such as slot, etc
- neighborhood(elements: str | ElementName | List[ElementName]) References [source]#
Return a list of all slots, classes and types that touch any element in elements, including the element itself
@param elements: Element names to do proximity with @return: All slots and classes that touch element
- output: str | None = None#
Path to output file. Note all generators may not implement this uniformly, see https://github.com/linkml/linkml/issues/923
- own_slot_names(cls: ClassDefinitionName | ClassDefinition) List[SlotDefinitionName] [source]#
- own_slots(cls: ClassDefinitionName | ClassDefinition) List[SlotDefinition] [source]#
Return the list of slots owned the class definition. An “own slot” is any
cls
slot that does not appear in the class is_a parent. Own_slots include:any slot whose domain is cls
slot_usage entries
slots from mixins entries
slots from apply_to entries
@param cls: class name or class definition name @return: list of owned slots. List is sorted if sort_class_slots is true, otherwise in class order
- parent(element: ClassDefinition | SlotDefinition) ClassDefinition | SlotDefinition | None [source]#
Return the parent of element, if any
- range_type_path(typ: TypeDefinition) List[str] [source]#
Return a formatted list of range types from the base up
- Parameters:
typ – type definition whose name is to be formatted
- Returns:
List of possible types with base at the leftmost
- schema: str | TextIO | SchemaDefinition | Generator | Path#
metamodel compliant schema. Can be URI, file name, actual schema, another generator, an open file or a pre-parsed schema
- schemaview: SchemaView | None = None#
A wrapper onto the schema object
- serialize(**kwargs) str [source]#
Generate output in the required format
- Parameters:
kwargs – Generator specific parameters
- Returns:
Generated output
- slot_name(name: str) str [source]#
Return the underscored version of the aliased slot name if name is a slot. Prepend
unknown_
if the name isn’t valid.
- slot_range_path(slot_or_name: str | SlotDefinition) List[str] [source]#
Return an ordered list of slot ranges from distal to proximal
- Parameters:
slot_or_name – slot whose range is being typed
- Returns:
ordered list of types from base type forward
- sort_class_slots: ClassVar[bool] = False#
True means visit class slots in alphabetical order
- Type:
Visitor ClassVar
- uses_schemaloader: ClassVar[bool] = True#
Old-style generator that uses the SchemaLoader and visitor pattern
- useuris: bool | None = None#
True means declared class slot uri’s are used. False means use model uris
- visit_all_class_slots: ClassVar[bool] = False#
False means only visit own slots, True means visit all slots
- Type:
Visitor ClassVar
- visit_class(cls: ClassDefinition) str | bool | None [source]#
Visited once per schema class
@param cls: class being visited @return: Visit slots and end class. False means skip and go on
- visit_class_slot(cls: ClassDefinition, aliased_slot_name: str, slot: SlotDefinition) str | None [source]#
Visited for each slot in a class. If class level visit_all_slots is true, this is visited once for any class that is inherited (class itself, is_a, mixin, apply_to). Otherwise, just the own slots.
@param cls: containing class @param aliased_slot_name: Aliased slot name. May not be unique across all class slots @param slot: being visited
- visit_enum(enum: EnumDefinition) str | None [source]#
Visited once for every enum definition in the schema
@param enum: Enum definition
- visit_schema(**kwargs) str | None [source]#
Visited once at the beginning of generation
@param kwargs: Arguments passed through from CLI – implementation dependent
- visit_slot(aliased_slot_name: str, slot: SlotDefinition) str | None [source]#
Visited once for every slot definition in the schema.
@param aliased_slot_name: Aliased name of the slot. May not be unique @param slot: visited slot
- visit_subset(subset: SubsetDefinition) str | None [source]#
Visited once for every subset definition in the schema
#param subset: Subset definition
- visit_type(typ: TypeDefinition) str | None [source]#
Visited once for every type definition in the schema
@param typ: Type definition
linkml.utils.helpers module#
linkml.utils.mergeutils module#
- linkml.utils.mergeutils.alias_root(schema: SchemaDefinition, slotname: SlotDefinitionName) SlotDefinitionName | None [source]#
Return the ultimate alias of a slot
- linkml.utils.mergeutils.merge_classes(schema: SchemaDefinition, target: ClassDefinition, source: ClassDefinition, at_end: bool = False) None [source]#
Merge the slots in source into target
- Parameters:
schema – Containing schema
target – mergee
source – class to merge
at_end – True means add mergee to the end. False to the front
- linkml.utils.mergeutils.merge_dicts(target: Dict[str, Element], source: Dict[str, Element], imported_from: str, imported_from_uri: str, merge_imports: bool) None [source]#
- linkml.utils.mergeutils.merge_enums(schema: SchemaDefinition, target: EnumDefinition, source: EnumDefinition, at_end: bool = False) None [source]#
Merge the slots in source into target
- Parameters:
schema – Containing schema
target – mergee
source – enum to merge
at_end – True means add mergee to the end. False to the front
- linkml.utils.mergeutils.merge_namespaces(target: SchemaDefinition, mergee: SchemaDefinition, namespaces) None [source]#
Add the mergee namespace definitions to target
- Parameters:
target –
mergee –
namespaces –
- Returns:
- linkml.utils.mergeutils.merge_schemas(target: SchemaDefinition, mergee: SchemaDefinition, imported_from: str | None = None, namespaces: Namespaces | None = None, merge_imports: bool = True) None [source]#
Merge mergee into target
- linkml.utils.mergeutils.merge_slots(target: SlotDefinition | TypeDefinition, source: SlotDefinition | TypeDefinition, skip: List[SlotDefinitionName | TypeDefinitionName] | None = None, inheriting: bool = True) None [source]#
Merge slot source into target
- Parameters:
target – slot to merge into
source – slot to be merged from
skip – Properties to not merge (used to prevent provenance such as ‘inherited from’ from propagating)
inheriting – True means source is the parent. False means that everything gets copied
- linkml.utils.mergeutils.set_from_schema(schema: SchemaDefinition) None [source]#
- linkml.utils.mergeutils.slot_usage_name(usage_name: SlotDefinitionName, owning_class: ClassDefinition) SlotDefinitionName [source]#
Synthesize a unique name for an overridden slot
- Parameters:
usage_name –
owning_class –
- Returns:
Synthesized name
linkml.utils.rawloader module#
- linkml.utils.rawloader.load_raw_schema(data: str | dict | TextIO | Path, source_file: str | None = None, source_file_date: str | None = None, source_file_size: int | None = None, base_dir: str | None = None, merge_modules: bool | None = True, emit_metadata: bool | None = True) SchemaDefinition [source]#
Load and flatten SchemaDefinition from a file name, a URL or a block of text
@param data: URL, file name or block of text YAML Object or open file handle @param source_file: Source file name for the schema if data is type TextIO @param source_file_date: timestamp of source file if data is type TextIO @param source_file_size: size of source file if data is type TextIO @param base_dir: Working directory or base URL of sources @param merge_modules: True means combine modules into one source, false means keep separate @param emit_metadata: True means add source file info to the output @return: Un-processed Schema Definition object
linkml.utils.schemaloader module#
- class linkml.utils.schemaloader.SchemaLoader(data: str | TextIO | SchemaDefinition | dict | Path, base_dir: str | None = None, namespaces: Namespaces | None = None, useuris: bool | None = None, importmap: Mapping[str, str] | None = None, logger: Logger | None = None, mergeimports: bool | None = True, emit_metadata: bool | None = True, source_file_date: str | None = None, source_file_size: int | None = None)[source]#
Bases:
object
- merge_class(cls: ClassDefinition, merged_classes: List[ClassDefinitionName]) None [source]#
Merge parent class information into target class
- Parameters:
cls – target class
merged_classes – list of class names that have been merged. Used to do distal ancestor resolution
- merge_enum(enum: EnumDefinition, merged_enums: List[EnumDefinitionName]) None [source]#
Merge parent enumeration information into target enum
- Parameters:
enum – target enumeration
merged_enums – list of enum names that have been merged. Used to do distal ancestor resolution
- merge_slot(slot: SlotDefinition, merged_slots: List[SlotDefinitionName]) None [source]#
Merge parent slot information into target slot
- Parameters:
slot – target slot
merged_slots – list of slot names that have been merged. Used to do a distal ancestor resolution
- merge_type(typ: TypeDefinition, merged_types: List[TypeDefinitionName]) None [source]#
Merge parent type information into target type :param typ: target type :param merged_types: list of type names that have bee merged.
- process_slot_usage_definitions()[source]#
Slot usages can be used to completely define slots. Iterate over the class hierarchy finding all slot definitions that are introduced strictly as usages and add them to the slots component
- process_slot_usages(cls: ClassDefinition) None [source]#
Connect any slot usage items
- Parameters:
cls – class to process
- Returns:
usage item
- static raise_value_errors(error: str, loc_str: str | TypedNode | Iterator[TypedNode] | None) None [source]#
- resolve() SchemaDefinition [source]#
Reconcile a loaded schema, applying is_a, mixins, apply_to’s and other such things. Also validate the content and load a SchemaSynopsis entry
- Returns:
Fully resolved definition
- slot_definition_for(slotname: SlotDefinitionName, cls: ClassDefinition) SlotDefinition | None [source]#
Find the most proximal definition for slotname in the context of cls
- static slot_name_for(slot: SlotDefinition) str [source]#
linkml.utils.schemasynopsis module#
- class linkml.utils.schemasynopsis.SchemaSynopsis(schema: linkml_runtime.linkml_model.meta.SchemaDefinition, typerefs: Dict[linkml_runtime.linkml_model.meta.TypeDefinitionName, linkml.utils.typereferences.References] = <factory>, slotrefs: Dict[linkml_runtime.linkml_model.meta.SlotDefinitionName, linkml.utils.typereferences.References] = <factory>, classrefs: Dict[linkml_runtime.linkml_model.meta.ClassDefinitionName, linkml.utils.typereferences.References] = <factory>, subsetrefs: Dict[linkml_runtime.linkml_model.meta.SubsetDefinitionName, linkml.utils.typereferences.References] = <factory>, enumrefs: Dict[linkml_runtime.linkml_model.meta.EnumDefinitionName, linkml.utils.typereferences.References] = <factory>, typebases: Dict[str, Set[linkml_runtime.linkml_model.meta.TypeDefinitionName]] = <factory>, typeofs: Dict[linkml_runtime.linkml_model.meta.TypeDefinitionName, linkml_runtime.linkml_model.meta.TypeDefinitionName] = <factory>, slotclasses: Dict[linkml_runtime.linkml_model.meta.SlotDefinitionName, Set[linkml_runtime.linkml_model.meta.ClassDefinitionName]] = <factory>, definingslots: Dict[linkml_runtime.linkml_model.meta.SlotDefinitionName, Set[linkml_runtime.linkml_model.meta.ClassDefinitionName]] = <factory>, slotusages: Dict[linkml_runtime.linkml_model.meta.SlotDefinitionName, Set[linkml_runtime.linkml_model.meta.ClassDefinitionName]] = <factory>, owners: Dict[linkml_runtime.linkml_model.meta.SlotDefinitionName, Set[linkml_runtime.linkml_model.meta.ClassDefinitionName]] = <factory>, inverses: Dict[str, Set[str]] = <factory>, ownslots: Dict[linkml_runtime.linkml_model.meta.ClassDefinitionName, Set[linkml_runtime.linkml_model.meta.SlotDefinitionName]] = <factory>, codesets: Dict[rdflib.term.URIRef, Set[linkml_runtime.linkml_model.meta.EnumDefinitionName]] = <factory>, roots: linkml.utils.typereferences.References = <factory>, isarefs: Dict[linkml_runtime.linkml_model.meta.DefinitionName, linkml.utils.typereferences.References] = <factory>, mixinrefs: Dict[linkml_runtime.linkml_model.meta.DefinitionName, linkml.utils.typereferences.References] = <factory>, mixins: linkml.utils.typereferences.References = <factory>, abstracts: linkml.utils.typereferences.References = <factory>, applytos: linkml.utils.typereferences.References = <factory>, applytorefs: Dict[linkml_runtime.linkml_model.meta.DefinitionName, linkml.utils.typereferences.References] = <factory>, rangerefs: Dict[linkml_runtime.linkml_model.meta.ElementName, Set[linkml_runtime.linkml_model.meta.SlotDefinitionName]] = <factory>, inschema: Dict[str, linkml.utils.typereferences.References] = <factory>, **_kwargs)[source]#
Bases:
object
- abstracts: References#
- add_ref(fromtype: RefType, fromname: ElementName, totype: RefType, toname: ElementName) None [source]#
Add an inverse reference, indicating that to type/name is referenced by from type/name
- Parameters:
fromtype – Referencer type
fromname – Referencer name
totype – Referencee type
toname – Referencee name
- Returns:
- applytorefs: Dict[DefinitionName, References]#
- applytos: References#
- classrefs: Dict[ClassDefinitionName, References]#
- enumrefs: Dict[EnumDefinitionName, References]#
- inschema: Dict[str, References]#
- isarefs: Dict[DefinitionName, References]#
- mixinrefs: Dict[DefinitionName, References]#
- mixins: References#
- roots: References#
- schema: SchemaDefinition#
- slotrefs: Dict[SlotDefinitionName, References]#
- subsetrefs: Dict[SubsetDefinitionName, References]#
- summarize_class_definition(k: ClassDefinitionName, v: ClassDefinition) None [source]#
Summarize class definition element
- Parameters:
k – Class name
v – Class definition
- Returns:
- summarize_definition(typ: RefType, k: DefinitionName, v: Definition) None [source]#
Summarize slot and class definitions
- Parameters:
typ – type (slot or class)
k – name
v – definition
- Returns:
- summarize_element(typ: RefType, k: ElementName, v: Element) None [source]#
Summarize element level items
- Parameters:
typ – element type
k – element name
v – element definition
- Returns:
- summarize_enum_definition(k: EnumDefinitionName, v: EnumDefinition)[source]#
Summarize enum definition
- Parameters:
k – Enum name
v – Enum definition
- Returns:
- summarize_slot_definition(k: SlotDefinitionName, v: SlotDefinition) None [source]#
Summarize a slot definition :param k: slot name :param v: slot definition :return:
- summarize_type_definition(k: TypeDefinitionName, v: TypeDefinition)[source]#
Summarize type definition
- Parameters:
k – Type name
v – Type definition
- Returns:
- typerefs: Dict[TypeDefinitionName, References]#
linkml.utils.typereferences module#
- class linkml.utils.typereferences.References(classrefs: ~typing.Set[~linkml_runtime.linkml_model.meta.ClassDefinitionName] = <factory>, slotrefs: ~typing.Set[~linkml_runtime.linkml_model.meta.SlotDefinitionName] = <factory>, typerefs: ~typing.Set[~linkml_runtime.linkml_model.meta.TypeDefinitionName] = <factory>, subsetrefs: ~typing.Set[~linkml_runtime.linkml_model.meta.SubsetDefinitionName] = <factory>, enumrefs: ~typing.Set[~linkml_runtime.linkml_model.meta.EnumDefinitionName] = <factory>)[source]#
Bases:
object
Summary of references to a given class. The reference class is the key to the dictionary carrying classrefs
- update(other: References) None [source]#
linkml.utils.validation module#
- linkml.utils.validation.validate_object(data: YAMLRoot, schema: str | TextIO | SchemaDefinition, target_class: Type[YAMLRoot] | None = None, closed: bool = True)[source]#
validates instance data against a schema
- Parameters:
data – LinkML instance to be validates
schema – LinkML schema
target_class – class in schema to validate against
closed –
- Returns:
Module contents#
- linkml.utils.deprecation_warning(name: str)[source]#
Call this with the name of the deprecation object wherever the deprecated functionality will be used
This function will
emit a warning if the current version is greater than
deprecated_in
log that the deprecated feature was accessed in
EMITTED
for testing deprecations and muting warnings