Skip to content

Commit 1f8147e

Browse files
VALI-5456 :: Properly Document Existing Scripting Module Scripts (#41)
1 parent c29d04f commit 1f8147e

10 files changed

+1051
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from ast import Str
2+
from collections.abc import Callable
3+
from xmlrpc.client import Boolean
4+
from valispace import API
5+
import warnings
6+
import time
7+
8+
VALISPACE = {
9+
'domain': 'https://demonstration.valispace.com/',
10+
'username': 'AutomationsAPI',
11+
'password': 'AutomationsAPI'
12+
}
13+
14+
DEFAULT_VALUES = {
15+
"project": 24,
16+
}
17+
18+
19+
def get_map(api: API, endpoint: Str = "/", name: Str = "id", name_map_func: Callable[[Str], Boolean] = None, filter_func: Callable[[Str], Boolean]= None):
20+
"""
21+
Function that given an endpoint returns a dict with specific keys.
22+
If function is provided it generates the key. name_map_func must receive an object instance.
23+
Otherwise key will be the property of each object specified in name.
24+
"""
25+
mapping = {}
26+
if not name_map_func:
27+
name_map_func = lambda x: x[name]
28+
for inst in api.get(endpoint):
29+
if filter_func and not filter_func(inst):
30+
# Filtered out
31+
continue
32+
33+
key = name_map_func(inst)
34+
if not mapping.get(key):
35+
mapping[key] = inst
36+
else:
37+
warnings.warn(f"Warning ---> Key: {key} already has an object. Some data may be lost in mapping.")
38+
return mapping
39+
40+
41+
def main(**kwargs):
42+
43+
api = API(
44+
url = VALISPACE.get('domain'),
45+
username = VALISPACE.get('username'),
46+
password = VALISPACE.get('password'),
47+
warn_https = VALISPACE.get('warn_https', False),
48+
)
49+
50+
all_deployment_users = get_map(api, f"user/", "id")
51+
52+
requirement_data = kwargs['triggered_objects'][0]
53+
54+
#requirement_data = api.get('requirements/81') #just for testing now without automation to receive the trigger object
55+
print(requirement_data)
56+
time.sleep(15)
57+
58+
user_changing_req = all_deployment_users[requirement_data['updated_by']]
59+
username = user_changing_req['first_name']+" "+user_changing_req['last_name']
60+
user = f"<span class=\"quill-autocomplete\" host=\"user\" itemid=\"{user_changing_req['id']}\" name=\"{username}\" field=\"displayName\"><user editable=\"true\" itemid=\"{user_changing_req['id']}\" field=\"displayName\">@{username}</user></span>"
61+
requirement = f"<span class=\"quill-autocomplete\" host=\"requirement\" itemid=\"{requirement_data['id']}\" name=\"{requirement_data['identifier']}\" field=\"identifier\"><requirement editable=\"true\" itemid=\"{requirement_data['id']}\" field=\"identifier\">${requirement_data['identifier']}</requirement></span>"
62+
63+
print(requirement_data["children"])
64+
req_children = requirement_data["children"]
65+
if len(req_children)>0:
66+
for children in req_children:
67+
68+
discussionData = {
69+
"title" : user + " -> Parent of "+ requirement + " was updated",
70+
"project": DEFAULT_VALUES["project"],
71+
"content_type" : 120,
72+
"group": 1,
73+
"object_id": children,
74+
}
75+
createdDiscussion = api.post('discussions/', data=discussionData)
76+
77+
print("Discussions created for each Children")
78+
79+
pass
80+
81+
if __name__=='__main__':
82+
main()

templates/newtask.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from valispace import API
2+
from datetime import datetime, timedelta
3+
4+
5+
6+
VALISPACE = {
7+
'domain': 'https://demonstration.valispace.com/',
8+
'username': 'AutomationsAPI',
9+
'password': 'AutomationsAPI'
10+
}
11+
12+
DEFAULT_VALUES = {
13+
"project": 24,
14+
"start_date": "",
15+
"today_date": ""
16+
}
17+
18+
def main(**kwargs):
19+
20+
api = API(
21+
url = VALISPACE.get('domain'),
22+
username = VALISPACE.get('username'),
23+
password = VALISPACE.get('password')
24+
)
25+
DEFAULT_VALUES["start_date"] = api.get('project/'+str(DEFAULT_VALUES["project"]))['start_date']
26+
DEFAULT_VALUES["today_date"] = datetime.now().strftime("%Y-%m-%d")
27+
print (DEFAULT_VALUES["today_date"])
28+
print(kwargs)
29+
30+
#object_id = triggers[0]['id']
31+
32+
taskData = {
33+
"title" : "testing Task Creation",
34+
"project": DEFAULT_VALUES["project"],
35+
"description" : "this is the description of the testing tasks",
36+
"duration" : 5,
37+
"duration_unit" : "days",
38+
"start_date" : DEFAULT_VALUES["today_date"],
39+
"content_type" : 38,
40+
#"object_id": object_id,
41+
"object_id": 13700,
42+
"public" : True
43+
}
44+
createdTask = api.post('user-tasks/', data=taskData)
45+
updatedTask = api.request('PUT', 'user-tasks/'+str(createdTask['id'])+"/add-member/", data={"member": "5"})
46+
47+
pass
48+
49+
if __name__=='__main__':
50+
main()

templates/requirementstats.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import csv, json
2+
from valispace import API
3+
4+
import warnings
5+
import os
6+
import urllib.request
7+
import time
8+
9+
from ast import Str
10+
from collections.abc import Callable
11+
from xmlrpc.client import Boolean
12+
13+
VALISPACE = {
14+
'domain': 'https://demonstration.valispace.com/',
15+
'username': 'AutomationsAPI',
16+
'password': 'AutomationsAPI'
17+
}
18+
19+
DEFAULT_VALUES = {
20+
"project": 24,
21+
}
22+
23+
def get_map(api: API, endpoint: Str = "/", name: Str = "id", name_map_func: Callable[[Str], Boolean] = None, filter_func: Callable[[Str], Boolean]= None):
24+
"""
25+
Function that given an endpoint returns a dict with specific keys.
26+
If function is provided it generates the key. name_map_func must receive an object instance.
27+
Otherwise key will be the property of each object specified in name.
28+
"""
29+
mapping = {}
30+
if not name_map_func:
31+
name_map_func = lambda x: x[name]
32+
for inst in api.get(endpoint):
33+
if filter_func and not filter_func(inst):
34+
# Filtered out
35+
continue
36+
37+
key = name_map_func(inst)
38+
if not mapping.get(key):
39+
mapping[key] = inst
40+
else:
41+
warnings.warn(f"Warning ---> Key: {key} already has an object. Some data may be lost in mapping.")
42+
return mapping
43+
44+
def main(**kwargs):
45+
46+
api = API(
47+
url = VALISPACE.get('domain'),
48+
username = VALISPACE.get('username'),
49+
password = VALISPACE.get('password')
50+
)
51+
52+
all_project_requirements = get_map(api, f"requirements/complete/?project="+str(DEFAULT_VALUES["project"]), "id")
53+
54+
nr_reqs = 0 #15093
55+
nr_req_w_children = 0 #15094
56+
nr_req_w_vm = 0 #15095
57+
nr_req_vm_analysis = 0 #15099
58+
nr_req_vm_review = 0 #15100
59+
nr_req_vm_inspection = 0 #15101
60+
nr_req_vm_rules = 0 #15102
61+
nr_req_vm_tests = 0 #15103
62+
nr_req_state_draft = 0 #15096
63+
nr_req_state_in_review = 0 #15097
64+
nr_req_state_final = 0 #15098
65+
nr_req_verified = 0 #15104
66+
nr_req_not_verified = 0 #15105
67+
68+
nr_reqs = len(all_project_requirements)
69+
70+
for requirement in all_project_requirements:
71+
req_data=all_project_requirements[requirement]
72+
73+
if req_data['verified'] == True:
74+
nr_req_verified += 1
75+
elif req_data['verified'] == False:
76+
nr_req_not_verified += 1
77+
78+
if req_data['total_children'] != 0:
79+
nr_req_w_children += 1
80+
81+
if req_data['state'] == 7: #but for other projects we need to get by state name
82+
nr_req_state_draft += 1
83+
elif req_data['state'] == 8:
84+
nr_req_state_in_review += 1
85+
elif req_data['state'] == 9:
86+
nr_req_state_final += 1
87+
88+
if req_data['verification_methods'] != None:
89+
nr_req_w_vm += 1
90+
for vm in req_data['verification_methods'] :
91+
if vm['method'] != None:
92+
vm_name = vm['method']['name']
93+
if vm_name == "Analysis":
94+
nr_req_vm_analysis += 1
95+
elif vm_name == "Inspection":
96+
nr_req_vm_inspection += 1
97+
elif vm_name == "Review":
98+
nr_req_vm_review += 1
99+
elif vm_name == "Rules":
100+
nr_req_vm_rules += 1
101+
elif vm_name == "Test":
102+
nr_req_vm_tests += 1
103+
104+
print("going to patch")
105+
time.sleep(15)
106+
107+
api.request("patch", "valis/15093/", data={"formula":nr_reqs})
108+
api.request("patch", "valis/15094/", data={"formula":nr_req_w_children})
109+
api.request("patch", "valis/15095/", data={"formula":nr_req_w_vm})
110+
api.request("patch", "valis/15096/", data={"formula":nr_req_state_draft})
111+
api.request("patch", "valis/15097/", data={"formula":nr_req_state_in_review})
112+
api.request("patch", "valis/15098/", data={"formula":nr_req_state_final})
113+
api.request("patch", "valis/15099/", data={"formula":nr_req_vm_analysis})
114+
api.request("patch", "valis/15100/", data={"formula":nr_req_vm_review})
115+
api.request("patch", "valis/15101/", data={"formula":nr_req_vm_inspection})
116+
api.request("patch", "valis/15102/", data={"formula":nr_req_vm_rules})
117+
api.request("patch", "valis/15103/", data={"formula":nr_req_vm_tests})
118+
api.request("patch", "valis/15104/", data={"formula":nr_req_verified})
119+
api.request("patch", "valis/15105/", data={"formula":nr_req_not_verified})
120+
121+
pass
122+
123+
if __name__=='__main__':
124+
main()

templates/simpleconnectiontest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from valispace import API
2+
from datetime import datetime, timedelta
3+
import time
4+
5+
VALISPACE = {
6+
'domain': 'https://demonstration.valispace.com/',
7+
'username': 'AutomationsAPI',
8+
'password': 'AutomationsAPI'
9+
}
10+
11+
DEFAULT_VALUES = {
12+
"project": 24,
13+
"start_date": "",
14+
"today_date": ""
15+
}
16+
17+
def main(**kwargs):
18+
19+
api = API(
20+
url = VALISPACE.get('domain'),
21+
username = VALISPACE.get('username'),
22+
password = VALISPACE.get('password')
23+
#warn_https=False
24+
)
25+
DEFAULT_VALUES["start_date"] = api.get('project/'+str(DEFAULT_VALUES["project"]))['start_date']
26+
DEFAULT_VALUES["today_date"] = datetime.now().strftime("%Y-%m-%d")
27+
print (DEFAULT_VALUES["today_date"])
28+
time.sleep(60)
29+
print(kwargs)
30+
31+
pass
32+
33+
if __name__=='__main__':
34+
main()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function [response] = main(data)
2+
%% This is the first function to be called when executing the script
3+
% -----------------------------------------------------------------
4+
% Data: Scalar structure with data comming from Valispace.
5+
%
6+
% Example of use:
7+
% mass = data.mass
8+
% ------------------------------------------------------------------
9+
% Response: Scalar structure with data to send to Valispace.
10+
%
11+
% Example of use:
12+
% response = struct()
13+
% response.total_mass = data.mass * 10
14+
% response.double_mass = data.mass * 2
15+
% ------------------------------------------------------------------
16+
17+
% Get inputs from Valispace
18+
P_in = data.P_in;
19+
V_in = data.V_in;
20+
21+
% Previous code from simulation
22+
V_in = 1.2 ;
23+
F_1 = 25 ;
24+
PV_1 = P_in*V_in;
25+
m_dot = 6;
26+
density = 5.761;
27+
A_inlet = m_dot / (density*V_in);
28+
A_2 = A_inlet/2.5;
29+
m_dot2 = 5;
30+
V_2 = m_dot2/(density*A_2);
31+
P_2 = PV_1/V_2;
32+
A_orifice = F_1/P_2;
33+
PV_2 = P_2*V_2;
34+
V_out = 70.58882;
35+
Pout = PV_2/V_out;
36+
37+
% Send outputs back to Valispace
38+
response = struct()
39+
response.Pout = Pout;
40+
41+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pathlib import Path
2+
from typing import Dict, Any
3+
import oct2py # pylint: disable=import-error
4+
5+
6+
def main(**kwargs) -> Dict[str, Any]:
7+
"""
8+
This is the main function to execute your script and it must exists.
9+
You shouldn't modify this function unless you are familiar with oct2py.
10+
11+
Other functions and files can be also created. You have at your disposal
12+
to import Valispace API, oct2py, scipy, numpy and pint.
13+
14+
:param kwargs: Dictionary with data received from Valispace.
15+
:type kwargs: Dict[str, Any]
16+
17+
:return: Dictionary with data to send back to Valispace.
18+
:rtype: Dict[str, Any]
19+
"""
20+
# Initialize octave and add all .m files to octave
21+
octave = oct2py.Oct2Py()
22+
octave.addpath(str(Path(__file__).resolve().parent))
23+
24+
# Run main function from main.m and send response back to Valispace
25+
return octave.main(kwargs)

0 commit comments

Comments
 (0)