ucsschool.importer.reader package#

Submodules#

ucsschool.importer.reader.base_reader module#

Base class of all input readers.

class ucsschool.importer.reader.base_reader.BaseReader(filename: str, header_lines: int | None = 0, **kwargs: Any)[source]#

Bases: object

Base class of all input readers.

Subclasses must override get_roles(), map() and read().

Parameters:
  • filename (str) – Path to file with user data.

  • header_lines (int) – Number of lines before the actual data starts.

  • kwargs (dict) – optional parameters for use in derived classes

next() ImportUser#

Generates ImportUsers from input data.

Returns:

ImportUser

Return type:

ImportUser

get_roles(input_data: Dict[str, Any]) Iterable[str][source]#

IMPLEMENT ME Detect the ucsschool.lib.roles from the input data.

Parameters:

input_data (dict) – dict user from read()

Returns:

[ucsschool.lib.roles, ..]

Return type:

list(str)

map(input_data: Dict[str, str], cur_user_roles: Iterable[str]) ImportUser[source]#

IMPLEMENT ME Creates a ImportUser object from a users dict (self.cur_entry). Data will not be modified, just copied.

Parameters:
  • input_data (dict) – user from read()

  • cur_user_roles (list(str)) – [ucsschool.lib.roles, ..]

Returns:

ImportUser

Return type:

ImportUser

read(*args: Any, **kwargs: Any) Iterator[Dict[str, str]][source]#

IMPLEMENT ME Generator that returns dicts of read users Sets self.entry_count and self.input_data on each read.

Parameters:
  • args (tuple) – arguments for implemented reader

  • kwargs (dict) – arguments for implemented reader

Returns:

iter([user, …])

Return type:

Iterator

get_data_mapping(input_data: Iterable[str]) Dict[str, Any][source]#

IMPLEMENT ME Create a mapping from the configured input mapping to the actual input data. This is configuration and input format specific. See csv_reader for an example. Used by ImportUser.format_from_scheme().

Parameters:

input_data (list(str)) – raw input data as stored in ImportUser.input_data

Returns:

key->input_data-value mapping

Return type:

dict

get_imported_udm_property_names(import_user: ImportUser) List[str][source]#

IMPLEMENT ME Return all udm attributes which are directly set by the reader class. This is configuration and input format specific. See csv_reader for an example. This function is used to set which attributes should be loaded from ldap into udm_properties for users which will be deleted.

Parameters:

import_user (ImportUser) – an ImportUser object

Returns:

list of udm attibute names

Return type:

list(str)

ucsschool.importer.reader.csv_reader module#

CSV reader for CSV files using the new import format.

exception ucsschool.importer.reader.csv_reader.UnsupportedEncodingError(message)[source]#

Bases: Exception

ucsschool.importer.reader.csv_reader.py3_decode(data: str | bytes, encoding: str) str[source]#
class ucsschool.importer.reader.csv_reader.CsvReader(filename: str, header_lines: int | None = 0, **kwargs: Any)[source]#

Bases: BaseReader

Reads CSV files and turns lines to ImportUser objects.

Parameters:
  • filename (str) – Path to file with user data.

  • header_lines (int) – Number of lines before the actual data starts.

  • kwargs (dict) – optional parameters for use in derived classes

encoding = 'utf-8'#
static get_encoding()[source]#

Get encoding of file filename_or_file.

Handles both magic libraries.

Parameters:

filename_or_file (str or file) – filename or open file

Returns:

encoding of filename_or_file

Return type:

str

get_dialect(fp: BinaryIO, encoding: str) Type[Dialect][source]#

Overwrite me to force a certain CSV dialect.

Parameters:
  • fp (file) – open file to read from

  • encoding – encoding in file

Returns:

CSV dialect

Return type:

csv.Dialect

read(*args: Any, **kwargs: Any) Iterator[Dict[str, str]][source]#

Generate dicts from a CSV file.

Parameters:
  • args – ignored

  • kwargs (dict) – if it has a dict csv_reader_args, that will be used as additional arguments for the DictReader constructor.

Returns:

iterator over list of dicts

Return type:

Iterator

handle_input(mapping_key: str, mapping_value: str, csv_value: str, import_user: ImportUser) bool[source]#

This is a hook into map().

IMPLEMENT ME if you wish to handle certain columns from the CSV file yourself.

Parameters:
  • mapping_key (str) – the key in config[“csv”][“mapping”]

  • mapping_value (str) – the value in config[“csv”][“mapping”]

  • csv_value (str) – the associated value from the CSV line

  • import_user (ImportUser) – the object to modify

Returns:

True if the field was handled here. It will be ignored in map(). False if map() should handle the field.

Return type:

bool

get_roles(input_data: Dict[str, Any]) Iterable[str][source]#

Detect the ucsschool.lib.roles from the input data or configuration.

IMPLEMENT ME if the user role is not set in the configuration (file or by cmdline) or in the CSV mapped by __role.

__role can be something else, if configured in _csv_roles_key.

Parameters:

input_data (dict) – dict user from read()

Returns:

list of roles [ucsschool.lib.roles, ..]

Return type:

list(str)

get_roles_from_configuration(input_data)[source]#
get_roles_from_csv(input_data: Dict[str, Any]) Iterable[str][source]#
map(input_data: Dict[str, str], cur_user_roles: Iterable[str]) ImportUser[source]#

Creates a ImportUser object from a users dict. Data will not be modified, just copied.

Parameters:
  • input_data (dict) – user from read()

  • cur_user_roles (list(str)) – [ucsschool.lib.roles, ..]

Returns:

ImportUser instance

Return type:

ImportUser

get_data_mapping(input_data: Iterable[str]) Dict[str, Any][source]#

Create a mapping from the configured input mapping to the actual input data. Used by ImportUser.format_from_scheme().

Parameters:

input_data (list(str)) – “raw” input data as stored in ImportUser.input_data

Returns:

key->input_data-value mapping

Return type:

dict

get_imported_udm_property_names(import_user: ImportUser) List[str][source]#

Return a list of udm attribute names for udm attribute values which are set through config[“csv”][“mapping”]

Parameters:

import_user (ImportUser) – an ImportUser object

Returns:

list of udm attibute names

Return type:

list(str)

class ucsschool.importer.reader.csv_reader.UTF8Recoder(f: BinaryIO, encoding: str)[source]#

Bases: object

Iterator that reads an encoded stream and reencodes the input to UTF-8. Blatantly copied from docs.python.org/2/library/csv.html

next() bytes[source]#

ucsschool.importer.reader.http_api_csv_reader module#

CSV reader for CSV files created for HTTP-API import.

class ucsschool.importer.reader.http_api_csv_reader.HttpApiCsvReader(*args, **kwargs)[source]#

Bases: CsvReader

Parameters:
  • filename (str) – Path to file with user data.

  • header_lines (int) – Number of lines before the actual data starts.

  • kwargs (dict) – optional parameters for use in derived classes

handle_input(mapping_key, mapping_value, csv_value, import_user)[source]#

Handle class names (prepend school name to class names).

ucsschool.importer.reader.test_csv_reader module#

CSV reader for CSV files created by TestUserCsvExporter.

class ucsschool.importer.reader.test_csv_reader.TestCsvReader[source]#

Bases: CsvReader

This class has been deprecated. Please use “CsvReader” instead. It now also handles a “__role” column (replace “__type” in the mapping configuration with “__role”).

Parameters:
  • filename (str) – Path to file with user data.

  • header_lines (int) – Number of lines before the actual data starts.

  • kwargs (dict) – optional parameters for use in derived classes