Skip to content

Commit

Permalink
install scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Dec 24, 2024
1 parent 7b93998 commit 7383571
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 111 deletions.
122 changes: 31 additions & 91 deletions commune/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,21 @@

class Client(c.Module):

def __init__( self, module : str = 'module',
key : Optional[str]= None ,
def __init__( self,
module : str = 'module',
key : Optional[str]= None,
network: Optional[bool] = 'local',
mode: Optional[str] = 'http',
serializer: Optional[c.Module] = 'serializer',
**kwargs
**kwargs
):
self.serializer = c.module(serializer)()
self.key = c.get_key(key, create_if_not_exists=True)
self.set_address(module, network=network, mode=mode)

def set_address(self, module, network='local', mode='http'):
if c.is_address(module):
address = module
else:
namespace = c.namespace(network=network)
if module in namespace:
address = namespace[module]
else:
raise Exception(f'Module {module} not found in namespace {namespace}')
prefix = f'{mode}://'
address = c.namespace(network=network).get(module)
self.network = network
self.mode = mode
self.address = prefix + address if not address.startswith(prefix) else address
self.address = address
self.session = requests.Session()

@classmethod
Expand Down Expand Up @@ -76,20 +67,14 @@ def test(self, module='module::test_client'):
key = c.get_key(module)
assert info['key'] == key.ss58_address
return {'info': info, 'key': str(key)}


def get_url(self, fn, mode='http'):
if '/' in str(fn):
module, fn = module.split('/')
address, fn = address.split('/')
else:
module = self.module
module_address = self.address
ip = c.ip()
if ip in module_address:
module_address = module_address.replace(ip, '0.0.0.0')
url = f"{module_address}/{fn}/"
return url

address = self.address
address = address if address.startswith(mode) else f'{mode}://{address}'
return f"{address}/{fn}/"

def get_data(self, args=[], kwargs={}, params = None):
# derefernece
Expand Down Expand Up @@ -117,27 +102,16 @@ def forward(self,
timeout:int=2,
key : str = None,
mode: str = 'http',
data=None,
headers = None,
stream:bool = False):

key = self.resolve_key(key)
url = self.get_url(fn=fn, mode=mode)
data = data or self.get_data(params=params, args=args, kwargs=kwargs, )
data = self.get_data(params=params, args=args, kwargs=kwargs )
headers = headers or self.get_header(data=data, key=key)
try:
try:
response = self.session.post(url, json=data, headers=headers, timeout=timeout, stream=stream)
if 'text/event-stream' in response.headers.get('Content-Type', ''):
return self.stream(response)
if 'application/json' in response.headers.get('Content-Type', ''):
result = response.json()
elif 'text/plain' in response.headers.get('Content-Type', ''):
result = response.text
else:
result = response.content
if response.status_code != 200:
raise Exception(result)
result = self.serializer.deserialize(result)
result = self.process_response(response)
except Exception as e:
result = c.detailed_error(e)
return result
Expand All @@ -155,17 +129,29 @@ def resolve_key(self,key=None):
if isinstance(key, str):
key = c.get_key(key)
return key

def process_response(self, response):
if 'text/event-stream' in response.headers.get('Content-Type', ''):
return self.stream(response)
if 'application/json' in response.headers.get('Content-Type', ''):
result = response.json()
elif 'text/plain' in response.headers.get('Content-Type', ''):
result = response.text
else:
result = response.content
if response.status_code != 200:
raise Exception(result)
result = self.serializer.deserialize(result)
return result

def stream(self, response):
try:
for chunk in response.iter_lines():
line = self.process_stream_line(chunk)
yield line
yield self.process_stream_line(chunk)
except Exception as e:
print(f'Error in stream: {e}')
yield None
yield c.detailed_error(e)

def process_stream_line(self, line, stream_prefix = 'data: '):
def process_stream_line(self, line , stream_prefix = 'data: '):
event_data = line.decode('utf-8')
if event_data.startswith(stream_prefix):
event_data = event_data[len(stream_prefix):]
Expand All @@ -175,13 +161,6 @@ def process_stream_line(self, line, stream_prefix = 'data: '):
if event_data.startswith('{') and event_data.endswith('}') and 'data' in event_data:
event_data = json.loads(event_data)['data']
return event_data

@staticmethod
def check_response(x) -> bool:
if isinstance(x, dict) and 'error' in x:
return False
else:
return True

class Virtual:
def __init__(self, client: str ='ReactAgentModule'):
Expand All @@ -206,43 +185,4 @@ def get_header(self, data, key):
}
headers['signature'] = key.sign({'data': data, 'time': headers['time']}).hex()

return headers

def forcurl(self,
fn: str = 'info',
args: list = None,
kwargs: dict = None,
timeout: int = 2,
key: str = None,
**extra_kwargs) -> str:
# Resolve the key and URL
key = self.resolve_key(key)
url = self.get_url(fn=fn)

# Prepare the data
data = self.get_data(args=args or [], kwargs=kwargs or {}, **extra_kwargs)
headers = self.get_header(data=data, key=key)
# Prepare headers

# Build curl command
curl_cmd = ['curl', '-X POST']

# Add headers
for header_name, header_value in headers.items():
curl_cmd.append(f"-H '{header_name}: {header_value}'")

# Add data
if isinstance(data, str):
data_str = data
else:
data_str = json.dumps(data)
curl_cmd.append(f"-d '{data_str}'")
curl_cmd.append(f"'{url}'")
curl_cmd.append(f'--max-time {timeout}')
response = os.popen(' '.join(curl_cmd)).read()
return response

def __str__ ( self ):
return "Client(address={})".format(self.address)
def __repr__ ( self ):
return self.__str__()
return headers
6 changes: 2 additions & 4 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@

PWD=$(pwd)
NAME=$(basename $PWD)
docker build -t $NAME $PWD
NAME=$(basename $(pwd))
docker build -t $NAME $(pwd)
4 changes: 2 additions & 2 deletions scripts/enter.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PWD=$(pwd)
NAME=$(basename $PWD)

NAME=$(basename $(pwd))
docker exec -it $NAME /bin/bash
5 changes: 0 additions & 5 deletions scripts/install.sh

This file was deleted.

1 change: 1 addition & 0 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ fi
--restart unless-stopped \
--privileged \
$NAME"
echo "Starting $NAME"
eval docker run $CONTAINER_PARAMS
9 changes: 2 additions & 7 deletions scripts/stop.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@

PWD=$(pwd)
NAME=$(basename $PWD)
CONTAINER_NAME=$NAME
# RESOLVE PORT RANGE

NAME=$(basename $(pwd))
CONTAINER_EXISTS=$(docker ps -q -f name=$NAME)
if [ $CONTAINER_EXISTS ]; then
echo "STOPPING CONTAINER $NAME"
echo "STOPPING --> $NAME"
docker kill $NAME
CONTAINER_ID=$(docker ps -aq -f name=$NAME)
docker rm $NAME
Expand Down
4 changes: 2 additions & 2 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

PWD=$(pwd); NAME=$(basename $PWD)
NAME=$(basename $(pwd));
CONTAINER_EXISTS=$(docker ps -a | grep $NAME)
if [ -z "$CONTAINER_EXISTS" ]; then
make run
./scripts/start.sh
fi
docker exec -it $NAME pytest /$NAME/tests

0 comments on commit 7383571

Please sign in to comment.