Last active
April 29, 2025 08:28
-
-
Save martinclaus/c6f229de82769b0b4ae6c7bf3b232106 to your computer and use it in GitHub Desktop.
Converts hub login names to unix system user names when using the tljh.UserCreatingSpawner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Author: Martin Claus <[email protected]> | |
import sys | |
# Maybe there is a better way to import tljh. | |
sys.path.insert(0, '/opt/tljh/hub/lib/python3.6/site-packages/') | |
from tljh.normalize import generate_system_username | |
import argparse | |
def _convert_to_system_username(username, *args): | |
return generate_system_username(f'jupyter-{username}') | |
def _table_output(username, max_len, *args): | |
return f"{username:<{max_len}s} {_convert_to_system_username(username)}" | |
parser = argparse.ArgumentParser( | |
description=""" | |
Converts hub login names to unix system user names when using the tljh.UserCreatingSpawner.\n | |
See https://github.com/jupyterhub/the-littlest-jupyterhub/blob/master/tljh/user_creating_spawner.py#L21 | |
""" | |
) | |
parser.add_argument( | |
'usernames', metavar='USERNAME', type=str, nargs='+', | |
help='Hub username to convert' | |
) | |
parser.add_argument( | |
'--table', '-t', dest='renderer', action='store_const', | |
const=_table_output, | |
default=_convert_to_system_username, | |
help="Print tabular output of hub usernames with corresponding system usernames." | |
) | |
def main(): | |
args = parser.parse_args() | |
max_width = max(map(len, args.usernames)) | |
print('\n'.join(map(args.renderer, args.usernames, (max_width for n in args.usernames)))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!