Skip to content

Commit 7c428f4

Browse files
committed
addresses review by @cmungall
1 parent 6dfbed2 commit 7c428f4

File tree

5 files changed

+73
-37
lines changed

5 files changed

+73
-37
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ build-backend = "poetry.core.masonry.api"
3232
sheets2linkml = "schemasheets.schemamaker:convert"
3333
linkml2sheets = "schemasheets.schema_exporter:export_schema"
3434
sheets2project = "schemasheets.sheets_to_project:multigen"
35-
generate-populate = 'schemasheets.generate_populate:cli'
35+
36+
linkml2schemasheets-template = 'schemasheets.generate_populate:cli'

schemasheets/examine_schemasheet.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

schemasheets/generate_populate.py

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
import logging
2+
from typing import List
3+
4+
import pkg_resources
15
import yaml
26
from click import command, option, Choice
37
from jsonasobj2 import as_dict
48
from linkml_runtime import SchemaView
9+
510
from schemasheets.conf.configschema import ColumnSettings
611
from schemasheets.schema_exporter import SchemaExporter
712
from schemasheets.schemasheet_datamodel import TableConfig, ColumnConfig
8-
import logging
9-
import pkg_resources
13+
from typing import List, Dict
1014

1115
# todo write tests
1216

@@ -75,29 +79,33 @@ def setup_logging(log_file=None, log_level=logging.INFO):
7579
)
7680

7781

78-
def tabulate_unique_values(list_):
82+
def tabulate_unique_values(value_list):
7983
"""
8084
tabulate the number of appearances of each unique values in a list
8185
82-
Args:
83-
list_ (list): The list to tabulate.
84-
85-
Returns:
86-
dict: A dict mapping each unique value to the number of times it appears in the list.
86+
:param value_list: The list to tabulate, potentially with repeat occurrences of list items.
87+
:returns: A dict mapping each unique value to the number of times it appears in the list.
8788
"""
8889

89-
unique_values = set(list_)
90+
unique_values = set(value_list)
9091
value_counts = {}
9192
for value in unique_values:
92-
count = list_.count(value)
93+
count = value_list.count(value)
9394
value_counts[value] = count
9495

9596
sorted_value_counts = sorted(value_counts.items(), key=lambda x: x[1], reverse=True)
9697

9798
return sorted_value_counts
9899

99100

100-
def discover_source_usage(source_view):
101+
def discover_source_usage(source_view: SchemaView) -> tuple[List[str], List[str]]:
102+
"""
103+
Discover the meta slots and annotations used in the source code.
104+
105+
:param source_view: A SchemaView of the source schema.
106+
:returns: A tuple of the meta slots and annotations that were discovered as used in the source schema.
107+
"""
108+
101109
discovered_meta_slots = []
102110
discovered_annotations = []
103111
source_classes = source_view.all_classes()
@@ -125,9 +133,27 @@ def discover_source_usage(source_view):
125133
return discovered_meta_slots, discovered_annotations
126134

127135

128-
def do_usage_report(style, meta_view, meta_type_names, meta_enum_names, discovered_annotations,
129-
discovered_source_slots, logger):
130-
logger.warning(style)
136+
def do_usage_report(
137+
style: str,
138+
meta_view: SchemaView,
139+
meta_type_names: List[str],
140+
meta_enum_names: List[str],
141+
discovered_annotations: List[str],
142+
discovered_source_slots: List[str],
143+
logger: logging.Logger,
144+
) -> tuple[TableConfig, Dict[str, Dict[str, str]]]:
145+
"""
146+
Perform a usage report on the metamodel.
147+
148+
:param style: The style of the usage report.
149+
:param meta_view: The metamodel to analyze.
150+
:param meta_type_names: The list of types discovered in the metamodel.
151+
:param meta_enum_names: The list of enums discovered in the metamodel.
152+
:param discovered_annotations: The list of annotations discovered in the source schema.
153+
:param discovered_source_slots: The list of metaslots used in the source 's SLotDefinitions and ClassDefinitions.
154+
:param logger: The logger to use.
155+
:returns: A tuple of the table config and the untemplateable metaslots.
156+
"""
131157

132158
columns_dict = {}
133159

@@ -154,7 +180,11 @@ def do_usage_report(style, meta_view, meta_type_names, meta_enum_names, discover
154180
current_induced_slots = meta_view.class_induced_slots(current_root)
155181
for cis in current_induced_slots:
156182

157-
temp_dict = {"range": cis.range, "multivalued": cis.multivalued, "type_range": cis.range in meta_type_names}
183+
temp_dict = {
184+
"range": cis.range,
185+
"multivalued": cis.multivalued,
186+
"type_range": cis.range in meta_type_names,
187+
}
158188
if style == "exhaustive" or cis.name in discovered_source_slots:
159189
if cis.name not in slot_scan_results:
160190
slot_scan_results[cis.name] = temp_dict
@@ -274,6 +304,13 @@ def do_usage_report(style, meta_view, meta_type_names, meta_enum_names, discover
274304
def cli(source_path, output_path, debug_report_path, verbose, log_file, report_style):
275305
"""
276306
A CLI tool to generate a slot usage schemasheet from the LinkML metamodel and a source schema.
307+
308+
:param source_path: A filesystem or URL path to the schema that should be analysed and reported
309+
:param output_path: The filesystem path where the generated schemasheet will be written.
310+
:param debug_report_path: An optional filesystem path where a YAML report of excluded metaslots will be saved.
311+
:param verbose: If true, DEBUG logging will be used.
312+
:param log_file: An optional filesystem path where log messages will be saved.
313+
:param report_style: exhaustive means that all non-excluded metaslots will be included. concise means that only those metaslots used by the source schema will be included.
277314
"""
278315

279316
# in some cases it will be better to get this from a local filesystem, not a URL...

schemasheets/schema_exporter.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@ def _configuration_has_primary_keys_for(table_config: TableConfig, metatype: str
2727
return False
2828

2929

30-
def get_fields(cls):
30+
def get_fields(cls: type) -> List[str]:
3131
"""
3232
Get the fields in a class.
3333
34-
Args:
35-
cls (type): The class to get the fields from.
36-
37-
Returns:
38-
list: A list of the fields in the class.
34+
:param cls: The class to get the fields from.
35+
:returns: fields present in the inputs class, as a list of strings
3936
"""
4037

4138
fields: list[str] = []
@@ -47,14 +44,21 @@ def get_fields(cls):
4744

4845

4946
def infer_descriptor_rows(table_config: TableConfig) -> List[ROW]:
47+
"""
48+
Infers SchemaSheet descriptor rows, as required by SchemaExporter, for a given TableConfig.
49+
50+
:param table_config: The TableConfig to infer the descriptor rows for.
51+
:returns: A list of descriptor rows.
52+
"""
53+
5054
cs_fields = get_fields(ColumnSettings)
5155
cs_fields.sort()
5256

53-
handled_attributes = ["header", ] + cs_fields
57+
desired_schemasheets_columns = ["header", ] + cs_fields
5458

5559
descriptor_rows: list[dict[str, str]] = []
5660

57-
for ha in handled_attributes:
61+
for schemasheet_col in desired_schemasheets_columns:
5862
index = 0
5963
temp_dict = {}
6064
i_s_count = 0
@@ -65,18 +69,18 @@ def infer_descriptor_rows(table_config: TableConfig) -> List[ROW]:
6569
prefix = '>'
6670

6771
# todo differentiate between a verbatim header and a slugged element_name
68-
if ha == "header":
72+
if schemasheet_col == "header":
6973
temp_dict[tcck] = f"{prefix}{tccv.name}"
7074

71-
elif ha == "internal_separator":
75+
elif schemasheet_col == "internal_separator":
7276
i_s = tccv.settings.internal_separator
7377
if i_s:
7478
temp_dict[tcck] = f'{prefix}internal_separator: "{i_s}"'
7579
i_s_count += 1
7680
else:
7781
temp_dict[tcck] = f'{prefix}'
7882

79-
elif ha == "inner_key":
83+
elif schemasheet_col == "inner_key":
8084
i_k = tccv.settings.inner_key
8185
if i_k:
8286
temp_dict[tcck] = f'{prefix}inner_key: "{i_k}"'
@@ -86,10 +90,10 @@ def infer_descriptor_rows(table_config: TableConfig) -> List[ROW]:
8690

8791
index += 1
8892

89-
if ha == "internal_separator" and i_s_count == 0:
93+
if schemasheet_col == "internal_separator" and i_s_count == 0:
9094
temp_dict = {}
9195

92-
if ha == "inner_key" and i_k_count == 0:
96+
if schemasheet_col == "inner_key" and i_k_count == 0:
9397
temp_dict = {}
9498

9599
if temp_dict:
@@ -181,6 +185,7 @@ def export_element(self, element: Element, parent: Optional[Element], schemaview
181185
:param table_config:
182186
:return:
183187
"""
188+
184189
# Step 1: determine both primary key (pk) column, a pk of any parent
185190
pk_col = None
186191
parent_pk_col = None

scripts.makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ scripts-clean:
1010
rm -rf populated-with-generated-spec.tsv
1111

1212
populated-with-generated-spec.tsv: schemasheets/conf/configschema.yaml
13-
$(RUN) generate-populate \
13+
$(RUN) linkml2schemasheets-template \
1414
--debug-report-path populated-generated-debug-report.yaml \
1515
--log-file populated-with-generated-spec-log.txt \
1616
--output-path $@ \

0 commit comments

Comments
 (0)