Skip to content

Commit f20359c

Browse files
Merge pull request #29 from synccomputingcode/graham/configure
[PROD-1091] [Databricks Conference '23] Updates to sync-cli configure command
2 parents 9af6577 + 6492084 commit f20359c

File tree

4 files changed

+28
-48
lines changed

4 files changed

+28
-48
lines changed

sync/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Library for leveraging the power of Sync"""
2-
__version__ = "0.0.14"
2+
__version__ = "0.0.15"
33

44
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"

sync/api/projects.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""
33

44
import logging
5-
65
from typing import List
76

87
from sync.api.predictions import get_predictions
@@ -42,7 +41,7 @@ def create_project(
4241
app_id: str,
4342
description: str = None,
4443
s3_url: str = None,
45-
prediction_preference: Preference = Preference.BALANCED,
44+
prediction_preference: Preference = Preference.ECONOMY,
4645
prediction_params: dict = None,
4746
) -> Response[dict]:
4847
"""Creates a Sync project for tracking and optimizing Apache Spark applications
@@ -53,7 +52,7 @@ def create_project(
5352
:type description: str, optional
5453
:param s3_url: S3 URL under which to store project configurations and logs, defaults to None
5554
:type s3_url: str, optional
56-
:param prediction_preference: preferred prediction solution, defaults to Preference.BALANCED
55+
:param prediction_preference: preferred prediction solution, defaults to `Preference.ECONOMY`
5756
:type prediction_preference: Preference, optional
5857
:param prediction_params: dictionary of prediction parameters, defaults to None. Valid options are documented here - https://developers.synccomputing.com/reference/create_project_v1_projects_post
5958
:type prediction_preference: dict, optional
@@ -67,7 +66,7 @@ def create_project(
6766
"description": description,
6867
"s3_url": s3_url,
6968
"prediction_preference": prediction_preference,
70-
"prediction_params": prediction_params
69+
"prediction_params": prediction_params,
7170
}
7271
)
7372
)

sync/cli/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,10 @@ def configure():
4343
show_default=False,
4444
)
4545

46-
project_url = click.prompt(
47-
"Default project S3 URL", default=CONFIG.default_project_url or OPTIONAL_DEFAULT
48-
)
4946
prediction_preference = click.prompt(
5047
"Default prediction preference",
5148
type=click.Choice([p.value for p in Preference]),
52-
default=(CONFIG.default_prediction_preference or Preference.BALANCED).value,
49+
default=(CONFIG.default_prediction_preference or Preference.ECONOMY).value,
5350
)
5451

5552
dbx_host = OPTIONAL_DEFAULT
@@ -74,7 +71,6 @@ def configure():
7471
init(
7572
APIKey(api_key_id=api_key_id, api_key_secret=api_key_secret),
7673
Configuration(
77-
default_project_url=project_url if project_url != OPTIONAL_DEFAULT else None,
7874
default_prediction_preference=prediction_preference,
7975
),
8076
DatabricksConf(host=dbx_host, token=dbx_token, aws_region_name=dbx_region)

sync/config.py

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from urllib.parse import urlparse
99

1010
import boto3 as boto
11-
from pydantic import BaseSettings, Field, validator
11+
from pydantic import BaseSettings, Field, validator, Extra
1212

1313
from .models import Preference
1414

@@ -39,25 +39,12 @@ def customise_sources(cls, init_settings, env_settings, file_secret_settings):
3939

4040

4141
class Configuration(BaseSettings):
42-
default_project_url: Union[str, None] = Field(
43-
description="default location for Sync project data"
44-
)
45-
default_prediction_preference: Union[Preference, None] = Preference.BALANCED
42+
default_prediction_preference: Union[Preference, None] = Preference.ECONOMY
4643
api_url: str = Field("https://api.synccomputing.com", env="SYNC_API_URL")
4744

48-
@validator("default_project_url")
49-
def validate_url(cls, url):
50-
if url:
51-
# There are valid S3 URLs (e.g. with spaces) not supported by Pydantic URL types: https://docs.pydantic.dev/usage/types/#urls
52-
# Hence the manual validation here
53-
parsed_url = urlparse(url.rstrip("/"))
54-
55-
if parsed_url.scheme != "s3":
56-
raise ValueError("Only S3 URLs please!")
57-
58-
return parsed_url.geturl()
59-
6045
class Config:
46+
extra = Extra.ignore
47+
6148
@classmethod
6249
def customise_sources(cls, init_settings, env_settings, file_secret_settings):
6350
return (init_settings, env_settings, json_config_settings_source(CONFIG_FILE))
@@ -124,8 +111,11 @@ def get_api_key() -> APIKey:
124111
:rtype: APIKey
125112
"""
126113
global _api_key
127-
if not _api_key:
128-
_api_key = APIKey()
114+
if _api_key is None:
115+
try:
116+
_api_key = APIKey()
117+
except ValueError:
118+
pass
129119
return _api_key
130120

131121

@@ -136,11 +126,21 @@ def get_config() -> Configuration:
136126
:rtype: Configuration
137127
"""
138128
global _config
139-
if not _config:
129+
if _config is None:
140130
_config = Configuration()
141131
return _config
142132

143133

134+
def get_databricks_config() -> DatabricksConf:
135+
global _db_config
136+
if _db_config is None:
137+
try:
138+
_db_config = DatabricksConf()
139+
except ValueError:
140+
pass
141+
return _db_config
142+
143+
144144
CONFIG: Configuration
145145
_config = None
146146
API_KEY: APIKey
@@ -151,26 +151,11 @@ def get_config() -> Configuration:
151151

152152
def __getattr__(name):
153153
if name == "CONFIG":
154-
global _config
155-
if _config is None:
156-
_config = Configuration()
157-
return _config
154+
return get_config()
158155
elif name == "API_KEY":
159-
global _api_key
160-
if _api_key is None:
161-
try:
162-
_api_key = APIKey()
163-
except ValueError:
164-
pass
165-
return _api_key
156+
return get_api_key()
166157
elif name == "DB_CONFIG":
167-
global _db_config
168-
if _db_config is None:
169-
try:
170-
_db_config = DatabricksConf()
171-
except ValueError:
172-
pass
173-
return _db_config
158+
return get_databricks_config()
174159
else:
175160
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
176161

0 commit comments

Comments
 (0)