Skip to content

Commit

Permalink
refacotr
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Apr 17, 2024
1 parent 7f5f17c commit a346ead
Show file tree
Hide file tree
Showing 26 changed files with 1,168 additions and 739 deletions.
9 changes: 0 additions & 9 deletions bounty/bounty.py

This file was deleted.

2 changes: 0 additions & 2 deletions bounty/bounty.yaml

This file was deleted.

File renamed without changes.
53 changes: 53 additions & 0 deletions commune/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import commune as c
import os

class History(c.Module):
def __init__(self, folder_path='history'):
self.folder_path = self.resolve_path(folder_path)



def set_folder_path(self, path):
self.folder_path = self.resolve_path(path)
assert os.path.isdir(self.folder_path), f"History path {self.folder_path} does not exist"
c.print(f"History path: {self.folder_path}", color='green')

def add(self, item:dict, path=None):
if 'timestamp' not in item:
item['timestamp'] = c.timestamp()
path = path or (self.folder_path + '/' + str(item['timestamp']))
return self.put(path, item)

def paths(self, key=None, max_age=None):
files = []
current_timestamp = c.timestamp()
for file in c.ls(self.folder_path):
timestamp = self.get_file_timestamp(file)
if max_age and current_timestamp - timestamp > max_age:
continue
files.append(file)
return files

def get_file_timestamp(self, file):
return int(file.split('/')[-1].split('.')[0])

def history_paths(self, search=None, n=1000, reverse=False):
paths = self.ls(self.folder_path)
sorted_paths = sorted(paths, reverse=reverse)
if search:
sorted_paths = [p for p in sorted_paths if search in p]
return sorted_paths[:n]

def history(self, search=None, n=100, reverse=True, idx=None):
history_paths = self.history_paths(n=n, reverse=reverse, search=search)
history = [c.get(s) for s in history_paths]
if idx:
return history[idx]
return history

def last_n(self, n=1):
return self.history(n=n)




22 changes: 9 additions & 13 deletions commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ def add_key(cls, path:str, mnemonic:str = None, password:str=None, refresh:bool=
if password != None:
key_json = cls.encrypt(data=key_json, password=password)
cls.put(path, key_json)
cls.update_keys()
cls.update()
return json.loads(key_json)


@classmethod
def update_keys(cls, **kwargs):
def update(cls, **kwargs):
return cls.key2address(update=True,**kwargs)

@classmethod
Expand Down Expand Up @@ -363,23 +363,15 @@ def get_keys(cls, search=None, clean_failed_keys=False):


@classmethod
def key2address(cls, search=None, max_age=1000, cache=True, **kwargs):
def key2address(cls, search=None, max_age=1000, update=False, **kwargs):
path = 'key2address'
key2address = []
cache_path = f'cache_{path}'
if hasattr(cls, cache_path):
return getattr(cls, cache_path)

key2address = cls.get(path, key2address,max_age=max_age)

key2address = cls.get(path, key2address,max_age=max_age, update=update)
if len(key2address) == 0:
key2address = { k: v.ss58_address for k,v in cls.get_keys(search).items()}
cls.put(path, key2address)
if search != None:
key2address = {k:v for k,v in key2address.items() if search in k}

if cache:
setattr(cls, cache_path, key2address)

return key2address

Expand Down Expand Up @@ -424,6 +416,10 @@ def keys(cls, search : str = None, **kwargs):
if search != None:
keys = [key for key in keys if search in key]
return keys

@classmethod
def n(cls, *args, **kwargs):
return len(cls.key2address(*args, **kwargs))

@classmethod
def key_exists(cls, key, **kwargs):
Expand All @@ -448,7 +444,7 @@ def rm_key(cls, key=None):
if key not in keys:
raise Exception(f'key {key} not found, available keys: {keys}')
c.rm(key2path[key])
cls.update_keys()
cls.update()
assert c.exists(key2path[key]) == False, 'key not deleted'

return {'deleted':[key]}
Expand Down
Loading

0 comments on commit a346ead

Please sign in to comment.