|
17 | 17 |
|
18 | 18 | logging.basicConfig(level=logging.INFO) |
19 | 19 |
|
20 | | -formats = icat.dumpfile.Backends.keys() |
21 | | -config = icat.config.Config() |
22 | | -config.add_variable('file', ("-o", "--outputfile"), |
23 | | - dict(help="output file name or '-' for stdout"), |
24 | | - default='-') |
25 | | -config.add_variable('format', ("-f", "--format"), |
26 | | - dict(help="output file format", choices=formats), |
27 | | - default='YAML') |
28 | | -config.add_variable('investigation', ("investigation",), |
29 | | - dict(help="name and optionally visit id " |
30 | | - "(separated by a colon) of the investigation")) |
31 | | -client, conf = config.getconfig() |
32 | | - |
33 | | -if client.apiversion < '4.4': |
34 | | - raise RuntimeError("Sorry, ICAT version %s is too old, need 4.4.0 or newer." |
35 | | - % client.apiversion) |
36 | | -client.login(conf.auth, conf.credentials) |
37 | | - |
38 | | - |
39 | 20 | # ------------------------------------------------------------ |
40 | 21 | # helper |
41 | 22 | # ------------------------------------------------------------ |
42 | 23 |
|
43 | | -def getinvestigation(invid): |
| 24 | +def get_investigation_id(client, invid): |
44 | 25 | """Search the investigation id from name and optionally visitid.""" |
| 26 | + query = Query(client, "Investigation", attributes=["id"]) |
45 | 27 | l = invid.split(':') |
46 | | - if len(l) == 1: |
47 | | - # No colon, invid == name |
48 | | - searchexp = "Investigation.id [name='%s']" % tuple(l) |
49 | | - elif len(l) == 2: |
| 28 | + query.addConditions({"name": "= '%s'" % l[0]}) |
| 29 | + if len(l) == 2: |
50 | 30 | # one colon, invid == name:visitId |
51 | | - searchexp = "Investigation.id [name='%s' AND visitId='%s']" % tuple(l) |
| 31 | + query.addConditions({"visitId": "= '%s'" % l[1]}) |
52 | 32 | else: |
53 | 33 | # too many colons |
54 | 34 | raise RuntimeError("Invalid investigation identifier '%s'" % invid) |
55 | | - return (client.assertedSearch(searchexp)[0]) |
| 35 | + return client.assertedSearch(query)[0] |
56 | 36 |
|
57 | | -def mergesearch(sexps): |
| 37 | +def mergesearch(client, queries): |
58 | 38 | """Do many searches and merge the results in one list excluding dups.""" |
59 | 39 | objs = set() |
60 | | - for se in sexps: |
| 40 | + for se in queries: |
61 | 41 | objs.update(client.search(se)) |
62 | 42 | return list(objs) |
63 | 43 |
|
| 44 | +# The following helper functions control what ICAT objects are written |
| 45 | +# in each of the dumpfile chunks. There are three options for the |
| 46 | +# items in each list: either queries expressed as Query objects, or |
| 47 | +# queries expressed as string expressions, or lists of objects. In |
| 48 | +# the first two cases, the search results will be written, in the last |
| 49 | +# case, the objects are written as provided. |
| 50 | + |
| 51 | +def get_auth_types(client, invid): |
| 52 | + """Users and groups related to the investigation. |
| 53 | + """ |
| 54 | + # We need the users related to our investigation via |
| 55 | + # InvestigationUser, the users member of one of the groups related |
| 56 | + # via InvestigationGroup, and the instrument scientists from the |
| 57 | + # instruments related to the investigations. These are |
| 58 | + # independent searches, but the results are likely to overlap. So |
| 59 | + # we need to search and merge results first. |
| 60 | + usersearch = [ |
| 61 | + Query(client, "User", conditions={ |
| 62 | + "investigationUsers." |
| 63 | + "investigation.id": "= %d" % invid, |
| 64 | + }), |
| 65 | + Query(client, "User", conditions={ |
| 66 | + "userGroups.grouping.investigationGroups." |
| 67 | + "investigation.id": "= %d" % invid, |
| 68 | + }), |
| 69 | + Query(client, "User", conditions={ |
| 70 | + "instrumentScientists.instrument.investigationInstruments." |
| 71 | + "investigation.id": "= %d" % invid, |
| 72 | + }), |
| 73 | + ] |
| 74 | + return [ |
| 75 | + mergesearch(client, usersearch), |
| 76 | + Query(client, "Grouping", conditions={ |
| 77 | + "investigationGroups.investigation.id": "= %d" % invid, |
| 78 | + }, includes=["userGroups.user"], aggregate="DISTINCT", order=True), |
| 79 | + ] |
| 80 | + |
| 81 | +def get_static_types(client, invid): |
| 82 | + """Static stuff that exists independently of the investigation in ICAT. |
| 83 | + """ |
| 84 | + # Similar situation for ParameterType as for User: need to merge |
| 85 | + # ParameterType used for InvestigationParameter, SampleParameter, |
| 86 | + # DatasetParameter, and DatafileParameter. |
| 87 | + ptsearch = [ |
| 88 | + Query(client, "ParameterType", conditions={ |
| 89 | + "investigationParameters." |
| 90 | + "investigation.id": "= %d" % invid, |
| 91 | + }, includes=["facility", "permissibleStringValues"]), |
| 92 | + Query(client, "ParameterType", conditions={ |
| 93 | + "sampleParameters.sample." |
| 94 | + "investigation.id": "= %d" % invid, |
| 95 | + }, includes=["facility", "permissibleStringValues"]), |
| 96 | + Query(client, "ParameterType", conditions={ |
| 97 | + "datasetParameters.dataset." |
| 98 | + "investigation.id": "= %d" % invid, |
| 99 | + }, includes=["facility", "permissibleStringValues"]), |
| 100 | + Query(client, "ParameterType", conditions={ |
| 101 | + "datafileParameters.datafile.dataset." |
| 102 | + "investigation.id": "= %d" % invid, |
| 103 | + }, includes=["facility", "permissibleStringValues"]), |
| 104 | + ] |
| 105 | + return [ |
| 106 | + Query(client, "Facility", |
| 107 | + conditions={ |
| 108 | + "investigations.id": "= %d" % invid, |
| 109 | + }, |
| 110 | + order=True), |
| 111 | + Query(client, "Instrument", |
| 112 | + conditions={ |
| 113 | + "investigationInstruments.investigation.id": "= %d" % invid, |
| 114 | + }, |
| 115 | + includes=["facility", "instrumentScientists.user"], |
| 116 | + order=True), |
| 117 | + mergesearch(client, ptsearch), |
| 118 | + Query(client, "InvestigationType", |
| 119 | + conditions={ |
| 120 | + "investigations.id": "= %d" % invid, |
| 121 | + }, |
| 122 | + includes=["facility"], |
| 123 | + order=True), |
| 124 | + Query(client, "SampleType", |
| 125 | + conditions={ |
| 126 | + "samples.investigation.id": "= %d" % invid, |
| 127 | + }, |
| 128 | + includes=["facility"], |
| 129 | + aggregate="DISTINCT", |
| 130 | + order=True), |
| 131 | + Query(client, "DatasetType", |
| 132 | + conditions={ |
| 133 | + "datasets.investigation.id": "= %d" % invid, |
| 134 | + }, |
| 135 | + includes=["facility"], |
| 136 | + aggregate="DISTINCT", |
| 137 | + order=True), |
| 138 | + Query(client, "DatafileFormat", |
| 139 | + conditions={ |
| 140 | + "datafiles.dataset.investigation.id": "= %d" % invid, |
| 141 | + }, |
| 142 | + includes=["facility"], |
| 143 | + aggregate="DISTINCT", |
| 144 | + order=True), |
| 145 | + ] |
| 146 | + |
| 147 | +def get_investigation_types(client, invid): |
| 148 | + """The investigation and all the stuff that belongs to it. |
| 149 | + """ |
| 150 | + # The set of objects to be included in the Investigation. |
| 151 | + inv_includes = { |
| 152 | + "facility", "type.facility", "investigationInstruments", |
| 153 | + "investigationInstruments.instrument.facility", "shifts", |
| 154 | + "keywords", "publications", "investigationUsers", |
| 155 | + "investigationUsers.user", "investigationGroups", |
| 156 | + "investigationGroups.grouping", "parameters", |
| 157 | + "parameters.type.facility" |
| 158 | + } |
| 159 | + return [ |
| 160 | + Query(client, "Investigation", |
| 161 | + conditions={"id":"in (%d)" % invid}, |
| 162 | + includes=inv_includes), |
| 163 | + Query(client, "Sample", |
| 164 | + conditions={"investigation.id":"= %d" % invid}, |
| 165 | + includes={"investigation", "type.facility", |
| 166 | + "parameters", "parameters.type.facility"}, |
| 167 | + order=True), |
| 168 | + Query(client, "Dataset", |
| 169 | + conditions={"investigation.id":"= %d" % invid}, |
| 170 | + includes={"investigation", "type.facility", "sample", |
| 171 | + "parameters", "parameters.type.facility"}, |
| 172 | + order=True), |
| 173 | + Query(client, "Datafile", |
| 174 | + conditions={"dataset.investigation.id":"= %d" % invid}, |
| 175 | + includes={"dataset", "datafileFormat.facility", |
| 176 | + "parameters", "parameters.type.facility"}, |
| 177 | + order=True) |
| 178 | + ] |
64 | 179 |
|
65 | 180 | # ------------------------------------------------------------ |
66 | 181 | # Do it |
67 | 182 | # ------------------------------------------------------------ |
68 | 183 |
|
69 | | -invid = getinvestigation(conf.investigation) |
70 | | - |
| 184 | +formats = icat.dumpfile.Backends.keys() |
| 185 | +config = icat.config.Config() |
| 186 | +config.add_variable('file', ("-o", "--outputfile"), |
| 187 | + dict(help="output file name or '-' for stdout"), |
| 188 | + default='-') |
| 189 | +config.add_variable('format', ("-f", "--format"), |
| 190 | + dict(help="output file format", choices=formats), |
| 191 | + default='YAML') |
| 192 | +config.add_variable('investigation', ("investigation",), |
| 193 | + dict(help="name and optionally visit id " |
| 194 | + "(separated by a colon) of the investigation")) |
| 195 | +client, conf = config.getconfig() |
71 | 196 |
|
72 | | -# We need the users related to our investigation via |
73 | | -# InvestigationUser, the users member of one of the groups related via |
74 | | -# InvestigationGroup, and the instrument scientists from the |
75 | | -# instruments related to the investigations. These are independent |
76 | | -# searches, but the results are likely to overlap. So we need to |
77 | | -# search and merge results first. Similar situation for ParameterType. |
78 | | -usersearch = [("User <-> InvestigationUser <-> Investigation [id=%d]"), |
79 | | - ("User <-> UserGroup <-> Grouping <-> InvestigationGroup " |
80 | | - "<-> Investigation [id=%d]"), |
81 | | - ("User <-> InstrumentScientist <-> Instrument " |
82 | | - "<-> InvestigationInstrument <-> Investigation [id=%d]")] |
83 | | -ptsearch = [("ParameterType INCLUDE Facility, PermissibleStringValue " |
84 | | - "<-> InvestigationParameter <-> Investigation [id=%d]"), |
85 | | - ("ParameterType INCLUDE Facility, PermissibleStringValue " |
86 | | - "<-> SampleParameter <-> Sample <-> Investigation [id=%d]"), |
87 | | - ("ParameterType INCLUDE Facility, PermissibleStringValue " |
88 | | - "<-> DatasetParameter <-> Dataset <-> Investigation [id=%d]"), |
89 | | - ("ParameterType INCLUDE Facility, PermissibleStringValue " |
90 | | - "<-> DatafileParameter <-> Datafile <-> Dataset " |
91 | | - "<-> Investigation [id=%d]"), ] |
| 197 | +if client.apiversion < '4.4': |
| 198 | + raise RuntimeError("Sorry, ICAT version %s is too old, need 4.4.0 or newer." |
| 199 | + % client.apiversion) |
| 200 | +client.login(conf.auth, conf.credentials) |
92 | 201 |
|
93 | | -# The set of objects to be included in the Investigation. |
94 | | -inv_includes = { "facility", "type.facility", "investigationInstruments", |
95 | | - "investigationInstruments.instrument.facility", "shifts", |
96 | | - "keywords", "publications", "investigationUsers", |
97 | | - "investigationUsers.user", "investigationGroups", |
98 | | - "investigationGroups.grouping", "parameters", |
99 | | - "parameters.type.facility" } |
100 | 202 |
|
101 | | -# The following lists control what ICAT objects are written in each of |
102 | | -# the dumpfile chunks. There are three options for the items in each |
103 | | -# list: either queries expressed as Query objects, or queries |
104 | | -# expressed as string expressions, or lists of objects. In the first |
105 | | -# two cases, the seacrh results will be written, in the last case, the |
106 | | -# objects are written as provided. We assume that there is only one |
107 | | -# relevant facility, e.g. that all objects related to the |
108 | | -# investigation are related to the same facility. We may thus ommit |
109 | | -# the facility from the ORDER BY clauses. |
110 | | -authtypes = [mergesearch([s % invid for s in usersearch]), |
111 | | - ("Grouping ORDER BY name INCLUDE UserGroup, User " |
112 | | - "<-> InvestigationGroup <-> Investigation [id=%d]" % invid)] |
113 | | -statictypes = [("Facility ORDER BY name"), |
114 | | - ("Instrument ORDER BY name " |
115 | | - "INCLUDE Facility, InstrumentScientist, User " |
116 | | - "<-> InvestigationInstrument <-> Investigation [id=%d]" |
117 | | - % invid), |
118 | | - (mergesearch([s % invid for s in ptsearch])), |
119 | | - ("InvestigationType ORDER BY name INCLUDE Facility " |
120 | | - "<-> Investigation [id=%d]" % invid), |
121 | | - ("SampleType ORDER BY name, molecularFormula INCLUDE Facility " |
122 | | - "<-> Sample <-> Investigation [id=%d]" % invid), |
123 | | - ("DatasetType ORDER BY name INCLUDE Facility " |
124 | | - "<-> Dataset <-> Investigation [id=%d]" % invid), |
125 | | - ("DatafileFormat ORDER BY name, version INCLUDE Facility " |
126 | | - "<-> Datafile <-> Dataset <-> Investigation [id=%d]" % invid)] |
127 | | -investtypes = [Query(client, "Investigation", |
128 | | - conditions={"id":"in (%d)" % invid}, |
129 | | - includes=inv_includes), |
130 | | - Query(client, "Sample", order=["name"], |
131 | | - conditions={"investigation.id":"= %d" % invid}, |
132 | | - includes={"investigation", "type.facility", |
133 | | - "parameters", "parameters.type.facility"}), |
134 | | - Query(client, "Dataset", order=["name"], |
135 | | - conditions={"investigation.id":"= %d" % invid}, |
136 | | - includes={"investigation", "type.facility", "sample", |
137 | | - "parameters", "parameters.type.facility"}), |
138 | | - Query(client, "Datafile", order=["dataset.name", "name"], |
139 | | - conditions={"dataset.investigation.id":"= %d" % invid}, |
140 | | - includes={"dataset", "datafileFormat.facility", |
141 | | - "parameters", "parameters.type.facility"})] |
| 203 | +invid = get_investigation_id(client, conf.investigation) |
142 | 204 |
|
143 | 205 | with open_dumpfile(client, conf.file, conf.format, 'w') as dumpfile: |
144 | | - dumpfile.writedata(authtypes) |
145 | | - dumpfile.writedata(statictypes) |
146 | | - dumpfile.writedata(investtypes) |
| 206 | + dumpfile.writedata(get_auth_types(client, invid)) |
| 207 | + dumpfile.writedata(get_static_types(client, invid)) |
| 208 | + dumpfile.writedata(get_investigation_types(client, invid)) |
0 commit comments