Skip to content

Commit

Permalink
[python sdk] add v1beta1 models (#1252)
Browse files Browse the repository at this point in the history
* [python sdk] add v1beta1 models

* upgrade version of python SDK to 0.0.3

* remove v1Alpha3 python sdk

* add some python models manually: v1Time and V1UnstructuredUnstructured

* bring back v1alpha3

* create separate python sdk for v1alpha3 and v1beta1

* move on

* release pkg on pypi.org

* remove dist files

* refine
  • Loading branch information
sperlingxx authored Aug 18, 2020
1 parent 051d1de commit 1d18594
Show file tree
Hide file tree
Showing 281 changed files with 12,954 additions and 387 deletions.
43 changes: 43 additions & 0 deletions hack/gen-python-sdk/gen_sdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

# Copyright 2019 The Kubeflow Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

SWAGGER_JAR_URL="http://search.maven.org/maven2/io/swagger/swagger-codegen-cli/2.4.6/swagger-codegen-cli-2.4.6.jar"
SWAGGER_CODEGEN_JAR="hack/gen-python-sdk/swagger-codegen-cli.jar"
SWAGGER_CODEGEN_CONF="hack/gen-python-sdk/swagger_config.json"
SWAGGER_CODEGEN_FILE="pkg/apis/KATIB_VERSION/swagger.json"
TMP_CODEGEN_PATH="sdk/tmp/KATIB_VERSION"
SDK_OUTPUT_PATH="sdk/python"
POST_GEN_PYTHON_HANDLER="hack/gen-python-sdk/post_gen.py"
KATIB_VERSIONS=(v1alpha3 v1beta1)

echo "Downloading the swagger-codegen JAR package ..."
wget -O ${SWAGGER_CODEGEN_JAR} ${SWAGGER_JAR_URL}


for VERSION in ${KATIB_VERSIONS[@]}; do
echo "Generating Python SDK for Kubeflow Katib ${VERSION} ..."
SWAGGER_FILE=${SWAGGER_CODEGEN_FILE/KATIB_VERSION/$VERSION}
TMP_PATH=${TMP_CODEGEN_PATH/KATIB_VERSION/$VERSION}
java -jar ${SWAGGER_CODEGEN_JAR} generate -i ${SWAGGER_FILE} -l python -o ${TMP_PATH} -c ${SWAGGER_CODEGEN_CONF} -v

python ${POST_GEN_PYTHON_HANDLER} ${TMP_PATH} ${SDK_OUTPUT_PATH}/${VERSION}
done

rm ${SWAGGER_CODEGEN_JAR}
95 changes: 95 additions & 0 deletions hack/gen-python-sdk/post_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import os
import shutil
import sys


def _rewrite_helper(input_file, output_file, rewrite_rules):
rules = rewrite_rules or []
lines = []
with open(input_file, 'r') as f:
while True:
line = f.readline()
if not line:
break
for rule in rules:
line = rule(line)
lines.append(line)
with open(output_file, 'w') as f:
f.writelines(lines)


def update_python_sdk(src, dest, versions=('v1alpha3', 'v1beta1')):
# tiny transformers to refine generated codes
rewrite_rules = [
lambda l: l.replace('import katib', 'import kubeflow.katib'),
lambda l: l.replace('from katib', 'from kubeflow.katib'),
]

src_dirs = [
os.path.join(src, 'katib'),
os.path.join(src, 'katib', 'models'),
os.path.join(src, 'test'),
os.path.join(src, 'docs')
]
dest_dirs = [
os.path.join(dest, 'kubeflow', 'katib'),
os.path.join(dest, 'kubeflow', 'katib', 'models'),
os.path.join(dest, 'test'),
os.path.join(dest, 'docs')
]

for src_dir, dest_dir in zip(src_dirs, dest_dirs):
# remove previous generated files explicitly, in case of deprecated instances
for file in os.listdir(dest_dir):
path = os.path.join(dest_dir, file)
if not os.path.isfile(path):
continue
for v in versions:
if v in file.lower():
os.remove(path)
break
# fill latest generated files
for file in os.listdir(src_dir):
in_file = os.path.join(src_dir, file)
out_file = os.path.join(dest_dir, file)
if not os.path.isfile(in_file):
continue
_rewrite_helper(in_file, out_file, rewrite_rules)

# update doc for API Endpoints and Models README.md
buffer = []
update_buffer = []
with open(os.path.join(src, 'README.md'), 'r') as src_f:
anchor = 0
for line in src_f.readlines():
if line.startswith('## Documentation For Models'):
if anchor == 0:
anchor = 1
elif line.startswith('##') and anchor == 1:
anchor = 2
if anchor == 0:
continue
if anchor == 2:
break
update_buffer.append(line)
with open(os.path.join(dest, 'README.md'), 'r') as dest_f:
anchor = 0
for line in dest_f.readlines():
if line.startswith('## Documentation For Models'):
if anchor == 0:
buffer.extend(update_buffer)
anchor = 1
elif line.startswith('##') and anchor == 1:
anchor = 2
if anchor == 1:
continue
buffer.append(line)
with open(os.path.join(dest, 'README.md'), 'w') as dest_f:
dest_f.writelines(buffer)

# clear working dictionary
shutil.rmtree(src)


if __name__ == '__main__':
update_python_sdk(src=sys.argv[1], dest=sys.argv[2])
11 changes: 11 additions & 0 deletions hack/gen-python-sdk/swagger_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"packageName" : "katib",
"projectName" : "katib",
"packageVersion": "0.1",
"importMappings": {
"V1Container": "from kubernetes.client import V1Container",
"V1ListMeta": "from kubernetes.client import V1ListMeta",
"V1ObjectMeta": "from kubernetes.client import V1ObjectMeta",
"V1HTTPGetAction": "from kubernetes.client import V1HTTPGetAction"
}
}
2 changes: 1 addition & 1 deletion sdk/python/README.md → sdk/python/v1alpha3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Python 2.7 and 3.4+
### pip install

```sh
pip install kubeflow-katib
pip install kubeflow-katib==0.0.2
```

Then import package:
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions sdk/python/v1alpha3/docs/V1Time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# V1Time

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
7 changes: 7 additions & 0 deletions sdk/python/v1alpha3/docs/V1UnstructuredUnstructured.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# V1UnstructuredUnstructured

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**algorithm_name** | **str** | | [optional]
**algorithm_settings** | [**list[V1alpha3AlgorithmSetting]**](V1alpha3AlgorithmSetting.md) | Key-value pairs representing settings for suggestion algorithms. |
**algorithm_settings** | [**list[V1alpha3AlgorithmSetting]**](V1alpha3AlgorithmSetting.md) | Key-value pairs representing settings for suggestion algorithms. | [optional]
**early_stopping** | [**V1alpha3EarlyStoppingSpec**](V1alpha3EarlyStoppingSpec.md) | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Name | Type | Description | Notes
**objective** | [**V1alpha3ObjectiveSpec**](V1alpha3ObjectiveSpec.md) | Describes the objective of the experiment. | [optional]
**parallel_trial_count** | **int** | How many trials can be processed in parallel. Defaults to 3 | [optional]
**parameters** | [**list[V1alpha3ParameterSpec]**](V1alpha3ParameterSpec.md) | List of hyperparameter configurations. | [optional]
**resume_policy** | **str** | Describes resuming policy which usually take effect after experiment terminated. | [optional]
**trial_template** | [**V1alpha3TrialTemplate**](V1alpha3TrialTemplate.md) | Template for each run of the trial. | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
]
},
"metadata": {},
"output_type": "display_data"
"output_type": "execute_result"
},
{
"data": {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,23 @@
# flake8: noqa

"""
katib
Katib
swagger description for katib # noqa: E501
OpenAPI spec version: v0.1
Swagger description for Katib # noqa: E501
OpenAPI spec version: v1alpha3-0.1
Generated by: https://github.com/swagger-api/swagger-codegen.git
"""


from __future__ import absolute_import

# import utils and constants
from kubeflow.katib.utils import utils
from kubeflow.katib.constants import constants
# import apis into sdk package

# import ApiClient
from kubeflow.katib.api_client import ApiClient
from kubeflow.katib.configuration import Configuration
from kubeflow.katib.api.katib_client import KatibClient

# import ApiClient
from kubeflow.katib.api_client import ApiClient
from kubeflow.katib.configuration import Configuration

# import models into sdk package
from kubeflow.katib.models.v1alpha3_algorithm_setting import V1alpha3AlgorithmSetting
from kubeflow.katib.models.v1alpha3_algorithm_spec import V1alpha3AlgorithmSpec
Expand Down Expand Up @@ -67,4 +59,3 @@
from kubeflow.katib.models.v1alpha3_trial_spec import V1alpha3TrialSpec
from kubeflow.katib.models.v1alpha3_trial_status import V1alpha3TrialStatus
from kubeflow.katib.models.v1alpha3_trial_template import V1alpha3TrialTemplate

File renamed without changes.
Loading

0 comments on commit 1d18594

Please sign in to comment.