From 3a49202452cc8d67366bf4674ee3ac28799c8261 Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Fri, 25 Oct 2024 17:05:19 +0900 Subject: [PATCH 1/7] Debugging events decoding --- examples/transactions_examples.py | 19 ++ failed_events.json | 291 ++++++++++++++++++++++++++++++ flow_py_sdk/cadence/constants.py | 2 + flow_py_sdk/cadence/decode.py | 10 + flow_py_sdk/cadence/kinds.py | 16 +- flow_py_sdk/client/entities.py | 55 +++++- myenv/bin/Activate.ps1 | 247 +++++++++++++++++++++++++ myenv/bin/activate | 69 +++++++ myenv/bin/activate.csh | 26 +++ myenv/bin/activate.fish | 69 +++++++ myenv/bin/pip | 8 + myenv/bin/pip3 | 8 + myenv/bin/pip3.11 | 8 + myenv/bin/python | 1 + myenv/bin/python3 | 1 + myenv/bin/python3.11 | 1 + myenv/pyvenv.cfg | 5 + 17 files changed, 820 insertions(+), 16 deletions(-) create mode 100644 failed_events.json create mode 100644 myenv/bin/Activate.ps1 create mode 100644 myenv/bin/activate create mode 100644 myenv/bin/activate.csh create mode 100644 myenv/bin/activate.fish create mode 100755 myenv/bin/pip create mode 100755 myenv/bin/pip3 create mode 100755 myenv/bin/pip3.11 create mode 120000 myenv/bin/python create mode 120000 myenv/bin/python3 create mode 120000 myenv/bin/python3.11 create mode 100644 myenv/pyvenv.cfg diff --git a/examples/transactions_examples.py b/examples/transactions_examples.py index 4727d27..f091bca 100644 --- a/examples/transactions_examples.py +++ b/examples/transactions_examples.py @@ -281,3 +281,22 @@ async def run(self, ctx: Config): f"transaction proposer: {transaction.proposal_key.address.hex()}" ) self.log.info(f"transaction script: {transaction.script.decode('utf-8')}") + + +class DebuggingFetchTransactionByIdExample(Example): + def __init__(self) -> None: + super().__init__(tag="T.LL.", name="DebuggingFetchTransactionByIdExample", sort_order=501) + + async def run(self, ctx: Config): + # First Step : Create a client to connect to the flow blockchain + # flow_client function creates a client using the host and port + async with flow_client( + # host=ctx.access_node_host, port=ctx.access_node_port + host="access.mainnet.nodes.onflow.org", port=9000 + ) as client: + + tx_id = "ef6e6dcba87c5f853e0ec940dd382d609ffe11109c28ed239843d71618fa1d71" + + transaction = await client.get_transaction_result(id=bytes.fromhex(tx_id)) + + print(f"Transaction ID: {tx_id}") diff --git a/failed_events.json b/failed_events.json new file mode 100644 index 0000000..502dc93 --- /dev/null +++ b/failed_events.json @@ -0,0 +1,291 @@ +[ + { + "value": { + "id": "flow.AccountCapabilityControllerIssued", + "fields": [ + { + "name": "id", + "value": { + "value": "3", + "type": "UInt64" + } + }, + { + "name": "address", + "value": { + "value": "0x646997908e65fb70", + "type": "Address" + } + }, + { + "name": "type", + "value": { + "value": { + "staticType": { + "type": { + "kind": "Account" + }, + "kind": "Reference", + "authorization": { + "kind": "EntitlementConjunctionSet", + "entitlements": [ + { "kind": "Entitlement", "typeID": "Storage" }, + { "kind": "Entitlement", "typeID": "Contracts" }, + { "kind": "Entitlement", "typeID": "Keys" }, + { "kind": "Entitlement", "typeID": "Inbox" }, + { "kind": "Entitlement", "typeID": "Capabilities" } + ] + } + } + }, + "type": "Type" + } + } + ] + }, + "type": "Event" + }, + { + "value": { + "id": "flow.StorageCapabilityControllerIssued", + "fields": [ + { + "name": "id", + "value": { + "value": "148", + "type": "UInt64" + } + }, + { + "name": "address", + "value": { + "value": "0x0342200ce19810f6", + "type": "Address" + } + }, + { + "name": "type", + "value": { + "value": { + "staticType": { + "type": { + "type": "", + "kind": "Resource", + "typeID": "A.befbaccb5032a457.ContractManager.Manager", + "fields": [ + { "type": { "kind": "UInt64" }, "id": "uuid" }, + { + "type": { + "type": { + "kind": "Account" + }, + "kind": "Reference", + "authorization": { + "kind": "EntitlementConjunctionSet", + "entitlements": [ + { "kind": "Entitlement", "typeID": "Storage" }, + { "kind": "Entitlement", "typeID": "Contracts" }, + { "kind": "Entitlement", "typeID": "Keys" }, + { "kind": "Entitlement", "typeID": "Inbox" }, + { "kind": "Entitlement", "typeID": "Capabilities" } + ] + } + }, + "kind": "Capability", + "id": "acct" + }, + { + "type": { + "type": { + "type": "", + "kind": "Resource", + "typeID": "A.707c0b39a8d689cb.FungibleTokenRouter.Router", + "fields": [ + { "type": { "kind": "UInt64" }, "id": "uuid" }, + { "type": { "kind": "Address" }, "id": "defaultAddress" }, + { + "type": { + "key": { "kind": "String" }, + "value": { "kind": "Address" }, + "kind": "Dictionary" + }, + "id": "addressOverrides" + } + ] + }, + "kind": "Reference", + "authorization": { + "kind": "EntitlementConjunctionSet", + "entitlements": [ + { + "kind": "Entitlement", + "typeID": "A.707c0b39a8d689cb.FungibleTokenRouter.Owner" + } + ] + } + }, + "kind": "Capability", + "id": "routerCap" + }, + { + "type": { + "key": { "kind": "String" }, + "value": { "kind": "AnyStruct" }, + "kind": "Dictionary" + }, + "id": "data" + }, + { + "type": { + "key": { "kind": "String" }, + "value": { "kind": "AnyResource" }, + "kind": "Dictionary" + }, + "id": "resources" + } + ] + }, + "kind": "Reference", + "authorization": { + "kind": "Unauthorized", + "entitlements": null + } + } + }, + "type": "Type" + } + }, + { + "name": "path", + "value": { + "value": { + "domain": "storage", + "identifier": "ContractManager_0xbefbaccb5032a457" + }, + "type": "Path" + } + } + ] + }, + "type": "Event" + }, + { + "value": { + "id": "flow.CapabilityPublished", + "fields": [ + { + "name": "address", + "value": { + "value": "0x0342200ce19810f6", + "type": "Address" + } + }, + { + "name": "path", + "value": { + "value": { + "domain": "public", + "identifier": "ContractManager_0xbefbaccb5032a457" + }, + "type": "Path" + } + }, + { + "name": "capability", + "value": { + "value": { + "borrowType": { + "type": { + "type": "", + "kind": "Resource", + "typeID": "A.befbaccb5032a457.ContractManager.Manager", + "fields": [ + { "type": { "kind": "UInt64" }, "id": "uuid" }, + { + "type": { + "type": { + "kind": "Account" + }, + "kind": "Reference", + "authorization": { + "kind": "EntitlementConjunctionSet", + "entitlements": [ + { "kind": "Entitlement", "typeID": "Storage" }, + { "kind": "Entitlement", "typeID": "Contracts" }, + { "kind": "Entitlement", "typeID": "Keys" }, + { "kind": "Entitlement", "typeID": "Inbox" }, + { "kind": "Entitlement", "typeID": "Capabilities" } + ] + } + }, + "kind": "Capability", + "id": "acct" + }, + { + "type": { + "type": { + "type": "", + "kind": "Resource", + "typeID": "A.707c0b39a8d689cb.FungibleTokenRouter.Router", + "fields": [ + { "type": { "kind": "UInt64" }, "id": "uuid" }, + { "type": { "kind": "Address" }, "id": "defaultAddress" }, + { + "type": { + "key": { "kind": "String" }, + "value": { "kind": "Address" }, + "kind": "Dictionary" + }, + "id": "addressOverrides" + } + ] + }, + "kind": "Reference", + "authorization": { + "kind": "EntitlementConjunctionSet", + "entitlements": [ + { + "kind": "Entitlement", + "typeID": "A.707c0b39a8d689cb.FungibleTokenRouter.Owner" + } + ] + } + }, + "kind": "Capability", + "id": "routerCap" + }, + { + "type": { + "key": { "kind": "String" }, + "value": { "kind": "AnyStruct" }, + "kind": "Dictionary" + }, + "id": "data" + }, + { + "type": { + "key": { "kind": "String" }, + "value": { "kind": "AnyResource" }, + "kind": "Dictionary" + }, + "id": "resources" + } + ] + }, + "kind": "Reference", + "authorization": { + "kind": "Unauthorized", + "entitlements": null + } + }, + "address": "0x0342200ce19810f6", + "id": "148" + }, + "type": "Capability" + } + } + ] + }, + "type": "Event" + } +] diff --git a/flow_py_sdk/cadence/constants.py b/flow_py_sdk/cadence/constants.py index 821f324..ccd15aa 100644 --- a/flow_py_sdk/cadence/constants.py +++ b/flow_py_sdk/cadence/constants.py @@ -65,6 +65,8 @@ inclusiveRangeTypeStr = "InclusiveRange" capabilityTypeStr = "Capability" functionTypeStr = "Function" +entitlementConjunctionSetStr = "EntitlementConjunctionSet" +entitlementKey = "Entitlement" fix64_scale = int(8) fix64_factor = int(100_000_000) diff --git a/flow_py_sdk/cadence/decode.py b/flow_py_sdk/cadence/decode.py index 9159d14..5054daf 100644 --- a/flow_py_sdk/cadence/decode.py +++ b/flow_py_sdk/cadence/decode.py @@ -42,6 +42,16 @@ def decode(obj: [dict[Any, Any]]) -> Union[Value, Kind]: decoder = _cadence_decoders[type_] return decoder(obj) + # Recursively handle nested dictionaries and lists + for key, value in obj.items(): + if isinstance(value, dict): + obj[key] = decode(value) + elif isinstance(value, list): + obj[key] = [decode(item) for item in value] + + # Logging for further troubleshooting + print(f"Unhandled structure: {obj}") + raise NotImplementedError() diff --git a/flow_py_sdk/cadence/kinds.py b/flow_py_sdk/cadence/kinds.py index 094c13a..b0df5f5 100644 --- a/flow_py_sdk/cadence/kinds.py +++ b/flow_py_sdk/cadence/kinds.py @@ -350,13 +350,15 @@ def __init__(self, entitlements: list[EntitlementBaseKind]) -> None: @classmethod def decode(cls, value) -> "Kind": - entitlements_val = value[c.entitlementsKey] - entitlements = [ - decode(v).as_kind(EntitlementBaseKind) for v in entitlements_val - ] - return cls( - entitlements, - ) + try: + entitlements_val = value[c.entitlementsKey] + entitlements = [ + decode(v).as_kind(EntitlementBaseKind) for v in entitlements_val + ] + return cls(entitlements) + except Exception as e: + print(f"Error decoding entitlements for {cls.kind_str()}: {value}. Error: {e}") + return None def encode_kind(self) -> dict: return {c.entitlementsKey: [e.encode() for e in self.entitlements]} diff --git a/flow_py_sdk/client/entities.py b/flow_py_sdk/client/entities.py index dbb4126..7f3fa7b 100644 --- a/flow_py_sdk/client/entities.py +++ b/flow_py_sdk/client/entities.py @@ -1,4 +1,5 @@ import json +import logging from datetime import datetime from typing import Dict, List @@ -7,6 +8,7 @@ from flow_py_sdk.cadence import cadence_object_hook from flow_py_sdk.proto.flow import entities, access +log = logging.getLogger(__name__) class Account(object): def __init__( @@ -149,17 +151,40 @@ def __init__( self.transaction_index: int = transaction_index self.event_index: int = event_index self.payload: bytes = payload - self.value: cadence.Event = json.loads(payload, object_hook=cadence_object_hook) + + # Add error handling for deserializing payload + max_payload_size = 1 * 1024 * 1024 # 1 MB size limit for payload + if len(payload) > max_payload_size: + logging.warning(f"Payload too large for event {event_index}: {len(payload)} bytes. Skipping deserialization.") + self.value = None + else: + try: + # Attempt to decode the payload + self.value: cadence.Event = json.loads(payload, object_hook=cadence_object_hook) + except json.JSONDecodeError as e: + # Handle JSON decode errors specifically + print(payload) + # logging.error(f"JSON decode error for event {event_index} with payload: {payload[:100]}... Error: {str(e)}") + self.value = None + except Exception as e: + print(payload) + # Log the full payload and exception details for unexpected errors + # logging.error(f"Unexpected error deserializing payload for event {event_index} with payload: {payload[:100]}... Error: {str(e)}") + self.value = None @classmethod def from_proto(cls, proto: entities.Event) -> "Event": - return Event( - _type=proto.type, - transaction_id=proto.transaction_id, - transaction_index=proto.transaction_index, - event_index=proto.event_index, - payload=proto.payload, - ) + try: + return Event( + _type=proto.type, + transaction_id=proto.transaction_id, + transaction_index=proto.transaction_index, + event_index=proto.event_index, + payload=proto.payload, + ) + except Exception as e: + logging.error(f"Failed to deserialize event {proto.event_index}: {str(e)}") + return None # Returning None if deserialization fails class Transaction(object): @@ -287,13 +312,25 @@ def from_proto( cls, proto: access.TransactionResultResponse, id: bytes, + max_events: int = 50 ) -> "TransactionResultResponse": + events = [] + for i, event_proto in enumerate(proto.events[:max_events]): + try: + events.append(Event.from_proto(event_proto)) + except Exception as e: + print(f"Failed to deserialize event {i}: {e}") + continue + + if len(proto.events) > max_events: + print(f"Only processed {max_events} out of {len(proto.events)} events due to size limits.") + return TransactionResultResponse( id_=id, status=proto.status, status_code=proto.status_code, error_message=proto.error_message, - events=[Event.from_proto(e) for e in proto.events], + events=events, ) diff --git a/myenv/bin/Activate.ps1 b/myenv/bin/Activate.ps1 new file mode 100644 index 0000000..eeea358 --- /dev/null +++ b/myenv/bin/Activate.ps1 @@ -0,0 +1,247 @@ +<# +.Synopsis +Activate a Python virtual environment for the current PowerShell session. + +.Description +Pushes the python executable for a virtual environment to the front of the +$Env:PATH environment variable and sets the prompt to signify that you are +in a Python virtual environment. Makes use of the command line switches as +well as the `pyvenv.cfg` file values present in the virtual environment. + +.Parameter VenvDir +Path to the directory that contains the virtual environment to activate. The +default value for this is the parent of the directory that the Activate.ps1 +script is located within. + +.Parameter Prompt +The prompt prefix to display when this virtual environment is activated. By +default, this prompt is the name of the virtual environment folder (VenvDir) +surrounded by parentheses and followed by a single space (ie. '(.venv) '). + +.Example +Activate.ps1 +Activates the Python virtual environment that contains the Activate.ps1 script. + +.Example +Activate.ps1 -Verbose +Activates the Python virtual environment that contains the Activate.ps1 script, +and shows extra information about the activation as it executes. + +.Example +Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv +Activates the Python virtual environment located in the specified location. + +.Example +Activate.ps1 -Prompt "MyPython" +Activates the Python virtual environment that contains the Activate.ps1 script, +and prefixes the current prompt with the specified string (surrounded in +parentheses) while the virtual environment is active. + +.Notes +On Windows, it may be required to enable this Activate.ps1 script by setting the +execution policy for the user. You can do this by issuing the following PowerShell +command: + +PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser + +For more information on Execution Policies: +https://go.microsoft.com/fwlink/?LinkID=135170 + +#> +Param( + [Parameter(Mandatory = $false)] + [String] + $VenvDir, + [Parameter(Mandatory = $false)] + [String] + $Prompt +) + +<# Function declarations --------------------------------------------------- #> + +<# +.Synopsis +Remove all shell session elements added by the Activate script, including the +addition of the virtual environment's Python executable from the beginning of +the PATH variable. + +.Parameter NonDestructive +If present, do not remove this function from the global namespace for the +session. + +#> +function global:deactivate ([switch]$NonDestructive) { + # Revert to original values + + # The prior prompt: + if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { + Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt + Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT + } + + # The prior PYTHONHOME: + if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { + Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME + Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME + } + + # The prior PATH: + if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { + Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH + Remove-Item -Path Env:_OLD_VIRTUAL_PATH + } + + # Just remove the VIRTUAL_ENV altogether: + if (Test-Path -Path Env:VIRTUAL_ENV) { + Remove-Item -Path env:VIRTUAL_ENV + } + + # Just remove VIRTUAL_ENV_PROMPT altogether. + if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) { + Remove-Item -Path env:VIRTUAL_ENV_PROMPT + } + + # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: + if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { + Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force + } + + # Leave deactivate function in the global namespace if requested: + if (-not $NonDestructive) { + Remove-Item -Path function:deactivate + } +} + +<# +.Description +Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the +given folder, and returns them in a map. + +For each line in the pyvenv.cfg file, if that line can be parsed into exactly +two strings separated by `=` (with any amount of whitespace surrounding the =) +then it is considered a `key = value` line. The left hand string is the key, +the right hand is the value. + +If the value starts with a `'` or a `"` then the first and last character is +stripped from the value before being captured. + +.Parameter ConfigDir +Path to the directory that contains the `pyvenv.cfg` file. +#> +function Get-PyVenvConfig( + [String] + $ConfigDir +) { + Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" + + # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). + $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue + + # An empty map will be returned if no config file is found. + $pyvenvConfig = @{ } + + if ($pyvenvConfigPath) { + + Write-Verbose "File exists, parse `key = value` lines" + $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath + + $pyvenvConfigContent | ForEach-Object { + $keyval = $PSItem -split "\s*=\s*", 2 + if ($keyval[0] -and $keyval[1]) { + $val = $keyval[1] + + # Remove extraneous quotations around a string value. + if ("'""".Contains($val.Substring(0, 1))) { + $val = $val.Substring(1, $val.Length - 2) + } + + $pyvenvConfig[$keyval[0]] = $val + Write-Verbose "Adding Key: '$($keyval[0])'='$val'" + } + } + } + return $pyvenvConfig +} + + +<# Begin Activate script --------------------------------------------------- #> + +# Determine the containing directory of this script +$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition +$VenvExecDir = Get-Item -Path $VenvExecPath + +Write-Verbose "Activation script is located in path: '$VenvExecPath'" +Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" +Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" + +# Set values required in priority: CmdLine, ConfigFile, Default +# First, get the location of the virtual environment, it might not be +# VenvExecDir if specified on the command line. +if ($VenvDir) { + Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" +} +else { + Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." + $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") + Write-Verbose "VenvDir=$VenvDir" +} + +# Next, read the `pyvenv.cfg` file to determine any required value such +# as `prompt`. +$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir + +# Next, set the prompt from the command line, or the config file, or +# just use the name of the virtual environment folder. +if ($Prompt) { + Write-Verbose "Prompt specified as argument, using '$Prompt'" +} +else { + Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" + if ($pyvenvCfg -and $pyvenvCfg['prompt']) { + Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" + $Prompt = $pyvenvCfg['prompt']; + } + else { + Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)" + Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" + $Prompt = Split-Path -Path $venvDir -Leaf + } +} + +Write-Verbose "Prompt = '$Prompt'" +Write-Verbose "VenvDir='$VenvDir'" + +# Deactivate any currently active virtual environment, but leave the +# deactivate function in place. +deactivate -nondestructive + +# Now set the environment variable VIRTUAL_ENV, used by many tools to determine +# that there is an activated venv. +$env:VIRTUAL_ENV = $VenvDir + +if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { + + Write-Verbose "Setting prompt to '$Prompt'" + + # Set the prompt to include the env name + # Make sure _OLD_VIRTUAL_PROMPT is global + function global:_OLD_VIRTUAL_PROMPT { "" } + Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT + New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt + + function global:prompt { + Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " + _OLD_VIRTUAL_PROMPT + } + $env:VIRTUAL_ENV_PROMPT = $Prompt +} + +# Clear PYTHONHOME +if (Test-Path -Path Env:PYTHONHOME) { + Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME + Remove-Item -Path Env:PYTHONHOME +} + +# Add the venv to the PATH +Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH +$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" diff --git a/myenv/bin/activate b/myenv/bin/activate new file mode 100644 index 0000000..301a280 --- /dev/null +++ b/myenv/bin/activate @@ -0,0 +1,69 @@ +# This file must be used with "source bin/activate" *from bash* +# you cannot run it directly + +deactivate () { + # reset old environment variables + if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then + PATH="${_OLD_VIRTUAL_PATH:-}" + export PATH + unset _OLD_VIRTUAL_PATH + fi + if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then + PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" + export PYTHONHOME + unset _OLD_VIRTUAL_PYTHONHOME + fi + + # This should detect bash and zsh, which have a hash command that must + # be called to get it to forget past commands. Without forgetting + # past commands the $PATH changes we made may not be respected + if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then + hash -r 2> /dev/null + fi + + if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then + PS1="${_OLD_VIRTUAL_PS1:-}" + export PS1 + unset _OLD_VIRTUAL_PS1 + fi + + unset VIRTUAL_ENV + unset VIRTUAL_ENV_PROMPT + if [ ! "${1:-}" = "nondestructive" ] ; then + # Self destruct! + unset -f deactivate + fi +} + +# unset irrelevant variables +deactivate nondestructive + +VIRTUAL_ENV="/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv" +export VIRTUAL_ENV + +_OLD_VIRTUAL_PATH="$PATH" +PATH="$VIRTUAL_ENV/bin:$PATH" +export PATH + +# unset PYTHONHOME if set +# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) +# could use `if (set -u; : $PYTHONHOME) ;` in bash +if [ -n "${PYTHONHOME:-}" ] ; then + _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" + unset PYTHONHOME +fi + +if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then + _OLD_VIRTUAL_PS1="${PS1:-}" + PS1="(myenv) ${PS1:-}" + export PS1 + VIRTUAL_ENV_PROMPT="(myenv) " + export VIRTUAL_ENV_PROMPT +fi + +# This should detect bash and zsh, which have a hash command that must +# be called to get it to forget past commands. Without forgetting +# past commands the $PATH changes we made may not be respected +if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then + hash -r 2> /dev/null +fi diff --git a/myenv/bin/activate.csh b/myenv/bin/activate.csh new file mode 100644 index 0000000..6536251 --- /dev/null +++ b/myenv/bin/activate.csh @@ -0,0 +1,26 @@ +# This file must be used with "source bin/activate.csh" *from csh*. +# You cannot run it directly. +# Created by Davide Di Blasi . +# Ported to Python 3.3 venv by Andrew Svetlov + +alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate' + +# Unset irrelevant variables. +deactivate nondestructive + +setenv VIRTUAL_ENV "/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv" + +set _OLD_VIRTUAL_PATH="$PATH" +setenv PATH "$VIRTUAL_ENV/bin:$PATH" + + +set _OLD_VIRTUAL_PROMPT="$prompt" + +if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then + set prompt = "(myenv) $prompt" + setenv VIRTUAL_ENV_PROMPT "(myenv) " +endif + +alias pydoc python -m pydoc + +rehash diff --git a/myenv/bin/activate.fish b/myenv/bin/activate.fish new file mode 100644 index 0000000..d822583 --- /dev/null +++ b/myenv/bin/activate.fish @@ -0,0 +1,69 @@ +# This file must be used with "source /bin/activate.fish" *from fish* +# (https://fishshell.com/); you cannot run it directly. + +function deactivate -d "Exit virtual environment and return to normal shell environment" + # reset old environment variables + if test -n "$_OLD_VIRTUAL_PATH" + set -gx PATH $_OLD_VIRTUAL_PATH + set -e _OLD_VIRTUAL_PATH + end + if test -n "$_OLD_VIRTUAL_PYTHONHOME" + set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME + set -e _OLD_VIRTUAL_PYTHONHOME + end + + if test -n "$_OLD_FISH_PROMPT_OVERRIDE" + set -e _OLD_FISH_PROMPT_OVERRIDE + # prevents error when using nested fish instances (Issue #93858) + if functions -q _old_fish_prompt + functions -e fish_prompt + functions -c _old_fish_prompt fish_prompt + functions -e _old_fish_prompt + end + end + + set -e VIRTUAL_ENV + set -e VIRTUAL_ENV_PROMPT + if test "$argv[1]" != "nondestructive" + # Self-destruct! + functions -e deactivate + end +end + +# Unset irrelevant variables. +deactivate nondestructive + +set -gx VIRTUAL_ENV "/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv" + +set -gx _OLD_VIRTUAL_PATH $PATH +set -gx PATH "$VIRTUAL_ENV/bin" $PATH + +# Unset PYTHONHOME if set. +if set -q PYTHONHOME + set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME + set -e PYTHONHOME +end + +if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" + # fish uses a function instead of an env var to generate the prompt. + + # Save the current fish_prompt function as the function _old_fish_prompt. + functions -c fish_prompt _old_fish_prompt + + # With the original prompt function renamed, we can override with our own. + function fish_prompt + # Save the return status of the last command. + set -l old_status $status + + # Output the venv prompt; color taken from the blue of the Python logo. + printf "%s%s%s" (set_color 4B8BBE) "(myenv) " (set_color normal) + + # Restore the return status of the previous command. + echo "exit $old_status" | . + # Output the original/"old" prompt. + _old_fish_prompt + end + + set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" + set -gx VIRTUAL_ENV_PROMPT "(myenv) " +end diff --git a/myenv/bin/pip b/myenv/bin/pip new file mode 100755 index 0000000..fe189ff --- /dev/null +++ b/myenv/bin/pip @@ -0,0 +1,8 @@ +#!/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv/bin/python +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/myenv/bin/pip3 b/myenv/bin/pip3 new file mode 100755 index 0000000..fe189ff --- /dev/null +++ b/myenv/bin/pip3 @@ -0,0 +1,8 @@ +#!/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv/bin/python +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/myenv/bin/pip3.11 b/myenv/bin/pip3.11 new file mode 100755 index 0000000..fe189ff --- /dev/null +++ b/myenv/bin/pip3.11 @@ -0,0 +1,8 @@ +#!/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv/bin/python +# -*- coding: utf-8 -*- +import re +import sys +from pip._internal.cli.main import main +if __name__ == '__main__': + sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) + sys.exit(main()) diff --git a/myenv/bin/python b/myenv/bin/python new file mode 120000 index 0000000..c6a3ca3 --- /dev/null +++ b/myenv/bin/python @@ -0,0 +1 @@ +/Users/lealobanov/miniconda3/bin/python \ No newline at end of file diff --git a/myenv/bin/python3 b/myenv/bin/python3 new file mode 120000 index 0000000..d8654aa --- /dev/null +++ b/myenv/bin/python3 @@ -0,0 +1 @@ +python \ No newline at end of file diff --git a/myenv/bin/python3.11 b/myenv/bin/python3.11 new file mode 120000 index 0000000..d8654aa --- /dev/null +++ b/myenv/bin/python3.11 @@ -0,0 +1 @@ +python \ No newline at end of file diff --git a/myenv/pyvenv.cfg b/myenv/pyvenv.cfg new file mode 100644 index 0000000..fbd7b3f --- /dev/null +++ b/myenv/pyvenv.cfg @@ -0,0 +1,5 @@ +home = /Users/lealobanov/miniconda3/bin +include-system-site-packages = false +version = 3.11.5 +executable = /Users/lealobanov/miniconda3/bin/python3.11 +command = /Users/lealobanov/miniconda3/bin/python -m venv /Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv From 3a5433d8cb448cc53f75cd98524bb1c42ead20ce Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Fri, 25 Oct 2024 17:06:25 +0900 Subject: [PATCH 2/7] Clean up logs --- flow_py_sdk/cadence/decode.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/flow_py_sdk/cadence/decode.py b/flow_py_sdk/cadence/decode.py index 5054daf..b56c0a2 100644 --- a/flow_py_sdk/cadence/decode.py +++ b/flow_py_sdk/cadence/decode.py @@ -49,9 +49,6 @@ def decode(obj: [dict[Any, Any]]) -> Union[Value, Kind]: elif isinstance(value, list): obj[key] = [decode(item) for item in value] - # Logging for further troubleshooting - print(f"Unhandled structure: {obj}") - raise NotImplementedError() From 402e0f905d9b6c0fa9d174c954284a5e7917574c Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Mon, 28 Oct 2024 14:49:42 +0900 Subject: [PATCH 3/7] Clean up --- .gitignore | 1 + flow_py_sdk/client/entities.py | 34 +++++++++------------------------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 9ed9f2b..dad76bf 100644 --- a/.gitignore +++ b/.gitignore @@ -139,6 +139,7 @@ ENV/ env.bak/ venv.bak/ pythonenv* +myenv # Spyder project settings .spyderproject diff --git a/flow_py_sdk/client/entities.py b/flow_py_sdk/client/entities.py index 7f3fa7b..404c326 100644 --- a/flow_py_sdk/client/entities.py +++ b/flow_py_sdk/client/entities.py @@ -8,8 +8,6 @@ from flow_py_sdk.cadence import cadence_object_hook from flow_py_sdk.proto.flow import entities, access -log = logging.getLogger(__name__) - class Account(object): def __init__( self, @@ -152,25 +150,15 @@ def __init__( self.event_index: int = event_index self.payload: bytes = payload - # Add error handling for deserializing payload - max_payload_size = 1 * 1024 * 1024 # 1 MB size limit for payload - if len(payload) > max_payload_size: - logging.warning(f"Payload too large for event {event_index}: {len(payload)} bytes. Skipping deserialization.") + try: + # Attempt to decode the payload + self.value: cadence.Event = json.loads(payload, object_hook=cadence_object_hook) + except json.JSONDecodeError as e: + logging.error(f"JSON decode error for event {event_index} with payload: {payload[:100]}... Error: {str(e)}") + self.value = None + except Exception as e: + logging.error(f"Unexpected error deserializing payload for event {event_index} with payload: {payload[:100]}... Error: {str(e)}") self.value = None - else: - try: - # Attempt to decode the payload - self.value: cadence.Event = json.loads(payload, object_hook=cadence_object_hook) - except json.JSONDecodeError as e: - # Handle JSON decode errors specifically - print(payload) - # logging.error(f"JSON decode error for event {event_index} with payload: {payload[:100]}... Error: {str(e)}") - self.value = None - except Exception as e: - print(payload) - # Log the full payload and exception details for unexpected errors - # logging.error(f"Unexpected error deserializing payload for event {event_index} with payload: {payload[:100]}... Error: {str(e)}") - self.value = None @classmethod def from_proto(cls, proto: entities.Event) -> "Event": @@ -312,19 +300,15 @@ def from_proto( cls, proto: access.TransactionResultResponse, id: bytes, - max_events: int = 50 ) -> "TransactionResultResponse": events = [] - for i, event_proto in enumerate(proto.events[:max_events]): + for i, event_proto in enumerate(proto.events): try: events.append(Event.from_proto(event_proto)) except Exception as e: print(f"Failed to deserialize event {i}: {e}") continue - if len(proto.events) > max_events: - print(f"Only processed {max_events} out of {len(proto.events)} events due to size limits.") - return TransactionResultResponse( id_=id, status=proto.status, From a6df9dde41878690073d91c7905b5710c88775b9 Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Mon, 28 Oct 2024 15:04:32 +0900 Subject: [PATCH 4/7] Clean up logging --- examples/main.py | 1 - flow_py_sdk/cadence/constants.py | 2 - flow_py_sdk/cadence/decode.py | 76 ++++++++++++++++++-------------- flow_py_sdk/client/entities.py | 6 ++- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/examples/main.py b/examples/main.py index fe0b57c..917393c 100644 --- a/examples/main.py +++ b/examples/main.py @@ -8,7 +8,6 @@ log = logging.getLogger(__name__) - async def run_async(ctx: Config, examples: list[str]) -> Annotated[bool, "Success"]: if not examples: return await example_registry.run_all(ctx) diff --git a/flow_py_sdk/cadence/constants.py b/flow_py_sdk/cadence/constants.py index ccd15aa..821f324 100644 --- a/flow_py_sdk/cadence/constants.py +++ b/flow_py_sdk/cadence/constants.py @@ -65,8 +65,6 @@ inclusiveRangeTypeStr = "InclusiveRange" capabilityTypeStr = "Capability" functionTypeStr = "Function" -entitlementConjunctionSetStr = "EntitlementConjunctionSet" -entitlementKey = "Entitlement" fix64_scale = int(8) fix64_factor = int(100_000_000) diff --git a/flow_py_sdk/cadence/decode.py b/flow_py_sdk/cadence/decode.py index b56c0a2..c621557 100644 --- a/flow_py_sdk/cadence/decode.py +++ b/flow_py_sdk/cadence/decode.py @@ -2,7 +2,7 @@ from flow_py_sdk.cadence.value import Value from flow_py_sdk.cadence.kind import Kind - +import logging import flow_py_sdk.cadence.constants as c _cadence_decoders: dict[str, Callable[[Any], Value]] = {} @@ -17,40 +17,50 @@ def add_cadence_kind_decoder(t: Type[Kind]): _cadence_kind_decoders[t.kind_str()] = t.decode -def decode(obj: [dict[Any, Any]]) -> Union[Value, Kind]: - # json decoder starts from bottom up, so it's possible that this is already decoded - if isinstance(obj, Value) or isinstance(obj, Kind): - return obj - - # if there is an id key, it's already decoded and it is either a field or a parameter - if c.idKey in obj: - return obj - - # if there is no type key we cant decode it directly, but it could be part of a dictionary or composite or path - if c.kindKey not in obj and c.typeKey not in obj: +def decode(obj: dict[Any, Any]) -> Union[Value, Kind, dict]: + try: + # Check if already decoded + if isinstance(obj, Value) or isinstance(obj, Kind): + return obj + + # If obj has an idKey, treat as already decoded field or parameter + if c.idKey in obj: + return obj + + # Check for kindKey or typeKey to determine appropriate decoder + if c.kindKey in obj: + kind = obj[c.kindKey] + if kind in _cadence_kind_decoders: + decoder = _cadence_kind_decoders[kind] + return decoder(obj) + + if c.typeKey in obj: + type_ = obj[c.typeKey] + if type_ in _cadence_decoders: + decoder = _cadence_decoders[type_] + return decoder(obj) + + # Recursively handle nested structures + if isinstance(obj, dict): + for key, value in obj.items(): + if isinstance(value, dict): + obj[key] = decode(value) # Recursive call for nested dict + elif isinstance(value, list): + obj[key] = [decode(item) for item in value] # Recursive list handling + + elif isinstance(obj, list): + obj = [decode(item) for item in obj] # Handle obj itself as a list if list type + + return obj # Return the object if no decoder applies + + except KeyError as e: + logging.error(f"Unhandled key during decode: {e}. Returning raw object.") + return obj + + except NotImplementedError: + logging.error(f"Decoding not implemented for object: {obj}") return obj - if c.kindKey in obj: - kind = obj[c.kindKey] - if kind in _cadence_kind_decoders: - decoder = _cadence_kind_decoders[kind] - return decoder(obj) - - if c.typeKey in obj: - type_ = obj[c.typeKey] - if type_ in _cadence_decoders: - decoder = _cadence_decoders[type_] - return decoder(obj) - - # Recursively handle nested dictionaries and lists - for key, value in obj.items(): - if isinstance(value, dict): - obj[key] = decode(value) - elif isinstance(value, list): - obj[key] = [decode(item) for item in value] - - raise NotImplementedError() - def cadence_object_hook(obj: [dict[Any, Any]]) -> Any: return decode(obj) diff --git a/flow_py_sdk/client/entities.py b/flow_py_sdk/client/entities.py index 404c326..605dc9a 100644 --- a/flow_py_sdk/client/entities.py +++ b/flow_py_sdk/client/entities.py @@ -304,9 +304,11 @@ def from_proto( events = [] for i, event_proto in enumerate(proto.events): try: - events.append(Event.from_proto(event_proto)) + event = Event.from_proto(event_proto) + if event is not None: + events.append(event) except Exception as e: - print(f"Failed to deserialize event {i}: {e}") + logging.error(f"Failed to deserialize event {i}: {e}") continue return TransactionResultResponse( From 7e67d9b5a25bcda8f58818dce9cf31782e9a5b57 Mon Sep 17 00:00:00 2001 From: Lea Lobanov <44328396+lealobanov@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:08:46 +0900 Subject: [PATCH 5/7] Delete myenv directory --- myenv/bin/Activate.ps1 | 247 ---------------------------------------- myenv/bin/activate | 69 ----------- myenv/bin/activate.csh | 26 ----- myenv/bin/activate.fish | 69 ----------- myenv/bin/pip | 8 -- myenv/bin/pip3 | 8 -- myenv/bin/pip3.11 | 8 -- myenv/bin/python | 1 - myenv/bin/python3 | 1 - myenv/bin/python3.11 | 1 - myenv/pyvenv.cfg | 5 - 11 files changed, 443 deletions(-) delete mode 100644 myenv/bin/Activate.ps1 delete mode 100644 myenv/bin/activate delete mode 100644 myenv/bin/activate.csh delete mode 100644 myenv/bin/activate.fish delete mode 100755 myenv/bin/pip delete mode 100755 myenv/bin/pip3 delete mode 100755 myenv/bin/pip3.11 delete mode 120000 myenv/bin/python delete mode 120000 myenv/bin/python3 delete mode 120000 myenv/bin/python3.11 delete mode 100644 myenv/pyvenv.cfg diff --git a/myenv/bin/Activate.ps1 b/myenv/bin/Activate.ps1 deleted file mode 100644 index eeea358..0000000 --- a/myenv/bin/Activate.ps1 +++ /dev/null @@ -1,247 +0,0 @@ -<# -.Synopsis -Activate a Python virtual environment for the current PowerShell session. - -.Description -Pushes the python executable for a virtual environment to the front of the -$Env:PATH environment variable and sets the prompt to signify that you are -in a Python virtual environment. Makes use of the command line switches as -well as the `pyvenv.cfg` file values present in the virtual environment. - -.Parameter VenvDir -Path to the directory that contains the virtual environment to activate. The -default value for this is the parent of the directory that the Activate.ps1 -script is located within. - -.Parameter Prompt -The prompt prefix to display when this virtual environment is activated. By -default, this prompt is the name of the virtual environment folder (VenvDir) -surrounded by parentheses and followed by a single space (ie. '(.venv) '). - -.Example -Activate.ps1 -Activates the Python virtual environment that contains the Activate.ps1 script. - -.Example -Activate.ps1 -Verbose -Activates the Python virtual environment that contains the Activate.ps1 script, -and shows extra information about the activation as it executes. - -.Example -Activate.ps1 -VenvDir C:\Users\MyUser\Common\.venv -Activates the Python virtual environment located in the specified location. - -.Example -Activate.ps1 -Prompt "MyPython" -Activates the Python virtual environment that contains the Activate.ps1 script, -and prefixes the current prompt with the specified string (surrounded in -parentheses) while the virtual environment is active. - -.Notes -On Windows, it may be required to enable this Activate.ps1 script by setting the -execution policy for the user. You can do this by issuing the following PowerShell -command: - -PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser - -For more information on Execution Policies: -https://go.microsoft.com/fwlink/?LinkID=135170 - -#> -Param( - [Parameter(Mandatory = $false)] - [String] - $VenvDir, - [Parameter(Mandatory = $false)] - [String] - $Prompt -) - -<# Function declarations --------------------------------------------------- #> - -<# -.Synopsis -Remove all shell session elements added by the Activate script, including the -addition of the virtual environment's Python executable from the beginning of -the PATH variable. - -.Parameter NonDestructive -If present, do not remove this function from the global namespace for the -session. - -#> -function global:deactivate ([switch]$NonDestructive) { - # Revert to original values - - # The prior prompt: - if (Test-Path -Path Function:_OLD_VIRTUAL_PROMPT) { - Copy-Item -Path Function:_OLD_VIRTUAL_PROMPT -Destination Function:prompt - Remove-Item -Path Function:_OLD_VIRTUAL_PROMPT - } - - # The prior PYTHONHOME: - if (Test-Path -Path Env:_OLD_VIRTUAL_PYTHONHOME) { - Copy-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME -Destination Env:PYTHONHOME - Remove-Item -Path Env:_OLD_VIRTUAL_PYTHONHOME - } - - # The prior PATH: - if (Test-Path -Path Env:_OLD_VIRTUAL_PATH) { - Copy-Item -Path Env:_OLD_VIRTUAL_PATH -Destination Env:PATH - Remove-Item -Path Env:_OLD_VIRTUAL_PATH - } - - # Just remove the VIRTUAL_ENV altogether: - if (Test-Path -Path Env:VIRTUAL_ENV) { - Remove-Item -Path env:VIRTUAL_ENV - } - - # Just remove VIRTUAL_ENV_PROMPT altogether. - if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) { - Remove-Item -Path env:VIRTUAL_ENV_PROMPT - } - - # Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether: - if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) { - Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force - } - - # Leave deactivate function in the global namespace if requested: - if (-not $NonDestructive) { - Remove-Item -Path function:deactivate - } -} - -<# -.Description -Get-PyVenvConfig parses the values from the pyvenv.cfg file located in the -given folder, and returns them in a map. - -For each line in the pyvenv.cfg file, if that line can be parsed into exactly -two strings separated by `=` (with any amount of whitespace surrounding the =) -then it is considered a `key = value` line. The left hand string is the key, -the right hand is the value. - -If the value starts with a `'` or a `"` then the first and last character is -stripped from the value before being captured. - -.Parameter ConfigDir -Path to the directory that contains the `pyvenv.cfg` file. -#> -function Get-PyVenvConfig( - [String] - $ConfigDir -) { - Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg" - - # Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue). - $pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue - - # An empty map will be returned if no config file is found. - $pyvenvConfig = @{ } - - if ($pyvenvConfigPath) { - - Write-Verbose "File exists, parse `key = value` lines" - $pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath - - $pyvenvConfigContent | ForEach-Object { - $keyval = $PSItem -split "\s*=\s*", 2 - if ($keyval[0] -and $keyval[1]) { - $val = $keyval[1] - - # Remove extraneous quotations around a string value. - if ("'""".Contains($val.Substring(0, 1))) { - $val = $val.Substring(1, $val.Length - 2) - } - - $pyvenvConfig[$keyval[0]] = $val - Write-Verbose "Adding Key: '$($keyval[0])'='$val'" - } - } - } - return $pyvenvConfig -} - - -<# Begin Activate script --------------------------------------------------- #> - -# Determine the containing directory of this script -$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition -$VenvExecDir = Get-Item -Path $VenvExecPath - -Write-Verbose "Activation script is located in path: '$VenvExecPath'" -Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)" -Write-Verbose "VenvExecDir Name: '$($VenvExecDir.Name)" - -# Set values required in priority: CmdLine, ConfigFile, Default -# First, get the location of the virtual environment, it might not be -# VenvExecDir if specified on the command line. -if ($VenvDir) { - Write-Verbose "VenvDir given as parameter, using '$VenvDir' to determine values" -} -else { - Write-Verbose "VenvDir not given as a parameter, using parent directory name as VenvDir." - $VenvDir = $VenvExecDir.Parent.FullName.TrimEnd("\\/") - Write-Verbose "VenvDir=$VenvDir" -} - -# Next, read the `pyvenv.cfg` file to determine any required value such -# as `prompt`. -$pyvenvCfg = Get-PyVenvConfig -ConfigDir $VenvDir - -# Next, set the prompt from the command line, or the config file, or -# just use the name of the virtual environment folder. -if ($Prompt) { - Write-Verbose "Prompt specified as argument, using '$Prompt'" -} -else { - Write-Verbose "Prompt not specified as argument to script, checking pyvenv.cfg value" - if ($pyvenvCfg -and $pyvenvCfg['prompt']) { - Write-Verbose " Setting based on value in pyvenv.cfg='$($pyvenvCfg['prompt'])'" - $Prompt = $pyvenvCfg['prompt']; - } - else { - Write-Verbose " Setting prompt based on parent's directory's name. (Is the directory name passed to venv module when creating the virtual environment)" - Write-Verbose " Got leaf-name of $VenvDir='$(Split-Path -Path $venvDir -Leaf)'" - $Prompt = Split-Path -Path $venvDir -Leaf - } -} - -Write-Verbose "Prompt = '$Prompt'" -Write-Verbose "VenvDir='$VenvDir'" - -# Deactivate any currently active virtual environment, but leave the -# deactivate function in place. -deactivate -nondestructive - -# Now set the environment variable VIRTUAL_ENV, used by many tools to determine -# that there is an activated venv. -$env:VIRTUAL_ENV = $VenvDir - -if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) { - - Write-Verbose "Setting prompt to '$Prompt'" - - # Set the prompt to include the env name - # Make sure _OLD_VIRTUAL_PROMPT is global - function global:_OLD_VIRTUAL_PROMPT { "" } - Copy-Item -Path function:prompt -Destination function:_OLD_VIRTUAL_PROMPT - New-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Description "Python virtual environment prompt prefix" -Scope Global -Option ReadOnly -Visibility Public -Value $Prompt - - function global:prompt { - Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) " - _OLD_VIRTUAL_PROMPT - } - $env:VIRTUAL_ENV_PROMPT = $Prompt -} - -# Clear PYTHONHOME -if (Test-Path -Path Env:PYTHONHOME) { - Copy-Item -Path Env:PYTHONHOME -Destination Env:_OLD_VIRTUAL_PYTHONHOME - Remove-Item -Path Env:PYTHONHOME -} - -# Add the venv to the PATH -Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH -$Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH" diff --git a/myenv/bin/activate b/myenv/bin/activate deleted file mode 100644 index 301a280..0000000 --- a/myenv/bin/activate +++ /dev/null @@ -1,69 +0,0 @@ -# This file must be used with "source bin/activate" *from bash* -# you cannot run it directly - -deactivate () { - # reset old environment variables - if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then - PATH="${_OLD_VIRTUAL_PATH:-}" - export PATH - unset _OLD_VIRTUAL_PATH - fi - if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then - PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" - export PYTHONHOME - unset _OLD_VIRTUAL_PYTHONHOME - fi - - # This should detect bash and zsh, which have a hash command that must - # be called to get it to forget past commands. Without forgetting - # past commands the $PATH changes we made may not be respected - if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then - hash -r 2> /dev/null - fi - - if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then - PS1="${_OLD_VIRTUAL_PS1:-}" - export PS1 - unset _OLD_VIRTUAL_PS1 - fi - - unset VIRTUAL_ENV - unset VIRTUAL_ENV_PROMPT - if [ ! "${1:-}" = "nondestructive" ] ; then - # Self destruct! - unset -f deactivate - fi -} - -# unset irrelevant variables -deactivate nondestructive - -VIRTUAL_ENV="/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv" -export VIRTUAL_ENV - -_OLD_VIRTUAL_PATH="$PATH" -PATH="$VIRTUAL_ENV/bin:$PATH" -export PATH - -# unset PYTHONHOME if set -# this will fail if PYTHONHOME is set to the empty string (which is bad anyway) -# could use `if (set -u; : $PYTHONHOME) ;` in bash -if [ -n "${PYTHONHOME:-}" ] ; then - _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" - unset PYTHONHOME -fi - -if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then - _OLD_VIRTUAL_PS1="${PS1:-}" - PS1="(myenv) ${PS1:-}" - export PS1 - VIRTUAL_ENV_PROMPT="(myenv) " - export VIRTUAL_ENV_PROMPT -fi - -# This should detect bash and zsh, which have a hash command that must -# be called to get it to forget past commands. Without forgetting -# past commands the $PATH changes we made may not be respected -if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then - hash -r 2> /dev/null -fi diff --git a/myenv/bin/activate.csh b/myenv/bin/activate.csh deleted file mode 100644 index 6536251..0000000 --- a/myenv/bin/activate.csh +++ /dev/null @@ -1,26 +0,0 @@ -# This file must be used with "source bin/activate.csh" *from csh*. -# You cannot run it directly. -# Created by Davide Di Blasi . -# Ported to Python 3.3 venv by Andrew Svetlov - -alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate' - -# Unset irrelevant variables. -deactivate nondestructive - -setenv VIRTUAL_ENV "/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv" - -set _OLD_VIRTUAL_PATH="$PATH" -setenv PATH "$VIRTUAL_ENV/bin:$PATH" - - -set _OLD_VIRTUAL_PROMPT="$prompt" - -if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then - set prompt = "(myenv) $prompt" - setenv VIRTUAL_ENV_PROMPT "(myenv) " -endif - -alias pydoc python -m pydoc - -rehash diff --git a/myenv/bin/activate.fish b/myenv/bin/activate.fish deleted file mode 100644 index d822583..0000000 --- a/myenv/bin/activate.fish +++ /dev/null @@ -1,69 +0,0 @@ -# This file must be used with "source /bin/activate.fish" *from fish* -# (https://fishshell.com/); you cannot run it directly. - -function deactivate -d "Exit virtual environment and return to normal shell environment" - # reset old environment variables - if test -n "$_OLD_VIRTUAL_PATH" - set -gx PATH $_OLD_VIRTUAL_PATH - set -e _OLD_VIRTUAL_PATH - end - if test -n "$_OLD_VIRTUAL_PYTHONHOME" - set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME - set -e _OLD_VIRTUAL_PYTHONHOME - end - - if test -n "$_OLD_FISH_PROMPT_OVERRIDE" - set -e _OLD_FISH_PROMPT_OVERRIDE - # prevents error when using nested fish instances (Issue #93858) - if functions -q _old_fish_prompt - functions -e fish_prompt - functions -c _old_fish_prompt fish_prompt - functions -e _old_fish_prompt - end - end - - set -e VIRTUAL_ENV - set -e VIRTUAL_ENV_PROMPT - if test "$argv[1]" != "nondestructive" - # Self-destruct! - functions -e deactivate - end -end - -# Unset irrelevant variables. -deactivate nondestructive - -set -gx VIRTUAL_ENV "/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv" - -set -gx _OLD_VIRTUAL_PATH $PATH -set -gx PATH "$VIRTUAL_ENV/bin" $PATH - -# Unset PYTHONHOME if set. -if set -q PYTHONHOME - set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME - set -e PYTHONHOME -end - -if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" - # fish uses a function instead of an env var to generate the prompt. - - # Save the current fish_prompt function as the function _old_fish_prompt. - functions -c fish_prompt _old_fish_prompt - - # With the original prompt function renamed, we can override with our own. - function fish_prompt - # Save the return status of the last command. - set -l old_status $status - - # Output the venv prompt; color taken from the blue of the Python logo. - printf "%s%s%s" (set_color 4B8BBE) "(myenv) " (set_color normal) - - # Restore the return status of the previous command. - echo "exit $old_status" | . - # Output the original/"old" prompt. - _old_fish_prompt - end - - set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" - set -gx VIRTUAL_ENV_PROMPT "(myenv) " -end diff --git a/myenv/bin/pip b/myenv/bin/pip deleted file mode 100755 index fe189ff..0000000 --- a/myenv/bin/pip +++ /dev/null @@ -1,8 +0,0 @@ -#!/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv/bin/python -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/myenv/bin/pip3 b/myenv/bin/pip3 deleted file mode 100755 index fe189ff..0000000 --- a/myenv/bin/pip3 +++ /dev/null @@ -1,8 +0,0 @@ -#!/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv/bin/python -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/myenv/bin/pip3.11 b/myenv/bin/pip3.11 deleted file mode 100755 index fe189ff..0000000 --- a/myenv/bin/pip3.11 +++ /dev/null @@ -1,8 +0,0 @@ -#!/Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv/bin/python -# -*- coding: utf-8 -*- -import re -import sys -from pip._internal.cli.main import main -if __name__ == '__main__': - sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) - sys.exit(main()) diff --git a/myenv/bin/python b/myenv/bin/python deleted file mode 120000 index c6a3ca3..0000000 --- a/myenv/bin/python +++ /dev/null @@ -1 +0,0 @@ -/Users/lealobanov/miniconda3/bin/python \ No newline at end of file diff --git a/myenv/bin/python3 b/myenv/bin/python3 deleted file mode 120000 index d8654aa..0000000 --- a/myenv/bin/python3 +++ /dev/null @@ -1 +0,0 @@ -python \ No newline at end of file diff --git a/myenv/bin/python3.11 b/myenv/bin/python3.11 deleted file mode 120000 index d8654aa..0000000 --- a/myenv/bin/python3.11 +++ /dev/null @@ -1 +0,0 @@ -python \ No newline at end of file diff --git a/myenv/pyvenv.cfg b/myenv/pyvenv.cfg deleted file mode 100644 index fbd7b3f..0000000 --- a/myenv/pyvenv.cfg +++ /dev/null @@ -1,5 +0,0 @@ -home = /Users/lealobanov/miniconda3/bin -include-system-site-packages = false -version = 3.11.5 -executable = /Users/lealobanov/miniconda3/bin/python3.11 -command = /Users/lealobanov/miniconda3/bin/python -m venv /Users/lealobanov/Desktop/Projects/flow-py-sdk/myenv From 51edd23512313edbbd4492f526450422fd305428 Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Mon, 28 Oct 2024 15:16:40 +0900 Subject: [PATCH 6/7] Lint --- examples/main.py | 1 + examples/transactions_examples.py | 9 ++++++--- flow_py_sdk/cadence/decode.py | 10 +++++++--- flow_py_sdk/cadence/kinds.py | 14 +++++--------- flow_py_sdk/client/entities.py | 17 ++++++++++++----- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/examples/main.py b/examples/main.py index 917393c..fe0b57c 100644 --- a/examples/main.py +++ b/examples/main.py @@ -8,6 +8,7 @@ log = logging.getLogger(__name__) + async def run_async(ctx: Config, examples: list[str]) -> Annotated[bool, "Success"]: if not examples: return await example_registry.run_all(ctx) diff --git a/examples/transactions_examples.py b/examples/transactions_examples.py index f091bca..1246848 100644 --- a/examples/transactions_examples.py +++ b/examples/transactions_examples.py @@ -285,16 +285,19 @@ async def run(self, ctx: Config): class DebuggingFetchTransactionByIdExample(Example): def __init__(self) -> None: - super().__init__(tag="T.LL.", name="DebuggingFetchTransactionByIdExample", sort_order=501) + super().__init__( + tag="T.LL.", name="DebuggingFetchTransactionByIdExample", sort_order=501 + ) async def run(self, ctx: Config): # First Step : Create a client to connect to the flow blockchain # flow_client function creates a client using the host and port async with flow_client( # host=ctx.access_node_host, port=ctx.access_node_port - host="access.mainnet.nodes.onflow.org", port=9000 + host="access.mainnet.nodes.onflow.org", + port=9000, ) as client: - + tx_id = "ef6e6dcba87c5f853e0ec940dd382d609ffe11109c28ed239843d71618fa1d71" transaction = await client.get_transaction_result(id=bytes.fromhex(tx_id)) diff --git a/flow_py_sdk/cadence/decode.py b/flow_py_sdk/cadence/decode.py index c621557..fd9480a 100644 --- a/flow_py_sdk/cadence/decode.py +++ b/flow_py_sdk/cadence/decode.py @@ -46,16 +46,20 @@ def decode(obj: dict[Any, Any]) -> Union[Value, Kind, dict]: if isinstance(value, dict): obj[key] = decode(value) # Recursive call for nested dict elif isinstance(value, list): - obj[key] = [decode(item) for item in value] # Recursive list handling + obj[key] = [ + decode(item) for item in value + ] # Recursive list handling elif isinstance(obj, list): - obj = [decode(item) for item in obj] # Handle obj itself as a list if list type + obj = [ + decode(item) for item in obj + ] # Handle obj itself as a list if list type return obj # Return the object if no decoder applies except KeyError as e: logging.error(f"Unhandled key during decode: {e}. Returning raw object.") - return obj + return obj except NotImplementedError: logging.error(f"Decoding not implemented for object: {obj}") diff --git a/flow_py_sdk/cadence/kinds.py b/flow_py_sdk/cadence/kinds.py index b0df5f5..af53bb3 100644 --- a/flow_py_sdk/cadence/kinds.py +++ b/flow_py_sdk/cadence/kinds.py @@ -350,15 +350,11 @@ def __init__(self, entitlements: list[EntitlementBaseKind]) -> None: @classmethod def decode(cls, value) -> "Kind": - try: - entitlements_val = value[c.entitlementsKey] - entitlements = [ - decode(v).as_kind(EntitlementBaseKind) for v in entitlements_val - ] - return cls(entitlements) - except Exception as e: - print(f"Error decoding entitlements for {cls.kind_str()}: {value}. Error: {e}") - return None + entitlements_val = value[c.entitlementsKey] + entitlements = [ + decode(v).as_kind(EntitlementBaseKind) for v in entitlements_val + ] + return cls(entitlements) def encode_kind(self) -> dict: return {c.entitlementsKey: [e.encode() for e in self.entitlements]} diff --git a/flow_py_sdk/client/entities.py b/flow_py_sdk/client/entities.py index 605dc9a..2112e04 100644 --- a/flow_py_sdk/client/entities.py +++ b/flow_py_sdk/client/entities.py @@ -8,6 +8,7 @@ from flow_py_sdk.cadence import cadence_object_hook from flow_py_sdk.proto.flow import entities, access + class Account(object): def __init__( self, @@ -152,12 +153,18 @@ def __init__( try: # Attempt to decode the payload - self.value: cadence.Event = json.loads(payload, object_hook=cadence_object_hook) + self.value: cadence.Event = json.loads( + payload, object_hook=cadence_object_hook + ) except json.JSONDecodeError as e: - logging.error(f"JSON decode error for event {event_index} with payload: {payload[:100]}... Error: {str(e)}") + logging.error( + f"JSON decode error for event {event_index} with payload: {payload[:100]}... Error: {str(e)}" + ) self.value = None except Exception as e: - logging.error(f"Unexpected error deserializing payload for event {event_index} with payload: {payload[:100]}... Error: {str(e)}") + logging.error( + f"Unexpected error deserializing payload for event {event_index} with payload: {payload[:100]}... Error: {str(e)}" + ) self.value = None @classmethod @@ -302,7 +309,7 @@ def from_proto( id: bytes, ) -> "TransactionResultResponse": events = [] - for i, event_proto in enumerate(proto.events): + for i, event_proto in enumerate(proto.events): try: event = Event.from_proto(event_proto) if event is not None: @@ -310,7 +317,7 @@ def from_proto( except Exception as e: logging.error(f"Failed to deserialize event {i}: {e}") continue - + return TransactionResultResponse( id_=id, status=proto.status, From f9f4dc62812243e5a1c69b99c397a86bc24f0306 Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Thu, 31 Oct 2024 23:52:07 +0900 Subject: [PATCH 7/7] Raise exception instead of skipping decoding event when failure --- flow_py_sdk/cadence/decode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flow_py_sdk/cadence/decode.py b/flow_py_sdk/cadence/decode.py index fd9480a..758f229 100644 --- a/flow_py_sdk/cadence/decode.py +++ b/flow_py_sdk/cadence/decode.py @@ -58,12 +58,12 @@ def decode(obj: dict[Any, Any]) -> Union[Value, Kind, dict]: return obj # Return the object if no decoder applies except KeyError as e: - logging.error(f"Unhandled key during decode: {e}. Returning raw object.") - return obj + logging.error(f"Unhandled key during decode: {e}. Object: {obj}") + raise except NotImplementedError: logging.error(f"Decoding not implemented for object: {obj}") - return obj + raise def cadence_object_hook(obj: [dict[Any, Any]]) -> Any: