def get_functions(self, **kwargs): api = ce_api.FunctionsApi(self.client) f_list = api_utils.api_call(api.get_functions_api_v1_functions_get) functions = [Function(**f.to_dict()) for f in f_list] if kwargs: functions = client_utils.filter_objects(functions, **kwargs) return functions
def get_function_versions(self, function_id: Text, **kwargs) -> List[FunctionVersion]: api = ce_api.FunctionsApi(self.client) fv_list = api_utils.api_call( api. get_function_versions_api_v1_functions_function_id_versions_get, function_id) versions = [FunctionVersion(**fv.to_dict()) for fv in fv_list] if kwargs: versions = client_utils.filter_objects(versions, **kwargs) return versions
def from_callable(cls, client, fn, params): path = inspect.getfile(fn) name = fn.__name__ message = 'Automatic message used by the Python SDK' with open(path, 'rb') as file: data = file.read() encoded_file = base64.b64encode(data).decode() fn_api = ce_api.FunctionsApi(client.client) fn_list = api_utils.api_call( func=fn_api.get_functions_api_v1_functions_get) matching_fn_list = [fn for fn in fn_list if fn.name == name] if len(matching_fn_list) == 0: logging.info('No matching functions found! Pushing a new ' 'function!') func = api_utils.api_call( func=fn_api.create_function_api_v1_functions_post, body=models.Function.creator( name=name, function_type=FunctionTypes.model.name, udf_path=name, message=message, file_contents=encoded_file)) version = func.function_versions[0] elif len(matching_fn_list) == 1: logging.info('Matching functions found! Pushing a new ' 'function version!') func = matching_fn_list[0] version = api_utils.api_call( func=fn_api. create_function_version_api_v1_functions_function_id_versions_post, function_id=func.id, body=models.FunctionVersion.creator( udf_path=name, message=message, file_contents=encoded_file)) else: raise ValueError('Too many functions with a matching name') fn = '@'.join([func.id, version.id]) params = params return cls(fn=fn, params=params)
def list_functions(info): """List the given custom functions""" api = ce_api.FunctionsApi(api_client(info)) f_list = api_call(api.get_functions_api_v1_functions_get) declare('You have declared {count} different ' 'function(s) so far. \n'.format(count=len(f_list))) if f_list: table = [] for f in f_list: table.append({'ID': format_uuid(f.id), 'Name': f.name, 'Type': f.function_type, 'Created At': f.created_at}) click.echo(tabulate(table, headers='keys', tablefmt='presto')) click.echo()
def magic_function(self, function_id, version_id): import sys from IPython import get_ipython if 'ipykernel' not in sys.modules: raise EnvironmentError('The magic function is only usable in a ' 'Jupyter notebook.') api = ce_api.FunctionsApi(self.client) function_version = api_utils.api_call( api. get_function_version_api_v1_functions_function_id_versions_version_id_get, function_id, version_id) f = base64.b64decode(function_version.file_contents).decode('utf-8') get_ipython().set_next_input(f)
def create_function(info, local_path, name, func_type, udf_name, message): """Register a custom function to use with the Core Engine""" click.echo('Registering the function {}.'.format(udf_name)) with open(local_path, 'rb') as file: data = file.read() encoded_file = base64.b64encode(data).decode() api = ce_api.FunctionsApi(api_client(info)) api_call(api.create_function_api_v1_functions_post, FunctionCreate(name=name, function_type=func_type, udf_path=udf_name, message=message, file_contents=encoded_file)) declare('Function registered.')
def push_function(self, name: Text, function_type: Text, local_path: Text, udf_name: Text, message: Text = None) -> FunctionVersion: with open(local_path, 'rb') as file: data = file.read() encoded_file = base64.b64encode(data).decode() api = ce_api.FunctionsApi(self.client) fn_list = api_utils.api_call( func=api.get_functions_api_v1_functions_get) matching_fn_list = [fn for fn in fn_list if fn.name == name] if len(matching_fn_list) == 0: logging.info('No matching functions found! Pushing a new ' 'function!') func = api_utils.api_call( api.create_function_api_v1_functions_post, Function.creator(name=name, function_type=function_type, udf_path=udf_name, message=message, file_contents=encoded_file)) return FunctionVersion(**func.function_versions[0].to_dict()) elif len(matching_fn_list) == 1: logging.info('Matching functions found! Pushing a new ' 'function version!') func = matching_fn_list[0] return api_utils.api_call( api. create_function_version_api_v1_functions_function_id_versions_post, func.id, FunctionVersion.creator(udf_path=name, message=message, file_contents=encoded_file))
def update_function(info, function_id, local_path, udf_name, message): """Add a new version to a function and update it""" click.echo('Updating the function {}.'.format( format_uuid(function_id))) api = ce_api.FunctionsApi(api_client(info)) f_list = api_call(api.get_functions_api_v1_functions_get) f_uuid = find_closest_uuid(function_id, f_list) with open(local_path, 'rb') as file: data = file.read() encoded_file = base64.b64encode(data).decode() api_call( api.create_function_version_api_v1_functions_function_id_versions_post, FunctionVersionCreate(udf_path=udf_name, message=message, file_contents=encoded_file), f_uuid) declare('Function updated!')
def list_versions(info, function_id): """List of versions for a selected custom function""" api = ce_api.FunctionsApi(api_client(info)) f_list = api_call(api.get_functions_api_v1_functions_get) f_uuid = find_closest_uuid(function_id, f_list) v_list = api_call( api.get_function_versions_api_v1_functions_function_id_versions_get, f_uuid) declare('Function with {id} has {count} ' 'versions.\n'.format(id=format_uuid(function_id), count=len(v_list))) if v_list: table = [] for v in v_list: table.append({'ID': format_uuid(v.id), 'Created At': v.created_at, 'Description': v.message}) click.echo(tabulate(table, headers='keys', tablefmt='presto')) click.echo()
def pull_function_version(info, function_id, version_id, output_path): """Download a version of a given custom function""" api = ce_api.FunctionsApi(api_client(info)) # Infer the function uuid and name f_list = api_call(api.get_functions_api_v1_functions_get) f_uuid = find_closest_uuid(function_id, f_list) f_name = [f.name for f in f_list if f.id == f_uuid][0] # Infer the version uuid v_list = api_call( api.get_function_versions_api_v1_functions_function_id_versions_get, f_uuid) v_uuid = find_closest_uuid(version_id, v_list) notice('Downloading the function with the following parameters: \n' 'Name: {f_name}\n' 'function_id: {f_id}\n' 'version_id: {v_id}\n'.format(f_name=f_name, f_id=format_uuid(f_uuid), v_id=format_uuid(v_uuid))) # Get the file and write it to the output path encoded_file = api_call( api.get_function_version_api_v1_functions_function_id_versions_version_id_get, f_uuid, v_uuid) # Derive the output path and download if output_path is None: output_path = os.path.join(os.getcwd(), '{}@{}.py'.format(f_name, v_uuid)) with open(output_path, 'wb') as f: f.write(base64.b64decode(encoded_file.file_contents)) declare('File downloaded to {}'.format(output_path))
def pull_function_version(self, function_id, version_id, output_path=None): api = ce_api.FunctionsApi(self.client) f = api_utils.api_call( api.get_single_function_api_v1_functions_function_id_get, function_id) f_name = f.name function_version = api_utils.api_call( api. get_function_version_api_v1_functions_function_id_versions_version_id_get, function_id, version_id) if output_path is None: output_path = os.path.join(os.getcwd(), '{}@{}.py'.format(f_name, version_id)) with open(output_path, 'wb') as f: f.write(base64.b64decode(function_version.file_contents)) loader = machinery.SourceFileLoader(fullname='user_module', path=output_path) user_module = types.ModuleType(loader.name) loader.exec_module(user_module) return getattr(user_module, function_version.udf_path)