Skip to content

Commit 3dee1f7

Browse files
committed
Enable dataspaces for customcode run
1 parent b79f1d9 commit 3dee1f7

3 files changed

Lines changed: 67 additions & 13 deletions

File tree

src/datacustomcode/io/reader/query_api.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import (
1919
TYPE_CHECKING,
2020
Final,
21+
Optional,
2122
Union,
2223
)
2324

@@ -76,18 +77,31 @@ class QueryAPIDataCloudReader(BaseDataCloudReader):
7677
CONFIG_NAME = "QueryAPIDataCloudReader"
7778

7879
def __init__(
79-
self, spark: SparkSession, credentials_profile: str = "default"
80+
self,
81+
spark: SparkSession,
82+
credentials_profile: str = "default",
83+
dataspace: Optional[str] = None,
8084
) -> None:
8185
self.spark = spark
8286
credentials = Credentials.from_available(profile=credentials_profile)
8387

84-
self._conn = SalesforceCDPConnection(
85-
credentials.login_url,
86-
credentials.username,
87-
credentials.password,
88-
credentials.client_id,
89-
credentials.client_secret,
90-
)
88+
if dataspace is not None:
89+
self._conn = SalesforceCDPConnection(
90+
credentials.login_url,
91+
credentials.username,
92+
credentials.password,
93+
credentials.client_id,
94+
credentials.client_secret,
95+
dataspace=dataspace,
96+
)
97+
else:
98+
self._conn = SalesforceCDPConnection(
99+
credentials.login_url,
100+
credentials.username,
101+
credentials.password,
102+
credentials.client_id,
103+
credentials.client_secret,
104+
)
91105

92106
def read_dlo(
93107
self,

src/datacustomcode/io/writer/print.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,20 @@ def __init__(
3030
spark: SparkSession,
3131
reader: Optional[QueryAPIDataCloudReader] = None,
3232
credentials_profile: str = "default",
33+
dataspace: Optional[str] = None,
3334
) -> None:
3435
super().__init__(spark)
35-
self.reader = (
36-
QueryAPIDataCloudReader(self.spark, credentials_profile)
37-
if reader is None
38-
else reader
39-
)
36+
if reader is None:
37+
if dataspace is not None:
38+
self.reader = QueryAPIDataCloudReader(
39+
self.spark, credentials_profile, dataspace=dataspace
40+
)
41+
else:
42+
self.reader = QueryAPIDataCloudReader(
43+
self.spark, credentials_profile
44+
)
45+
else:
46+
self.reader = reader
4047

4148
def validate_dataframe_columns_against_dlo(
4249
self,

src/datacustomcode/run.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import importlib
16+
import json
17+
import os
1618
from pathlib import Path
1719
import runpy
1820
import sys
@@ -36,6 +38,37 @@ def run_entrypoint(
3638
profile: The credentials profile to use.
3739
"""
3840
add_py_folder(entrypoint)
41+
42+
# Read dataspace from config.json if it exists
43+
entrypoint_dir = os.path.dirname(entrypoint)
44+
config_json_path = os.path.join(entrypoint_dir, "config.json")
45+
if os.path.exists(config_json_path):
46+
try:
47+
with open(config_json_path, "r") as f:
48+
config_json = json.load(f)
49+
dataspace = config_json.get("dataspace")
50+
if dataspace:
51+
# Add dataspace to reader config options
52+
if (
53+
config.reader_config
54+
and hasattr(config.reader_config, "options")
55+
):
56+
config.reader_config.options["dataspace"] = dataspace
57+
# Add dataspace to writer config options (for PrintDataCloudWriter)
58+
if (
59+
config.writer_config
60+
and hasattr(config.writer_config, "options")
61+
):
62+
config.writer_config.options["dataspace"] = dataspace
63+
except json.JSONDecodeError as err:
64+
raise ValueError(
65+
f"config.json at {config_json_path} is not valid JSON"
66+
) from err
67+
except FileNotFoundError as err:
68+
raise FileNotFoundError(
69+
f"config.json not found at {config_json_path}"
70+
) from err
71+
3972
if profile != "default":
4073
if config.reader_config and hasattr(config.reader_config, "options"):
4174
config.reader_config.options["credentials_profile"] = profile

0 commit comments

Comments
 (0)