prograph 1.3.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- prograph/__init__.py +36 -0
- prograph/__main__.py +5 -0
- prograph/_version.py +375 -0
- prograph/cli/__init__.py +1 -0
- prograph/cli/_iri_utils.py +124 -0
- prograph/cli/emit_genie_space.py +107 -0
- prograph/cli/generate_views.py +140 -0
- prograph/cli/main.py +420 -0
- prograph/cli/schema_inferrer.py +746 -0
- prograph/cli/ttl_loader.py +289 -0
- prograph/cli/upload_genie_space.py +84 -0
- prograph/config/__init__.py +27 -0
- prograph/config/yaml_config.py +848 -0
- prograph/demos/__init__.py +14 -0
- prograph/demos/turbofan/README.md +74 -0
- prograph/demos/turbofan/SCENARIO.md +209 -0
- prograph/demos/turbofan/__init__.py +75 -0
- prograph/demos/turbofan/create_tables.sql +206 -0
- prograph/demos/turbofan/data/engine-instances.ttl +1123 -0
- prograph/demos/turbofan/data/sensor-timeseries.csv +90 -0
- prograph/demos/turbofan/demo.py +162 -0
- prograph/demos/turbofan/genie_qa_notebook.py +434 -0
- prograph/demos/turbofan/graph.yaml +1098 -0
- prograph/demos/turbofan/instance_graph_demo.py +32 -0
- prograph/demos/turbofan/load.py +223 -0
- prograph/demos/turbofan/mapping.r2rml.ttl +991 -0
- prograph/demos/turbofan/ontology/engine-maintenance.ttl +606 -0
- prograph/demos/turbofan/payload-shapes.ttl +33 -0
- prograph/demos/turbofan/queries/example-queries.sparql +215 -0
- prograph/demos/turbofan/shapes.ttl +75 -0
- prograph/demos/turbofan/tables.py +106 -0
- prograph/demos/turbofan/turbofan_schema.py +393 -0
- prograph/demos/turbofan/validation_demo.py +153 -0
- prograph/describe.py +231 -0
- prograph/endpoint/__init__.py +12 -0
- prograph/endpoint/app.py +722 -0
- prograph/endpoint/cli.py +280 -0
- prograph/endpoint/credentials.py +240 -0
- prograph/endpoint/engines.py +418 -0
- prograph/endpoint/negotiate.py +101 -0
- prograph/endpoint/results.py +181 -0
- prograph/endpoint/service.py +31 -0
- prograph/engine.py +2718 -0
- prograph/execution.py +455 -0
- prograph/executors/__init__.py +6 -0
- prograph/executors/base.py +11 -0
- prograph/executors/databricks_sql_executor.py +51 -0
- prograph/executors/duckdb_executor.py +24 -0
- prograph/executors/mysql_executor.py +20 -0
- prograph/executors/oracle_executor.py +24 -0
- prograph/executors/pandas_executor.py +72 -0
- prograph/executors/postgresql_executor.py +35 -0
- prograph/executors/spark_executor.py +88 -0
- prograph/executors/sqlserver_executor.py +35 -0
- prograph/extras/__init__.py +1 -0
- prograph/extras/graphql/__init__.py +33 -0
- prograph/extras/graphql/execute.py +25 -0
- prograph/extras/graphql/schema.py +314 -0
- prograph/extras/instance_graph.py +559 -0
- prograph/genie/__init__.py +83 -0
- prograph/genie/databricks_uploader.py +306 -0
- prograph/genie/deploy.py +393 -0
- prograph/genie/formats.py +414 -0
- prograph/genie/instruction_generator.py +375 -0
- prograph/genie/sample_query_generator.py +256 -0
- prograph/genie/shacl_views.py +318 -0
- prograph/genie/space_emitter.py +329 -0
- prograph/genie/view_generator.py +628 -0
- prograph/genie/view_writer.py +172 -0
- prograph/graph/__init__.py +1 -0
- prograph/graph/ansi_session.py +348 -0
- prograph/graph/databricks_sql_graph.py +300 -0
- prograph/graph/drivers.py +143 -0
- prograph/graph/duckdb_graph.py +68 -0
- prograph/graph/mysql_graph.py +38 -0
- prograph/graph/oracle_graph.py +285 -0
- prograph/graph/pandas_graph.py +343 -0
- prograph/graph/postgresql_graph.py +644 -0
- prograph/graph/spark_graph.py +556 -0
- prograph/graph/sql_graph.py +83 -0
- prograph/graph/sqlserver_graph.py +72 -0
- prograph/hints.py +95 -0
- prograph/logging.py +550 -0
- prograph/mcp/__init__.py +29 -0
- prograph/mcp/__main__.py +180 -0
- prograph/mcp/bootstrap.py +98 -0
- prograph/mcp/server.py +601 -0
- prograph/mcp/sparql.py +397 -0
- prograph/mcp/tools.py +504 -0
- prograph/nlp/__init__.py +79 -0
- prograph/nlp/client.py +305 -0
- prograph/nlp/databricks_tools.py +369 -0
- prograph/nlp/graph_context.py +313 -0
- prograph/nlp/interface.py +652 -0
- prograph/nlp/prompts/__init__.py +19 -0
- prograph/nlp/prompts/examples.py +258 -0
- prograph/nlp/prompts/system.py +239 -0
- prograph/nlp/providers/__init__.py +14 -0
- prograph/nlp/providers/anthropic.py +274 -0
- prograph/nlp/providers/databricks.py +312 -0
- prograph/nlp/providers/openai.py +203 -0
- prograph/nlp/schema_context.py +78 -0
- prograph/ontology/__init__.py +53 -0
- prograph/ontology/alignment.py +542 -0
- prograph/ontology/hierarchy.py +310 -0
- prograph/ontology/imports.py +587 -0
- prograph/ontology/layer.py +991 -0
- prograph/ontology/skos.py +61 -0
- prograph/owl/__init__.py +27 -0
- prograph/owl/defined_classes.py +323 -0
- prograph/parameters.py +242 -0
- prograph/parser/__init__.py +1 -0
- prograph/parser/cypher/CypherLexer.py +836 -0
- prograph/parser/cypher/CypherListener.py +966 -0
- prograph/parser/cypher/CypherParser.py +10854 -0
- prograph/parser/cypher/CypherVisitor.py +543 -0
- prograph/parser/cypher/__init__.py +1 -0
- prograph/parser/sparql/SparqlLexer.py +801 -0
- prograph/parser/sparql/SparqlParser.py +10146 -0
- prograph/parser/sparql/SparqlParserListener.py +1056 -0
- prograph/parser/sparql/SparqlParserVisitor.py +593 -0
- prograph/parser/sparql/__init__.py +8 -0
- prograph/plan/__init__.py +3 -0
- prograph/plan/ir/__init__.py +1 -0
- prograph/plan/ir/casts.py +338 -0
- prograph/plan/ir/consistency.py +397 -0
- prograph/plan/ir/construct.py +90 -0
- prograph/plan/ir/cost.py +234 -0
- prograph/plan/ir/cypher_answer.py +1081 -0
- prograph/plan/ir/cypher_apoc.py +717 -0
- prograph/plan/ir/cypher_apoc_convert.py +505 -0
- prograph/plan/ir/cypher_apoc_date.py +845 -0
- prograph/plan/ir/cypher_apoc_map.py +496 -0
- prograph/plan/ir/cypher_apoc_meta.py +384 -0
- prograph/plan/ir/cypher_apoc_path.py +597 -0
- prograph/plan/ir/cypher_apoc_procedures.py +163 -0
- prograph/plan/ir/cypher_apoc_text.py +785 -0
- prograph/plan/ir/cypher_front.py +5474 -0
- prograph/plan/ir/cypher_functions.py +565 -0
- prograph/plan/ir/cypher_lists.py +935 -0
- prograph/plan/ir/cypher_maps.py +370 -0
- prograph/plan/ir/cypher_mixed.py +604 -0
- prograph/plan/ir/cypher_ontology.py +226 -0
- prograph/plan/ir/cypher_procedures.py +191 -0
- prograph/plan/ir/cypher_runtime_errors.py +107 -0
- prograph/plan/ir/cypher_temporal.py +1357 -0
- prograph/plan/ir/cypher_temporal_arith.py +339 -0
- prograph/plan/ir/cypher_values.py +546 -0
- prograph/plan/ir/cypher_write.py +612 -0
- prograph/plan/ir/dataset.py +643 -0
- prograph/plan/ir/dates.py +102 -0
- prograph/plan/ir/entry.py +992 -0
- prograph/plan/ir/expr.py +260 -0
- prograph/plan/ir/facts.py +1171 -0
- prograph/plan/ir/fresh.py +291 -0
- prograph/plan/ir/freshness.py +625 -0
- prograph/plan/ir/identity.py +797 -0
- prograph/plan/ir/java_datetime.py +265 -0
- prograph/plan/ir/java_regex.py +391 -0
- prograph/plan/ir/join_order.py +39 -0
- prograph/plan/ir/key_checks.py +433 -0
- prograph/plan/ir/keys.py +454 -0
- prograph/plan/ir/lower.py +5323 -0
- prograph/plan/ir/ops.py +619 -0
- prograph/plan/ir/owl_rl.py +1119 -0
- prograph/plan/ir/owl_rl_rules.py +725 -0
- prograph/plan/ir/profiles.py +694 -0
- prograph/plan/ir/ql.py +987 -0
- prograph/plan/ir/rdfs.py +342 -0
- prograph/plan/ir/rewrite.py +1782 -0
- prograph/plan/ir/saturate.py +339 -0
- prograph/plan/ir/semantics.py +103 -0
- prograph/plan/ir/slot.py +509 -0
- prograph/plan/ir/sparql_front.py +3418 -0
- prograph/plan/ir/strings.py +534 -0
- prograph/plan/ir/term_functions.py +274 -0
- prograph/plan/ir/terms.py +84 -0
- prograph/plan/ir/xpath_regex.py +264 -0
- prograph/plan/run_record.py +65 -0
- prograph/plan/sql/__init__.py +21 -0
- prograph/plan/sql/dialect.py +3726 -0
- prograph/plan/sql/mysql_serializer.py +103 -0
- prograph/plan/sql/names.py +65 -0
- prograph/plan/sql/oracle_serializer.py +306 -0
- prograph/plan/sql/pg_serializer.py +501 -0
- prograph/plan/sql/rel_algebra.py +781 -0
- prograph/plan/sql/sql_serializer.py +1419 -0
- prograph/plan/sql/sqlserver_serializer.py +603 -0
- prograph/provenance.py +222 -0
- prograph/r2rml/__init__.py +48 -0
- prograph/r2rml/catalog.py +259 -0
- prograph/r2rml/compile.py +372 -0
- prograph/r2rml/direct.py +140 -0
- prograph/r2rml/export.py +495 -0
- prograph/r2rml/model.py +180 -0
- prograph/r2rml/parser.py +546 -0
- prograph/r2rml/schema.py +120 -0
- prograph/read_only.py +122 -0
- prograph/results.py +157 -0
- prograph/rules/__init__.py +168 -0
- prograph/rules/__main__.py +32 -0
- prograph/schema/__init__.py +60 -0
- prograph/schema/change_set.py +50 -0
- prograph/schema/collations.py +198 -0
- prograph/schema/compile_schema.py +388 -0
- prograph/schema/context.py +845 -0
- prograph/schema/examples.py +234 -0
- prograph/schema/iri.py +817 -0
- prograph/schema/literals.py +94 -0
- prograph/schema/lpg_view.py +617 -0
- prograph/schema/mapping.py +1803 -0
- prograph/schema/maps_schema.py +40 -0
- prograph/schema/naming.py +77 -0
- prograph/schema/query_sources.py +59 -0
- prograph/schema/rows.py +215 -0
- prograph/schema/source_map.py +1103 -0
- prograph/schema/store_view.py +459 -0
- prograph/schema/types.py +569 -0
- prograph/schema/xsd.py +569 -0
- prograph/shacl/__init__.py +30 -0
- prograph/shacl/dqv.py +58 -0
- prograph/shacl/generate.py +255 -0
- prograph/shacl/report.py +138 -0
- prograph/shacl/validator.py +164 -0
- prograph/store.py +1737 -0
- prograph/utils/__init__.py +6 -0
- prograph/utils/exceptions.py +308 -0
- prograph/utils/helpers.py +126 -0
- prograph/warnings.py +46 -0
- prograph/writes.py +369 -0
- prograph-1.3.3.dist-info/METADATA +281 -0
- prograph-1.3.3.dist-info/RECORD +237 -0
- prograph-1.3.3.dist-info/WHEEL +5 -0
- prograph-1.3.3.dist-info/entry_points.txt +3 -0
- prograph-1.3.3.dist-info/licenses/LICENSE +201 -0
- prograph-1.3.3.dist-info/licenses/NOTICE +10 -0
- prograph-1.3.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"""TTL Loader, parse Turtle files and build pandas DataFrames.
|
|
2
|
+
|
|
3
|
+
Classifies RDF triples into nodes (rdf:type), relationships (URI objects),
|
|
4
|
+
and properties (literal objects), then groups them into per-label and
|
|
5
|
+
per-relationship-type DataFrames.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from collections import defaultdict
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
from rdflib import Graph, RDF, RDFS, OWL, Namespace, URIRef, Literal, BNode
|
|
14
|
+
RDFLIB_AVAILABLE = True
|
|
15
|
+
except ImportError:
|
|
16
|
+
RDFLIB_AVAILABLE = False
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
import pandas as pd
|
|
20
|
+
PANDAS_AVAILABLE = True
|
|
21
|
+
except ImportError:
|
|
22
|
+
PANDAS_AVAILABLE = False
|
|
23
|
+
|
|
24
|
+
from prograph.schema.naming import rel_type_from_local_name, resolve_rel_type
|
|
25
|
+
from prograph.schema.naming import column_from_local_name
|
|
26
|
+
from prograph.cli._iri_utils import (
|
|
27
|
+
extract_local_name,
|
|
28
|
+
is_ontology_triple,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _parse_literal_value(lit: Any) -> Any:
|
|
33
|
+
"""An rdflib Literal as a Python value, by the one rule for RDF data
|
|
34
|
+
(prograph.schema.literals)."""
|
|
35
|
+
from prograph.schema.literals import literal_value
|
|
36
|
+
return literal_value(lit)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _resolve_blank_node_type(graph: 'Graph', bnode: Any) -> Optional[str]:
|
|
40
|
+
"""Resolve a blank node to its rdf:type local name.
|
|
41
|
+
|
|
42
|
+
Handles patterns like: :hasHealthState [ a :Normal ] → 'Normal'
|
|
43
|
+
"""
|
|
44
|
+
if not RDFLIB_AVAILABLE:
|
|
45
|
+
return None
|
|
46
|
+
for _, _, type_obj in graph.triples((bnode, RDF.type, None)):
|
|
47
|
+
if isinstance(type_obj, URIRef):
|
|
48
|
+
return extract_local_name(str(type_obj))
|
|
49
|
+
return None
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def load_ttl(
|
|
53
|
+
files: List[str],
|
|
54
|
+
schema_path: Optional[str] = None,
|
|
55
|
+
include_ontology: bool = False,
|
|
56
|
+
) -> Dict[str, 'pd.DataFrame']:
|
|
57
|
+
"""Parse TTL files and build DataFrames per node label and relationship type.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
files: List of TTL file paths to parse.
|
|
61
|
+
schema_path: Optional path to a ProGraph YAML config. When provided,
|
|
62
|
+
property IRIs are mapped to column names defined in the schema.
|
|
63
|
+
include_ontology: If True, include OWL/RDFS schema triples
|
|
64
|
+
(owl:Class, rdfs:subClassOf, etc.). Default False.
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
Dict mapping label/rel_type names to pandas DataFrames.
|
|
68
|
+
Node DataFrames have a ``_uri`` column plus property columns.
|
|
69
|
+
Relationship DataFrames have ``_start_uri`` and ``_end_uri`` columns
|
|
70
|
+
plus any property columns.
|
|
71
|
+
|
|
72
|
+
Raises:
|
|
73
|
+
ImportError: If rdflib or pandas is not installed.
|
|
74
|
+
"""
|
|
75
|
+
if not RDFLIB_AVAILABLE:
|
|
76
|
+
raise ImportError(
|
|
77
|
+
"rdflib is required for load-ttl. Install it with: "
|
|
78
|
+
"pip install prograph[cli]"
|
|
79
|
+
)
|
|
80
|
+
if not PANDAS_AVAILABLE:
|
|
81
|
+
raise ImportError(
|
|
82
|
+
"pandas is required for load-ttl. Install it with: "
|
|
83
|
+
"pip install prograph[cli]"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
# Load optional YAML schema for column name mapping
|
|
87
|
+
schema_mappings = _load_schema_mappings(schema_path) if schema_path else None
|
|
88
|
+
|
|
89
|
+
# Parse all TTL files into a single rdflib graph
|
|
90
|
+
g = Graph()
|
|
91
|
+
for fpath in files:
|
|
92
|
+
g.parse(fpath, format='turtle')
|
|
93
|
+
|
|
94
|
+
# Classify triples
|
|
95
|
+
# subject_data[subject_uri] = {'_labels': set(), 'props': {pred_name: value, ...}}
|
|
96
|
+
subject_data: Dict[str, Dict[str, Any]] = defaultdict(
|
|
97
|
+
lambda: {'_labels': set(), '_props': {}}
|
|
98
|
+
)
|
|
99
|
+
# rel_rows[rel_type] = [{'_start_uri': ..., '_end_uri': ..., ...}, ...]
|
|
100
|
+
rel_rows: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
|
|
101
|
+
|
|
102
|
+
for subj, pred, obj in g:
|
|
103
|
+
# Skip blank node subjects (they are inlined into their parent)
|
|
104
|
+
if isinstance(subj, BNode):
|
|
105
|
+
continue
|
|
106
|
+
|
|
107
|
+
# Optionally filter out ontology triples
|
|
108
|
+
if not include_ontology and is_ontology_triple(subj, pred, obj):
|
|
109
|
+
continue
|
|
110
|
+
|
|
111
|
+
subj_str = str(subj)
|
|
112
|
+
pred_str = str(pred)
|
|
113
|
+
|
|
114
|
+
# rdf:type → label assignment
|
|
115
|
+
if pred_str == str(RDF.type):
|
|
116
|
+
obj_str = str(obj)
|
|
117
|
+
# Skip ontology types even when include_ontology=True for label purposes
|
|
118
|
+
if obj_str in (
|
|
119
|
+
str(OWL.Class), str(OWL.ObjectProperty), str(OWL.DatatypeProperty),
|
|
120
|
+
str(OWL.FunctionalProperty), str(OWL.TransitiveProperty),
|
|
121
|
+
str(OWL.SymmetricProperty), str(OWL.Ontology),
|
|
122
|
+
str(OWL.Restriction), str(RDFS.Datatype),
|
|
123
|
+
):
|
|
124
|
+
continue
|
|
125
|
+
label = extract_local_name(obj_str)
|
|
126
|
+
subject_data[subj_str]['_labels'].add(label)
|
|
127
|
+
continue
|
|
128
|
+
|
|
129
|
+
# rdfs:label and rdfs:comment → properties
|
|
130
|
+
if pred_str == str(RDFS.label):
|
|
131
|
+
prop_name = 'label'
|
|
132
|
+
value = _parse_literal_value(obj)
|
|
133
|
+
subject_data[subj_str]['_props'][prop_name] = value
|
|
134
|
+
continue
|
|
135
|
+
if pred_str == str(RDFS.comment):
|
|
136
|
+
prop_name = 'comment'
|
|
137
|
+
value = _parse_literal_value(obj)
|
|
138
|
+
subject_data[subj_str]['_props'][prop_name] = value
|
|
139
|
+
continue
|
|
140
|
+
|
|
141
|
+
# Blank node object → resolve type as property value
|
|
142
|
+
if isinstance(obj, BNode):
|
|
143
|
+
bnode_type = _resolve_blank_node_type(g, obj)
|
|
144
|
+
if bnode_type:
|
|
145
|
+
prop_name = extract_local_name(pred_str)
|
|
146
|
+
prop_name = _map_property_name(prop_name, schema_mappings)
|
|
147
|
+
subject_data[subj_str]['_props'][prop_name] = bnode_type
|
|
148
|
+
continue
|
|
149
|
+
|
|
150
|
+
# URI object → relationship
|
|
151
|
+
if isinstance(obj, URIRef):
|
|
152
|
+
rel_type = rel_type_from_local_name(extract_local_name(pred_str))
|
|
153
|
+
rel_type = _map_rel_type(rel_type, schema_mappings)
|
|
154
|
+
rel_rows[rel_type].append({
|
|
155
|
+
'_start_uri': subj_str,
|
|
156
|
+
'_end_uri': str(obj),
|
|
157
|
+
})
|
|
158
|
+
continue
|
|
159
|
+
|
|
160
|
+
# Literal object → property
|
|
161
|
+
if isinstance(obj, Literal):
|
|
162
|
+
prop_name = extract_local_name(pred_str)
|
|
163
|
+
prop_name = _map_property_name(prop_name, schema_mappings)
|
|
164
|
+
value = _parse_literal_value(obj)
|
|
165
|
+
|
|
166
|
+
# Handle multi-valued literals: first value wins for node properties
|
|
167
|
+
# (multi-valued URIs are handled as separate relationship rows above)
|
|
168
|
+
existing = subject_data[subj_str]['_props'].get(prop_name)
|
|
169
|
+
if existing is not None:
|
|
170
|
+
# Already have a value, keep the first one
|
|
171
|
+
# (Multi-valued literals for properties are unusual;
|
|
172
|
+
# multi-valued URI objects become relationship rows)
|
|
173
|
+
pass
|
|
174
|
+
else:
|
|
175
|
+
subject_data[subj_str]['_props'][prop_name] = value
|
|
176
|
+
continue
|
|
177
|
+
|
|
178
|
+
# Build DataFrames per label
|
|
179
|
+
label_rows: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
|
|
180
|
+
for subj_uri, data in subject_data.items():
|
|
181
|
+
labels = data['_labels']
|
|
182
|
+
props = data['_props']
|
|
183
|
+
if not labels:
|
|
184
|
+
continue
|
|
185
|
+
for label in labels:
|
|
186
|
+
row = {'_uri': subj_uri}
|
|
187
|
+
row.update(props)
|
|
188
|
+
label_rows[label].append(row)
|
|
189
|
+
|
|
190
|
+
# Each column one type: a predicate whose values mix datatypes gets the
|
|
191
|
+
# one rule in prograph.schema.literals.
|
|
192
|
+
from prograph.schema.literals import typed_frame
|
|
193
|
+
frames: Dict[str, pd.DataFrame] = {}
|
|
194
|
+
for label, rows in sorted(label_rows.items()):
|
|
195
|
+
frames[label] = typed_frame(rows)
|
|
196
|
+
|
|
197
|
+
# Build DataFrames per relationship type
|
|
198
|
+
for rel_type, rows in sorted(rel_rows.items()):
|
|
199
|
+
frames[rel_type] = typed_frame(rows)
|
|
200
|
+
|
|
201
|
+
return frames
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def write_frames(
|
|
205
|
+
frames: Dict[str, Any],
|
|
206
|
+
output_dir: str,
|
|
207
|
+
fmt: str = 'csv',
|
|
208
|
+
) -> None:
|
|
209
|
+
"""Write DataFrames to files in the output directory.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
frames: Dict of label/rel_type → DataFrame
|
|
213
|
+
output_dir: Directory to write files to
|
|
214
|
+
fmt: Output format ('csv' or 'parquet')
|
|
215
|
+
"""
|
|
216
|
+
out = Path(output_dir)
|
|
217
|
+
out.mkdir(parents=True, exist_ok=True)
|
|
218
|
+
|
|
219
|
+
for name, df in frames.items():
|
|
220
|
+
safe_name = column_from_local_name(name)
|
|
221
|
+
if fmt == 'parquet':
|
|
222
|
+
df.to_parquet(out / f'{safe_name}.parquet', index=False)
|
|
223
|
+
else:
|
|
224
|
+
df.to_csv(out / f'{safe_name}.csv', index=False)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
# ---------------------------------------------------------------------------
|
|
228
|
+
# Schema-aware mapping helpers
|
|
229
|
+
# ---------------------------------------------------------------------------
|
|
230
|
+
|
|
231
|
+
def _load_schema_mappings(
|
|
232
|
+
schema_path: str,
|
|
233
|
+
) -> Optional[Dict[str, Any]]:
|
|
234
|
+
"""Load a ProGraph YAML config and extract property/rel mappings."""
|
|
235
|
+
import yaml
|
|
236
|
+
path = Path(schema_path)
|
|
237
|
+
if not path.exists():
|
|
238
|
+
return None
|
|
239
|
+
with open(path, 'r') as f:
|
|
240
|
+
data = yaml.safe_load(f)
|
|
241
|
+
if not data:
|
|
242
|
+
return None
|
|
243
|
+
|
|
244
|
+
# Build reverse maps: IRI local name → column name
|
|
245
|
+
prop_map: Dict[str, str] = {}
|
|
246
|
+
rel_map: Dict[str, str] = {}
|
|
247
|
+
|
|
248
|
+
for node in data.get('nodes', []):
|
|
249
|
+
for prop_key, col_name in node.get('properties', {}).items():
|
|
250
|
+
# prop_key is the RDF local name, col_name is the DB column
|
|
251
|
+
prop_map[prop_key] = prop_key # keep the property name from schema
|
|
252
|
+
prop_map[col_name] = prop_key # allow reverse lookup too
|
|
253
|
+
|
|
254
|
+
for rel in data.get('relationships', []):
|
|
255
|
+
rel_type = rel.get('type', '')
|
|
256
|
+
rel_map[rel_type] = rel_type
|
|
257
|
+
|
|
258
|
+
return {'properties': prop_map, 'relationships': rel_map}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def _map_property_name(
|
|
262
|
+
prop_name: str,
|
|
263
|
+
schema_mappings: Optional[Dict[str, Any]],
|
|
264
|
+
) -> str:
|
|
265
|
+
"""Map an RDF property local name to a schema column name if available."""
|
|
266
|
+
if schema_mappings is None:
|
|
267
|
+
return prop_name
|
|
268
|
+
prop_map = schema_mappings.get('properties', {})
|
|
269
|
+
# Exact match
|
|
270
|
+
if prop_name in prop_map:
|
|
271
|
+
return prop_map[prop_name]
|
|
272
|
+
# Case-insensitive match
|
|
273
|
+
lower = prop_name.lower()
|
|
274
|
+
for key, val in prop_map.items():
|
|
275
|
+
if key.lower() == lower:
|
|
276
|
+
return val
|
|
277
|
+
return prop_name
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _map_rel_type(
|
|
281
|
+
rel_type: str,
|
|
282
|
+
schema_mappings: Optional[Dict[str, Any]],
|
|
283
|
+
) -> str:
|
|
284
|
+
"""Map an RDF predicate to a relationship type if available."""
|
|
285
|
+
if schema_mappings is None:
|
|
286
|
+
return rel_type
|
|
287
|
+
rel_map = schema_mappings.get('relationships', {})
|
|
288
|
+
key = resolve_rel_type(rel_type, rel_map)
|
|
289
|
+
return rel_map[key] if key is not None else rel_type
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""``prograph upload-genie-space``, CLI wrapper around :func:`deploy_space`.
|
|
2
|
+
|
|
3
|
+
Thin adapter: parses CLI args, delegates to
|
|
4
|
+
:func:`prograph.genie.deploy_space`, prints a human-friendly summary.
|
|
5
|
+
|
|
6
|
+
The same operation from Python (notebook / script / app):
|
|
7
|
+
|
|
8
|
+
.. code-block:: python
|
|
9
|
+
|
|
10
|
+
from prograph.genie import deploy_space
|
|
11
|
+
result = deploy_space(
|
|
12
|
+
config_path="graph.yaml",
|
|
13
|
+
ontology_path="ontology.ttl",
|
|
14
|
+
warehouse_id="06bb...",
|
|
15
|
+
target_catalog="workspace",
|
|
16
|
+
space_name="My Space",
|
|
17
|
+
)
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import sys
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
from typing import Any, Optional
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def upload_genie_space_command(
|
|
28
|
+
config_path: str,
|
|
29
|
+
warehouse_id: str,
|
|
30
|
+
ontology_path: Optional[str] = None,
|
|
31
|
+
space_name: Optional[str] = None,
|
|
32
|
+
space_description: str = "",
|
|
33
|
+
target_catalog: Optional[str] = None,
|
|
34
|
+
target_schema: str = "prograph_views",
|
|
35
|
+
dialect_name: str = "databricks_sql",
|
|
36
|
+
include_labels: Optional[list[str]] = None,
|
|
37
|
+
exclude_labels: Optional[list[str]] = None,
|
|
38
|
+
summary_instructions: bool = False,
|
|
39
|
+
max_sample_queries: int = 25,
|
|
40
|
+
host: Optional[str] = None,
|
|
41
|
+
token: Optional[str] = None,
|
|
42
|
+
parent_path: Optional[str] = None,
|
|
43
|
+
update_if_exists: bool = False,
|
|
44
|
+
existing_space_id: Optional[str] = None,
|
|
45
|
+
) -> dict[str, Any]:
|
|
46
|
+
"""Execute the ``upload-genie-space`` command.
|
|
47
|
+
|
|
48
|
+
Surface-compatible with the pre-W8 version; under the hood, all
|
|
49
|
+
logic is delegated to :func:`prograph.genie.deploy_space`. The
|
|
50
|
+
only thing this layer adds is the warning-to-stderr printing.
|
|
51
|
+
"""
|
|
52
|
+
from prograph.config import load_config, validate_config
|
|
53
|
+
from prograph.genie import deploy_space
|
|
54
|
+
|
|
55
|
+
# Emit warnings to stderr the way the CLI always has, the
|
|
56
|
+
# library function deliberately doesn't print, so we re-emit
|
|
57
|
+
# here for CLI users.
|
|
58
|
+
config = load_config(Path(config_path))
|
|
59
|
+
for warning in validate_config(config):
|
|
60
|
+
print(f"warning: {warning}", file=sys.stderr)
|
|
61
|
+
|
|
62
|
+
return deploy_space(
|
|
63
|
+
config_path=config_path,
|
|
64
|
+
ontology_path=ontology_path,
|
|
65
|
+
warehouse_id=warehouse_id,
|
|
66
|
+
space_name=space_name,
|
|
67
|
+
space_description=space_description,
|
|
68
|
+
target_catalog=target_catalog,
|
|
69
|
+
target_schema=target_schema,
|
|
70
|
+
dialect_name=dialect_name,
|
|
71
|
+
include_labels=include_labels,
|
|
72
|
+
exclude_labels=exclude_labels,
|
|
73
|
+
summary_instructions=summary_instructions,
|
|
74
|
+
max_sample_queries=max_sample_queries,
|
|
75
|
+
host=host,
|
|
76
|
+
token=token,
|
|
77
|
+
parent_path=parent_path,
|
|
78
|
+
update_if_exists=update_if_exists,
|
|
79
|
+
existing_space_id=existing_space_id,
|
|
80
|
+
source_label=str(Path(config_path).resolve()),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
__all__ = ["upload_genie_space_command"]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"""ProGraph Configuration Module.
|
|
2
|
+
|
|
3
|
+
Provides YAML-based configuration for graph schemas and data sources,
|
|
4
|
+
inspired by Databricks Metric Views and Asset Bundles patterns.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from .yaml_config import (
|
|
8
|
+
GraphConfig,
|
|
9
|
+
SourceConfig,
|
|
10
|
+
NodeConfig,
|
|
11
|
+
RelationshipConfig,
|
|
12
|
+
load_config,
|
|
13
|
+
load_config_from_string,
|
|
14
|
+
load_dataframes,
|
|
15
|
+
validate_config,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
'GraphConfig',
|
|
20
|
+
'SourceConfig',
|
|
21
|
+
'NodeConfig',
|
|
22
|
+
'RelationshipConfig',
|
|
23
|
+
'load_config',
|
|
24
|
+
'load_config_from_string',
|
|
25
|
+
'load_dataframes',
|
|
26
|
+
'validate_config',
|
|
27
|
+
]
|