浏览代码

Add downloads and object repr's

Matthias Vogelgesang 9 年之前
父节点
当前提交
7f96f48856
共有 5 个文件被更改,包括 112 次插入11 次删除
  1. 42 0
      client.py
  2. 20 9
      dm/client.py
  3. 30 0
      dm/models.py
  4. 18 0
      output
  5. 2 2
      reco.py

+ 42 - 0
client.py

@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+import dm
+import dm.models
+import os
+import logging
+import logging.config
+from xml.etree import ElementTree
+
+
+logging.config.fileConfig("log.ini")
+logging.getLogger('urllib3').setLevel(logging.WARN)
+
+client = dm.Client(token="TjMgcEtjGyL2qXZ4", token_secret="xBJS0Oq9j5Hjc8Kq")
+
+user = client.get_user(8)
+logging.info("{} token={} secret={}".format(user, user.credentials.key, user.credentials.secret))
+
+# for obj in client.get_objects():
+#     logging.info("object id={} uuid={}", obj.id, obj.uuid)
+
+objects = client.get_objects()
+
+ap = client.get_accesspoints('file://')[0]
+logging.info("Using {}".format(ap))
+
+# download = client.create_download(objects[1], ap)
+
+# print download.url, download.status
+
+for download in client.get_downloads():
+    logging.info("download id={} owner={} group={} url={} status={}".format(download.id, download.owner_uuid, download.group_uuid, download.url, download.status))
+
+
+# for ingest in client.get_ingests():
+#     logging.info("Found ingest={} [status={}]".format(ingest.id, ingest.status))
+
+#     if ingest.id == 30151:
+#         ingest.status = dm.models.IngestStatus.PRE_INGEST_RUNNING
+#         logging.info(" status={}".format(ingest.status))
+
+#         ingest.status = dm.models.IngestStatus.PRE_INGEST_FINISHED
+#         logging.info(" status={}".format(ingest.status))

+ 20 - 9
dm/client.py

@@ -3,7 +3,8 @@ import requests
 import logging
 from xml.etree import ElementTree
 from requests_oauthlib import OAuth1
-from .models import (Object, Ingest, Investigation, User, AccessPoint)
+from .models import (Object, Download, Ingest, Investigation, User,
+        AccessPoint)
 from .exceptions import (Timeout, PermissionError, NotFoundError,
         ArbitraryError, InvalidInput)
 
@@ -60,19 +61,29 @@ class Client(object):
     def get_objects(self, limit=None):
         return [Object(self, oid) for oid in self.get_object_ids(limit)]
 
-    def create_object(self, investigation_id, uploader_id, label=None, note=None):
+    def create_object(self, investigation_id, uploader, label=None, note=None):
         url = self.url('basemetadata/investigations', investigation_id, 'digitalObjects')
-        return Object(self.post(url, label=label, uploaderId=uploader_id, note=note))
+        return Object(self.post(url, label=label, uploaderId=uploader.id, note=note))
 
-    def delete_object(self, object_id):
-        self.delete(self.url('basemetadata/digitalObjects', object_id))
+    def delete_object(self, obj):
+        self.delete(self.url('basemetadata/digitalObjects', obj.id))
 
-    def create_ingest(self, object_id, accesspoint_uuid):
+    def get_download_ids(self, limit=None):
+        return self.get_collection('staging/downloads', 'id', limit)
+
+    def get_downloads(self, limit=None):
+        return [Download(self, oid) for oid in self.get_download_ids(limit)]
+
+    def create_download(self, obj, accesspoint):
+        url = self.url('staging/downloads')
+        return Download(self.post(url, objectId=obj.id, accessPoint=accesspoint.uuid))
+
+    def create_ingest(self, obj, accesspoint):
         url = self.url('staging/ingests')
-        return Ingest(self.post(url, objectId=object_id, accessPoint=accesspoint_uuid))
+        return Ingest(self.post(url, objectId=obj.uuid, accessPoint=accesspoint.uuid))
 
-    def delete_ingest(self, ingest_id):
-        self.delete(self.url('staging/ingests', ingest_id))
+    def delete_ingest(self, ingest):
+        self.delete(self.url('staging/ingests', ingest.id))
 
     def get_accesspoint_ids(self):
         return self.get_collection('staging/accesspoints', 'id')

