Skip to content

Commit 9cb3832

Browse files
NRL-2099 Make output cuter
1 parent 2c36f41 commit 9cb3832

File tree

3 files changed

+61
-44
lines changed

3 files changed

+61
-44
lines changed

layer/nrlf/core/constants.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -754,37 +754,37 @@ def list():
754754
V2PermissionKey.ACCESS_CONTROLS.value: {
755755
"display": "access controls",
756756
"display_singular": "access control",
757-
"permission_lookup": None,
757+
"attribute_lookup": None,
758758
"all_assignable_permission_items": AccessControls.list(),
759759
},
760760
V2PermissionKey.TYPES.value: {
761761
"display": "pointer types",
762762
"display_singular": "pointer type",
763-
"permission_lookup": TYPE_ATTRIBUTES,
763+
"attribute_lookup": TYPE_ATTRIBUTES,
764764
"all_assignable_permission_items": PointerTypes.list(),
765765
},
766766
V2PermissionKey.CATEGORIES.value: {
767767
"display": "categories",
768768
"display_singular": "category",
769-
"permission_lookup": CATEGORY_ATTRIBUTES,
769+
"attribute_lookup": CATEGORY_ATTRIBUTES,
770770
"all_assignable_permission_items": Categories.list(),
771771
},
772772
V2PermissionKey.INTERACTIONS.value: {
773773
"display": "interactions",
774774
"display_singular": "interaction",
775-
"permission_lookup": "",
775+
"attribute_lookup": "",
776776
"all_assignable_permission_items": "",
777777
},
778778
V2PermissionKey.PRODUCE_FOR_AUTHORS.value: {
779779
"display": "produce for authors",
780780
"display_singular": "produce for author",
781-
"permission_lookup": "",
781+
"attribute_lookup": "",
782782
"all_assignable_permission_items": "",
783783
},
784784
V2PermissionKey.PRODUCE_FOR_CUSTODIANS.value: {
785785
"display": "produce for custodians",
786786
"display_singular": "produce for custodian",
787-
"permission_lookup": "",
787+
"attribute_lookup": "",
788788
"all_assignable_permission_items": "",
789789
},
790790
}

scripts/manage_permissions.py

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def list():
5858
]
5959

6060

61-
print(f"Using NRL environment: {nrl_env}")
62-
print(f"Using NRL auth bucket: {nrl_auth_bucket_name}")
63-
print(f"Compare and confirm mode: {COMPARE_AND_CONFIRM}")
61+
print(f"📚 Using NRL environment: {nrl_env}")
62+
print(f"🪣 Using NRL auth bucket: {nrl_auth_bucket_name}")
63+
print(f"🔍 Compare and confirm mode: {COMPARE_AND_CONFIRM}")
6464
print()
6565

6666

@@ -85,7 +85,7 @@ def _list_s3_keys(file_key_prefix: str) -> list[str]:
8585
keys.extend([item["Key"] for item in page["Contents"]])
8686

8787
if not keys:
88-
print(f"No files found with prefix: {file_key_prefix}")
88+
print(f"👀 No files found with prefix: {file_key_prefix}")
8989
return []
9090

9191
return keys
@@ -97,11 +97,11 @@ def _get_perms_from_s3(file_key: str) -> str | None:
9797
try:
9898
item = s3.get_object(Bucket=nrl_auth_bucket_name, Key=file_key)
9999
except s3.exceptions.NoSuchKey:
100-
print(f"Permissions file {file_key} does not exist in the bucket.")
100+
print(f"👀 Permissions file {file_key} does not exist in the bucket.")
101101
return None
102102

103103
if "Body" not in item:
104-
print(f"No body found for permissions file {file_key}.")
104+
print(f"❌ Error: no body found for permissions file {file_key}.")
105105
return None
106106

