Skip to content

Commit

Permalink
chore: rearrange functions + split up main function (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Oct 4, 2023
1 parent d08fb57 commit 02fbf22
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 177 deletions.
57 changes: 44 additions & 13 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,45 @@


def modify_oaps(oaps: list[Oap]) -> list[Oap]:
"""Adapt this sample to your needs."""
for oap in oaps:
oap.scope.CR.append(BuiltinGroup.SYSTEM_ADMIN)
return oaps


def modify_doaps(doaps: list[Doap]) -> list[Doap]:
"""Adapt this sample to your needs."""
for doap in doaps:
if doap.target.group in [BuiltinGroup.PROJECT_MEMBER.value, BuiltinGroup.PROJECT_ADMIN.value]:
doap.scope = PUBLIC
return doaps


def main() -> None:
"""
The main method assembles a sample call of all available high-level functions.
"""
load_dotenv() # set login credentials from .env file as environment variables
host = Hosts.get_host("test")
shortcode = "F18E"
token = login(host)
def update_oaps(
host: str,
shortcode: str,
token: str,
) -> None:
"""Sample function to modify the Object Access Permissions of a project."""
resource_oaps = get_all_resource_oaps_of_project(
shortcode=shortcode,
host=host,
token=token,
)
resource_oaps_updated = modify_oaps(oaps=resource_oaps)
apply_updated_oaps_on_server(
resource_oaps=resource_oaps_updated,
host=host,
token=token,
)


def update_doaps(
host: str,
shortcode: str,
token: str,
) -> None:
"""Sample function to modify the Default Object Access Permissions of a project."""
project_doaps = get_doaps_of_project(
host=host,
shortcode=shortcode,
Expand All @@ -52,15 +70,28 @@ def main() -> None:
host=host,
token=token,
)
resource_oaps = get_all_resource_oaps_of_project(
shortcode=shortcode,


def main() -> None:
"""
The main function provides you with 2 sample functions:
one to update the Object Access Permissions of a project,
and one to update the Default Object Access Permissions of a project.
Both must first be adapted to your needs.
"""
load_dotenv() # set login credentials from .env file as environment variables
host = Hosts.get_host("test")
shortcode = "F18E"
token = login(host)

update_oaps(
host=host,
shortcode=shortcode,
token=token,
)
resource_oaps_updated = modify_oaps(oaps=resource_oaps)
apply_updated_oaps_on_server(
resource_oaps=resource_oaps_updated,
update_doaps(
host=host,
shortcode=shortcode,
token=token,
)

Expand Down
8 changes: 4 additions & 4 deletions dsp_permissions_scripts/utils/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests


def __get_token(host: str, email: str, pw: str) -> str:
def _get_token(host: str, email: str, pw: str) -> str:
"""
requests an access token from the API, provided host, email and password.
"""
Expand All @@ -15,7 +15,7 @@ def __get_token(host: str, email: str, pw: str) -> str:
return token


def __get_login_credentials(host: str) -> tuple[str, str]:
def _get_login_credentials(host: str) -> tuple[str, str]:
"""
Retrieve user email and password from the environment variables.
In case of localhost, return the default email/password for localhost.
Expand All @@ -41,8 +41,8 @@ def login(host: str) -> str:
Returns:
token: access token
"""
user, pw = __get_login_credentials(host)
token = __get_token(host, user, pw)
user, pw = _get_login_credentials(host)
token = _get_token(host, user, pw)
return token


Expand Down
8 changes: 4 additions & 4 deletions dsp_permissions_scripts/utils/doap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
logger = get_logger(__name__)


def __filter_doaps_by_target(
def _filter_doaps_by_target(
doaps: list[Doap],
target: DoapTargetType,
) -> list[Doap]:
Expand All @@ -34,7 +34,7 @@ def __filter_doaps_by_target(
return filtered_doaps


def __get_all_doaps_of_project(
def _get_all_doaps_of_project(
project_iri: str,
host: str,
token: str,
Expand Down Expand Up @@ -87,12 +87,12 @@ def get_doaps_of_project(
shortcode=shortcode,
host=host,
)
doaps = __get_all_doaps_of_project(
doaps = _get_all_doaps_of_project(
project_iri=project_iri,
host=host,
token=token,
)
filtered_doaps = __filter_doaps_by_target(
filtered_doaps = _filter_doaps_by_target(
doaps=doaps,
target=target,
)
Expand Down
10 changes: 5 additions & 5 deletions dsp_permissions_scripts/utils/doap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

logger = get_logger(__name__)

def __update_doap_scope(
def _update_doap_scope(
doap_iri: str,
scope: PermissionScope,
host: str,
Expand All @@ -34,7 +34,7 @@ def __update_doap_scope(
return new_doap


def __log_and_print_doap_update(
def _log_and_print_doap_update(
doap: Doap,
state: Literal["before", "after"],
) -> None:
Expand Down Expand Up @@ -64,12 +64,12 @@ def apply_updated_doaps_on_server(
heading = f"{get_timestamp()}: Updating {len(doaps)} DOAPs on {host}..."
print(f"\n{heading}\n{'=' * len(heading)}\n")
for d in doaps:
__log_and_print_doap_update(doap=d, state="before")
new_doap = __update_doap_scope(
_log_and_print_doap_update(doap=d, state="before")
new_doap = _update_doap_scope(
doap_iri=d.doap_iri,
scope=d.scope,
host=host,
token=token,
)
__log_and_print_doap_update(doap=new_doap, state="after")
_log_and_print_doap_update(doap=new_doap, state="after")
print(f"{get_timestamp()}: All DOAPs have been updated.")
Loading

0 comments on commit 02fbf22

Please sign in to comment.