caosadvancedtools.serverside.helper module#

exception caosadvancedtools.serverside.helper.DataModelError(rt, info='')#

Bases: RuntimeError

DataModelError indicates that the server-side script cannot work as intended due to missing data model entities or an otherwise incompatible data model.

class caosadvancedtools.serverside.helper.NameCollector#

Bases: object

get_unique_savename(name)#

make names unique by attaching numbers

This is for example use full if multiple files shall be saved into one directory but the names of them are not unique

caosadvancedtools.serverside.helper.get_argument_parser()#

Return a argparse.ArgumentParser for typical use-cases.

The parser expects a file name as data input (‘filename’) and an optional auth-token (’–auth-token’).

The parser can also be augmented for other use cases.

Return type:

argparse.ArgumentParser

caosadvancedtools.serverside.helper.get_data(filename, default=None)#

Load data from a json file as a dict.

Parameters:
  • filename (str) – The file’s path, relative or absolute.

  • default (dict) – Default data, which is overridden by the data in the file, if the keys are defined in the file.

Returns:

Data from the given file.

Return type:

dict

caosadvancedtools.serverside.helper.get_file_via_download(ent, logger=<Logger caosadvancedtools.serverside.helper (WARNING)>)#

downloads the given file entity

The typical error handling is done.

caosadvancedtools.serverside.helper.get_shared_filename(filename)#

prefix a filename with a path to a shared resource directory

Parameters:

filename (str) – Filename to be prefixed; e.g. log.txt.

Returns:

(filename, filepath), where filename is the name that can be shared with users, such that they can retrieve the file from the shared directory. filepath is the path that can be used in a script to actually store the file; e.g. with open(filepath, ‘w’) as fi…

Return type:

tuple

caosadvancedtools.serverside.helper.get_timestamp()#

Return a ISO 8601 compliante timestamp (second precision)

caosadvancedtools.serverside.helper.init_data_model(entities)#

Return True iff all entities exist and their role and possibly their data type is correct.

This implementation follows a fail-fast approach. The first entity with problems will raise an exception.

Parameters:

entities (iterable of linkahead.Entity) – The data model entities which are to be checked for existence.

Raises:

DataModelError – If any entity in entities does not exist or the role or data type is not matching.

Returns:

True if all entities exist and their role and data type are matching.

Return type:

bool

caosadvancedtools.serverside.helper.parse_arguments(args: list[str] | None = None)#

Use the standard parser and parse the arguments.

Call with parse_arguments() to parse the command line arguments.

Parameters:

args (list[str], optional) – Arguments to parse. Default is sys.argv.

Returns:

Parsed arguments.

Return type:

dict

caosadvancedtools.serverside.helper.print_bootstrap(text, kind, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)#

Wrap a text into a Bootstrap (3.3.7) DIV.alert and print it to a file.

Parameters:
  • text (str) – The text body of the bootstrap alert.

  • kind (str) – One of [“success”, “info”, “warning”, “danger”]

  • file (file, optional) – Print the alert to this file. Default: sys.stdout.

Return type:

None

caosadvancedtools.serverside.helper.print_error(text)#

Shortcut for print_bootstrap(text, kine=”danger”)

The text body is also prefixed with “<b>ERROR:</b> “.

Parameters:

text (str) – The text body of the bootstrap alert.

Return type:

None

caosadvancedtools.serverside.helper.print_info(text)#

Shortcut for print_bootstrap(text, kine=”info”)

The text body is also prefixed with “<b>Info:</b> “.

Parameters:

text (str) – The text body of the bootstrap alert.

Return type:

None

caosadvancedtools.serverside.helper.print_success(text)#

Shortcut for print_bootstrap(text, kine=”success”)

The text body is also prefixed with “<b>Success:</b> “.

Parameters:

text (str) – The text body of the bootstrap alert.

Return type:

None

caosadvancedtools.serverside.helper.print_warning(text)#

Shortcut for print_bootstrap(text, kine=”warning”)

The text body is also prefixed with “<b>Warning:</b> “.

Parameters:

text (str) – The text body of the bootstrap alert.

Return type:

None

caosadvancedtools.serverside.helper.recordtype_is_child_of(rt, parent)#

Return True iff the RecordType is a child of another Entity.

The parent Entity can be a direct or indirect parent.

Parameters:
  • rt (linkahead.Entity) – The child RecordType.

  • parent (str or int) – The parent’s name or id.

Returns:

True iff rt is a child of parent

Return type:

bool

caosadvancedtools.serverside.helper.send_mail(from_addr=None, to=None, subject=None, body=None, cc=None, bcc=None, send_mail_bin=None)#

Deprecated. Use linkahead.util.email.send_email instead.

caosadvancedtools.serverside.helper.wrap_bootstrap_alert(text, kind)#

Wrap a text into a Bootstrap (3.3.7) DIV.alert.

Parameters:
  • text (str) – The text body of the bootstrap alert.

  • kind (str) – One of [“success”, “info”, “warning”, “danger”]

Returns:

alert – A HTML str of a Bootstrap DIV.alert

Return type:

str