107107
return item["Body"].read().decode("utf-8")
@@ -111,10 +111,10 @@ def _build_lookup_path(
111111
supplier_type: str, app_id: str, org_ods: str | None
112112
) -> str | None:
113113
if supplier_type.lower() not in SupplierType.list():
114-
print(f"Error: invalid supplier {supplier_type}")
114+
print(f"Error: invalid supplier {supplier_type}")
115115
return
116116
if not app_id:
117-
print("Error: please provide an app_id")
117+
print("Error: please provide an app_id")
118118
return
119119

120120
if org_ods:
@@ -123,17 +123,17 @@ def _build_lookup_path(
123123

124124

125125
def _load_or_setup_perms(lookup_path: str) -> dict:
126-
print(f"Looking up permissions for {lookup_path}")
126+
print(f"Looking up permissions for {lookup_path}")
127127
perms_ugly = _get_perms_from_s3(lookup_path)
128128
if not perms_ugly:
129-
print("Setting up new permissions file...")
129+
print("Setting up new permissions file...")
130130
return {}
131131
print()
132132
return json.loads(perms_ugly)
133133

134134

135135
def _confirm_proceed(
136-
prompt: str = "Do you want to proceed with these changes?",
136+
prompt: str = "Do you want to proceed with these changes?",
137137
) -> bool:
138138
"""
139139
If COMPARE_AND_CONFIRM=true, ask the user to confirm before writing changes.
@@ -143,7 +143,7 @@ def _confirm_proceed(
143143
print()
144144
confirm = input(f"{prompt} (yes/NO): ").strip().lower()
145145
if confirm != "yes":
146-
print("Operation cancelled at user request.")
146+
print("Operation cancelled at user request.")
147147
return False
148148
return True
149149

@@ -167,11 +167,14 @@ def _save_updated_perms(
167167
ContentType="application/json",
168168
)
169169
print()
170-
print(f"{success_message.strip()} for {lookup_path}")
170+
print(f"🎉 {success_message.strip()} for {lookup_path}")
171171

172172
print()
173173
show_perms(supplier_type, app_id, org_ods)
174174

175+
print("💡 Remember to update the lambda layer for these changes to take effect")
176+
print()
177+
175178

176179
json_file_ending = ".json"
177180

@@ -185,7 +188,7 @@ def list_apps(supplier_type: SupplierType) -> None:
185188
list_apps producer
186189
"""
187190
if supplier_type.lower() not in SupplierType.list():
188-
print(f"Error: invalid supplier {supplier_type}")
191+
print(f"Error: invalid supplier {supplier_type}")
189192
return
190193

191194
keys = _list_s3_keys(f"{supplier_type}/")
@@ -198,18 +201,23 @@ def list_apps(supplier_type: SupplierType) -> None:
198201
apps_with_orgs = {key for key in apps if key and not key.endswith(json_file_ending)}
199202

200203
if not apps:
201-
print("No applications found in the bucket.")
204+
print(f"👀 No applications found in the {nrl_env} bucket.")
202205
return
203206

204-
print(f"There are {len(apps)} apps in {nrl_env} env")
207+
def there_are_x_apps(app_count: int):
208+
is_are = "is" if app_count is 1 else "are"
209+
s = "" if app_count is 1 else "s"
210+
return f"There {is_are} {app_count} app{s}"
211+
212+
print(f"{there_are_x_apps(len(apps))} in the {nrl_env} env")
205213

206214
print()
207-
print(f"There are {len(apps_with_orgs)} apps containing org-level permissions:")
215+
print(f"{there_are_x_apps(len(apps_with_orgs))} containing org-level permissions:")
208216
for app_with_orgs in apps_with_orgs:
209217
print(f"- {app_with_orgs}")
210218

211219
print()
212-
print(f"There are {len(app_level_perm_files)} apps with app-level permissions:")
220+
print(f"{there_are_x_apps(len(app_level_perm_files))} with app-level permissions:")
213221
for app_level in app_level_perm_files:
214222
print(f"- {app_level}")
215223
print()
@@ -223,7 +231,7 @@ def list_orgs(supplier_type: SupplierType, app_id: str) -> None:
223231
list_orgs producer <app_id>
224232
"""
225233
if supplier_type.lower() not in SupplierType.list():
226-
print(f"Error: invalid supplier {supplier_type}")
234+
print(f"Error: invalid supplier {supplier_type}")
227235
return
228236

229237
keys = _list_s3_keys(f"{supplier_type}/{app_id}/")
@@ -234,9 +242,14 @@ def list_orgs(supplier_type: SupplierType, app_id: str) -> None:
234242
]
235243

236244
if not orgs:
237-
print(f"No organizations found for {supplier_type} app {app_id}.")
245+
print()
246+
print(f"👀 No organizations found for {supplier_type} app {app_id}.")
247+
return
238248

239-
print(f"There are {len(orgs)} organizations for app {app_id}:")
249+
org_count = len(orgs)
250+
is_are = "is" if org_count == 1 else "are"
251+
s = "" if org_count == 1 else "s"
252+
print(f"There {is_are} {org_count} organization{s} for app {app_id}:")
240253
for org in orgs:
241254
print(f"- {org}")
242255
print()
@@ -321,12 +334,12 @@ def show_perms(supplier_type: SupplierType, app_id: str, org_ods=None) -> None:
321334
perms_ugly = _get_perms_from_s3(lookup_path)
322335

323336
if not perms_ugly:
324-
print(f"No permissions file found for {lookup_path}.")
337+
print(f"👀 No permissions file found for {lookup_path}.")
325338
return
326339

327340
perms_pretty = json.loads(perms_ugly)
328341
if not perms_pretty:
329-
print(f"No permissions found in file for {lookup_path}.")
342+
print(f"👀 No permissions found in file for {lookup_path}.")
330343
return
331344

332345
print(f"{lookup_path} is allowed access to the following...")
@@ -335,7 +348,7 @@ def show_perms(supplier_type: SupplierType, app_id: str, org_ods=None) -> None:
335348
_print_perm_with_lookup(
336349
perm,
337350
perms_pretty.get(perm, []),
338-
PERMISSION_KEY_ATTRIBUTES.get(perm)["permission_lookup"],
351+
PERMISSION_KEY_ATTRIBUTES.get(perm)["attribute_lookup"],
339352
)
340353

341354

@@ -360,7 +373,7 @@ def add_perm(
360373
add_perm access_controls producer <app_id> allow_all_types allow_supersede_with_delete_failure
361374
"""
362375
if permission_key not in currently_supported_permission_keys:
363-
print(f"Error: invalid permission being set: {permission_key}")
376+
print(f"Error: invalid permission being set: {permission_key}")
364377
print(f"Supported permission keys: {currently_supported_permission_keys}")
365378
return
366379

@@ -377,19 +390,21 @@ def add_perm(
377390

378391
if not items_to_add:
379392
print(
380-
f"No {permission_name} provided. Please specify at least one {permission_name_singular}."
393+
f"❌ Error: no {permission_name} provided. Please specify at least one {permission_name_singular}."
381394
)
382395
return
383396

384397
if len(items_to_add) == 1 and items_to_add[0] == "all":
385-
print(f"Setting permissions for access to all {permission_name}.")
398+
print(f"📚 Setting permissions for access to all {permission_name}.")
386399
items_to_add = all_assignable_permission_items
387400

388401
unknown_items = [
389402
item for item in items_to_add if item not in all_assignable_permission_items
390403
]
391404
if unknown_items:
392-
print(f"Error: Unknown {permission_name} provided: {', '.join(unknown_items)}")
405+
print(
406+
f"❌ Error: Unknown {permission_name} provided: {', '.join(unknown_items)}"
407+
)
393408
print()
394409
return
395410

@@ -401,7 +416,7 @@ def add_perm(
401416
]
402417
if already_added_items:
403418
print(
404-
f"Error: Unable to add {permission_name}. These {permission_name} are already assigned to {lookup_path}:"
419+
f"Error: Unable to add {permission_name}. These {permission_name} are already assigned to {lookup_path}:"
405420
)
406421
_print_perm_with_lookup("", already_added_items, permission_lookup)
407422
print()
@@ -416,7 +431,7 @@ def add_perm(
416431

417432
add_count = len(items_to_add)
418433
if not _confirm_proceed(
419-
f"Do you want to proceed with these changes and add {add_count} {permission_name if add_count >1 else permission_name_singular}?"
434+
f"Do you want to proceed with these changes and add {add_count} {permission_name_singular if add_count == 1 else permission_name}?"
420435
):
421436
return
422437

@@ -442,7 +457,7 @@ def remove_perm(
442457
remove_perm access_controls producer <app_id> allow_all_types allow_supersede_with_delete_failure
443458
"""
444459
if permission_key not in currently_supported_permission_keys:
445-
print(f"Error: invalid permission being set: {permission_key}")
460+
print(f"Error: invalid permission being set: {permission_key}")
446461
print(f"Supported permission keys: {currently_supported_permission_keys}")
447462
return
448463

@@ -459,19 +474,21 @@ def remove_perm(
459474

460475
if not items_to_remove:
461476
print(
462-
f"No {permission_name} provided. Please specify at least one {permission_name_singular}."
477+
f"👀 No {permission_name} provided. Please specify at least one {permission_name_singular}."
463478
)
464479
return
465480

466481
unknown_items = [
467482
item for item in items_to_remove if item not in all_assignable_permission_items
468483
]
469484
if unknown_items:
470-
print(f"Error: Unknown {permission_name} provided: {', '.join(unknown_items)}")
485+
print(
486+
f"❌ Error: Unknown {permission_name} provided: {', '.join(unknown_items)}"
487+
)
471488
print()
472489
return
473490

474-
print(f"Looking up permissions for {lookup_path}")
491+
print(f"Looking up permissions for {lookup_path}")
475492
perms_ugly = _get_perms_from_s3(lookup_path)
476493
if not perms_ugly:
477494
return
@@ -486,7 +503,7 @@ def remove_perm(
486503
]
487504
if items_not_assigned:
488505
print(
489-
f"Error: Unable to remove {permission_name}. These {permission_name} aren't assigned to {lookup_path}:"
506+
f"Error: Unable to remove {permission_name}. These {permission_name} aren't assigned to {lookup_path}:"
490507
)
491508
_print_perm("", items_not_assigned)
492509
print()
@@ -503,7 +520,7 @@ def remove_perm(
503520

504521
remove_count = len(items_to_remove)
505522
if not _confirm_proceed(
506-
f"Do you want to proceed with these changes and remove {remove_count} {permission_name if remove_count >1 else permission_name_singular}?"
523+
f"Do you want to proceed with these changes and remove {remove_count} {permission_name_singular if remove_count == 1 else permission_name}?"
507524
):
508525
return
509526

@@ -530,7 +547,7 @@ def clear_perms(supplier_type: SupplierType, app_id: str, org_ods=None) -> None:
530547
current_perms = _get_perms_from_s3(lookup_path)
531548
if not current_perms or current_perms == "{}":
532549
print(
533-
f"No need to clear permissions for {lookup_path} as it currently has no permissions set."
550+
f"⏭️ No need to clear permissions for {lookup_path} as it currently has no permissions set."
534551
)
535552
return
536553

scripts/tests/test_manage_permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def test_add_perm_rejects_invalid_permission_key(capsys):
189189
def test_add_perm_rejects_no_items(capsys):
190190
add_perm("types", "producer", APP_ID, ORG_ODS)
191191

192-
assert "No pointer types provided" in capsys.readouterr().out
192+
assert "no pointer types provided" in capsys.readouterr().out
193193

194194

195195
def test_add_perm_rejects_unknown_items(capsys):

0 commit comments

Comments
 (0)