graflo.util.onto¶
Utility ontology classes for resource patterns and configurations.
This module provides data classes for managing resource patterns (files and database tables) and configurations used throughout the system. These classes support resource discovery, pattern matching, and configuration management.
Key Components
- ResourcePattern: Abstract base class for resource patterns
- FilePattern: Configuration for file pattern matching
- TablePattern: Configuration for database table pattern matching
- Patterns: Collection of named resource patterns with connection management
FilePattern
dataclass
¶
Bases: ResourcePattern
Pattern for matching files.
Attributes:
| Name | Type | Description |
|---|---|---|
regex |
str | None
|
Regular expression pattern for matching filenames |
sub_path |
None | Path
|
Path to search for matching files (default: "./") |
date_field |
str | None
|
Name of the date field to filter on (for date-based filtering) |
date_filter |
str | None
|
SQL-style date filter condition (e.g., "> '2020-10-10'") |
date_range_start |
str | None
|
Start date for range filtering (e.g., "2015-11-11") |
date_range_days |
int | None
|
Number of days after start date (used with date_range_start) |
Source code in graflo/util/onto.py
__post_init__()
¶
Initialize and validate the file pattern.
Ensures that sub_path is a Path object and is not None.
Source code in graflo/util/onto.py
get_resource_type()
¶
Get resource type.
FilePattern always represents a FILE resource type. The specific file format (CSV, JSON, JSONL, Parquet, etc.) is automatically detected by the loader based on file extensions.
Source code in graflo/util/onto.py
matches(filename)
¶
Check if pattern matches a filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Filename to match |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if pattern matches |
Source code in graflo/util/onto.py
Patterns
dataclass
¶
Bases: BaseDataclass
Collection of named resource patterns with connection management.
This class manages a collection of resource patterns (files or tables), each associated with a name. It efficiently handles PostgreSQL connections by grouping tables that share the same connection configuration.
The constructor accepts: - resource_mapping: dict mapping resource_name -> (file_path or table_name) - postgres_connections: dict mapping config_key -> PostgresConfig where config_key identifies a connection configuration - postgres_tables: dict mapping table_name -> (config_key, schema_name, table_name)
Attributes:
| Name | Type | Description |
|---|---|---|
patterns |
dict[str, TablePattern | FilePattern]
|
Dictionary mapping resource names to ResourcePattern instances |
postgres_configs |
dict[tuple[str, str | None], Any]
|
Dictionary mapping (config_key, schema_name) to PostgresConfig |
postgres_table_configs |
dict[str, tuple[str, str | None, str]]
|
Dictionary mapping resource_name to (config_key, schema_name, table_name) |
Source code in graflo/util/onto.py
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
__post_init__()
¶
Initialize Patterns from resource mappings and PostgreSQL configurations.
Source code in graflo/util/onto.py
add_file_pattern(name, file_pattern)
¶
Add a file pattern to the collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the pattern |
required |
file_pattern
|
FilePattern
|
FilePattern instance |
required |
add_table_pattern(name, table_pattern)
¶
Add a table pattern to the collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the pattern |
required |
table_pattern
|
TablePattern
|
TablePattern instance |
required |
get_postgres_config(resource_name)
¶
Get PostgreSQL connection config for a resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_name
|
str
|
Name of the resource |
required |
Returns:
| Type | Description |
|---|---|
Any
|
PostgresConfig if resource is a PostgreSQL table, None otherwise |
Source code in graflo/util/onto.py
get_resource_type(resource_name)
¶
Get the resource type for a resource name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_name
|
str
|
Name of the resource |
required |
Returns:
| Type | Description |
|---|---|
ResourceType | None
|
ResourceType enum value or None if not found |
Source code in graflo/util/onto.py
get_table_info(resource_name)
¶
Get table name and schema for a PostgreSQL table resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_name
|
str
|
Name of the resource |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str | None] | None
|
Tuple of (table_name, schema_name) or None if not a table resource |
Source code in graflo/util/onto.py
ResourcePattern
dataclass
¶
Bases: BaseDataclass, ABC
Abstract base class for resource patterns (files or tables).
Provides common API for pattern matching and resource identification. All concrete pattern types inherit from this class.
Attributes:
| Name | Type | Description |
|---|---|---|
resource_name |
str | None
|
Name of the resource this pattern matches |
Source code in graflo/util/onto.py
get_resource_type()
abstractmethod
¶
Get the type of resource this pattern matches.
Returns:
| Name | Type | Description |
|---|---|---|
ResourceType |
ResourceType
|
Resource type enum value |
matches(resource_identifier)
abstractmethod
¶
Check if pattern matches a resource identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_identifier
|
str
|
Identifier to match (filename or table name) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if pattern matches |
Source code in graflo/util/onto.py
ResourceType
¶
Bases: BaseEnum
Resource types for data sources.
Resource types distinguish between different data source categories. File type detection (CSV, JSON, JSONL, Parquet, etc.) is handled automatically by the loader based on file extensions.
Attributes:
| Name | Type | Description |
|---|---|---|
FILE |
File-based data source (any format: CSV, JSON, JSONL, Parquet, etc.) |
|
SQL_TABLE |
SQL database table (e.g., PostgreSQL table) |
Source code in graflo/util/onto.py
TablePattern
dataclass
¶
Bases: ResourcePattern
Pattern for matching database tables.
Attributes:
| Name | Type | Description |
|---|---|---|
table_name |
str
|
Exact table name or regex pattern |
schema_name |
str | None
|
Schema name (optional, defaults to public) |
database |
str | None
|
Database name (optional) |
date_field |
str | None
|
Name of the date field to filter on (for date-based filtering) |
date_filter |
str | None
|
SQL-style date filter condition (e.g., "> '2020-10-10'") |
date_range_start |
str | None
|
Start date for range filtering (e.g., "2015-11-11") |
date_range_days |
int | None
|
Number of days after start date (used with date_range_start) |
Source code in graflo/util/onto.py
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 | |
__post_init__()
¶
Validate table pattern after initialization.
Source code in graflo/util/onto.py
build_where_clause()
¶
Build SQL WHERE clause from date filtering parameters.
Returns:
| Type | Description |
|---|---|
str
|
WHERE clause string (without the WHERE keyword) or empty string if no filters |
Source code in graflo/util/onto.py
get_resource_type()
¶
matches(table_identifier)
¶
Check if pattern matches a table name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_identifier
|
str
|
Table name to match (format: schema.table or just table) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if pattern matches |