07-python-API-for-users.ipynb 22.3 KB
Newer Older
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction Python API for iRODS\n",
    "\n",
    "**Authors**\n",
    "- Arthur Newton (SURFsara)\n",
    "- Christine Staiger (SURFsara)\n",
    "- Claudia Behnke (SURFsara)\n",
    "\n",
    "**License**\n",
    "Copyright 2018 SURFsara BV\n",
    "\n",
    "Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n",
    "\n",
    "http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n",
    "\n",
    "## Goal\n",
    "You will learn how to interact with iRODS via the python API. In this module we will explore the API in interactive mode. You will:\n",
    "\n",
    "- Up and download data\n",
    "- Up and download data collections\n",
    "- Add and edit metadata\n",
    "- Set Accession control lists for data objects and collections\n",
    "- Query for data using user defined metadata\n",
    "\n",
    "## Exploring the python API to iRODS in ipython\n",
    "We prepared some data for you. Please clone the repository to  your home folder:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!git clone https://git.wur.nl/staig001/irodstraining.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%cd irodstraining/\n",
    "%ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prerequisites\n",
    "- Credentials and access to an iRODS instance\n",
    "- [python-irodsclient](https://github.com/irods/python-irodsclient)\n",
    "\n",
    "Install the *irods-pythonclient*:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install --upgrade python-irodsclient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import irods\n",
    "irods.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data objects\n",
    "### Connect to iRODS\n",
    "To connect we need information on the iRODS server, your user name and other details that the iRODS admin can provide you with.\n",
    "\n",
    "Note, in the tutorial we are working with open passwords. In real life please protect your passwords by loading them from encrypted files or getting them interactively e.g. with the getpass module in python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from irods.session import iRODSSession\n",
    "session = iRODSSession(host='scomp1447.wurnet.nl', \n",
    "                       port=1247, user='irods-user1', \n",
