Skip to content

Commit a55ba85

Browse files
authored
Adding docstrings and removing unused imports (#117)
1 parent 3542b10 commit a55ba85

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

schemasheets/schemamaker.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
"""Converts a schema sheet into a LinkML schema"""
12
import codecs
23
import contextlib
3-
import os
44
import sys
55
import csv
66
import logging
7-
import tempfile
87
from urllib.request import urlopen
98
from copy import copy
109

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

16-
from linkml_runtime.dumpers import yaml_dumper
1715
from linkml_runtime.linkml_model import Annotation, Example
1816
from linkml_runtime.linkml_model.meta import SchemaDefinition, ClassDefinition, Prefix, \
1917
SlotDefinition, EnumDefinition, PermissibleValue, SubsetDefinition, TypeDefinition, Element, Setting
@@ -35,24 +33,40 @@ class SchemaSheetRowException(Exception):
3533
@dataclass
3634
class SchemaMaker:
3735
"""
38-
Engine for making LinkML schemas from Schema Sheets
36+
Engine for making LinkML schemas from Schema Sheets.
3937
"""
4038
schema: SchemaDefinition = None
39+
"""Generated schema."""
40+
4141
element_map: Dict[Tuple[str, str], Element] = None
42+
4243
metamodel: SchemaView = None
44+
"""Schema describing LinkML elements."""
45+
4346
cardinality_vocabulary: str = None
47+
4448
use_attributes: bool = None
49+
"""If True, use attributes instead of slots."""
50+
4551
default_name: str = None
52+
"""Default name for the schema."""
53+
4654
unique_slots: bool = None
55+
"""If True, slots are unique across classes."""
56+
4757
gsheet_id: str = None
58+
"""Google sheet ID."""
59+
4860
table_config_path: str = None
61+
"""Path to table configuration file."""
62+
4963
base_schema_path: str = None
5064

5165
def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDefinition:
5266
"""
53-
Create a LinkML schema from a collection of Schema Sheets
67+
Create a LinkML schema from one or more Schema Sheets.
5468
55-
:param csv_files: schema sheets
69+
:param csv_files: schema sheets paths
5670
:param kwargs:
5771
:return: generated schema
5872
"""
@@ -68,7 +82,7 @@ def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDef
6882
if not isinstance(csv_files, list):
6983
csv_files = [csv_files]
7084
for f in csv_files:
71-
self.merge_sheet(f, **kwargs)
85+
self.load_and_merge_sheet(f, **kwargs)
7286
self.schema.imports.append('linkml:types')
7387
self.schema.prefixes['linkml'] = Prefix('linkml', 'https://w3id.org/linkml/')
7488
self._tidy_slot_usage()
@@ -83,7 +97,7 @@ def create_schema(self, csv_files: Union[str, List[str]], **kwargs) -> SchemaDef
8397

8498
def _tidy_slot_usage(self):
8599
"""
86-
removes all slot usages marked inapplicable
100+
removes all slot usages marked inapplicable.
87101
88102
:return:
89103
"""
@@ -93,9 +107,9 @@ def _tidy_slot_usage(self):
93107
c.slots.remove(sn)
94108
del c.slot_usage[sn]
95109

96-
def merge_sheet(self, file_name: str, delimiter='\t') -> None:
110+
def load_and_merge_sheet(self, file_name: str, delimiter='\t') -> None:
97111
"""
98-
Merge information from the given schema sheet into the current schema
112+
Merge information from the given schema sheet into the current schema.
99113
100114
:param file_name: schema sheet
101115
:param delimiter: default is tab

schemasheets/schemasheet_datamodel.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Core data model for a SchemaSheet."""
12
import csv
23
from dataclasses import dataclass
34
from typing import Union, Dict, List, Any
@@ -18,8 +19,7 @@
1819
DESCRIPTOR = str
1920
ROW = Dict[str, Any]
2021

21-
22-
#c = ClassDefinition
22+
# Vocabulary for types
2323
T_SCHEMA = 'schema'
2424
T_CLASS = 'class'
2525
T_SLOT = 'slot'
@@ -101,6 +101,9 @@ def add_info(self, info: Union[Dict, DESCRIPTOR]) -> None:
101101
self.maps_to = info
102102
mm = get_metamodel()
103103
snmap = mm.slot_name_mappings()
104+
for k, v in snmap.items():
105+
if k != v.name:
106+
print(k,v.name)
104107
# TODO: use alias
105108
snmap['uri'] = snmap['type_uri']
106109
if self.maps_to.startswith("metaslot."):
@@ -271,4 +274,4 @@ def get_configmodel() -> SchemaView:
271274
"""
272275
package = 'schemasheets.conf.configschema'
273276
data = pkgutil.get_data(package, f'configschema.yaml')
274-
return SchemaView(data.decode("utf-8"))
277+
return SchemaView(data.decode("utf-8"))

tests/test_schema_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
def test_roundtrip_schema():
6262
"""
63-
Tests linkml2sheets by roundtripping from the standard personinfo schema in YAML
63+
Tests linkml2sheets by round-tripping from the standard personinfo schema in YAML
6464
"""
6565
sm = SchemaMaker()
6666
# sheets2linkml, from SHEET

tests/test_structured_syntax.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434

3535

3636
def test_structured_syntax():
37+
"""
38+
Test that structured syntax is roundtripped
39+
"""
3740
sm = ss.SchemaMaker()
3841
sheet_path = str(INPUT_DIR / "structured_syntax.tsv")
3942
out_path = str(OUTPUT_DIR / "structured_syntax-roundtrip.tsv")

0 commit comments

Comments
 (0)