Skip to content
Snippets Groups Projects
Commit f0e7434e authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

FIX: example script

parent 9c1411d5
No related branches found
No related tags found
2 merge requests!39Release 0.4.0,!20created draft for generic analysis method
...@@ -26,6 +26,17 @@ ...@@ -26,6 +26,17 @@
""" """
An exemplary script that illustrates how scripts can be used in conjunction An exemplary script that illustrates how scripts can be used in conjunction
with the generic_analysis module. with the generic_analysis module.
Data model:
Analysis:
sources: REFEERENCE
scripts: FILE
results: REFEERENCE
mean_value: DOUBLE
Person:
Email: TEXT
""" """
import argparse import argparse
...@@ -37,7 +48,7 @@ from datetime import datetime ...@@ -37,7 +48,7 @@ from datetime import datetime
import caosdb as db import caosdb as db
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
from caosadvancedtools.cfood import assure_property_is from caosadvancedtools.cfood import assure_property_is, get_property
from caosadvancedtools.guard import INSERT, UPDATE from caosadvancedtools.guard import INSERT, UPDATE
from caosadvancedtools.guard import global_guard as guard from caosadvancedtools.guard import global_guard as guard
from caosadvancedtools.serverside.helper import send_mail as main_send_mail from caosadvancedtools.serverside.helper import send_mail as main_send_mail
...@@ -47,7 +58,7 @@ logger = logging.getLogger(__name__) ...@@ -47,7 +58,7 @@ logger = logging.getLogger(__name__)
guard.set_level(level=UPDATE) guard.set_level(level=UPDATE)
def send_mail(self, changes, receipient): def send_mail(changes, receipient):
""" calls sendmail in order to send a mail to the curator about pending """ calls sendmail in order to send a mail to the curator about pending
changes changes
...@@ -78,11 +89,15 @@ The following changes where done automatically. ...@@ -78,11 +89,15 @@ The following changes where done automatically.
def main(args): def main(args):
if hasattr(args, "auth_token") and args.auth_token:
db.configure_connection(auth_token=args.auth_token)
# TODO (maybe) can these checks be replaced by a more declaritive appoach? # TODO (maybe) can these checks be replaced by a more declaritive appoach?
try: try:
dataAnalysisRecord = db.Record(id=args.entityid).retrieve() dataAnalysisRecord = db.Record(id=args.entityid).retrieve()
except db.TransactionError: except db.TransactionError:
logger.error("Cannot retrieve dataAnalysisRecord with id ={}".format( logger.error("Cannot retrieve Record with id ={}".format(
args.entityid args.entityid
)) ))
...@@ -123,40 +138,42 @@ def main(args): ...@@ -123,40 +138,42 @@ def main(args):
# An update should only be done if necessary: assure_property_is should be # An update should only be done if necessary: assure_property_is should be
# used instead of direct calls to 'update'. # used instead of direct calls to 'update'.
to_be_updated = [] to_be_updated = db.Container()
assure_property_is( assure_property_is(
dataAnalysisRecord, dataAnalysisRecord,
"mean_value", "mean_value",
mean, mean,
to_be_updated=to_be_updated to_be_updated=to_be_updated
) )
# TODO (maybe) this is not really meaningful since an uploaded file will always # TODO (maybe) this is not really meaningful since an uploaded file will
# be different.... Compare checksums of files? # always be different.... Compare checksums of files?
assure_property_is( assure_property_is(
dataAnalysisRecord, dataAnalysisRecord,
"results", "results",
fig.id, [fig.id],
to_be_updated=to_be_updated to_be_updated=to_be_updated
) )
if len(to_be_updated) > 0: if len(to_be_updated) > 0:
people = db.execute_query("FIND RECORD Person WHICH IS REFERENCEED BY " print(to_be_updated)
to_be_updated.update()
people = db.execute_query("FIND RECORD Person WHICH IS REFERENCED BY "
"{}".format(dataAnalysisRecord.id)) "{}".format(dataAnalysisRecord.id))
for person in people: for person in people:
if person.get_property("Email") is not None: if person.get_property("Email") is not None:
send_mail(to_be_updated, receipient=person.get_property( send_mail([str(el) for el in to_be_updated],
"Email")) receipient=person.get_property("Email").value)
# TODO (must) what should be done with the old file? Removed if not referenced? # TODO (must) what should be done with the old file? Removed if not referenced?
# TODO (maybe) inform about updates (reuse stuff from crawler.py?) # TODO (maybe) inform about updates (reuse stuff from crawler.py?)
# TODO (must) sketch meaningful logging # TODO (must) sketch meaningful logging
# TODO (must) how to send an email?
def parse_args(): def parse_args():
parser = argparse.ArgumentParser(description=__doc__, parser = argparse.ArgumentParser(description=__doc__,
formatter_class=RawTextHelpFormatter) formatter_class=RawTextHelpFormatter)
parser.add_argument("--auth-token")
parser.add_argument("entityid", parser.add_argument("entityid",
help="The ID of the DataAnalysis Record.", type=int) help="The ID of the DataAnalysis Record.", type=int)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment