Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Dec 24, 2024
1 parent 7383571 commit 21aa017
Show file tree
Hide file tree
Showing 20 changed files with 203 additions and 262 deletions.
18 changes: 9 additions & 9 deletions commune/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import commune as c

class Client(c.Module):
class Client:

def __init__( self,
module : str = 'module',
Expand Down Expand Up @@ -73,6 +73,7 @@ def get_url(self, fn, mode='http'):
address, fn = address.split('/')
else:
address = self.address
print('address', address, self.address)
address = address if address.startswith(mode) else f'{mode}://{address}'
return f"{address}/{fn}/"

Expand Down Expand Up @@ -176,13 +177,12 @@ def __getattr__(self, key):
else:
return lambda *args, **kwargs : self.remote_call(*args, remote_fn=key, **kwargs)

def get_header(self, data, key):
headers = {
'Content-Type': 'application/json',
def get_header(self, data, key: 'Key'):
time_str = str(c.time())
return {
'key': key.ss58_address,
'crypto_type': str(key.crypto_type),
'time': str(c.time()),
}
headers['signature'] = key.sign({'data': data, 'time': headers['time']}).hex()

return headers
'time': time_str,
'Content-Type': 'application/json',
'signature': key.sign({'data': data, 'time': time_str}).hex()
}
4 changes: 4 additions & 0 deletions commune/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import binascii
import re
import secrets
import base64
from base64 import b64encode
import hashlib
from Crypto import Random
Expand Down Expand Up @@ -1419,6 +1420,9 @@ def storage_migration(self):

return new_key2path

# if __name__ == "__main__":
# Key.run()




Expand Down
52 changes: 28 additions & 24 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def convert_module(cls, module):
module.module_name = module.name = lambda *args, **kwargs : c.module_name(module)
module.key = c.get_key(module.module_name(), create_if_not_exists=True)
module.resolve_module = lambda *args, **kwargs : c.resolve_module(module)
module.resolve_path = lambda p, **kwargs : c.resolve_path(c.storage_path + '/' + module.module_name() + p, **kwargs)
module.storage_dir = lambda *args, **kwargs : c.storage_dir(module)
module.filepath = lambda *args, **kwargs : c.filepath(module)
module.dirpath = lambda *args, **kwargs : c.dirpath(module)
module.code = lambda *args, **kwargs : c.code(module)
Expand Down Expand Up @@ -165,8 +165,9 @@ def syspath(self):
return sys.path

@classmethod
def storage_dir(cls):
return f'{c.storage_path}/{cls.module_name()}'
def storage_dir(cls, module=None):
module = cls.resolve_module(module)
return f'{c.storage_path}/{module.module_name()}'

@classmethod
def is_module(cls, obj) -> bool:
Expand Down Expand Up @@ -343,7 +344,6 @@ def encrypt(cls,data: Union[str, bytes], password: str = None, key: str = None,
def decrypt(cls, data: Any, password : str = None, key: str = None, **kwargs) -> bytes:
return c.get_key(key).decrypt(data, password=password)


@classmethod
def sign(cls, data:dict = None, key: str = None, **kwargs) -> bool:
return c.get_key(key).sign(data, **kwargs)
Expand Down Expand Up @@ -493,7 +493,7 @@ def fn2route(cls):
tree = c.tree()
for module, fns in routes.items():
is_module = bool( module in tree)
splitter = '/' if is_module else '.'
splitter = '/' if is_module else '/'
for fn in fns:
fn2route[fn] = module + splitter + fn
return fn2route
Expand Down Expand Up @@ -572,7 +572,7 @@ def set_config(self, config:Optional[Union[str, dict]]=None ) -> 'Munch':
return self.config

@classmethod
def config(cls, module=None, to_munch=True) -> 'Munch':
def config(cls, module=None, to_munch=True, fn='__init__') -> 'Munch':
'''
Returns the config
'''
Expand Down Expand Up @@ -677,21 +677,24 @@ def mv(cls, path1, path2):
return path2

@classmethod
def resolve_path(cls, path:str = None, extension:Optional[str]=None):
def resolve_path(cls,
path:str = None,
extension:Optional[str]=None,
storage_dir=None) -> str:
'''
Abspath except for when the path does not have a
leading / or ~ or . in which case it is appended to the storage dir
'''
if path == None:
return cls.storage_dir()
storage_dir = storage_dir or cls.storage_dir()
if path == None :
return storage_dir
if path.startswith('/'):
path = path
elif path.startswith('~') :
elif path.startswith('~/') :
path = os.path.expanduser(path)
elif path.startswith('.'):
elif path.startswith('./'):
path = os.path.abspath(path)
else:
storage_dir = cls.storage_dir()
if storage_dir not in path:
path = os.path.join(storage_dir, path)
if extension != None and not path.endswith(extension):
Expand Down Expand Up @@ -889,10 +892,6 @@ def is_encrypted(self, path:str) -> bool:
except:
return False

@classmethod
def storage_dir(cls):
return f'{c.storage_path}/{cls.module_name()}'

@staticmethod
def sleep(period):
time.sleep(period)
Expand Down Expand Up @@ -1456,13 +1455,18 @@ def import_module(cls, import_path:str ) -> 'Object':
return import_module(import_path)

@classmethod
def get_object(cls, key:str, **kwargs)-> Any:
def get_object(cls, key:str, splitters=['/', '::', ':', '.'], **kwargs)-> Any:
''' Import an object from a string with the format of {module_path}.{object}'''
key = key.replace('/', '.')
if '/' in key:
key = key.replace('/', '.')
module_obj = c.import_module('.'.join(key.split('.')[:-1]))
return getattr(module_obj, key.split('.')[-1])
module_path = None
object_name = None
for splitter in splitters:
if splitter in key:
module_path = '.'.join(key.split(splitter)[:-1])
object_name = key.split(splitter)[-1]
break
assert module_path != None and object_name != None, f'Invalid key {key}'
module_obj = c.import_module(module_path)
return getattr(module_obj, object_name)

@classmethod
def obj(cls, key:str, **kwargs)-> Any:
Expand Down Expand Up @@ -1901,8 +1905,8 @@ def epoch(self, *args, **kwargs):
],
"network": [
"networks",
"register_server",
"deregister_server",
"add_server",
"remove_server",
"server_exists",
"add_server",
"has_server",
Expand Down
82 changes: 81 additions & 1 deletion commune/modules/git/git.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import commune as c
import subprocess
import requests
import base64
import re


class git(c.Module):

def __init__(self, repo_url='commune-ai/commune'):
self.repo_url = repo_url
self.api_base = "https://api.github.com"
self.repo_path = self._get_repo_path()
def is_repo(self, libpath:str ):
# has the .git folder
return c.cmd(f'ls -a {libpath}').count('.git') > 0
Expand Down Expand Up @@ -133,4 +140,77 @@ def get_repos(self, username_or_org="openai"):
# Get the HTML of the repositories page
url = f"https://github.com/{username_or_org}?tab=repositories"
# response = requests.get(url)
return c.module('web')().page_content(url)["links"]
return c.module('web')().page_content(url)["links"]


def _get_repo_path(self):
"""Extract repository path from URL"""
return "/".join(self.repo_url.split("github.com/")[1].split("/"))

def get_file_content(self, path):
"""Get content of a specific file"""
url = f"{self.api_base}/repos/{self.repo_path}/contents/{path}"
response = requests.get(url)
if response.status_code == 200:
content = response.json()
if content.get("encoding") == "base64":
return base64.b64decode(content["content"]).decode()
return None

def get_directory_contents(self, path=""):
"""Get contents of a directory"""
url = f"{self.api_base}/repos/{self.repo_path}/contents/{path}"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to get contents of {path}")
print(response.text)
return []

def process_code(self, code_content):
"""Process the code content - example processing"""
if not code_content:
return None

# Example processing:
# 1. Remove comments
code_content = re.sub(r'#.*', '', code_content)
code_content = re.sub(r'"""[\s\S]*?"""', '', code_content)

# 2. Remove empty lines
code_content = "\n".join([line for line in code_content.split("\n") if line.strip()])

return code_content

def process_repository(self, path=""):
"""Process entire repository recursively"""
processed_files = {}
contents = self.get_directory_contents(path)

for item in contents:
if isinstance(item, dict):
if item["type"] == "file" and item["name"].endswith(".py"):
content = self.get_file_content(item["path"])
processed_content = self.process_code(content)
processed_files[item["path"]] = processed_content
elif item["type"] == "dir":
sub_processed = self.process_repository(item["path"])
processed_files.update(sub_processed)

return processed_files

@classmethod
def test(cls):
# Initialize processor
processor = cls("https://github.com/commune-ai/eliza")

# Process repository
processed_files = processor.process_repository()

# Print results
file2content = {file_path: processed_content if processed_content else "No content" for file_path, processed_content in processed_files.items()}
for file_path, processed_content in processed_files.items():
print(f"\nFile: {file_path}")
print("Processed content:")
print(processed_content[:200] + "..." if processed_content else "No content")
10 changes: 6 additions & 4 deletions commune/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def set_network(self, network:str, tempo:int=60, path=None, **kwargs):
return {'network': self.network, 'tempo': self.tempo, 'modules_path': self.modules_path}

def params(self,*args, **kwargs):
return { 'network': self.network, 'tempo' : self.tempo,'n': self.n}
return { 'network': self.network,
'tempo' : self.tempo,
'n': self.n}

def net(self):
return c.network()
Expand Down Expand Up @@ -52,7 +54,7 @@ def modules(self,
def namespace(self, search=None, max_age:int = tempo, update:bool = False, **kwargs) -> dict:
return {m['name']: '0.0.0.0' + ':' + m['address'].split(':')[-1] for m in self.modules(search=search, max_age=max_age, update=update)}

def register_server(self, name:str, address:str, key:str) -> None:
def add_server(self, name:str, address:str, key:str) -> None:
data = {'name': name, 'address': address, 'key': key}
modules = self.modules()
modules.append(data)
Expand All @@ -63,9 +65,9 @@ def register_from_signature(self, signature=None):
import json
assert c.verify(signature), 'Signature is not valid.'
data = json.loads(signature['data'])
return self.register_server(data['name'], data['address'])
return self.add_server(data['name'], data['address'])

def deregister_server(self, name:str, features=['name', 'key', 'address']) -> Dict:
def remove_server(self, name:str, features=['name', 'key', 'address']) -> Dict:
modules = self.modules()
modules = [m for m in modules if not any([m[f] == name for f in features])]
c.put(self.modules_path, modules)
Expand Down
Loading

0 comments on commit 21aa017

Please sign in to comment.