-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
370 lines (317 loc) · 13.2 KB
/
Copy pathapp.py
File metadata and controls
370 lines (317 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import logging
from enum import Enum
from pathlib import Path
from typing import Annotated, Callable, List
import dlt
import duckdb
import typer
from dlt.common.pipeline import LoadInfo
from dlt.extract.resource import DltResource
from dlt.extract.source import DltSource
from openhound.cli.collect import collect
from openhound.cli.convert import convert
from openhound.cli.preproc import preprocess
from openhound.core.asset import BaseAsset, EdgeDef, NodeDef
from openhound.core.collect import CollectContext, Collector
from openhound.core.convert import ConvertContext, Converter, Method
from openhound.core.models.extension import Extension
from openhound.core.preproc import PreProcContext, PreProcessor
from openhound.core.progress import Progress
from openhound.core.resources import safe_resource_wrapper
logger = logging.getLogger(__name__)
OutputPath = Annotated[
Path,
typer.Argument(
exists=False,
file_okay=False,
dir_okay=True,
resolve_path=True,
),
]
InputPath = Annotated[
Path,
typer.Argument(
exists=True, file_okay=True, dir_okay=True, readable=True, resolve_path=True
),
]
DEFAULT_LOOKUP_FILE = Path("lookup.duckdb")
class Contract(str, Enum):
evolve = "evolve"
freeze = "freeze"
discard_value = "discard_value"
discard_row = "discard_row"
class OpenHound:
def __init__(self, name: str, source_kind: str, help: str = "OpenGraph collector"):
self.name = name
self.source_kind = source_kind
self.help = help
# Extension metadata is loaded and validated when the extension is loaded in the CollectorManager
self.metadata: Extension | None = None
# Store the collect/convert/preproc methods for this source
self.collector: Callable | None = None
self.converter: Callable | None = None
self.preprocessor: Callable | None = None
self.lookup_factory: Callable | None = None
# Store DLT resources/transformers for this source to be used when building the DLT pipeline
self.dlt_source: DltSource | None = None
self.dlt_resources: list[DltResource] = []
self.dlt_transformers: list[DltResource] = []
self.table_contract: Contract = Contract.evolve
self.data_type_contract: Contract = Contract.freeze
self.columns_contract: Contract = Contract.evolve
# Store the graph definitions for this source
self.assets: list[BaseAsset] = []
self.nodes: list[NodeDef] = []
self.edges: list[EdgeDef] = []
def collect(
self,
help: str = "OpenGraph collect pipeline",
**kwargs,
):
"""Register a Typer CLI command that collects resources and stores them (filtered) on disk.
Args:u
name (str): Dataset name used for the DLT pipeline.
help (str, optional): Typer CLI help text.
progress (Literal["tqdm", "log", "alive_progress"], optional): Progress backend. Log is preferred for production use and alive_progress for interactive use.
"""
def decorator(func: Callable):
def wrapper(
output_path: OutputPath,
resources: List[str] = typer.Argument(None),
progress: Progress = typer.Option(
Progress.tqdm, help="Select progress tracker option"
),
tables: Contract = typer.Option(
Contract.evolve,
help="Contract applied when data contains newly seen resources/tables previously not collected",
),
columns: Contract = typer.Option(
Contract.evolve,
help="Contract applied when data contains values/keys not found in the Pydantic model",
),
data_type: Contract = typer.Option(
Contract.freeze,
help="Contract applied when fields do not match the data types defined in the Pydantic model",
),
) -> LoadInfo | None:
collector = Collector(
name=self.name,
output_path=output_path,
resources=resources,
progress=progress,
)
# TODO: Implement data/table/column contracts
# self.data_type_contract = data_type
# self.columns_contract = columns
# self.table_contract = tables
ctx = CollectContext(pipeline=collector)
source_method: DltSource = func(ctx)
if source_method:
return collector.run(source_method)
logger.debug(f"Registering collect command for {self.name}")
self.collector = wrapper
decorated = collect.command(name=self.name, help=help)(wrapper)
return decorated
return decorator
def convert(
self,
lookup: Callable | None = None,
help: str = "OpenGraph convert pipeline",
**typer_kwargs,
):
"""Register a Typer CLI command that converts collected resources to OpenGraph nodes and edges.
Args:
name (str): Dataset name used for the DLT pipeline.
lookup (Callable): Lookup helper from a DuckDB connection.
help (str, optional): Typer CLI help text.
progress (Literal["tqdm", "log", "alive_progress"], optional): Progress backend. Log is preferred for producteion use and alive_progress for interactive use.
"""
self.lookup_factory = lookup
def decorator(func: Callable):
def run_convert(
input_path: InputPath,
output_path: Path = Path("/tmp/openhound"),
lookup_file: Path = DEFAULT_LOOKUP_FILE,
progress: Progress = Progress.tqdm,
method: Method = Method.write,
) -> LoadInfo:
lookup_session = None
if lookup:
client = duckdb.connect(str(lookup_file), read_only=True)
lookup_session = lookup(client)
if isinstance(progress, str):
progress = Progress(progress)
converter = Converter(
name=self.name,
source_kind=self.source_kind,
input_path=input_path,
output_path=output_path,
lookup=lookup_session,
progress=progress,
method=method,
)
source_method, extra_context = func(
ConvertContext(
input_path=input_path,
output_path=output_path,
lookup=lookup_session,
pipeline=converter,
)
)
return converter.run(
source_method,
graph_resources=self.assets,
extra_context=extra_context,
)
def wrapper(
input_path: InputPath,
output_path: Annotated[
Path,
typer.Argument(
exists=False,
file_okay=False,
dir_okay=True,
resolve_path=True,
help="Output path to write OpenGraph JSON files",
),
],
# resources: List[str] = typer.Argument(None),
progress: Progress = typer.Option(
Progress.tqdm, help="Select progress tracker option"
),
lookup_file: Annotated[
Path,
typer.Option(
file_okay=True,
dir_okay=False,
readable=True,
resolve_path=True,
help="DuckDB lookup file path",
),
] = DEFAULT_LOOKUP_FILE,
) -> LoadInfo:
return run_convert(
input_path=input_path,
output_path=output_path,
lookup_file=lookup_file,
progress=progress,
method=Method.write,
)
logger.debug(f"Registering convert command for {self.name}")
self.converter = run_convert
decorated = convert.command(name=self.name, help=help, **typer_kwargs)(
wrapper
)
return decorated
return decorator
def preproc(
self,
transformer: Callable[[any], None] | None = None,
help: str = "OpenGraph preprocessing pipeline",
**typer_kwargs,
):
"""Register a Typer CLI command that performs optional preprocessing and builds lookup data for a source.
Args:
transformer (Callable, optional): Optional transformation function that takes a DuckDB connection and performs transformations.
help (str, optional): CLI help text.
progress (Literal["tqdm", "log", "alive_progress"], optional): Progress backend. Log is preferred for production use and alive_progress for interactive use.
"""
def decorator(func: Callable):
self.preprocessor = func
def wrapper(
input_path: InputPath,
output_file: Annotated[
Path,
typer.Argument(
file_okay=True,
dir_okay=False,
readable=True,
resolve_path=True,
),
] = DEFAULT_LOOKUP_FILE,
progress: Progress = typer.Option(
Progress.tqdm, help="Select progress tracker option"
),
) -> LoadInfo:
preprocessor = PreProcessor(
name=self.name,
input_path=input_path,
output_file=output_file,
progress=progress,
transformer=transformer,
)
resource_list = func(PreProcContext(pipeline=preprocessor))
return preprocessor.run(resources=resource_list)
logger.debug(f"Registering preproc command for {self.name}")
self.preprocessor = wrapper
decorated = preprocess.command(name=self.name, help=help, **typer_kwargs)(
wrapper
)
return decorated
return decorator
def transformer(
self,
*dlt_args,
**dlt_kwargs,
):
"""Decorator to register a DLT transformer with added exception handling."""
def decorator(func: Callable) -> DltResource:
transformer_name = dlt_kwargs.get("name", func.__name__)
safe_func = safe_resource_wrapper(func, transformer_name)
decorated = dlt.transformer(safe_func, *dlt_args, **dlt_kwargs)
self.dlt_resources.append(decorated)
return decorated # type: ignore
logger.debug(f"Registering transformer for {self.name}")
return decorator
def resource(
self,
*dlt_args,
**dlt_kwargs,
):
"""Decorator to register a DLT resource with added exception handling."""
def decorator(func: Callable) -> DltResource:
resource_name = dlt_kwargs.get("name", func.__name__)
safe_func = safe_resource_wrapper(func, resource_name)
decorated = dlt.resource(safe_func, *dlt_args, **dlt_kwargs)
self.dlt_resources.append(decorated) # type: ignore
return decorated # type: ignore
logger.debug(f"Registering resource for {self.name}")
return decorator
def source(
self,
*dlt_args,
**dlt_kwargs,
):
"""Decorator to register a DLT source with added exception handling.
Args:
name (str): Dataset name used for the DLT pipeline.
"""
def decorator(func: Callable) -> Callable:
decorated = dlt.source(func, *dlt_args, **dlt_kwargs)
self.dlt_source = decorated # type: ignore
return decorated # type: ignore
logger.debug(f"Registering source for {self.name}")
return decorator
def asset(
self,
node: NodeDef | None = None,
edges: list[EdgeDef] | None = None,
description: str = "Resource model for OpenGraph",
):
"""Decorator to register a resource class and its graph definitions (nodes/edges). This is used to automatically
generate documentation for each unique resource and implement rules/warnings when nodes/edges are returned
which are not declared.
Args:
name (str): Resource name
description (str, optional): Description of the resource
"""
def decorator(func: BaseAsset):
self.assets.append(func)
if node:
self.nodes.append(node)
if edges:
for edge in edges:
self.edges.append(edge)
return func
logger.debug(f"Registering asset for {self.name}")
return decorator