pieces-app / cli-agent

Pieces CLI for interacting with Pieces OS
https://docs.pieces.app/extensions-plugins/cli
MIT License
46 stars 15 forks source link

add null safety to avoid Nonetype object has no attribute #99

Closed Bishoy-at-pieces closed 3 months ago

Bishoy-at-pieces commented 3 months ago

Add none safety in the assets/assets_api.py in the original.fragment.string.raw to avoid any errors the fragment or the string or the raw might be None

@staticmethod
    def update_asset_value(file_path,asset_id):
        try:
            with open(file_path,"r") as f:
                data = f.read()
        except FileNotFoundError:
            show_error("Error in update asset","File not found")
            return
        asset_api = AssetApi(Settings.api_client)
        format_api = FormatApi(Settings.api_client)

        # get asset
        created = asset_api.asset_snapshot(asset_id, transferables=False)

        # update the original format's value
        original = format_api.format_snapshot(created.original.id, transferable=True)
        if original.classification.generic == ClassificationGenericEnum.IMAGE:
            show_error("Error in update asset","Original format is not supported")
            return
        if original.fragment.string.raw:
            original.fragment.string.raw = data
        elif original.file.string.raw:
            original.file.string.raw = data
        # check if the string value is not empty
        else :
            show_error("Error in update asset","Original value is empty")
            return

        format_api.format_update_value(transferable=False, format=original)

        print(f"{created.name} updated successfully.")