Skip to content

Commit

Permalink
Adding docstrings and removing unused imports (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmungall authored Jul 30, 2024
1 parent 3542b10 commit a55ba85
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
34 changes: 24 additions & 10 deletions schemasheets/schemamaker.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Converts a schema sheet into a LinkML schema"""
import codecs
import contextlib
import os
import sys
import csv
import logging
import tempfile
from urllib.request import urlopen
from copy import copy

Expand All @@ -13,7 +12,6 @@
from dataclasses import dataclass
from typing import List, Union, Any, Dict, Tuple, Generator, TextIO

from linkml_runtime.dumpers import yaml_dumper
from linkml_runtime.linkml_model import Annotation, Example
from linkml_runtime.linkml_model.meta import SchemaDefinition, ClassDefinition, Prefix, \
SlotDefinition, EnumDefinition, PermissibleValue, SubsetDefinition, TypeDefinition, Element, Setting
Expand All @@ -35,24 +33,40 @@ class SchemaSheetRowException(Exception):
@dataclass
class SchemaMaker:
"""
Engine for making LinkML schemas from Schema Sheets
Engine for making LinkML schemas from Schema Sheets.
"""
schema: SchemaDefinition = None
"""Generated schema."""

element_map: Dict[Tuple[str, str], Element] = None

metamodel: SchemaView = None
"""Schema describing LinkML elements."""

cardinality_vocabulary: str = None

use_attributes: bool = None
"""If True, use attributes instead of slots."""

default_name: str = None
"""Default name for the schema."""

unique_slots: bool = None
"""If True, slots are unique across classes."""

gsheet_id: str = None
"""Google sheet ID."""

table_config_path: str = None
"""Path to table configuration file."""

base_schema_path: str = None

def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDefinition:
"""
Create a LinkML schema from a collection of Schema Sheets
Create a LinkML schema from one or more Schema Sheets.
:param csv_files: schema sheets
:param csv_files: schema sheets paths
:param kwargs:
:return: generated schema
"""
Expand All @@ -68,7 +82,7 @@ def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDef
if not isinstance(csv_files, list):
csv_files = [csv_files]
for f in csv_files:
self.merge_sheet(f, **kwargs)
self.load_and_merge_sheet(f, **kwargs)
self.schema.imports.append('linkml:types')
self.schema.prefixes['linkml'] = Prefix('linkml', 'https://w3id.org/linkml/')
self._tidy_slot_usage()
Expand All @@ -83,7 +97,7 @@ def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDef

def _tidy_slot_usage(self):
"""
removes all slot usages marked inapplicable
removes all slot usages marked inapplicable.
:return:
"""
Expand All @@ -93,9 +107,9 @@ def _tidy_slot_usage(self):
c.slots.remove(sn)
del c.slot_usage[sn]

def merge_sheet(self, file_name: str, delimiter='\t') -> None:
def load_and_merge_sheet(self, file_name: str, delimiter='\t') -> None:
"""
Merge information from the given schema sheet into the current schema
Merge information from the given schema sheet into the current schema.
:param file_name: schema sheet
:param delimiter: default is tab
Expand Down
9 changes: 6 additions & 3 deletions schemasheets/schemasheet_datamodel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Core data model for a SchemaSheet."""
import csv
from dataclasses import dataclass
from typing import Union, Dict, List, Any
Expand All @@ -18,8 +19,7 @@
DESCRIPTOR = str
ROW = Dict[str, Any]


#c = ClassDefinition
# Vocabulary for types
T_SCHEMA = 'schema'
T_CLASS = 'class'
T_SLOT = 'slot'
Expand Down Expand Up @@ -101,6 +101,9 @@ def add_info(self, info: Union[Dict, DESCRIPTOR]) -> None:
self.maps_to = info
mm = get_metamodel()
snmap = mm.slot_name_mappings()
for k, v in snmap.items():
if k != v.name:
print(k,v.name)
# TODO: use alias
snmap['uri'] = snmap['type_uri']
if self.maps_to.startswith("metaslot."):
Expand Down Expand Up @@ -271,4 +274,4 @@ def get_configmodel() -> SchemaView:
"""
package = 'schemasheets.conf.configschema'
data = pkgutil.get_data(package, f'configschema.yaml')
return SchemaView(data.decode("utf-8"))
return SchemaView(data.decode("utf-8"))
2 changes: 1 addition & 1 deletion tests/test_schema_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

def test_roundtrip_schema():
"""
Tests linkml2sheets by roundtripping from the standard personinfo schema in YAML
Tests linkml2sheets by round-tripping from the standard personinfo schema in YAML
"""
sm = SchemaMaker()
# sheets2linkml, from SHEET
Expand Down
3 changes: 3 additions & 0 deletions tests/test_structured_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@


def test_structured_syntax():
"""
Test that structured syntax is roundtripped
"""
sm = ss.SchemaMaker()
sheet_path = str(INPUT_DIR / "structured_syntax.tsv")
out_path = str(OUTPUT_DIR / "structured_syntax-roundtrip.tsv")
Expand Down

0 comments on commit a55ba85

Please sign in to comment.