@@ -59,6 +59,7 @@ def __init__(
5959 # Used to store domain and user profile info retrieved from Studio environment.
6060 self ._domain_id = None
6161 self ._user_profile_name = None
62+ self ._in_studio_env = False
6263 self ._get_domain_and_user ()
6364
6465 def __str__ (self ):
@@ -70,25 +71,25 @@ def __repr__(self):
7071 return self .__str__ ()
7172
7273 def _get_domain_and_user (self ):
73- """Get and validate studio domain id and user profile from studio environment."""
74- if not self ._is_in_studio ():
74+ """Get domain id and user profile from Studio environment.
75+
76+ To verify Studio environment, we check if NOTEBOOK_METADATA_FILE exists
77+ and domain id and user profile name are present in the file.
78+ """
79+ if not os .path .isfile (NOTEBOOK_METADATA_FILE ):
7580 return
7681
7782 try :
7883 with open (NOTEBOOK_METADATA_FILE , "rb" ) as metadata_file :
7984 metadata = json .loads (metadata_file .read ())
80- if not self ._validate_domain_id (
81- metadata .get ("DomainId" )
82- ) or not self ._validate_user_profile_name (metadata .get ("UserProfileName" )):
83- logger .warning (
84- "NOTEBOOK_METADATA_FILE detected but failed to get valid domain and user"
85- " from it."
86- )
87- return
88- self ._domain_id = metadata .get ("DomainId" )
89- self ._user_profile_name = metadata .get ("UserProfileName" )
9085 except OSError as err :
91- logger .warning ("Could not load Studio metadata due to unexpected error. %s" , err )
86+ logger .warning ("Could not load metadata due to unexpected error. %s" , err )
87+ return
88+
89+ if "DomainId" in metadata and "UserProfileName" in metadata :
90+ self ._in_studio_env = True
91+ self ._domain_id = metadata .get ("DomainId" )
92+ self ._user_profile_name = metadata .get ("UserProfileName" )
9293
9394 def _get_presigned_url (
9495 self ,
@@ -142,10 +143,6 @@ def _get_presigned_url(
142143
143144 return url
144145
145- def _is_in_studio (self ):
146- """Check to see if NOTEBOOK_METADATA_FILE exists to verify Studio environment."""
147- return os .path .isfile (NOTEBOOK_METADATA_FILE )
148-
149146 def _open_url_in_web_browser (self , url : str ):
150147 """Open a URL in the default web browser.
151148
@@ -154,23 +151,6 @@ def _open_url_in_web_browser(self, url: str):
154151 """
155152 webbrowser .open (url )
156153
157- def _validate_domain_id (self , domain_id : Optional [str ] = None ):
158- """Validate domain id format.
159-
160- Args:
161- domain_id (str): Optional. The domain ID to validate. If one is not supplied,
162- self._domain_id will be used instead.
163- Default: ``None``
164-
165- Returns:
166- bool: Whether the supplied domain ID is valid.
167- """
168- if domain_id is None :
169- domain_id = self ._domain_id
170- if domain_id is None or len (domain_id ) > 63 :
171- return False
172- return True
173-
174154 def _validate_job_name (self , job_name : str ):
175155 """Validate training job name format.
176156
@@ -186,30 +166,35 @@ def _validate_job_name(self, job_name: str):
186166 f"Invalid job name. Job name must match regular expression { job_name_regex } "
187167 )
188168
189- def _validate_user_profile_name (self , user_profile_name : Optional [str ] = None ):
169+ def _validate_domain_id (self , domain_id : str ):
170+ """Validate domain id format.
171+
172+ Args:
173+ domain_id (str): Required. The domain ID to validate.
174+
175+ Returns:
176+ bool: Whether the supplied domain ID is valid.
177+ """
178+ if domain_id is None or len (domain_id ) > 63 :
179+ return False
180+ return True
181+
182+ def _validate_user_profile_name (self , user_profile_name : str ):
190183 """Validate user profile name format.
191184
192185 Args:
193- user_profile_name (str): Optional. The user profile name to validate. If one is not
194- supplied, self._user_profile_name will be used instead.
195- Default: ``None``
186+ user_profile_name (str): Required. The user profile name to validate.
196187
197188 Returns:
198189 bool: Whether the supplied user profile name is valid.
199190 """
200- if user_profile_name is None :
201- user_profile_name = self ._user_profile_name
202191 user_profile_name_regex = "^[a-zA-Z0-9](-*[a-zA-Z0-9]){0,62}"
203192 if user_profile_name is None or not re .fullmatch (
204193 user_profile_name_regex , user_profile_name
205194 ):
206195 return False
207196 return True
208197
209- def _validate_domain_and_user (self ):
210- """Helper function to consolidate validation calls."""
211- return self ._validate_domain_id () and self ._validate_user_profile_name ()
212-
213198 @abc .abstractmethod
214199 def get_app_url (self ):
215200 """Abstract method to generate a URL to help access the application in Studio.
0 commit comments