From 5be5c0595f869556c80ad16b231ab1d249c7eb90 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:01:25 +0000 Subject: [PATCH] Initial commit --- README.md | 7 ++++ delivery1/README.md | 1 + delivery2/README.md | 1 + delivery3/README.md | 1 + example/client.py | 89 +++++++++++++++++++++++++++++++++++++++++++ example/repository.py | 13 +++++++ 6 files changed, 112 insertions(+) create mode 100644 README.md create mode 100644 delivery1/README.md create mode 100644 delivery2/README.md create mode 100644 delivery3/README.md create mode 100644 example/client.py create mode 100644 example/repository.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..eed5633 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# sio_2425_project + +# Group members +- A +- B +- C + diff --git a/delivery1/README.md b/delivery1/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/delivery1/README.md @@ -0,0 +1 @@ + diff --git a/delivery2/README.md b/delivery2/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/delivery2/README.md @@ -0,0 +1 @@ + diff --git a/delivery3/README.md b/delivery3/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/delivery3/README.md @@ -0,0 +1 @@ + diff --git a/example/client.py b/example/client.py new file mode 100644 index 0000000..221f2f7 --- /dev/null +++ b/example/client.py @@ -0,0 +1,89 @@ +import os +import sys +import argparse +import logging +import json +import requests + +logging.basicConfig(format='%(levelname)s\t- %(message)s') +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +def load_state(): + state = {} + state_dir = os.path.join(os.path.expanduser('~'), '.sio') + state_file = os.path.join(state_dir, 'state.json') + + logger.debug('State folder: ' + state_dir) + logger.debug('State file: ' + state_file) + + if os.path.exists(state_file): + logger.debug('Loading state') + with open(state_file,'r') as f: + state = json.loads(f.read()) + + if state is None: + state = {} + + return state + +def parse_env(state): + if 'REP_ADDRESS' in os.environ: + state['REP_ADDRESS'] = os.getenv('REP_ADDRESS') + logger.debug('Setting REP_ADDRESS from Environment to: ' + state['REP_ADDRESS']) + + if 'REP_PUB_KEY' in os.environ: + rep_pub_key = os.getenv('REP_PUB_KEY') + logger.debug('Loading REP_PUB_KEY fron: ' + state['REP_PUB_KEY']) + if os.path.exists(rep_pub_key): + with open(rep_pub_key, 'r') as f: + state['REP_PUB_KEY'] = f.read() + logger.debug('Loaded REP_PUB_KEY from Environment') + return state + +def parse_args(state): + parser = argparse.ArgumentParser() + + parser.add_argument("-k", '--key', nargs=1, help="Path to the key file") + parser.add_argument("-r", '--repo', nargs=1, help="Address:Port of the repository") + parser.add_argument("-v", '--verbose', help="Increase verbosity", action="store_true") + + args = parser.parse_args() + if args.verbose: + logger.setLevel(logging.DEBUG) + logger.info('Setting log level to DEBUG') + + if args.key: + if not os.path.exists(args.key[0]) or not os.path.isfile(args.key[0]): + logger.error(f'Key file not found or invalid: {args.key[0]}') + sys.exit(-1) + + with open(args.key[0], 'r') as f: + state['REP_PUB_KEY'] = f.read() + logger.info('Overriding REP_PUB_KEY from command line') + + if args.repo: + state['REP_ADDRESS'] = args.repo[0] + logger.info('Overriding REP_ADDRESS from command line') + + +def save(state): + state_dir = os.path.join(os.path.expanduser('~'), '.sio') + state_file = os.path.join(state_dir, 'state.json') + + if not os.path.exists(state_dir): + logger.debug('Creating state folder') + os.mkdir(state_dir) + + with open(state_file, 'w') as f: + f.write(json.dumps(state, indent=4)) + + +state = load_state() +state = parse_env(state) +state = parse_args(state) + +""" Do something """ +req = requests.get(f'http://{state['REP_ADDRESS']}/organization/list') +print(req.json) +save(state) diff --git a/example/repository.py b/example/repository.py new file mode 100644 index 0000000..503adb0 --- /dev/null +++ b/example/repository.py @@ -0,0 +1,13 @@ +from flask import Flask +import json + +app = Flask(__name__) + +organizations = {} + +@app.route("/organization/list") +def org_list(): + return json.dumps(organizations) + +#@app.route("/organization/create") +#...