Skip to content

Commit

Permalink
Adding docstrings and removing unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
cmungall committed Jul 17, 2023
1 parent f3323b9 commit 7bf5d96
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
39 changes: 24 additions & 15 deletions schemasheets/schemamaker.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
"""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

import click
import yaml
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
from linkml_runtime.utils.schema_as_dict import schema_as_dict
from linkml_runtime.utils.schemaview import SchemaView, re

from schemasheets.schemasheet_datamodel import ColumnConfig, TableConfig, get_configmodel, get_metamodel, COL_NAME, \
DESCRIPTOR, \
from schemasheets.schemasheet_datamodel import ColumnConfig, TableConfig, get_configmodel, COL_NAME, \
tmap, T_CLASS, T_PV, T_SLOT, T_SUBSET, T_SCHEMA, T_ENUM, T_PREFIX, T_TYPE, SchemaSheet
from schemasheets.conf.configschema import Cardinality
from schemasheets.utils.google_sheets import gsheets_download_url
Expand All @@ -34,23 +31,38 @@ 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."""

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 @@ -61,7 +73,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 @@ -73,7 +85,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 @@ -83,18 +95,15 @@ 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
:return:
"""
logging.info(f'READING {file_name} D={delimiter}')
#with self.ensure_file(file_name) as tsv_file:
# reader = csv.DictReader(tsv_file, delimiter=delimiter)
with self.ensure_csvreader(file_name, delimiter=delimiter) as reader:
schemasheet = SchemaSheet.from_dictreader(reader)
if self.table_config_path:
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 @@ -16,8 +17,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 @@ -95,6 +95,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 @@ -265,4 +268,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 7bf5d96

Please sign in to comment.