gitlab_users.utils

Utility functions for CSV import/export and user data processing.

Includes helpers for reading and writing user data in CSV format.

Functions

export_users_to_csv(users, filepath)

Export a list of users to a CSV file.

get_user_data(user, dataname)

Retrieve specific user data from a user dictionary.

get_usernames_from_csv(filepath)

Return a list of usernames from a CSV or text file (first column).

get_users_from_csv(filepath)

Read users from a CSV file (with or without header).

get_user_data(user: dict, dataname: str) str

Retrieve specific user data from a user dictionary.

Parameters:
  • user – User data dictionary.

  • dataname – Key to retrieve from the user dictionary.

Returns:

The value associated with the dataname key.

Raises:

ValueError – If the dataname key is not found in the user dictionary.

export_users_to_csv(users: list[User], filepath: Path)

Export a list of users to a CSV file.

Parameters:
  • users – list of User objects to export.

  • filepath – Path to the output CSV file.

A typical output CSV file will have the following columns:

id,username,name,email,state
1,alice,Alice,alice@example.com,active
2,bob,Bob,bob@example.com,blocked
get_users_from_csv(filepath: Path) list[dict[str, str]]

Read users from a CSV file (with or without header).

Parameters:

filepath – Path to the CSV file.

Returns:

list of user dictionaries with keys – username, name, email, organization, location, group, access_level.

A typical input CSV file will have the following columns:

# username, name, email, [organization], [location], [group], [access_level]
# Note: The fields in square brackets are optional and can be omitted if not needed.
ww,Diana Prince,wonder.woman@themyscira.org,,Themyscira
wayne,Bruce Wayne,bruce.wayne@wayne-enterprises.com,Wayne Enterprises,Gotham City,board,owner
get_usernames_from_csv(filepath: Path) list[str]

Return a list of usernames from a CSV or text file (first column).

Parameters:

filepath – Path to the CSV or text file.

Returns:

list of usernames.