This example demonstrates how you can create a custom datum. This example consists of one python script /customDatum.py and manifest file /manifest.json. This page shows an example for version 5.1.
from DAP.Core import logIt
from CellFrame.Common import CustomDatum
from CellFrame.Network import Net
from CellFrame import AppCliServer
def datumCMD(argv, indexStrReply):
if (len(argv) == 3 or len(argv) == 5):
if (argv[1] == 'create'):
net = Net.byName(argv[2])
chain = net.getChainByName(argv[3])
res = CustomDatum.create(chain, argv[4].encode('utf-8'))
AppCliServer.setReplyText("Return hash datum: "+str(res), indexStrReply)
elif(argv[1] == 'load'):
data = CustomDatum.read(argv[2])
AppCliServer.setReplyText("Return data: "+str(data), indexStrReply)
else:
logIt.notice("This command can take three or four arguments.")
def init():
logIt.notice("Start plugin customDatum")
AppCliServer.cmdItemCreate("pyDatum", datumCMD, "Command for creation datum", """
Command for created and get info from custom datum:
create <net name> <chain name> <date in datum>
load <path for file>
""")
return 0
{
"name": "customDatum",
"version": "1.0",
"author": "DEMLABS (C) 2021",
"dependencies": [],
"description": "This plugin does the work of the backend for the blockchain explorer."
}
This /customDatum.py script contains two function: init
and datumCMD
.
The execution of the plugin starts with the init
. The datumCMD
function is passed to the AppCliServer.cmdItemCreate
method as an argument. The datumCMD
function will be called when running pyDatum command.
1. The plugin is initialized by the init
function:
def init():
2. Logging the message:
logIt.notice("Start plugin customDatum")
3. Adding a command using the cmdItemCreate
of the AppCliServer
class:
AppCliServer.cmdItemCreate("pyDatum", datumCMD, "Command for creation datum", """
Command for created and get info from custom datum:
create <net name> <chain name> <date in datum>
load <path for file>
""")
The "pyDatum"
is a command name. This function takes four arguments: a command name, a command handler function, short description of the command, full description of the command.
4. Returning 0. This means that the plugin has been successfully initialized:
return 0
1. The pwoCMD
functions gets two arguments: argv
and indexStrReptly
. The argv
is a list that contains the command name and the arguments that the user specified when running the pyDatum command. The indexStrReply
argument is intended to respond to the user.
def datumCMD(argv, indexStrReply):
2. Checking whether the user has specified five arguments
if (len(argv) == 3 or len(argv) == 5):
3. Handling the create command**:
if (argv[1] == 'create'):
net = Net.byName(argv[2])
chain = net.getChainByName(argv[3])
res = CustomDatum.create(chain, argv[4].encode('utf-8'))
AppCliServer.setReplyText("Return hash datum: "+str(res), indexStrReply)
4. Handling the load command**:
elif(argv[1] == 'load'):
data = CustomDatum.read(argv[2])
AppCliServer.setReplyText("Return data: "+str(data), ind