29
29
DEFAULT_ENDPOINT = "http://service.odps.aliyun.com/api"
30
30
DEFAULT_REGION_NAME = "cn"
31
31
LOGVIEW_HOST_DEFAULT = "http://logview.aliyun.com"
32
+ CATALOG_API_ENDPOINT_DEFAULT = "https://catalogapi.{region}.maxcompute.aliyun.com"
32
33
33
34
_ALTER_TABLE_REGEX = re .compile (
34
35
r"^\s*(drop|alter)\s+table\s*(|if\s+exists)\s+(?P<table_name>[^\s;]+)" , re .I
@@ -104,6 +105,7 @@ def __init__(
104
105
region_name = None ,
105
106
quota_name = None ,
106
107
namespace = None ,
108
+ catalog_endpoint = None ,
107
109
** kw
108
110
):
109
111
# avoid polluted copy sources :(
@@ -114,6 +116,7 @@ def __init__(
114
116
schema = utils .strip_if_str (schema )
115
117
logview_host = utils .strip_if_str (logview_host )
116
118
tunnel_endpoint = utils .strip_if_str (tunnel_endpoint )
119
+ catalog_endpoint = utils .strip_if_str (catalog_endpoint )
117
120
region_name = utils .strip_if_str (region_name )
118
121
quota_name = utils .strip_if_str (quota_name )
119
122
namespace = utils .strip_if_str (namespace )
@@ -133,6 +136,7 @@ def __init__(
133
136
app_account = app_account ,
134
137
logview_host = logview_host ,
135
138
tunnel_endpoint = tunnel_endpoint ,
139
+ catalog_endpoint = catalog_endpoint ,
136
140
region_name = region_name ,
137
141
quota_name = quota_name ,
138
142
namespace = namespace ,
@@ -178,7 +182,11 @@ def _init(
178
182
self .project = (
179
183
project or options .default_project or os .getenv ("ODPS_PROJECT_NAME" )
180
184
)
181
- self .region_name = region_name or self ._get_region_from_endpoint (self .endpoint )
185
+ self .region_name = (
186
+ region_name
187
+ or os .getenv ("ODPS_REGION_NAME" )
188
+ or self ._get_region_from_endpoint (self .endpoint )
189
+ )
182
190
self .namespace = (
183
191
namespace or options .default_namespace or os .getenv ("ODPS_NAMESPACE" )
184
192
)
@@ -206,6 +214,32 @@ def _init(
206
214
or os .getenv ("ODPS_TUNNEL_ENDPOINT" )
207
215
)
208
216
217
+ self ._catalog_endpoint = (
218
+ kw .pop ("catalog_endpoint" , None )
219
+ or options .catalog .endpoint
220
+ or os .getenv ("ODPS_CATALOG_ENDPOINT" )
221
+ or (
222
+ CATALOG_API_ENDPOINT_DEFAULT .format (region = self .region_name )
223
+ if self .region_name and CATALOG_API_ENDPOINT_DEFAULT
224
+ else None
225
+ )
226
+ )
227
+ if self ._catalog_endpoint is None :
228
+ self .catalog_rest = None
229
+ else :
230
+ self .catalog_rest = rest_client_cls (
231
+ self .account ,
232
+ self ._catalog_endpoint .rstrip ("/" ),
233
+ project ,
234
+ schema ,
235
+ app_account = self .app_account ,
236
+ proxy = options .api_proxy ,
237
+ region_name = self .region_name ,
238
+ namespace = self .namespace ,
239
+ tag = "Catalog" ,
240
+ ** rest_client_kwargs
241
+ )
242
+
209
243
self ._logview_host = (
210
244
kw .pop ("logview_host" , None )
211
245
or options .logview_host
@@ -506,6 +540,7 @@ def get_project(self, name=None, default_schema=None):
506
540
# use _schema to avoid requesting for tenant options
507
541
proj ._default_schema = default_schema or self ._schema
508
542
proj ._quota_name = self ._quota_name
543
+ proj ._catalog_endpoint = self ._catalog_endpoint
509
544
510
545
proj_ref = weakref .ref (proj )
511
546
@@ -2137,6 +2172,45 @@ def delete_offline_model(self, name, project=None, if_exists=False):
2137
2172
if not if_exists :
2138
2173
raise
2139
2174
2175
+ def list_models (self , project = None , schema = None ):
2176
+ """
2177
+ List models of project by optional filter conditions including prefix and owner.
2178
+
2179
+ :param project: project name, if not provided, will be the default project
2180
+ :param str schema: schema name, if not provided, will be the default schema
2181
+ :return: models
2182
+ :rtype: list
2183
+ """
2184
+ parent = self ._get_project_or_schema (project , schema )
2185
+ return parent .models .iterate ()
2186
+
2187
+ def get_model (self , name , project = None , schema = None ):
2188
+ """
2189
+ Get model by given name
2190
+
2191
+ :param name: model name
2192
+ :param project: project name, if not provided, will be the default project
2193
+ :param str schema: schema name, if not provided, will be the default schema
2194
+ :return: model
2195
+ :rtype: :class:`odps.models.ml.Model`
2196
+ :raise: :class:`odps.errors.NoSuchObject` if not exists
2197
+ """
2198
+ parent = self ._get_project_or_schema (project , schema )
2199
+ return parent .models [name ]
2200
+
2201
+ def exist_model (self , name , project = None , schema = None ):
2202
+ """
2203
+ If the model with given name exists or not.
2204
+
2205
+ :param name: model's name
2206
+ :param project: project name, if not provided, will be the default project
2207
+ :param str schema: schema name, if not provided, will be the default schema
2208
+ :return: True if model exists else False
2209
+ :rtype: bool
2210
+ """
2211
+ parent = self ._get_project_or_schema (project , schema )
2212
+ return name in parent .models
2213
+
2140
2214
def get_logview_host (self ):
2141
2215
"""
2142
2216
Get logview host address.
@@ -2514,7 +2588,8 @@ def _get_odps_from_model(self):
2514
2588
return cur .odps if cur else None
2515
2589
2516
2590
2517
- models .RestModel .odps = property (fget = _get_odps_from_model )
2591
+ models .JSONRestModel .odps = property (fget = _get_odps_from_model )
2592
+ models .XMLRestModel .odps = property (fget = _get_odps_from_model )
2518
2593
del _get_odps_from_model
2519
2594
2520
2595
try :
0 commit comments