-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrename_clarity_objects.py
executable file
·91 lines (68 loc) · 3.22 KB
/
rename_clarity_objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""
Author: John Arnn
Release Date: 10/12/2023
Usage:
This script is to be used to rename artifacts and containers in Clarity. You need to provide your Clarity
username and password, the lims id of the sample you want to change, weather its a artifact or container,
and the new_name.
Users must have an account in Clarity that has the System Adminastrator Role.
"""
import argparse
import xml.etree.ElementTree as ET
from lxml import etree
import requests
from requests.auth import HTTPBasicAuth
import re
import sys
parser = argparse.ArgumentParser(description="A script that updates Clarity Derived Samples and Container names")
# Required arguments
parser.add_argument("--user", required=True, help="User name")
parser.add_argument("--password", required=True, help="User's password")
parser.add_argument("--new_name", required=True, help="What you want to rename the sample too")
# Either list or file must be provided
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--artifact", help="limsid of derived sample or artifact")
group.add_argument("--container", help="limsid of container")
args = parser.parse_args()
headers = {'Content-Type': 'application/xml'}
# Check User Name and Password
xml = (requests.get("https://uphl-ngs.claritylims.com/api/v2/samples/%s" % 'WAG357A292',
auth=HTTPBasicAuth(args.user, args.password)).content).decode("utf-8")
check=(re.findall(r'<message>(.*?)</message>', xml))
if check == ['Unauthorized']:
sys.exit('User name or password is unauthorized. Please check username and password!')
if args.container:
info=requests.get(
"https://uphl-ngs.claritylims.com/api/v2/containers/%s" % args.container,
auth=HTTPBasicAuth(args.user, args.password)
).content
doc = etree.fromstring(info)
for e in doc.iterfind("name"):
e.text = args.new_name
xml=etree.tostring(doc).decode("utf-8")
print("Verify that one of the names is the intended name of the plate you want to change:")
print(set(info.decode("utf-8").split()).symmetric_difference(set(xml.split()))-{'<?xml', 'version="1.0"', 'standalone="yes"?>', 'encoding="UTF-8"'})
proceed = input("Continue: y/n? ")
if proceed == 'y':
requests.put(
"https://uphl-ngs.claritylims.com/api/v2/containers/%s" % args.container, data=xml, headers=headers,
auth=HTTPBasicAuth(args.user, args.password)
).content
if args.artifact:
info=requests.get(
"https://uphl-ngs.claritylims.com/api/v2/artifacts/%s" % args.artifact,
auth=HTTPBasicAuth(args.user, args.password)
).content
doc = etree.fromstring(info)
for e in doc.iterfind("name"):
e.text = args.new_name
xml=etree.tostring(doc).decode("utf-8")
print("Verify that one of the names is the intended name of the plate you want to change:")
print(set(info.decode("utf-8").split()).symmetric_difference(set(xml.split()))-{'<?xml', 'version="1.0"', 'standalone="yes"?>', 'encoding="UTF-8"'})
proceed = input("Continue: y/n? ")
if proceed == 'y':
requests.put(
"https://uphl-ngs.claritylims.com/api/v2/artifacts/%s" % args.artifact, data=xml, headers=headers,
auth=HTTPBasicAuth(args.user, args.password)
).content