I have a daemon which I am able to interact with through a cli-client. The daemon will perform some tasks in future e.g. monitoring the rate at which files are added to a directory.
Right now when I add a new function I need to write
- The function I would like to trigger
- A message format which the client can send to the daemon to execute the function
- A function in the client which takes the users command line argument and turns them in to a string which is sent to the daemon
Is there a better way of designing this interaction?
Below is the daemon
##################### Daemon ##################################
##################### Just for context ########################
from multiprocessing.connection import Listener
class Nark:
def __init__(self, folder_path):
self.path = folder_path
def change_path(self, folder_path):
self.path = folder_path
def get_path(self):
return self.path
class NarkPool:
def __init__(self):
self.narkdict = {}
def add_nark(self, name, path):
self.narkdict[name] = Nark(path)
def remove_nark(self, name):
self.narkdict.pop(name)
def change_path(self, name, path):
self.narkdict[name].change_path(path)
def get_path(self, name):
return self.narkdict[name].get_path()
def get_names(self):
return list(self.narkdict.keys())
################### Important Part Below #############################
address = ('localhost', 7389)
listener = Listener(address)
narks = NarkPool()
while True:
conn = listener.accept()
print ('connection accepted from', listener.last_accepted)
while True:
msg = conn.recv()
command = msg.split(' ') # command input would be eg. add <name> <path>
# Manually define action to be taken based on each command
if command[0] == 'add' : narks.add_nark(command[1], command[2])
if command[0] == 'remove' : narks.remove(command[1])
if command[0] == 'change_path': narks.change_path(command[1], command[2])
if command[0] == 'get_path' :
conn.send(narks.get_path(command[1]))
if command[0] == 'list':
conn.send(narks.get_names())
if msg :
conn.close()
break
listener.close()
And here is the client:
################# Client ##########################
import click
from multiprocessing.connection import Client
adress = ('localhost', 7389)
@click.group()
def cli():
pass
# for each possible daemon command create a function which
# takes respective user input and sends it to daemon
@cli.command()
@click.argument('name')
@click.argument('path')
def add_nark(name, path):
conn = Client(adress)
conn.send(f'add {name} {path}')
conn.close()
@cli.command()
@click.argument('name')
def remove_nark(name):
conn = Client(adress)
conn.send(f'remove {name}')
conn.close()
@cli.command()
@click.argument('name')
@click.argument('path')
def change_path(name, path):
conn = Client(adress)
conn.send(f'change_path {name} {path}')
conn.close()
@cli.command()
@click.argument('name')
def get_path(name):
conn = Client(adress)
conn.send(f'get_path {name}')
msg = False
while not msg:
msg = conn.recv()
print(msg)
conn.close()
@cli.command()
def list_runs():
conn = Client(adress)
conn.send(f'list')
msg = False
while not msg:
msg = conn.recv()
print(msg)
conn.close()
if __name__ == '__main__':
cli()