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, header_lines=0, **kwargs)[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()[source]

Generates ImportUsers from input data.

Returns:ImportUser
Return type:ImportUser
get_roles(input_data)[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, cur_user_roles)[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, **kwargs)[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)[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

ucsschool.importer.reader.csv_reader module

CSV reader for CSV files using the new import format.

class ucsschool.importer.reader.csv_reader.CsvReader(filename, header_lines=0, **kwargs)[source]

Bases: ucsschool.importer.reader.base_reader.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
_attrib_names = {}
_role_method = None
_csv_roles_mapping = {'teacher_and_staff': ['teacher', 'staff'], 'staffteacher': ['teacher', 'staff'], 'teacher': ['teacher'], 'student': ['pupil'], 'staff': ['staff']}
_csv_roles_key = None
_csv_roles_value = '__role'
encoding = 'utf-8'
static get_encoding(filename_or_file)[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)[source]

Overwrite me to force a certain CSV dialect.

Parameters:fp (file) – open file to read from
Returns:CSV dialect
Return type:csv.Dialect
read(*args, **kwargs)[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, mapping_value, csv_value, import_user)[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)[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)[source]
map(input_data, cur_user_roles)[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)[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
classmethod _get_attrib_name(import_user)[source]

Cached retrieval of names of Attributes of an ImportUser.

Parameters:import_user (ImportUser) – an ImportUser object
Returns:names of Attributes
Return type:list()
_get_missing_columns()[source]

Find fieldnames that were configured in the csv:mapping but are missing in the input data.

Returns:list(str)
class ucsschool.importer.reader.csv_reader.UTF8Recoder(f, encoding)[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()[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[source]

Bases: ucsschool.importer.reader.csv_reader.CsvReader

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: ucsschool.importer.reader.csv_reader.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”).

_role_method(input_data)
_csv_roles_value = '__type'