Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Converts hub login names to unix system user names when using the tljh.UserCreatingSpawner
#!/usr/bin/env python
# Author: Martin Claus <mclaus@geomar.de>
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