MultiServer: Data Storage Additions #1411

adds 3 new operations to datastorage that allows adding and removing of elements from list and dicts.
This commit is contained in:
KonoTyran 2023-01-24 21:14:46 -08:00 committed by GitHub
parent 28576f2b0d
commit 5393563700
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import argparse
import asyncio
import copy
import functools
import logging
import zlib
@ -43,6 +44,28 @@ min_client_version = Version(0, 1, 6)
print_command_compatability_threshold = Version(0, 3, 5) # Remove backwards compatibility around 0.3.7
colorama.init()
def remove_from_list(container, value):
try:
container.remove(value)
except ValueError:
pass
return container
def pop_from_container(container, value):
try:
container.pop(value)
except ValueError:
pass
return container
def update_dict(dictionary, entries):
dictionary.update(entries)
return dictionary
# functions callable on storable data on the server by clients
modify_functions = {
"add": operator.add, # add together two objects, using python's "+" operator (works on strings and lists as append)
@ -59,6 +82,10 @@ modify_functions = {
"and": operator.and_,
"left_shift": operator.lshift,
"right_shift": operator.rshift,
# lists/dicts
"remove": remove_from_list,
"pop": pop_from_container,
"update": update_dict,
}
@ -1739,7 +1766,7 @@ async def process_client_cmd(ctx: Context, client: Client, args: dict):
return
args["cmd"] = "SetReply"
value = ctx.stored_data.get(args["key"], args.get("default", 0))
args["original_value"] = value
args["original_value"] = copy.copy(value)
for operation in args["operations"]:
func = modify_functions[operation["operation"]]
value = func(value, operation["value"])

View File

@ -411,6 +411,9 @@ The following operations can be applied to a datastorage key
| xor | Applies a bitwise Exclusive OR to the current value of the key with `value`. |
| left_shift | Applies a bitwise left-shift to the current value of the key by `value`. |
| right_shift | Applies a bitwise right-shift to the current value of the key by `value`. |
| remove | List only: removes the first instance of `value` found in the list. |
| pop | List or Dict: for lists it will remove the index of the `value` given. for dicts it removes the element with the specified key of `value`. |
| update | Dict only: Updates the dictionary with the specified elements given in `value` creating new keys, or updating old ones if they previously existed. |
### SetNotify
Used to register your current session for receiving all [SetReply](#SetReply) packages of certain keys to allow your client to keep track of changes.