Utilities¶
The utilities module provides helper functions and common utilities used throughout the CLI for various tasks such as display formatting, logging, version management, and more.
General Utilities¶
General-purpose utility functions used across the SDK.
Copyright 2021 Kelvin Inc.
Licensed under the Kelvin Inc. Developer SDK License Agreement (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.kelvininc.com/developer-sdk-license
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
- kelvin.sdk.lib.utils.general_utils.guess_delimiter(line, delimiters=('\\t', '\\x00', '|', ','))[source]¶
Guess delimiter in line.
Parameters¶
- line: str
line of delimited text.
- delimiters: Iterable[str]
delimiters to check.
Returns¶
- str:
the guessed delimiter string.
- kelvin.sdk.lib.utils.general_utils.dict_to_yaml(content, comment_header=None)[source]¶
Convert a dictionary to a yaml.
Parameters¶
- content: dict
the dictionary content to convert to the yaml format.
- comment_header: Optional[Any]
the comment header to include in the yaml file. Do not include the ‘#’ character.
Returns¶
- str:
A string with the yaml format content.
- kelvin.sdk.lib.utils.general_utils.get_url_encoded_string(original_string)[source]¶
Return the url-encoded version of the provided string.
Parameters¶
- original_stringstr
the string to url encode
Returns¶
- str:
a url encoded string
- kelvin.sdk.lib.utils.general_utils.standardize_string(value)[source]¶
Given a specific value, replace its spaces and dashes with underscores to be snake-case compliant.
Parameters¶
- value: str
the string to be ‘standardized’.
Returns¶
- str:
the new, standardized string.
- kelvin.sdk.lib.utils.general_utils.open_link_in_browser(link)[source]¶
Open the specified link on the default web browser.
Parameters¶
- link: str
the link to open
Returns¶
- bool:
a boolean indicating whether the link was successfully opened.
- kelvin.sdk.lib.utils.general_utils.get_requirements_from_file(file_path)[source]¶
When provided with a path to a requirements file, yield a list of its requirements.
Parameters¶
- file_pathPath
the Path to the desired requirements file
Returns¶
- List[str]
a list containing all requirements in the file
- kelvin.sdk.lib.utils.general_utils.merge(x, *args, **kwargs)[source]¶
Merge two dictionaries.
- Return type:
TypeVar(T, bound=MutableMapping[str,Any])- Parameters:
Parameters¶
- xdict
the initial, mutable dictionary.
- argsMapping[str, Any]
the arguments to merge into the ‘x’ dictionary.
- kwargsAny
the keyword arguments to merge into the ‘x’ dictionary.
Returns¶
- dictionary:
the initial, mutated X dictionary.
- kelvin.sdk.lib.utils.general_utils.inflate(x, separator='.', result=None)[source]¶
Inflate flattened keys via separator into nested dictionary.
- kelvin.sdk.lib.utils.general_utils.get_bytes_as_human_readable(input_bytes_data)[source]¶
When provided with bytes data, return its ‘human-readable’ version.
Parameters¶
- input_bytes_dataUnion[Optional[int], Optional[float]]
the input int that corresponds to the bytes value.
Returns¶
- str:
a string containing the human-readable version of the input.
- kelvin.sdk.lib.utils.general_utils.get_iec_to_si_format_as_human_readable(input_data)[source]¶
When provided with a str in iec format, return its ‘human-readable’ version. ex: 256Mi
Parameters¶
- input_datastr
the input str in iec format. ex: 256Mi
Returns¶
- str:
a string containing the human-readable version of the input.
- kelvin.sdk.lib.utils.general_utils.get_datetime_as_human_readable(input_date)[source]¶
When provided with a datetime, retrieve its human readable form with the base date and its difference.
Parameters¶
- input_dateUnion[float, Optional[datetime]]
the datetime to display.
Returns¶
- str:
a string containing both the human readable datetime plus the difference to ‘now’
- kelvin.sdk.lib.utils.general_utils.parse_pydantic_errors(validation_error)[source]¶
Parse the provided ValidationError and break it down to a ‘pretty’ string message.
- Return type:
- Parameters:
validation_error (ValidationError)
Parameters¶
- validation_errorValidationError
the ValidationError to prettify.
Returns¶
- str:
a ‘pretty’ string with the parsed errors.
- kelvin.sdk.lib.utils.general_utils.get_files_from_dir(file_type, input_dir)[source]¶
Retrieve all files of a given type from the specified directory.
Parameters¶
- file_typestr
the file type to search for.
- input_dirstr
the directory to read the files from.
Returns¶
- List:
the list of all matching files
- kelvin.sdk.lib.utils.general_utils.unique_items(items)[source]¶
When provided with a list of items, retrieve the same list without duplicates and with the same order.
Parameters¶
- itemsList
the original list.
Returns¶
- List:
the ordered list.
- kelvin.sdk.lib.utils.general_utils.lower(x)[source]¶
Lower representation of data for serialisation.
- kelvin.sdk.lib.utils.general_utils.chdir(path)[source]¶
Change working directory and return to previous on exit.
- kelvin.sdk.lib.utils.general_utils.is_port_open(host, port)[source]¶
Check if a port is being used on a specific ip address
- kelvin.sdk.lib.utils.general_utils.get_system_information(pretty_keys=False)[source]¶
Get a dictionary with system-wide information.
Parameters¶
- pretty_keys: bool
indicates whether it should yield a dictionary with raw data or a ‘prettified’ one.
Returns¶
- dict:
a dictionary containing all system information.
Application Utilities¶
Utility functions for application management and validation.
Copyright 2021 Kelvin Inc.
Licensed under the Kelvin Inc. Developer SDK License Agreement (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.kelvininc.com/developer-sdk-license
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
- kelvin.sdk.lib.utils.application_utils.check_if_app_name_is_valid(app_name)[source]¶
Verify whether the provided app name is valid (or contains a forbidden word combination).
Raise an exception if the provided app name contains a forbidden keyword.
Parameters¶
- app_namestr
the app name to be verified.
Returns¶
- bool:
a boolean indicating whether the app name is valid.
Click Utilities¶
Utility functions and classes for Click CLI framework customizations.
Copyright 2021 Kelvin Inc.
Licensed under the Kelvin Inc. Developer SDK License Agreement (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.kelvininc.com/developer-sdk-license
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
- class kelvin.sdk.lib.utils.click_utils.ClickConfigs[source]¶
Bases:
object- all_verbose_commands = ['--verbose', '-v']¶
- all_help_commands = ['--help', '-h']¶
- class kelvin.sdk.lib.utils.click_utils.KSDKGroup(name=None, commands=None, invoke_without_command=False, no_args_is_help=None, subcommand_metavar=None, chain=False, result_callback=None, **kwargs)[source]¶
Bases:
Group- Parameters:
- command_tree: dict = {'agent': {'sync': 'Sync the AGENTS.md file to the application directory.'}, 'app': {'agent': 'Manage AI agent configuration files for applications.', 'build': 'Build a local application into a packaged image.', 'create': 'Create a new application based on the specified parameters.', 'images': 'Management and display of local images on the Local Application Registry.', 'mlflow': 'MLFlow integration commands.', 'samples': "Opens Kelvin's code samples GitHub repo.", 'test': 'Test local applications.', 'upload': "Upload an application to the platform's Application Registry."}, 'appregistry': {'delete': "(Deprecated) Delete an application from the platform's Application Registry.", 'download': '(Deprecated) Download an application from the platform and make it available locally.', 'list': "(Deprecated) List all the available applications on the platform's Application registry.", 'search': "(Deprecated) Search for specific apps on the platform's Application Registry.", 'show': '(Deprecated) Show the platform details and configurations for a specific application.', 'upload': "(Deprecated) Upload an application to the platform's Application Registry."}, 'apps': {'delete': "Delete an application from the platform's Application Registry.", 'download': 'Download an application from the platform and make it available locally.', 'list': "List all the available applications on the platform's Application registry.", 'search': "Search for specific apps on the platform's Application Registry.", 'show': 'Show the platform details and configurations for a specific application.', 'upload': "Upload an application to the platform's Application Registry."}, 'auth': {'login': 'Login on the platform.', 'logout': 'Logout from the platform.', 'reset': 'Reset all authentication and configuration cache.', 'token': 'Obtain an authentication token for the platform.'}, 'configuration': {'autocomplete': 'Generate command-completion configuration for KSDK commands.', 'list': 'List all the available configurations for this tool.', 'set': 'Set a local configuration for this tool.', 'unset': 'Unset a local configuration for this tool.'}, 'images': {'list': 'List all locally built applications.', 'remove': 'Remove an application from the local applications list.', 'unpack': 'Extract the content of an application from its built image into the specified directory.'}, 'ksdk': {'app': 'Create, build and manage applications.', 'appregistry': '(Deprecated) Manage platform Applications.', 'apps': 'Manage platform Applications.', 'auth': 'Platform authentication.', 'configuration': 'Local configurations that enhance the usage of this tool.', 'info': 'Provide system information and the currently logged platform.', 'reset': 'Reset all configurations & cache used by Kelvin SDK.', 'secret': "Manage platform 'secrets'.", 'workload': 'Manage and view application workloads.'}, 'mlflow': {'create': 'Create app based on MLFlow model.', 'import': 'Import a MLFlow model into an existing Kelvin App.', 'list': 'Search MLFlow models.'}, 'secret': {'create': 'Create a secret on the platform.', 'delete': 'Delete secrets on the platform.', 'list': 'List all the available secrets on the platform.', 'update': 'Update an existing secret on the platform.'}, 'test': {'csv': 'Publishes data from a csv to the application.', 'generator': 'Publishes data generated by a custom generator class', 'simulator': "Generates random data to application's inputs"}, 'workload': {'deploy': 'Deploy a workload with specified parameters.', 'list': 'List the workloads available on the platform.', 'logs': 'Display the logs of a specific workload.', 'search': 'Search for specific workloads.', 'show': 'Show the details of a specific workload.', 'start': 'Start a workload on a node.', 'stop': 'Stop a running workload.', 'undeploy': 'Undeploy and delete a workload.', 'update': 'Update a specific workload based with new configurations.'}}¶
- class kelvin.sdk.lib.utils.click_utils.KSDKCommand(name, context_settings=None, callback=None, params=None, help=None, epilog=None, short_help=None, options_metavar='[OPTIONS]', add_help_option=True, no_args_is_help=False, hidden=False, deprecated=False)[source]¶
Bases:
Command- Parameters:
- class kelvin.sdk.lib.utils.click_utils.ClickExpandedPath(exists=False, file_okay=True, dir_okay=True, writable=False, readable=True, resolve_path=False, allow_dash=False, path_type=None, executable=False)[source]¶
Bases:
PathA Click path argument that returns a
Path, not a string.- Parameters:
- class kelvin.sdk.lib.utils.click_utils.AppNameWithVersionType(version_required=False, allow_container=False)[source]¶
Bases:
StringParamTypeClick Custom App name with version validator Validates for app name with version “someapp:1.0.0” and/or container name “somecontainer.app”
Attributes¶
- version_required: bool, optional, default False
If True, version needs to be present in the string e.g. “app:1.0.0”
- allow_container: bool, optional, default False
If True, also the container name is allowed in the following format “somecontainer.app”
- convert(value, param, ctx)[source]¶
Convert the value to the correct type. This is not called if the value is
None(the missing value).This must accept string values from the command line, as well as values that are already the correct type. It may also convert other compatible types.
The
paramandctxarguments may beNonein certain situations, such as when converting prompt input.If the value cannot be converted, call
fail()with a descriptive message.
Display Utilities¶
Utility functions for formatting and displaying output to the console.
Copyright 2021 Kelvin Inc.
Licensed under the Kelvin Inc. Developer SDK License Agreement (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.kelvininc.com/developer-sdk-license
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
- class kelvin.sdk.lib.utils.display_utils.DisplayObject(raw_data, tabulated_data)[source]¶
Bases:
object
- kelvin.sdk.lib.utils.display_utils.display_yes_or_no_question(question, default='no')[source]¶
Ask a yes/no question via raw_input() and return their answer.
It must be “yes” (the default), “no” or None (meaning an answer is required of the user).
Parameters¶
- questionstr
is a string that is presented to the user.
- defaultstr
is the presumed answer if the user just hits <Enter>.
Returns¶
- bool:
The “answer” return value is True for “yes” or False for “no”.
- kelvin.sdk.lib.utils.display_utils.display_custom_yes_or_no_question(question, default='no')[source]¶
Ask a yes/no question via raw_input() and return their answer.
It must be “yes” (the default), “no” or None (meaning an answer is required of the user).
Parameters¶
- questionstr
is a string that is presented to the user.
- defaultstr
is the presumed answer if the user just hits <Enter>.
Returns¶
- bool:
The “answer” return value is True for “yes” or False for “no”.
- kelvin.sdk.lib.utils.display_utils.display_data_entries(data, attributes, header_names, table_title, should_display=True, no_data_message=None)[source]¶
Display generic resource data on the screen.
Will yield the first 10 results (if any is returned) to prevent an overwhelming console output.
If this is the case, a prompt will ask whether the user wishes to see more outputs.
- Return type:
- Parameters:
Parameters¶
- data: List
the data to be printed. Usually, a list of dictionaries.
- attributes: List[str]
the data attributes.
- header_names: List[str]
the ‘pretty’ keys to be displayed atop the table.
- table_title: str
the title to be displayed atop the table.
- should_display: bool
indicates whether it should print the result.
- no_data_message: str
the custom message to be displayed on the table if no data is passed.
Returns¶
- DisplayObject:
a symbolic boolean flag indicating the display of entries was successful.
- kelvin.sdk.lib.utils.display_utils.display_data_object(data, object_title=None, should_display=True)[source]¶
Displays the provided data object, be it an app or a node, in a simple, yaml-like format.
- Return type:
- Parameters:
Parameters¶
- data: Union[DataModelBase, Dict]
the data object to be displayed on screen.
- object_title: Optional[str]
the title to display above the object.
- should_display: bool
indicates whether it should print the result.
Returns¶
- DisplayObject:
a symbolic boolean flag indicating the display of the object was successful.
- kelvin.sdk.lib.utils.display_utils.pretty_colored_content(content, initial_indent=1, indent=1, level=0, show_arm=False)[source]¶
When provided with a dictionary, return the colorized, ‘prettified’ yaml-like object.
- Return type:
- Parameters:
Parameters¶
- content: dict
the content to ‘prettify’
- initial_indent: int
the initial indent dimension to split on.
- indent: int
the initial indent dimension to split on.
- level: int
variable to track the level of display.
- show_arm: bool
indicate whether the ‘pretty arm’ should be displayed before a key.
Returns¶
- str:
a ‘pretty’, indented yaml-like object.
- kelvin.sdk.lib.utils.display_utils.success_colored_message(message)[source]¶
Yield a ‘green colored’ message.
Parameters¶
- messagestr
the message to yield in the success format.
Returns¶
str
Deploy Bulk Utilities¶
Utility functions for bulk deployment operations.
Exception Utilities¶
Utility functions for exception handling and error formatting.
- kelvin.sdk.lib.utils.exception_utils.retrieve_error_message_from_api_exception(api_error)[source]¶
Returns the ‘pretty’ error message from the APIError.
- Return type:
- Parameters:
api_error (APIError)
Parameters¶
- api_errorAPIError
The exception yielded by the service call.
Returns¶
- str:
a string containing the error message of the APIError.
- kelvin.sdk.lib.utils.exception_utils.retrieve_error_message_from_keycloak_exception(keycloak_exception)[source]¶
Returns the ‘pretty’ error message from the KeycloakError.
- Return type:
- Parameters:
keycloak_exception (KeycloakError)
Parameters¶
- keycloak_exceptionKeycloakError
The exception yielded by the service call.
Returns¶
- str:
a string containing the error message of the KeycloakError.
Logger Utilities¶
Utility functions for logging configuration and management.
Copyright 2021 Kelvin Inc.
Licensed under the Kelvin Inc. Developer SDK License Agreement (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.kelvininc.com/developer-sdk-license
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
- class kelvin.sdk.lib.utils.logger_utils.LoggerProtocol(*args, **kwargs)[source]¶
Bases:
Protocol
- class kelvin.sdk.lib.utils.logger_utils.BaseLogFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]¶
Bases:
Formatter
- class kelvin.sdk.lib.utils.logger_utils.KSDKLogFormatter(log_color=None, debug=False)[source]¶
Bases:
BaseLogFormatterLogging Formatter to add colors and count warning / errors
- message_format = '[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s'¶
- FORMATS = {LogColor.COLORED: {10: '\x1b[36m[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s\x1b[39m', 20: '\x1b[39m[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s\x1b[39m', 25: '\x1b[32m[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s\x1b[39m', 30: '\x1b[33m[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s\x1b[39m', 40: '\x1b[31m[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s\x1b[39m', 50: '\x1b[31m[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s\x1b[39m'}, LogColor.COLORLESS: {10: '[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s', 20: '[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s', 25: '[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s', 30: '[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s', 40: '[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s', 50: '[%(logger)s][%(asctime)s][%(levelname).1s] %(message)s'}}¶
- format(record)[source]¶
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
- class kelvin.sdk.lib.utils.logger_utils.JSONLogFormatter(debug=False)[source]¶
Bases:
BaseLogFormatter- Parameters:
debug (bool)
- format(record)[source]¶
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
Version Utilities¶
Utility functions for version parsing and comparison.
Copyright 2021 Kelvin Inc.
Licensed under the Kelvin Inc. Developer SDK License Agreement (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.kelvininc.com/developer-sdk-license
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
- kelvin.sdk.lib.utils.version_utils.assess_version_status(current_version, minimum_version, latest_version, should_warn=True)[source]¶
Verifies whether the current KSDK version is supported.
Warn the user, if ‘should_warn’ is set to True, that the SDK is outdated. Raise an exception should it not respect the minimum version.
- Return type:
- Parameters:
Parameters¶
- current_versionstr
the current version to check.
- minimum_versionstr
the minimum accepted version.
- latest_versionstr
the latest version of the SDK.
- should_warnbool
if set to true, will warn the user in case the ksdk is out of version.
Returns¶
- VersionStatus:
the corresponding version status.
PyPI Utilities¶
Utility functions for PyPI package management operations.
Copyright 2021 Kelvin Inc.
Licensed under the Kelvin Inc. Developer SDK License Agreement (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.kelvininc.com/developer-sdk-license
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
- kelvin.sdk.lib.utils.pypi_utils.get_pypi_credentials()[source]¶
When provided with Kelvin SDK global configuration, retrieve the pypi credentials to install external dependencies required by the application.
- Return type:
Returns¶
- dict:
a dictionary containing the required pypi credentials for external dependencies.
- kelvin.sdk.lib.utils.pypi_utils.get_pretty_source_from_section(section, pypi_url)[source]¶
Return the pretty log entry for a given section & pypi url.
Parameters¶
- section :str
the section (env/pip) corresponding to the pip credential source.
- pypi_url :str
the pypi url associated to the section.
Returns¶
- str:
the redacted version of the original url.