Skip to content

Commit

Permalink
Added cloud.enable and liberty
Browse files Browse the repository at this point in the history
  • Loading branch information
onuratakan committed Jan 1, 2024
1 parent 9861c2b commit a0d422e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
54 changes: 48 additions & 6 deletions upsonic/remote/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,39 @@ def __init__(self, database_name, api_url, password=None, enable_hashing:bool=Fa
self.client_version = client_version


def enable(self, encryption_key="a"):
the_us = self
#Register the_us to globals
globals()["the_us"] = the_us

def active_module(self, module, encryption_key="a", compress=None):
for key, value in self.get_all(encryption_key=encryption_key).items():
if "_upsonic_" not in key:
try:
original_key = key
original_key_without_dow = key.replace(".", "_")
key = key.split(".")
module = __import__(key[0])
for attr in key[1:-1]:
module = getattr(module, attr)

the_encaps = f"""
def the_{original_key_without_dow}(*args, **kwargs):
return globals()["the_us"].get("{original_key}", encryption_key="{encryption_key}")(*args, **kwargs)
message = the_{original_key_without_dow}
"""
ldict = {}
exec(the_encaps, globals(),ldict)
message = ldict['message']



setattr(module, key[-1], copy.copy(message))
except:
self._log(f"[bold white]Error on patching '{key}'")


def active_module(self, module, encryption_key="a", compress=None, liberty=False):

functions = [obj for name, obj in inspect.getmembers(module)
if inspect.isfunction(obj)]
Expand All @@ -135,7 +166,7 @@ def active_module(self, module, encryption_key="a", compress=None):
time.sleep(0.1)

print("Thread_submitted", len(threads))
the_thread = threading.Thread(target=self.set, args=(name, element), kwargs={"encryption_key": encryption_key, "compress": compress})
the_thread = threading.Thread(target=self.set, args=(name, element), kwargs={"encryption_key": encryption_key, "compress": compress, "liberty": liberty})
the_thread.start()

thread = the_thread
Expand Down Expand Up @@ -361,10 +392,16 @@ def _update_set(self, key, meta):
return self.set(key+"_upsonic_meta", meta, update_operation=True, encryption_key=None)


def _liberty_set(self, key):
return self.set(key+"_upsonic_liberty", True, update_operation=True, encryption_key=None)


def _liberty_unset(self, key):
self.delete(key+"_upsonic_liberty")

def set(self, key, value, encryption_key="a", compress=None, cache_policy=0, locking_operation=False, update_operation=False, version_tag=None, no_version=False):


def set(self, key, value, encryption_key="a", compress=None, cache_policy=0, locking_operation=False, update_operation=False, version_tag=None, no_version=False, liberty=False):
if not locking_operation:
if self.lock_control(key):
self._log(f"[bold red] '{key}' is locked")
Expand All @@ -387,7 +424,12 @@ def set(self, key, value, encryption_key="a", compress=None, cache_policy=0, loc


if encryption_key is not None:
value = self.encrypt(encryption_key, value)
value = self.encrypt(encryption_key, value, liberty=liberty)

if liberty:
self._liberty_set(key)
else:
self._liberty_unset(key)


key = sha256(key.encode()).hexdigest() if self.key_encyption else key
Expand Down Expand Up @@ -499,12 +541,12 @@ def get(self, key, encryption_key="a", no_cache=False, version_tag=None, no_vers
else:
return None

def active(self, value=None, encryption_key="a", compress=None, just_name=False):
def active(self, value=None, encryption_key="a", compress=None, just_name=False, liberty=False):
def decorate(value):
key = value.__name__
if value.__module__ != "__main__" and value.__module__ != None and not just_name:
key = value.__module__ + "." + key
self.set(key, value, encryption_key=encryption_key, compress=compress)
self.set(key, value, encryption_key=encryption_key, compress=compress, liberty=liberty)

if value == None:
return decorate
Expand Down
23 changes: 23 additions & 0 deletions upsonic/remote/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,26 @@ def wrapper(*args, **kwargs):
return wrapper

return decorator


"""
def durable(func):
@wraps(func)
def runner(*args, **kwargs):
import random
result = None
run_id = random.randint(10000, 99999)
while result is None:
try:
result = func(*args, **kwargs)
except Exception as e:
import time
print(f"Exception occurred in function and frozed the statement, waiting for update: {e}")
cloud.set(func.__name__+"_upsonic_durable"+str(run_id), str(e))
time.sleep(5)
return result
return runner
"""
4 changes: 2 additions & 2 deletions upsonic/remote/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import textwrap

def encrypt(key, message):
if inspect.ismethod(message) or inspect.isfunction(message):
def encrypt(key, message, liberty=False):
if (inspect.ismethod(message) or inspect.isfunction(message)) and liberty == True:
new = inspect.getsource(message)
# Add space to every line of element
new = textwrap.dedent(new)
Expand Down

0 comments on commit a0d422e

Please sign in to comment.