aboutsummaryrefslogtreecommitdiff
path: root/gn3/case_attributes.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/case_attributes.py')
-rw-r--r--gn3/case_attributes.py29
1 files changed, 27 insertions, 2 deletions
diff --git a/gn3/case_attributes.py b/gn3/case_attributes.py
index 78fc579..cc5feb7 100644
--- a/gn3/case_attributes.py
+++ b/gn3/case_attributes.py
@@ -1,9 +1,14 @@
"""Implement case-attribute manipulations."""
+import os
+import csv
+import tempfile
from functools import reduce
from MySQLdb.cursors import DictCursor
from flask import jsonify, Response, Blueprint, current_app
+from gn3.commands import run_cmd
+
from gn3.db_utils import Connection, database_connection
from gn3.auth.authentication.users import User
@@ -84,9 +89,29 @@ def __process_edit_data__(form_data) -> tuple[dict, ...]:
"""Process data from form and return tuple of dicts."""
raise NotImplementedError
-def __compute_diff__(original_data: tuple[dict, ...], edit_data: tuple[dict, ...]):
+def __write_csv__(fieldnames, data):
+ """Write the given `data` to a csv file and return the path to the file."""
+ fd, filepath = tempfile.mkstemp(".csv")
+ os.close(fd)
+ with open(filepath, "w", encoding="utf-8") as csvfile:
+ writer = csv.DictWriter(filename, fieldnames=fieldnames, dialect="unix")
+ writer.writeheader()
+ writer.writerows(data)
+
+ return filepath
+
+def __compute_diff__(calabels: tuple[str, ...], original_data: tuple[dict, ...], edit_data: tuple[dict, ...]):
"""Return the diff of the data."""
- raise NotImplementedError
+ fieldnames = ["Strain"] + sorted(calabels) # Make first column the strain.
+ basefilename = __write_csv__(fieldnames, original_data)
+ deltafilename = __write_csv__(fieldnames, edit_data)
+ diff_results = run_cmd(json.dumps(
+ ["csvdiff", basefilename, deltafilename, "--format", "json"]))
+ os.unlink(basefilename)
+ os.unlink(deltafilename)
+ if diff_results["code"] == 0:
+ return json.loads(diff_results["output"])
+ return {}
def __queue_diff__(conn: Connection, user: User, diff) -> str:
"""