Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaked the code to use more helper lib functions #2

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import os
from pathlib import Path
import json
from syftbox.lib import ClientConfig
from syftbox.lib import Client, SyftPermission


class RingRunner:
def __init__(self):
self.client_config = ClientConfig.load(
os.getenv("SYFTBOX_CLIENT_CONFIG_PATH", None)
)
self.client_config = Client.load()
self.my_email = self.client_config["email"]
self.my_home = (
Path(self.client_config["sync_folder"])
/ self.my_email
/ "app_pipelines"
/ "ring"
self.my_home = Path(self.client_config.datasite_path) / "app_pipelines" / "ring"

self.permission = SyftPermission.mine_with_public_write(
self.client_config.email
)
self.syft_perm_json = {
"admin": [self.my_email],
"read": [self.my_email, "GLOBAL"],
"write": [self.my_email, "GLOBAL"],
"filepath": str(self.my_home / "_.syftperm"),
"terminal": False,
}

self.running_folder = self.my_home / "running"
self.done_folder = self.my_home / "done"
self.folders = [self.running_folder, self.done_folder]
Expand All @@ -32,10 +23,12 @@ def setup_folders(self):
print("Setting up the necessary folders.")
for folder in self.folders:
os.makedirs(folder, exist_ok=True)
with open(folder / "dummy", "w") as f:
pass
with open(self.my_home / "_.syftperm", "w") as f:
json.dump(self.syft_perm_json, f)
# no need for dummy files if the folders get created before writing
# with the data_writer as the permission file allows it

self.permission.ensure(
self.my_home
) # less noisy since it only writes if needed

def check_datafile_exists(self):
files = []
Expand All @@ -62,7 +55,7 @@ def data_read_and_increment(self, file_name):
to_send_email = ring_participants[to_send_idx]

# Read the secret value from secret.txt
with open(self.secret_file, 'r') as secret_file:
with open(self.secret_file, "r") as secret_file:
secret_value = int(secret_file.read().strip())

# Increment datum by the secret value instead of 1
Expand All @@ -72,12 +65,19 @@ def data_read_and_increment(self, file_name):
return data, to_send_email

def data_writer(self, file_name, result):
# should make sure the folder exists here so that they don't need dummy files
folder_path = os.path.dirname(file_name)

if not os.path.exists(folder_path):
# less noisy since it only writes if needed
os.makedirs(folder_path, exist_ok=True)

with open(file_name, "w") as f:
json.dump(result, f)

def send_to_new_person(self, to_send_email, datum):
output_path = (
Path(self.client_config['sync_folder'])
Path(self.client_config.sync_folder)
/ to_send_email
/ "app_pipelines"
/ "ring"
Expand Down
5 changes: 4 additions & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/sh
uv venv --allow-existing
uv pip install --upgrade syftbox
# this will use syftbox coming from the app runner by default
# if that fails it will locate syftbox using the global uv tool path
# which ensures you get editable mode and stand-alone running in one line
uv run python -c 'import syftbox' 2>/dev/null || export PYTHONPATH=$(uv tool run syftbox path):$PYTHONPATH
uv run python main.py