-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example for very generic dvm that can be controlled from a single…
… file
- Loading branch information
1 parent
608c5fa
commit 0c78e3e
Showing
5 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import json | ||
import os | ||
from datetime import timedelta | ||
from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKey, NostrSigner, Kind, RelayOptions | ||
|
||
from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv | ||
from nostr_dvm.utils.admin_utils import AdminConfig | ||
from nostr_dvm.utils.definitions import EventDefinitions | ||
from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config | ||
from nostr_dvm.utils.nip88_utils import NIP88Config | ||
from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag | ||
from nostr_dvm.utils.output_utils import post_process_list_to_events | ||
|
||
""" | ||
This File contains a Generic DVM that can be overwritten by the user | ||
Accepted Inputs: None | ||
Outputs: Text | ||
Params: None | ||
""" | ||
|
||
|
||
class GenericDVM(DVMTaskInterface): | ||
KIND: Kind = Kind(5000) | ||
TASK: str = "generic" | ||
FIX_COST: float = 0 | ||
dvm_config: DVMConfig | ||
options = {} | ||
|
||
async def init_dvm(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None, | ||
admin_config: AdminConfig = None, options=None): | ||
dvm_config.SCRIPT = os.path.abspath(__file__) | ||
if dvm_config.KIND is not None: | ||
self.KIND = dvm_config.KIND | ||
|
||
async def is_input_supported(self, tags, client=None, dvm_config=None): | ||
return True | ||
|
||
async def create_request_from_nostr_event(self, event, client=None, dvm_config=None): | ||
self.dvm_config = dvm_config | ||
print(self.dvm_config.PRIVATE_KEY) | ||
|
||
request_form = {"jobID": event.id().to_hex()} | ||
request_form['options'] = json.dumps(self.options) | ||
return request_form | ||
|
||
async def process(self, request_form): | ||
options = self.set_options(request_form) | ||
result = "I'm manipulating the DVM from my inside function\n" | ||
result += options["some_option"] | ||
print(result) | ||
return result | ||
|
||
|
||
# We build an example here that we can call by either calling this file directly from the main directory, | ||
# or by adding it to our playground. You can call the example and adjust it to your needs or redefine it in the | ||
# playground or elsewhere | ||
def build_example(name, identifier, admin_config, announce = False): | ||
|
||
admin_config = AdminConfig() | ||
admin_config.REBROADCAST_NIP89 = announce | ||
admin_config.REBROADCAST_NIP65_RELAY_LIST = announce | ||
admin_config.UPDATE_PROFILE = announce | ||
|
||
name = "Generic DVM" | ||
identifier = "a_very_generic_dvm" # Chose a unique identifier in order to get a lnaddress | ||
dvm_config = build_default_config(identifier) | ||
dvm_config.KIND = Kind(5050) # Manually set the Kind Number (see data-vending-machines.org) | ||
|
||
# Add NIP89 | ||
nip89info = { | ||
"name": name, | ||
"image": "https://image.nostr.build/28da676a19841dcfa7dcf7124be6816842d14b84f6046462d2a3f1268fe58d03.png", | ||
"about": "I'm an all purpose DVM'", | ||
"encryptionSupported": True, | ||
"cashuAccepted": True, | ||
"nip90Params": { | ||
} | ||
} | ||
|
||
nip89config = NIP89Config() | ||
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"]) | ||
nip89config.CONTENT = json.dumps(nip89info) | ||
|
||
options = { | ||
"some_option": "#RunDVM", | ||
} | ||
|
||
dvm = GenericDVM(name=name, dvm_config=dvm_config, nip89config=nip89config, | ||
admin_config=admin_config, options=options) | ||
|
||
async def process(request_form): | ||
options = dvm.set_options(request_form) | ||
result = "I'm manipulating the DVM from outside\n" | ||
result += options["some_option"] | ||
print(result) | ||
return result | ||
|
||
dvm.process = process # overwrite the process function with the above one | ||
return dvm | ||
|
||
|
||
if __name__ == '__main__': | ||
process_venv(GenericDVM) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
import dotenv | ||
from nostr_sdk import Kind | ||
|
||
from nostr_dvm.tasks.generic_dvm import GenericDVM | ||
from nostr_dvm.utils.admin_utils import AdminConfig | ||
from nostr_dvm.utils.dvmconfig import build_default_config | ||
from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag | ||
|
||
|
||
def playground(announce=False): | ||
admin_config = AdminConfig() | ||
admin_config.REBROADCAST_NIP89 = announce | ||
admin_config.REBROADCAST_NIP65_RELAY_LIST = announce | ||
admin_config.UPDATE_PROFILE = announce | ||
|
||
name = "Generic DVM" | ||
identifier = "a_very_generic_dvm" # Chose a unique identifier in order to get a lnaddress | ||
dvm_config = build_default_config(identifier) | ||
dvm_config.KIND = Kind(5050) # Manually set the Kind Number (see data-vending-machines.org) | ||
|
||
# Add NIP89 | ||
nip89info = { | ||
"name": name, | ||
"image": "https://image.nostr.build/28da676a19841dcfa7dcf7124be6816842d14b84f6046462d2a3f1268fe58d03.png", | ||
"about": "I'm an all purpose DVM'", | ||
"encryptionSupported": True, | ||
"cashuAccepted": True, | ||
"nip90Params": { | ||
} | ||
} | ||
|
||
nip89config = NIP89Config() | ||
nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"]) | ||
nip89config.CONTENT = json.dumps(nip89info) | ||
|
||
options = { | ||
"some_option": "#RunDVM", | ||
} | ||
|
||
dvm = GenericDVM(name=name, dvm_config=dvm_config, nip89config=nip89config, | ||
admin_config=admin_config, options=options) | ||
|
||
async def process(request_form): | ||
options = dvm.set_options(request_form) | ||
result = "I'm manipulating the DVM from outside\n" | ||
result += options["some_option"] | ||
print(result) | ||
return result | ||
|
||
dvm.process = process # overwrite the process function with the above one | ||
dvm.run(True) | ||
|
||
|
||
if __name__ == '__main__': | ||
env_path = Path('.env') | ||
if not env_path.is_file(): | ||
with open('.env', 'w') as f: | ||
print("Writing new .env file") | ||
f.write('') | ||
if env_path.is_file(): | ||
print(f'loading environment from {env_path.resolve()}') | ||
dotenv.load_dotenv(env_path, verbose=True, override=True) | ||
else: | ||
raise FileNotFoundError(f'.env file not found at {env_path} ') | ||
|
||
playground(announce=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters