Skip to content

Commit 3056578

Browse files
committed
cli-tidying
1 parent 9528000 commit 3056578

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

src/semsql/builder/cli.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import click
44
from linkml_runtime import SchemaView
55
from linkml_runtime.utils.formatutils import underscore
6+
from semsql.linkml import path_to_schema
67
from sqlalchemy import text
78

89
import semsql.builder.builder as builder
@@ -13,7 +14,7 @@
1314
@click.option("-v", "--verbose", count=True)
1415
@click.option("-q", "--quiet")
1516
def main(verbose: int, quiet: bool):
16-
"""Run the SSSOM CLI."""
17+
"""Run the SemSQL CLI."""
1718
if verbose >= 2:
1819
logging.basicConfig(level=logging.DEBUG)
1920
elif verbose == 1:
@@ -82,41 +83,40 @@ def query(input, query):
8283

8384

8485
@main.command()
85-
@click.argument("inputs", nargs=-1)
86+
@click.argument("views", nargs=-1)
8687
@click.option("--index/--no-index", default=True, help="Create indexes on each column")
8788
@click.option(
88-
"--name",
89-
"-n",
90-
help="Name of class/view to materialize. If blank, will perform for ALL",
89+
"--schema",
90+
"-s",
91+
help="Path o schema (optional)",
9192
)
92-
def view2table(inputs, name: str, index: bool):
93+
def view2table(views, schema, index: bool):
9394
"""
9495
Generates a command that turns a view into a table
9596
96-
See https://github.com/cmungall/semantic-sql/issues/9
97-
9897
Example usage:
99-
```
100-
semsql view2table src/linkml/rdf.yaml -n rdfs_label_statement | sqlite3 db/pato.db
101-
```
98+
99+
semsql view2table rdfs_label_statement | sqlite3 db/pato.db
100+
102101
"""
103-
for input in inputs:
104-
sv = SchemaView(input)
105-
for cn, c in sv.all_classes().items():
106-
tn = underscore(cn)
107-
if name is None or str(cn) == name or tn == name:
108-
view = get_viewdef(c)
109-
if view is not None:
110-
print(f"DROP VIEW {tn};")
111-
print(f"CREATE TABLE {tn} AS {view};")
112-
if index:
113-
for sn in sv.class_slots(cn):
114-
colname = underscore(sn)
115-
print(
116-
f"CREATE INDEX {tn}_{colname} ON {tn}({colname});"
117-
)
118-
else:
119-
logging.error(f"No view for {cn}")
102+
if not schema:
103+
schema = str(path_to_schema())
104+
sv = SchemaView(schema)
105+
for cn, c in sv.all_classes().items():
106+
tn = underscore(cn)
107+
if not views or str(cn) in views or tn in views:
108+
view = get_viewdef(c)
109+
if view is not None:
110+
print(f"DROP VIEW {tn};")
111+
print(f"CREATE TABLE {tn} AS {view};")
112+
if index:
113+
for sn in sv.class_slots(cn):
114+
colname = underscore(sn)
115+
print(
116+
f"CREATE INDEX {tn}_{colname} ON {tn}({colname});"
117+
)
118+
else:
119+
logging.error(f"No view for {cn}")
120120

121121

122122
if __name__ == "__main__":

src/semsql/linkml/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from pathlib import Path
2+
3+
this_path = Path(__file__).parent
4+
5+
def path_to_schema(schema_name: str = "semsql") -> Path:
6+
"""
7+
Returns path to the name of a schema
8+
9+
:param schema_name: defaults to global schema (semsql)
10+
:return:
11+
"""
12+
return this_path / f"{schema_name}.yaml"

tests/test_builder/test_cli.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def test_main_help(self):
2929
self.assertIn("make", out)
3030
self.assertIn("download", out)
3131
self.assertEqual(0, result.exit_code)
32+
self.assertEqual(0, self.runner.invoke(main, ["download", "--help"]).exit_code)
33+
self.assertEqual(0, self.runner.invoke(main, ["make", "--help"]).exit_code)
34+
self.assertEqual(0, self.runner.invoke(main, ["query", "--help"]).exit_code)
35+
self.assertEqual(0, self.runner.invoke(main, ["view2table", "--help"]).exit_code)
36+
37+
def test_view2table(self):
38+
result = self.runner.invoke(main, ["view2table", TEST_DB])
39+
out = result.stdout
40+
err = result.stderr
41+
self.assertEqual(0, result.exit_code)
3242

3343
@unittest.skip("Requires Docker or installing dependencies")
3444
def test_make_db(self):

0 commit comments

Comments
 (0)