caosadvancedtools.serverside package

Submodules

caosadvancedtools.serverside.generic_analysis module

Variante I: Python module implementiert eine ‘main’ function, die einen Record als Argument entgegennimmt und diesen um z.B. ‘results’ ergänzt und updated.

Variante II: Ein skript erhält eine ID als Argument (z.B. auf der command line) und updated das Objekt selbstständig.

Idealfall: Idempotenz; I.e. es ist egal, ob das Skript schon aufgerufen wurde. Ein weiterer Aufruf führt ggf. zu einem Update (aber nur bei Änderungen von z.B. Parametern)

Das aufgerufene Skript kann beliebige Eigenschaften benutzen und erstellen. ABER wenn die Standardeigenschaften (InputDataSet, etc) verwendet werden, kann der Record leicht erzeugt werden.

“Analyze” “Perform Anlysis”

Knopf an Record Form im WebUI im WebUI

v v

Winzskript, dass einen DataAnalysis-Stub erzeugt



v

execute_script Routine –> AnalysisSkript erhält den Stub und ggf. Nutzt Funktionen um Updates durchzuführen falls den Pythonmodulenamen notwendig, Email


Cronjob findet outdated DataAnalysis

Analyseskript macht update:
  • flexibel welche Änderungen vorgenommen werden (z.B. mehrere Records)

  • spezielle Funktionen sollten verwendet werden

  • Logging und informieren muss im Skript passieren

  • Skript kann mit subprocess aufgerufen werden (alternative unvollständige DataAnalysis einfügen)

# Features
  • Emailversand bei Insert oder Update

  • Kurze Info: “Create XY Analysis” kann vmtl automatisch erzeugt werden

  • Debug Info: müsste optional/bei Fehler zur Verfügung stehen.

  • Skript/Software version sollte gespeichert werden

Outlook: the part of the called scripts that interact with LinkAhead might in future be replaced by the Crawler. The working directory would be copied to the file server and then crawled.

caosadvancedtools.serverside.generic_analysis.call_script(script_name: str, record_id: int)
caosadvancedtools.serverside.generic_analysis.check_referenced_script(record: caosdb.common.models.Record)

return the name of a referenced script

If the supplied record does not have an appropriate Property warings are logged.

caosadvancedtools.serverside.generic_analysis.main()

This is for testing only.

caosadvancedtools.serverside.generic_analysis.run(dataAnalysisRecord: caosdb.common.models.Record)

run a data analysis script.

There are two options: 1. A python script installed as a pip package. 2. A generic script that can be executed on the command line.

Using a python package: It should be located in package plugin and implement at least a main function that takes a DataAnalysisRecord as a single argument. The script may perform changes to the Record and insert and update Entities.

Using a generic script: The only argument that is supplied to the script is the ID of the dataAnalysisRecord. Apart from the different Argument everything that is said for the python package holds here.

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 and an optional auth-token (‘–auth-token’).

The parser can also be augmented for other use cases.

Returns

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 caosdb.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)

Use the standard parser and parse the arguments.

Call with parse_arguments(args=sys.argv) to parse the command line arguments.

Parameters

args (list of str) – Arguments to parse.

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.

Returns

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.

Returns

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.

Returns

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.

Returns

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.

Returns

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 (caosdb.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, to, subject, body, cc=None, bcc=None, send_mail_bin=None)

Send an email via the configured send_mail client.

The relevant options in the pycaosdb.ini are:

[Misc] sendmail = …

Parameters
  • from_addr (str) – The sender’s email address.

  • to (str or list of str) – The recipient’s email address.

  • subject (str) – Subject of the email.

  • body (str) – The mail body, i.e. the text message.

  • cc (str or list of str (optional)) – Single or list of cc-recipients. Defaults to None.

  • bcc (str or list of str (optional)) – Single or list of bcc-recipients. Defaults to None.

  • send_mail_bin (str (optional)) – Path of sendmail client. Defaults to config[“Misc”][“sendmail”].

Raises
  • subprocess.CalledProcessError – If the sendmail client returned with a non-zero code.

  • caosdb.ConfigurationException – If the caosdb configuration has no Misc.sendmail configured while the send_mail_bin parameter is None.

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

caosadvancedtools.serverside.logging module

caosadvancedtools.serverside.logging.configure_server_side_logging(loggername='caosadvancedtools')

Set logging up to save one plain debugging log file, one plain info log file (for users) and a stdout stream with messages wrapped in html elements

returns the path to the file with debugging output

caosadvancedtools.serverside.sync module

Module contents