|
39 | 39 | from traceml.artifacts import V1RunArtifact |
40 | 40 |
|
41 | 41 |
|
| 42 | +def _prepare_version_registration_values( |
| 43 | + content: Optional[Union[str, Dict]], |
| 44 | + tags: Optional[Union[str, List[str]]], |
| 45 | + artifacts: Optional[List[str]], |
| 46 | +): |
| 47 | + if content: |
| 48 | + content = content if isinstance(content, str) else orjson_dumps(content) |
| 49 | + if tags is not None: |
| 50 | + tags = validate_tags(tags, validate_yaml=True) |
| 51 | + if artifacts is not None: |
| 52 | + artifacts = validate_tags(artifacts, validate_yaml=True) |
| 53 | + return content, tags, artifacts |
| 54 | + |
| 55 | + |
| 56 | +def _build_version_registration_config( |
| 57 | + *, |
| 58 | + to_update: bool, |
| 59 | + version: str, |
| 60 | + description: Optional[str] = None, |
| 61 | + tags: Optional[Union[str, List[str]]] = None, |
| 62 | + content: Optional[Union[str, Dict]] = None, |
| 63 | + readme: Optional[str] = None, |
| 64 | + run: Optional[str] = None, |
| 65 | + connection: Optional[str] = None, |
| 66 | + artifacts: Optional[List[str]] = None, |
| 67 | + stage: Optional[V1Stages] = None, |
| 68 | + stage_conditions: Optional[List[V1StageCondition]] = None, |
| 69 | +) -> V1ProjectVersion: |
| 70 | + if to_update: |
| 71 | + version_config = V1ProjectVersion.model_construct() |
| 72 | + if description is not None: |
| 73 | + version_config.description = description |
| 74 | + if tags: |
| 75 | + version_config.tags = tags |
| 76 | + if content: |
| 77 | + version_config.content = content # type: ignore |
| 78 | + if readme is not None: |
| 79 | + version_config.readme = readme |
| 80 | + if run: |
| 81 | + version_config.run = run |
| 82 | + if artifacts is not None: |
| 83 | + version_config.artifacts = artifacts |
| 84 | + if connection is not None: |
| 85 | + version_config.connection = connection |
| 86 | + if stage is not None: |
| 87 | + version_config.stage = stage |
| 88 | + if stage_conditions is not None: |
| 89 | + version_config.stage_conditions = stage_conditions |
| 90 | + return version_config |
| 91 | + |
| 92 | + return V1ProjectVersion.model_construct( |
| 93 | + name=version, |
| 94 | + description=description, |
| 95 | + tags=tags, |
| 96 | + run=run, |
| 97 | + readme=readme, |
| 98 | + artifacts=artifacts, |
| 99 | + connection=connection, |
| 100 | + content=content, |
| 101 | + stage=stage, |
| 102 | + stage_conditions=stage_conditions, |
| 103 | + ) |
| 104 | + |
| 105 | + |
42 | 106 | class ProjectClient(ClientMixin): |
43 | 107 | """ProjectClient is a client to communicate with Polyaxon projects endpoints. |
44 | 108 |
|
@@ -947,55 +1011,31 @@ def register_version( |
947 | 1011 | except (ApiException, HTTPError, AttributeError): |
948 | 1012 | to_update = False |
949 | 1013 |
|
950 | | - def _get_content() -> str: |
951 | | - return content if isinstance(content, str) else orjson_dumps(content) |
952 | | - |
953 | | - if content: |
954 | | - content = _get_content() |
955 | | - if tags is not None: |
956 | | - tags = validate_tags(tags, validate_yaml=True) |
957 | | - if artifacts is not None: |
958 | | - artifacts = validate_tags(artifacts, validate_yaml=True) |
959 | | - |
| 1014 | + content, tags, artifacts = _prepare_version_registration_values( |
| 1015 | + content=content, |
| 1016 | + tags=tags, |
| 1017 | + artifacts=artifacts, |
| 1018 | + ) |
| 1019 | + version_config = _build_version_registration_config( |
| 1020 | + to_update=to_update, |
| 1021 | + version=version, |
| 1022 | + description=description, |
| 1023 | + tags=tags, |
| 1024 | + content=content, |
| 1025 | + readme=readme, |
| 1026 | + run=run, |
| 1027 | + connection=connection, |
| 1028 | + artifacts=artifacts, |
| 1029 | + stage=stage, |
| 1030 | + stage_conditions=stage_conditions, |
| 1031 | + ) |
960 | 1032 | if to_update: |
961 | | - version_config = V1ProjectVersion.model_construct() |
962 | | - if description is not None: |
963 | | - version_config.description = description |
964 | | - if tags: |
965 | | - version_config.tags = tags |
966 | | - if content: |
967 | | - version_config.content = content # type: ignore |
968 | | - if readme is not None: |
969 | | - version_config.readme = readme |
970 | | - if run: |
971 | | - version_config.run = run |
972 | | - if artifacts is not None: |
973 | | - version_config.artifacts = artifacts |
974 | | - if connection is not None: |
975 | | - version_config.connection = connection |
976 | | - if stage is not None: |
977 | | - version_config.stage = stage |
978 | | - if stage_conditions is not None: |
979 | | - version_config.stage_conditions = stage_conditions |
980 | 1033 | return self.patch_version( |
981 | 1034 | kind=kind, |
982 | 1035 | version=version, |
983 | 1036 | data=version_config, |
984 | 1037 | ) |
985 | | - else: |
986 | | - version_config = V1ProjectVersion.model_construct( |
987 | | - name=version, |
988 | | - description=description, |
989 | | - tags=tags, |
990 | | - run=run, |
991 | | - readme=readme, |
992 | | - artifacts=artifacts, |
993 | | - connection=connection, |
994 | | - content=content, |
995 | | - stage=stage, |
996 | | - stage_conditions=stage_conditions, |
997 | | - ) |
998 | | - return self.create_version(kind=kind, data=version_config) |
| 1038 | + return self.create_version(kind=kind, data=version_config) |
999 | 1039 |
|
1000 | 1040 | @client_handler(check_no_op=True, check_offline=True) |
1001 | 1041 | def register_component_version( |
@@ -2378,54 +2418,30 @@ async def register_version( |
2378 | 2418 | except (ApiException, AttributeError): |
2379 | 2419 | to_update = False |
2380 | 2420 |
|
2381 | | - def _get_content() -> str: |
2382 | | - return content if isinstance(content, str) else orjson_dumps(content) |
2383 | | - |
2384 | | - if content: |
2385 | | - content = _get_content() |
2386 | | - if tags is not None: |
2387 | | - tags = validate_tags(tags, validate_yaml=True) |
2388 | | - if artifacts is not None: |
2389 | | - artifacts = validate_tags(artifacts, validate_yaml=True) |
2390 | | - |
2391 | | - if to_update: |
2392 | | - version_config = V1ProjectVersion.model_construct() |
2393 | | - if description is not None: |
2394 | | - version_config.description = description |
2395 | | - if tags: |
2396 | | - version_config.tags = tags |
2397 | | - if content: |
2398 | | - version_config.content = content # type: ignore |
2399 | | - if readme is not None: |
2400 | | - version_config.readme = readme |
2401 | | - if run: |
2402 | | - version_config.run = run |
2403 | | - if artifacts is not None: |
2404 | | - version_config.artifacts = artifacts |
2405 | | - if connection is not None: |
2406 | | - version_config.connection = connection |
2407 | | - if stage is not None: |
2408 | | - version_config.stage = stage |
2409 | | - if stage_conditions is not None: |
2410 | | - version_config.stage_conditions = stage_conditions |
2411 | | - return await self.patch_version( |
2412 | | - kind=kind, |
2413 | | - version=version, |
2414 | | - data=version_config, |
2415 | | - ) |
2416 | | - |
2417 | | - version_config = V1ProjectVersion.model_construct( |
2418 | | - name=version, |
| 2421 | + content, tags, artifacts = _prepare_version_registration_values( |
| 2422 | + content=content, |
| 2423 | + tags=tags, |
| 2424 | + artifacts=artifacts, |
| 2425 | + ) |
| 2426 | + version_config = _build_version_registration_config( |
| 2427 | + to_update=to_update, |
| 2428 | + version=version, |
2419 | 2429 | description=description, |
2420 | 2430 | tags=tags, |
2421 | | - run=run, |
| 2431 | + content=content, |
2422 | 2432 | readme=readme, |
2423 | | - artifacts=artifacts, |
| 2433 | + run=run, |
2424 | 2434 | connection=connection, |
2425 | | - content=content, |
| 2435 | + artifacts=artifacts, |
2426 | 2436 | stage=stage, |
2427 | 2437 | stage_conditions=stage_conditions, |
2428 | 2438 | ) |
| 2439 | + if to_update: |
| 2440 | + return await self.patch_version( |
| 2441 | + kind=kind, |
| 2442 | + version=version, |
| 2443 | + data=version_config, |
| 2444 | + ) |
2429 | 2445 | return await self.create_version(kind=kind, data=version_config) |
2430 | 2446 |
|
2431 | 2447 | @async_client_handler(check_no_op=True, check_offline=True) |
|
0 commit comments