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

Henter brukerinitialer fra miljøvariabelen «DAPLA_USER» #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 32 additions & 9 deletions src/statbank/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import pandas as pd

import datetime as dt
import getpass
import json
import os
import subprocess
from functools import partial
from pathlib import Path
from typing import TYPE_CHECKING

import ipywidgets as widgets
from IPython.display import display
Expand Down Expand Up @@ -474,7 +476,7 @@ def _validate_params_action(self, tableid: str) -> None:
def _validate_params_init(self) -> None:
"""Validates many of the parameters sent in on client-initialization."""
if not self.shortuser:
self.shortuser = self._get_user_tbf()
self.shortuser = self._get_user_initials()
if not self.cc:
self.cc = self.shortuser
if not self.bcc:
Expand All @@ -487,10 +489,31 @@ def _validate_params_init(self) -> None:
raise ValueError(error_msg)

@staticmethod
def _get_user_tbf() -> str:
user_mail = os.environ.get("GIT_USER_MAIL", "")
if not user_mail:
user_mail = os.environ.get("JUPYTERHUB_USER", "")
if "@" in user_mail:
user_mail = user_mail.split("@")[0]
return user_mail
def _get_user_initials() -> str:
attempts = (
partial(os.environ.get, "DAPLA_USER"),
partial(os.environ.get, "JUPYTERHUB_USER"),
lambda: subprocess.run(
["git", "config", "user.email"],
capture_output=True,
text=True,
check=False,
).stdout.strip(),
getpass.getuser,
partial(input, "Brukerinitialer: (tre bokstaver)"),
)

for func in attempts:
initials_or_email = func()

if not initials_or_email:
continue

initials = initials_or_email.partition("@")[0]
if not (len(initials) == 3 and initials.isalpha()):
continue

return initials

error_message = "Can't find the users email or initials in the system."
raise ValueError(error_message)
Loading