ofwat-dataprepkit 0.4__tar.gz
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.
- ofwat_dataprepkit-0.4/LICENSE +21 -0
- ofwat_dataprepkit-0.4/PKG-INFO +27 -0
- ofwat_dataprepkit-0.4/README.md +2 -0
- ofwat_dataprepkit-0.4/dataprepkit/__init__.py +2 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers/__init__.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers/connectors/__init__.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers/connectors/lakehouse/__init__.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers/connectors/lakehouse/lakehouse.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers/connectors/warehouse.py +140 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers/dims/__init__.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers/dims/dimension_registry.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers/transforms/__init__.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/helpers.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/processors/__init__.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/processors/dimensions/__init__.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/processors/dimensions/dim_common.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/processors/dimensions/dim_company.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/processors/dimensions/dim_measure.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/processors/dimensions/dim_region.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/processors/quarterly/__init__.py +0 -0
- ofwat_dataprepkit-0.4/dataprepkit/processors/quarterly/qdata_processor.py +0 -0
- ofwat_dataprepkit-0.4/ofwat_dataprepkit.egg-info/PKG-INFO +27 -0
- ofwat_dataprepkit-0.4/ofwat_dataprepkit.egg-info/SOURCES.txt +26 -0
- ofwat_dataprepkit-0.4/ofwat_dataprepkit.egg-info/dependency_links.txt +1 -0
- ofwat_dataprepkit-0.4/ofwat_dataprepkit.egg-info/requires.txt +4 -0
- ofwat_dataprepkit-0.4/ofwat_dataprepkit.egg-info/top_level.txt +1 -0
- ofwat_dataprepkit-0.4/setup.cfg +4 -0
- ofwat_dataprepkit-0.4/setup.py +29 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Ofwat
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ofwat-dataprepkit
|
|
3
|
+
Version: 0.4
|
|
4
|
+
Summary: ETL Tools for Fabric
|
|
5
|
+
Home-page: https://github.com/Ofwat/dataprepkit
|
|
6
|
+
Author: Ofwat
|
|
7
|
+
Project-URL: Source, https://github.com/Ofwat/dataprepkit
|
|
8
|
+
Project-URL: Tracker, https://github.com/Ofwat/dataprepkit/issues
|
|
9
|
+
Keywords: fabric etl
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: pandas
|
|
13
|
+
Requires-Dist: numpy
|
|
14
|
+
Requires-Dist: sqlalchemy
|
|
15
|
+
Requires-Dist: pyodbc
|
|
16
|
+
Dynamic: author
|
|
17
|
+
Dynamic: description
|
|
18
|
+
Dynamic: description-content-type
|
|
19
|
+
Dynamic: home-page
|
|
20
|
+
Dynamic: keywords
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
Dynamic: project-url
|
|
23
|
+
Dynamic: requires-dist
|
|
24
|
+
Dynamic: summary
|
|
25
|
+
|
|
26
|
+
# dataprepkit
|
|
27
|
+
A collection of helpers and utilities for SQL-based data ingestion, transformation, and pipeline orchestration.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Fabric Warehouse SQLAlchemy Engine Utilities
|
|
3
|
+
|
|
4
|
+
This module provides utility functions to create and validate SQLAlchemy engine
|
|
5
|
+
connections to Azure Fabric data warehouses using Azure Active Directory (AAD) tokens
|
|
6
|
+
for authentication. It handles automatic detection of the latest suitable ODBC driver
|
|
7
|
+
and integrates with Fabric's `notebookutils.credentials` (or mock credentials for local testing).
|
|
8
|
+
|
|
9
|
+
Functions:
|
|
10
|
+
- get_fabric_warehouse_engine: Create a SQLAlchemy engine for a given Fabric SQL endpoint.
|
|
11
|
+
- validate_fabric_warehouse_engine: Run a test query to verify the engine connection.
|
|
12
|
+
|
|
13
|
+
Dependencies:
|
|
14
|
+
- pyodbc
|
|
15
|
+
- sqlalchemy
|
|
16
|
+
- notebookutils (optional, Fabric environment)
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import logging as _logging
|
|
20
|
+
import re as _re
|
|
21
|
+
import struct as _struct
|
|
22
|
+
import pyodbc as _pyodbc
|
|
23
|
+
import sqlalchemy as _sa
|
|
24
|
+
|
|
25
|
+
_logger = _logging.getLogger(__name__)
|
|
26
|
+
|
|
27
|
+
# Explicitly define public API
|
|
28
|
+
__all__ = [
|
|
29
|
+
"get_fabric_warehouse_engine",
|
|
30
|
+
"validate_fabric_warehouse_engine"
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
# -----------------------------
|
|
34
|
+
# Handle Fabric-only dependency
|
|
35
|
+
# -----------------------------
|
|
36
|
+
try:
|
|
37
|
+
from notebookutils import credentials as _default_credentials # type: ignore
|
|
38
|
+
except ImportError:
|
|
39
|
+
# pylint: disable=R0903
|
|
40
|
+
class _MockCredentials:
|
|
41
|
+
# pylint: disable=C0103
|
|
42
|
+
def getToken(self, resource: str) -> str:
|
|
43
|
+
"""Mocking getToken from mssparkutls"""
|
|
44
|
+
_logger.warning("Using mock credentials for resource: %s", resource)
|
|
45
|
+
return "FAKE_TOKEN_FOR_LOCAL_TESTING"
|
|
46
|
+
_default_credentials = _MockCredentials()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _get_latest_sql_driver() -> str:
|
|
50
|
+
drivers = _pyodbc.drivers() # pylint: disable=I1101
|
|
51
|
+
sql_drivers = [d for d in drivers if "SQL Server" in d or "ODBC Driver" in d]
|
|
52
|
+
if not sql_drivers:
|
|
53
|
+
raise RuntimeError("No suitable ODBC driver for SQL Server found.")
|
|
54
|
+
|
|
55
|
+
def extract_version(name: str) -> int:
|
|
56
|
+
match = _re.search(r"(\d+)", name)
|
|
57
|
+
return int(match.group(1)) if match else 0
|
|
58
|
+
|
|
59
|
+
latest_driver = max(sql_drivers, key=extract_version)
|
|
60
|
+
_logger.info("Using ODBC driver: %s", latest_driver)
|
|
61
|
+
return latest_driver
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def get_fabric_warehouse_engine(
|
|
65
|
+
sql_endpoint: str,
|
|
66
|
+
port: int = 1433,
|
|
67
|
+
credentials=_default_credentials
|
|
68
|
+
) -> _sa.engine.Engine:
|
|
69
|
+
# pylint: disable=C0301
|
|
70
|
+
"""
|
|
71
|
+
Create and return a SQLAlchemy engine connected to an Azure Fabric data warehouse.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
sql_endpoint (str): The Fabric SQL endpoint to connect to.
|
|
75
|
+
port (int, optional): The TCP port for the SQL server. Defaults to 1433.
|
|
76
|
+
credentials (optional): An object with a getToken(resource) method. Defaults to Fabric's credentials.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
_sa.engine.Engine: A SQLAlchemy Engine instance connected to the Fabric warehouse.
|
|
80
|
+
|
|
81
|
+
Raises:
|
|
82
|
+
ValueError: If `sql_endpoint` is empty or None.
|
|
83
|
+
RuntimeError: If no suitable ODBC driver is found.
|
|
84
|
+
Exception: If token retrieval or engine creation fails.
|
|
85
|
+
"""
|
|
86
|
+
if not sql_endpoint:
|
|
87
|
+
raise ValueError("sql_endpoint is required and cannot be empty.")
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
driver = _get_latest_sql_driver()
|
|
91
|
+
server = f"{sql_endpoint},{port}"
|
|
92
|
+
|
|
93
|
+
token = credentials.getToken("https://database.windows.net/").encode("UTF-16-LE")
|
|
94
|
+
token_struct = _struct.pack(f"<I{len(token)}s", len(token), token)
|
|
95
|
+
|
|
96
|
+
connection_string = f"DRIVER={{{driver}}};SERVER={server}"
|
|
97
|
+
connection_url = _sa.engine.URL.create(
|
|
98
|
+
"mssql+pyodbc",
|
|
99
|
+
query={"odbc_connect": connection_string}
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
engine = _sa.create_engine(
|
|
103
|
+
connection_url,
|
|
104
|
+
connect_args={"attrs_before": {1256: token_struct}},
|
|
105
|
+
pool_pre_ping=True,
|
|
106
|
+
pool_recycle=3600
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
_logger.info("Successfully created Fabric SQL engine.")
|
|
110
|
+
return engine
|
|
111
|
+
|
|
112
|
+
except Exception as ex:
|
|
113
|
+
_logger.error("Failed to create Fabric engine: %s", ex, exc_info=True)
|
|
114
|
+
raise
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def validate_fabric_warehouse_engine(engine: _sa.engine.Engine) -> bool:
|
|
118
|
+
"""
|
|
119
|
+
Test the connection to the Fabric warehouse engine by executing a simple query.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
engine (_sa.engine.Engine): The SQLAlchemy engine connected to the Fabric warehouse.
|
|
123
|
+
|
|
124
|
+
Returns:
|
|
125
|
+
bool: True if the test query executes successfully and returns the expected result.
|
|
126
|
+
|
|
127
|
+
Raises:
|
|
128
|
+
Exception: If the query fails to execute or returns an unexpected result.
|
|
129
|
+
"""
|
|
130
|
+
try:
|
|
131
|
+
with engine.connect() as conn:
|
|
132
|
+
result = conn.execute(_sa.text("SELECT 1"))
|
|
133
|
+
value = result.scalar()
|
|
134
|
+
if value == 1:
|
|
135
|
+
_logger.info("Fabric warehouse engine test passed.")
|
|
136
|
+
return True
|
|
137
|
+
raise RuntimeError("Unexpected result from test query.")
|
|
138
|
+
except Exception as ex:
|
|
139
|
+
_logger.error("Test query failed: %s", ex, exc_info=True)
|
|
140
|
+
raise
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ofwat-dataprepkit
|
|
3
|
+
Version: 0.4
|
|
4
|
+
Summary: ETL Tools for Fabric
|
|
5
|
+
Home-page: https://github.com/Ofwat/dataprepkit
|
|
6
|
+
Author: Ofwat
|
|
7
|
+
Project-URL: Source, https://github.com/Ofwat/dataprepkit
|
|
8
|
+
Project-URL: Tracker, https://github.com/Ofwat/dataprepkit/issues
|
|
9
|
+
Keywords: fabric etl
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: pandas
|
|
13
|
+
Requires-Dist: numpy
|
|
14
|
+
Requires-Dist: sqlalchemy
|
|
15
|
+
Requires-Dist: pyodbc
|
|
16
|
+
Dynamic: author
|
|
17
|
+
Dynamic: description
|
|
18
|
+
Dynamic: description-content-type
|
|
19
|
+
Dynamic: home-page
|
|
20
|
+
Dynamic: keywords
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
Dynamic: project-url
|
|
23
|
+
Dynamic: requires-dist
|
|
24
|
+
Dynamic: summary
|
|
25
|
+
|
|
26
|
+
# dataprepkit
|
|
27
|
+
A collection of helpers and utilities for SQL-based data ingestion, transformation, and pipeline orchestration.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
setup.py
|
|
4
|
+
dataprepkit/__init__.py
|
|
5
|
+
dataprepkit/helpers.py
|
|
6
|
+
dataprepkit/helpers/__init__.py
|
|
7
|
+
dataprepkit/helpers/connectors/__init__.py
|
|
8
|
+
dataprepkit/helpers/connectors/warehouse.py
|
|
9
|
+
dataprepkit/helpers/connectors/lakehouse/__init__.py
|
|
10
|
+
dataprepkit/helpers/connectors/lakehouse/lakehouse.py
|
|
11
|
+
dataprepkit/helpers/dims/__init__.py
|
|
12
|
+
dataprepkit/helpers/dims/dimension_registry.py
|
|
13
|
+
dataprepkit/helpers/transforms/__init__.py
|
|
14
|
+
dataprepkit/processors/__init__.py
|
|
15
|
+
dataprepkit/processors/dimensions/__init__.py
|
|
16
|
+
dataprepkit/processors/dimensions/dim_common.py
|
|
17
|
+
dataprepkit/processors/dimensions/dim_company.py
|
|
18
|
+
dataprepkit/processors/dimensions/dim_measure.py
|
|
19
|
+
dataprepkit/processors/dimensions/dim_region.py
|
|
20
|
+
dataprepkit/processors/quarterly/__init__.py
|
|
21
|
+
dataprepkit/processors/quarterly/qdata_processor.py
|
|
22
|
+
ofwat_dataprepkit.egg-info/PKG-INFO
|
|
23
|
+
ofwat_dataprepkit.egg-info/SOURCES.txt
|
|
24
|
+
ofwat_dataprepkit.egg-info/dependency_links.txt
|
|
25
|
+
ofwat_dataprepkit.egg-info/requires.txt
|
|
26
|
+
ofwat_dataprepkit.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dataprepkit
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
# read the contents of your README file
|
|
4
|
+
from os import path
|
|
5
|
+
this_directory = path.abspath(path.dirname(__file__))
|
|
6
|
+
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
|
|
7
|
+
long_description = f.read()
|
|
8
|
+
|
|
9
|
+
setup(
|
|
10
|
+
name="ofwat-dataprepkit",
|
|
11
|
+
version="0.4",
|
|
12
|
+
author="Ofwat",
|
|
13
|
+
description='ETL Tools for Fabric',
|
|
14
|
+
long_description=long_description,
|
|
15
|
+
long_description_content_type='text/markdown',
|
|
16
|
+
keywords="fabric etl",
|
|
17
|
+
packages=find_packages(),
|
|
18
|
+
url="https://github.com/Ofwat/dataprepkit",
|
|
19
|
+
project_urls={
|
|
20
|
+
'Source': 'https://github.com/Ofwat/dataprepkit',
|
|
21
|
+
'Tracker': 'https://github.com/Ofwat/dataprepkit/issues',
|
|
22
|
+
},
|
|
23
|
+
install_requires=[
|
|
24
|
+
"pandas",
|
|
25
|
+
"numpy",
|
|
26
|
+
"sqlalchemy",
|
|
27
|
+
"pyodbc",
|
|
28
|
+
],
|
|
29
|
+
)
|