105
    "                       password='<your password>', zone='aliceZone', \n",
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
    "                       **{\"irods_default_hash_scheme\": \"MD5\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The main parameters needed to establish the connection are *host*, *port*, *user* and *password*. We can give other parameters which will set certain behaviour. Here for example we set that iRODS will by default create md5 checksums to verify the integrity of the data.\n",
    "Now let's list what data we have in our iRODS account:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coll = session.collections.get('/aliceZone/home/irods-user1')\n",
    "print(coll.path)\n",
    "print(coll.data_objects)\n",
    "print(coll.subcollections)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is not much or no data stored yet.\n",
    "Since we will need our home-collection path much more often in this tutorial let us save it in an extra variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iHome = coll.path"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Upload a data object\n",
    "The preferred way to upload data to iRODS is a data object *put*. \n",
    "\n",
    "Now we create the logical path and upload the German version of Alice in wonderland to iRODS. Note, we will also ensure that a checksum is created and verified:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import irods.keywords as kw\n",
    "# options for storing the checksum in the iCAT and verification of the checksum upon upload\n",
    "options = {kw.VERIFY_CHKSUM_KW: '', kw.REG_CHKSUM_KW: \"regChksum\"}\n",
    "iPath = iHome+'/Alice-DE.txt'\n",
    "session.data_objects.put('aliceInWonderland-DE.txt.utf-8', iPath, **options)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The object carries some vital system information, otherwise it is empty."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj = session.data_objects.get(iPath)\n",
    "print(\"Name: \", obj.name)\n",
    "print(\"Owner: \", obj.owner_name)\n",
    "print(\"Size: \", obj.size)\n",
    "print(\"Checksum:\", obj.checksum)\n",
    "print(\"Create: \", obj.create_time)\n",
    "print(\"Modify: \", obj.modify_time)\n",
    "print(\"Metadata: \", obj.metadata.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vars(obj)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can do actions and modifications on the data object, e.g renaming it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "session.data_objects.move(obj.path, iHome + '/Alice.txt')\n",
    "print(coll.data_objects)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating metadata\n",
    "Working with metadata is not completely intuitive, you need a good understanding of python dictionaries and the iRODS python API classes *dataobject*, *collection*, *iRODSMetaData* and *iRODSMetaCollection*.\n",
    "\n",
    "We start slowly with first creating some metadata for our data. \n",
    "Currently, our data object does not carry any user-defined metadata:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iPath = iHome + '/Alice.txt'\n",
    "obj = session.data_objects.get(iPath)\n",
    "print(obj.metadata.items())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a key, value, unit entry for our data object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "obj.metadata.add('SOURCE', 'python API training', 'version 1')\n",
    "obj.metadata.add('TYPE', 'test file')\n",
    "print(obj.metadata.items())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[(item.name, item.value, item.units) for item in obj.metadata.items()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Download a data object\n",
    "We can download a data object as follows (note that we use the environment variable 'HOME' that is defined to be your homefolder):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "options = {kw.VERIFY_CHKSUM_KW: ''}\n",
    "localpath = os.environ['HOME']+'/'+os.path.basename(obj.path)\n",
    "session.data_objects.get(obj.path,local_path=localpath, num_threads=2, **options)\n",
    "%ls /home/WUR/staig001"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Streaming data\n",
    "Streaming data is an alternative to upload large data to iRODS or to accumulate data in a data object over time. First you need to create an empty data object in iRODS beofre you can stream in the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "content = 'My contents!'.encode()\n",
    "obj = session.data_objects.create(iHome + '/stream.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This will create a place holder for the data object with no further metadata:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Name: \", obj.name)\n",
    "print(\"Owner: \", obj.owner_name)\n",
    "print(\"Size: \", obj.size)\n",
    "print(\"Checksum:\", obj.checksum)\n",
    "print(\"Create: \", obj.create_time)\n",
    "print(\"Modify: \", obj.modify_time)\n",
    "print(\"Metadata: \", obj.metadata.items())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now stream in our data into that placeholder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with obj.open('w') as obj_desc:\n",
    "    obj_desc.write(content)\n",
    "obj = session.data_objects.get(iHome + '/stream.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we check the metadata again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Name: \", obj.name)\n",
    "print(\"Owner: \", obj.owner_name)\n",
    "print(\"Size: \", obj.size)\n",
    "print(\"Checksum:\", obj.checksum)\n",
    "print(\"Create: \", obj.create_time)\n",
    "print(\"Modify: \", obj.modify_time)\n",
    "print(\"Metadata: \", obj.metadata.items())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Deleting data\n",
    "\n",
    "With the python API you can only unlink data:\n",
    "\n",
    "```py\n",
    "obj.unlink()\n",
    "```\n",
    "\n",
    "This will move the data object to its respective location under */zone/trash/home/user/obj* but it will not remove the data from the iRODS instance and will also not clean up the data storage and metadata entries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(coll.data_objects)\n",
    "obj.unlink()\n",
    "print(coll.data_objects)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## iRODS collections\n",
    "\n",
    "You can organise your data in iRODS just like on a POSIX file system.\n",
    "\n",
    "\n",
    "### Create a collection (even recursively)\n",
    "... and list its content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "session.collections.create(iHome + '/Books/Alice')\n",
    "print(coll.path)\n",
    "coll.subcollections"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can move the Alice in Wonderland text in that collection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coll = session.collections.get(iHome + '/Books/Alice')\n",
    "coll.data_objects\n",
    "session.data_objects.move(iHome + '/Alice.txt', coll.path)\n",
    "print(coll.data_objects)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Move a collection\n",
    "Just as data objects you can also move and rename collections with all their data objects and subcollections:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "session.collections.move(iHome + '/Books', iHome + '/MyBooks')\n",
    "coll = session.collections.get(iHome)\n",
    "coll.subcollections"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Remove a Collection\n",
    "Remove a collection recursively with all data objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coll = session.collections.get(iHome + '/MyBooks')\n",
    "coll.remove(recurse=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Do not be fooled, the python object 'coll' looks like as if the collection is still in iRODS. You need to refetch the collection (refresh)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coll.subcollections\n",
    "coll = session.collections.get(iHome)\n",
    "coll.subcollections"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Upload collection\n",
    "To upload a collection from the unix file system one has to iterate over the directory and create collections and data objects.\n",
    "We will upload the directory 'aliceInWonderland'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "dPath = os.environ['HOME']+'/irodstraining'+'/aliceInWonderland'\n",
    "print(dPath)\n",
    "walk = [dPath]\n",
    "print(walk)\n",
    "while len(walk) > 0:\n",
    "    for srcDir, dirs, files in os.walk(walk.pop()):\n",
    "        print(srcDir, dirs, files)\n",
    "        walk.extend(dirs)\n",
    "        iPath = iHome + srcDir.split(os.environ['HOME'])[1]\n",
    "        print(\"CREATE\", iPath)\n",
    "        newColl = session.collections.create(iPath)\n",
    "        for fname in files:\n",
    "            print(\"CREATE\", newColl.path+'/'+fname)\n",
    "            session.data_objects.put(srcDir+'/'+fname, newColl.path+'/'+fname)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Iterate over collection\n",
    "Similar to we walked over a directory with sub directories and files in the unix file system we can walk over collections and subcollections in iRODS. Here we walk over the whole aliceInWonderland collection and list Collections and Data objects:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for srcColl, colls, objs in coll.walk():\n",
    "    print('C-', srcColl.path)\n",
    "    for o in objs:\n",
    "        print(o.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sharing data\n",
    "You can set ACLs on data objects and collections in iRODS. \n",
    "To check the default ACLs do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(session.permissions.get(coll))\n",
    "print(session.permissions.get(obj))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[vars(p) for p in session.permissions.get(coll)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we share a collection with the iRODS group public. Every member of the group will have read rights."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from irods.access import iRODSAccess\n",
    "acl = iRODSAccess('read', coll.path, 'public', session.zone)\n",
    "session.permissions.set(acl)\n",
    "print(session.permissions.get(coll))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To withdraw certain ACLs do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "acl = iRODSAccess('null', coll.path, 'public', session.zone)\n",
    "session.permissions.set(acl)\n",
    "print(session.permissions.get(coll))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "One can also give 'write' access or set the 'own'ership.\n",
    "\n",
    "Collections have a special ACL, the 'inherit' ACL. If 'inherit' is set, all subcollections and data objects will inherit their ACLs from their parent collection automatically.\n",
    "\n",
    "## Searching for data in iRODS\n",
    "We will now try to find all data in this iRODS instance we have access to and which carries the key *AUTHOR* with value *Lewis Carroll*. And we need to assemble the iRODS logical path. First let us have a look at the information models that iRODS offers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vars(irods.models)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see that we have nodels for a Data object, colletcion, storage resource, user and their respective metadata (CollectionMeta, DataObjectMeta, ResourceMeta) etc.\n",
    "Now let us import some of these models and see how we can use them to search for information on the items in iRODS."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from irods.models import Collection, DataObject, CollectionMeta, DataObjectMeta"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We need the collection name and data object name of the data objects. This command will give us all data objec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = session.query(Collection.name, DataObject.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can filter the results for data objects which carry a user-defined metadata item with name 'AUTHOR' and value 'Lewis Carroll'. To this end we have to chain two filters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filteredQuery = query.filter(DataObjectMeta.name == 'AUTHOR').\\\n",
    "    filter(DataObjectMeta.value == 'Lewis Carroll')\n",
    "print(filteredQuery.all())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python prints the results neatly on the prompt, however to extract the information and parsing it to other functions is pretty complicated. Every entry you see in the output is not a string, but actually a python object with many functions. That gives you the advantage to link the output to the rows and comlumns in the sql database running in the background of iRODS. For normal user interaction, however, it needs some explanation and help.\n",
    "\n",
    "### Parsing the iquest output\n",
    "To work with the results of the query, we need to get them in an iterable format:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = filteredQuery.get_results()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Watch out**: *results* is a generator which you can only use once to iterate over.\n",
    "\n",
    "We can now iterate over the results and build our iRODS paths (*COLL_NAME/DATA_NAME*) of the data files:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iPaths = []\n",
    "\n",
    "for item in results:\n",
    "    for k in item.keys():\n",
    "        if k.icat_key == 'DATA_NAME':\n",
    "            name = item[k]\n",
    "        elif k.icat_key == 'COLL_NAME':\n",
    "            coll = item[k]\n",
    "        else:\n",
    "            continue\n",
    "    iPaths.append(coll+'/'+name)\n",
    "print('\\n'.join(iPaths))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How did we know which keys to use? \n",
    "We asked in the query for *Collection.name* and *DataObject.name*.\n",
    "Have look at these two objects:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(Collection.name.icat_key)\n",
    "print(DataObject.name.icat_key)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The *icat_key* is the keyword used in the database behind iRODS to store the information.\n",
    "\n",
    "Another example: Assume we want to check for the last date of change of the data (Watch out: there is also a DataObjectMeta.modify_time which is the last time of change of the Metadata!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "query = session.query(\n",
    "    DataObject.name, \n",
    "    DataObject.checksum, \n",
    "    DataObject.size, \n",
    "    DataObject.modify_time)\n",
    "\n",
    "filteredQuery = query.filter(DataObjectMeta.name == 'AUTHOR').\\\n",
    "    filter(DataObjectMeta.value == 'Lewis Carroll')\n",
    "print(filteredQuery.all())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The modify time is given in epoche in the overview. However, when we extract it from the iterator it is presented in datetime:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "results = filteredQuery.get_results()\n",
    "for item in results:\n",
    "    for key in item:\n",
    "        if key.icat_key == \"DATA_SIZE\":\n",
    "            print(str(item[key]/1024**3)+\" GB\")\n",
    "        print(item[key])\n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python 3.8.5",
   "language": "python",
   "name": "python3.8.5"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}