+ 30 - 0
dm/models.py

@@ -5,6 +5,12 @@ def map_entity_tags(root):
     return {e.tag: e.text for e in root.findall('./entities/entity/*')}
 
 
+
+class Layout(object):
+    def __init__(self, client, oid):
+        root = client.get(client.url('dataorganization/organization', oid))
+
+
 class Object(object):
     def __init__(self, client, oid):
         root = client.get(client.url('basemetadata/digitalObjects', oid))
@@ -16,6 +22,9 @@ class Object(object):
         node = root.find('entities/entity/uploader/userId')
         self.uploader = node.text if node is not None else None
 
+    def __repr__(self):
+        return '<Object:id={} uuid={}>'.format(self.id, self.uuid)
+
 
 class Credentials(object):
     def __init__(self, root):
@@ -48,6 +57,9 @@ class Ingest(object):
         self.object_uuid = entities.get('digitalObjectUuid')
         self.owner_uuid = entities.get('owernUuid')
 
+    def __repr__(self):
+        return '<Ingest:id={} owner_uuid={} url={}>'.format(self.id, self.owner_uuid, self.staging_url)
+
     @property
     def status(self):
         entities = map_entity_tags(self.client.get(self.client.url('staging/ingests', self.id)))
@@ -73,9 +85,13 @@ class Investigation(object):
 class AccessPoint(object):
     def __init__(self, client, oid):
         entities = map_entity_tags(client.get(client.url('staging/accesspoints', oid)))
+        self.id = oid
         self.url = entities['remoteBaseUrl']
         self.uuid = entities['uniqueIdentifier']
 
+    def __repr__(self):
+        return '<AccessPoint:id={} uuid={} url={}>'.format(self.id, self.uuid, self.url)
+
 
 class User(object):
     def __init__(self, client, oid):
@@ -88,3 +104,17 @@ class User(object):
         url = self.client.url('information/properties')
         response = client.get(url, userId=self.ldap_id)
         self.credentials = Credentials(response)
+
+    def __repr__(self):
+        return '<User:id={} ldap_id={}>'.format(self.id, self.ldap_id)
+
+
+class Download(object):
+    def __init__(self, client, oid):
+        root = client.get(client.url('staging/downloads', oid))
+        entities = map_entity_tags(root)
+        self.id = int(entities['id'])
+        self.owner_uuid = entities['ownerUuid']
+        self.group_uuid = entities['groupUuid']
+        self.url = entities['clientAccessUrl']
+        self.status = entities['status']

+ 18 - 0
output

@@ -0,0 +1,18 @@
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/usergroup/users/8
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/information/properties
+[ INFO    ] 2015-05-06T12:33:59 user id=8 ldap_id=3318 token=TjMgcEtjGyL2qXZ4 secret=xBJS0Oq9j5Hjc8Kq
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/15
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/23
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/9
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/10
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/12
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/16
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/8
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/25
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/22
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/basemetadata/digitalObjects/7
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/staging/accesspoints
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/staging/accesspoints/1
+[ INFO    ] 2015-05-06T12:33:59 accesspoint url=file:///mnt/processing/noldap/staging/ uuid=fd5d3a15-8f0d-4a10-8860-31b5dd5131ff
+[ DEBUG   ] 2015-05-06T12:33:59 GET http://kitdm.anka.kit.edu:8080/KITDM/rest/staging/downloads

+ 2 - 2
reco.py

@@ -119,10 +119,10 @@ class Application(object):
             user = self.client.get_user(obj.uploader)
             user_client = dm.Client(token=user.credentials.key, token_secret=user.credentials.secret)
 
-            new_obj = user_client.create_object(obj.investigation_id, user.id, label='foo2')
+            new_obj = user_client.create_object(obj.investigation_id, user, label='foo2')
             self.log.info(" Created new object={} [uuid={}]".format(new_obj.id, new_obj.uuid))
 
-            new_ingest = user_client.create_ingest(new_obj.uuid, self.accesspoint.uuid)
+            new_ingest = user_client.create_ingest(new_obj, self.accesspoint)
 
             # get the ingest again to acccess the staging url ...
             new_ingest = user_client.get_ingest(new_ingest.id)