Module misty2py.basic_skills.free_memory
This module removes non-system files from Misty's memory.
Functions
def delete_assets(misty: Misty,
assets: Dict,
ignore_list: List = []) ‑> List[str]-
Expand source code
def delete_assets(misty: Misty, assets: Dict, ignore_list: List = []) -> List[str]: """Deletes all non-system files except those that are in `ignore_list`. Args: misty (Misty): the Misty whose assets to delete. assets (Dict): The dictionary of assets; a list of file names under their asset type as the keyword. ignore_list (List, optional): The list of file names for files that should not be deleted. Defaults to `[]`. Returns: List[str]: The list of files that were succesfully deleted. """ action = "_delete" delete_list = [] for asset_type, files in assets.items(): for file in files: if file not in ignore_list: if asset_type == "recording": data = {"Name": file} else: data = {"FileName": file} response = misty.perform_action( asset_type + action, data=data ).parse_to_dict() response = response.get("rest_response", {}) status = response.get("success") if status: print("Successfully deleted the asset `%s`." % file) delete_list.append(file) else: print( "Failed to delete the asset `%s`. Message: `%s`" % (file, response) ) return delete_listDeletes all non-system files except those that are in
ignore_list.Args
misty:Misty- the Misty whose assets to delete.
assets:Dict- The dictionary of assets; a list of file names under their asset type as the keyword.
ignore_list:List, optional- The list of file names for files that should not be deleted. Defaults to
[].
Returns
List[str]- The list of files that were succesfully deleted.
def free_memory(misty: Misty,
assets: List = ['audio', 'image', 'video', 'recording'],
save: bool = True,
overwrite: bool = False,
save_dir: str = 'data') ‑> Dict-
Expand source code
def free_memory( misty: Misty, assets: List = ["audio", "image", "video", "recording"], save: bool = True, overwrite: bool = False, save_dir: str = "data", ) -> Dict: """Removes all non-system files in Misty's memory. Args: misty (Misty): the Misty whose memory to free. assets (List, optional): The list of asset types to delete. Defaults to `["audio", "image", "video", "recording"]`. save (bool, optional): Whether to save the files that are being deleted from Misty's memory. Defaults to `True`. overwrite (bool): Whether the overwrite the file if it exists. Defaults to `False`. save_dir (str, optional): The path to the directory that will store saved assets. Defaults to `"data"`. Returns: Dict: a dictionary with the key `overall_success` specifying whether all actions were successful and a key for every action containing the dictionarised Misty2pyResponse to that action. """ save_dir = get_abs_path(save_dir) enable_audio = misty.perform_action("audio_enable").parse_to_dict() enable_av = misty.perform_action("streaming_av_enable").parse_to_dict() enable_camera = misty.perform_action("camera_enable").parse_to_dict() assets_to_delete = get_non_system_assets(misty, assets=assets) deletion = {} if len(assets_to_delete) == 0: deletion["success"] = True deletion["message"] = "No non-system files present." else: failed_to_save_list = [] if save: failed_to_save_list = save_assets( misty, assets_to_delete, save_dir, overwrite ) deleted = delete_assets(misty, assets_to_delete, failed_to_save_list) if len(deleted) > 0: deletion["success"] = True deletion["message"] = "Successfully deleted following assets: %s" % str( deleted ) else: deletion["success"] = False deletion["message"] = "Failed to delete any assets." disable_audio = misty.perform_action("audio_disable").parse_to_dict() disable_av = misty.perform_action("streaming_av_disable").parse_to_dict() disable_camera = misty.perform_action("camera_disable").parse_to_dict() return success_of_action_dict( enable_audio=enable_audio, enable_av=enable_av, enable_camera=enable_camera, deletion={ "overall_success": deletion["success"], "misty2py_response": deletion, }, disable_audio=disable_audio, disable_av=disable_av, disable_camera=disable_camera, )Removes all non-system files in Misty's memory.
Args
misty:Misty- the Misty whose memory to free.
assets:List, optional- The list of asset types to delete. Defaults to
["audio", "image", "video", "recording"]. save:bool, optional- Whether to save the files that are being deleted from Misty's memory. Defaults to
True. overwrite:bool- Whether the overwrite the file if it exists. Defaults to
False. save_dir:str, optional- The path to the directory that will store saved assets. Defaults to
"data".
Returns
Dict- a dictionary with the key
overall_successspecifying whether all actions were successful and a key for every action containing the dictionarised Misty2pyResponse to that action.
def get_asset_properties(asset_type: str, file: str) ‑> Tuple[Dict, str, str]-
Expand source code
def get_asset_properties(asset_type: str, file: str) -> Tuple[Dict, str, str]: """Obtains the parameters, the name and the extension of a file. Args: asset_type (str): The type of the asset. file (str): The file name. Returns: Tuple[Dict, str, str]: the parameters (a dict with keys `Name` for recordings or `FileName` for other asset types containing the name and the key `Base64` set to `"true"`), the file name and the file extension. """ if asset_type == "recording": params = {"Name": file, "Base64": "true"} else: params = {"FileName": file, "Base64": "true"} split_file_name = file.split(".") name = split_file_name[0] if len(split_file_name) > 1: ext = split_file_name[1] else: ext = "unknown" return params, name, extObtains the parameters, the name and the extension of a file.
Args
asset_type:str- The type of the asset.
file:str- The file name.
Returns
Tuple[Dict, str, str]- the parameters (a dict with keys
Namefor recordings orFileNamefor other asset types containing the name and the keyBase64set to"true"), the file name and the file extension.
def get_non_system_assets(misty: Misty,
assets: List = ['audio', 'image', 'video', 'recording']) ‑> Dict-
Expand source code
def get_non_system_assets( misty: Misty, assets: List = ["audio", "image", "video", "recording"], ) -> Dict: """Obtains the list of all non-system assets that fall under one of the `assets` types. Args: misty (Misty): The Misty whose non system assets to obtain. assets (List, optional): The types of assets to obtain. Defaults to `["audio", "image", "video", "recording"]`. Returns: Dict: all non-system assets of a given type (keywords) listed as their names in the list under the type keyword. """ action = "_list" non_sys_assets = {} for asset in assets: data = misty.get_info(asset + action).parse_to_dict() data = data.get("rest_response", {}) result = data.get("result", []) for hit in result: name = hit.get("name") if not name: continue is_system = hit.get("systemAsset", True) if not is_system: if not non_sys_assets.get(asset): non_sys_assets[asset] = [] non_sys_assets[asset].append(name) print( "Found a non-system asset of type `%s` named `%s`." % (asset, name) ) return non_sys_assetsObtains the list of all non-system assets that fall under one of the
assetstypes.Args
misty:Misty- The Misty whose non system assets to obtain.
assets:List, optional- The types of assets to obtain. Defaults to
["audio", "image", "video", "recording"].
Returns
Dict- all non-system assets of a given type (keywords) listed as their names in the list under the type keyword.
def save_assets(misty: Misty,
assets: Dict,
location: str,
overwrite: bool) ‑> List[str]-
Expand source code
def save_assets( misty: Misty, assets: Dict, location: str, overwrite: bool ) -> List[str]: """Saves multiple assets. Args: misty (Misty): the Misty whose assets to save. assets (Dict): The dictionary of assets; a list of file names under their asset type as the keyword. location (str): The absolute path to the directory where the assets are saved. overwrite (bool): Whether the overwrite the file if it exists. Returns: List[str]: A list of file names for the files that failed to be saved. """ failed_list = [] action = "_file" for asset_type, files in assets.items(): for file in files: params, name, ext = get_asset_properties(asset_type, file) response = misty.get_info( asset_type + action, params=params ).parse_to_dict() response = response.get("rest_response", {}) result = response.get("result") if not result: failed_list.append(file) else: file_name = "%s_%s_%s_in_base64.txt" % (asset_type, name, ext) full_path = os.path.join(location, file_name) file_content = result.get("base64") if not file_content: failed_list.append(file) else: success = save_base64_str(full_path, file_content, overwrite) if not success: failed_list.append(file) return failed_listSaves multiple assets.
Args
misty:Misty- the Misty whose assets to save.
assets:Dict- The dictionary of assets; a list of file names under their asset type as the keyword.
location:str- The absolute path to the directory where the assets are saved.
overwrite:bool- Whether the overwrite the file if it exists.
Returns
List[str]- A list of file names for the files that failed to be saved.
def save_base64_str(full_path: str, content: str, overwrite: bool) ‑> bool-
Expand source code
def save_base64_str(full_path: str, content: str, overwrite: bool) -> bool: """Saves an asset content to the file specified by `full_path`. Args: full_path (str): The absolute path to the save location. content (str): The base64 content to save. overwrite (bool): Whether the overwrite the file if it exists. Returns: bool: The successfulness of the operation. """ if not overwrite and os.path.exists(full_path): print("File `%s` already exists, not overwriting." % full_path) return True try: with open(full_path, "w") as f: f.write(content) print("Asset saved into `%s`." % full_path) return True except Exception as e: print( "Failed to save the asset into `%s`. Error message: `%s`" % (full_path, e) ) return FalseSaves an asset content to the file specified by
full_path.Args
full_path:str- The absolute path to the save location.
content:str- The base64 content to save.
overwrite:bool- Whether the overwrite the file if it exists.
Returns
bool- The successfulness of the operation.