UbuntuOneFilesNotes11.10

Attachment '7.py'

Download

   1 #!/usr/bin/python
   2 
   3 import sys
   4 
   5 _login_success = False
   6 def login():
   7   from gobject import MainLoop
   8   from dbus.mainloop.glib import DBusGMainLoop
   9   from ubuntuone.platform.credentials import CredentialsManagementTool
  10 
  11   global _login_success
  12   _login_success = False
  13 
  14   DBusGMainLoop(set_as_default=True)
  15   loop = MainLoop()
  16 
  17   def quit(result):
  18     global _login_success
  19     loop.quit()
  20     if result:
  21 	    _login_success = True
  22 
  23   cd = CredentialsManagementTool()
  24   d = cd.login()
  25   d.addCallbacks(quit)
  26   loop.run()
  27   if not _login_success:
  28     sys.exit(1)
  29 
  30 def logout():
  31   from gobject import MainLoop
  32   from dbus.mainloop.glib import DBusGMainLoop
  33   from ubuntuone.platform.credentials import CredentialsManagementTool
  34 
  35   DBusGMainLoop(set_as_default=True)
  36   loop = MainLoop()
  37 
  38   def quit(result):
  39     loop.quit()
  40 
  41   cd = CredentialsManagementTool()
  42   d = cd.clear_credentials()
  43   d.addCallbacks(quit)
  44   loop.run()
  45 
  46 def create_volume(path):
  47   import ubuntuone.couch.auth as auth
  48   import urllib
  49   base = "https://one.ubuntu.com/api/file_storage/v1/volumes/~/"
  50   auth.request(base + urllib.quote(path), http_method="PUT")
  51 
  52 def put(local, remote):
  53   import json
  54   import ubuntuone.couch.auth as auth
  55   import mimetypes
  56   import urllib
  57 
  58   # Create remote path (which contains volume path)
  59   base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  60   answer = auth.request(base + urllib.quote(remote),
  61                         http_method="PUT",
  62                         request_body='{"kind":"file"}')
  63   node = json.loads(answer[1])
  64 
  65   # Read info about local file
  66   data = bytearray(open(local, 'rb').read())
  67   size = len(data)
  68   content_type = mimetypes.guess_type(local)[0]
  69   content_type = content_type or 'application/octet-stream'
  70   headers = {"Content-Length": str(size),
  71              "Content-Type": content_type}
  72 
  73   # Upload content of local file to content_path from original response
  74   base = "https://files.one.ubuntu.com"
  75   url = base + urllib.quote(node.get('content_path'), safe="/~")
  76   auth.request(url, http_method="PUT",
  77                headers=headers, request_body=data)
  78 
  79 def get(remote, local):
  80   import json
  81   import ubuntuone.couch.auth as auth
  82   import urllib
  83 
  84   # Request metadata
  85   base = "https://one.ubuntu.com/api/file_storage/v1/~/"
  86   answer = auth.request(base + urllib.quote(remote))
  87   node = json.loads(answer[1])
  88 
  89   # Request content
  90   base = "https://files.one.ubuntu.com"
  91   url = base + urllib.quote(node.get('content_path'), safe="/~")
  92   answer = auth.request(url)
  93   f = open(local, 'wb')
  94   f.write(answer[1])
  95 
  96 def get_children(path):
  97   import json
  98   import ubuntuone.couch.auth as auth
  99   import urllib
 100 
 101   # Request children metadata
 102   base = "https://one.ubuntu.com/api/file_storage/v1/~/"
 103   url = base + urllib.quote(path) + "?include_children=true"
 104   answer = auth.request(url)
 105 
 106   # Create file list out of json data
 107   filelist = []
 108   node = json.loads(answer[1])
 109   if node.get('has_children') == True:
 110     for child in node.get('children'):
 111       child_path = urllib.unquote(child.get('path')).lstrip('/')
 112       filelist += [child_path]
 113   print filelist
 114 
 115 def query(path):
 116   import json
 117   import ubuntuone.couch.auth as auth
 118   import urllib
 119 
 120   # Request metadata
 121   base = "https://one.ubuntu.com/api/file_storage/v1/~/"
 122   url = base + urllib.quote(path)
 123   answer = auth.request(url)
 124   node = json.loads(answer[1])
 125 
 126   # Print interesting info
 127   print 'Size:', node.get('size')
 128 
 129 if len(sys.argv) <= 1:
 130   print "Need more arguments"
 131   sys.exit(1)
 132 
 133 if sys.argv[1] == "login":
 134   login()
 135 elif sys.argv[1] == "logout":
 136   logout()
 137 elif sys.argv[1] == "create-volume":
 138   login()
 139   create_volume(sys.argv[2])
 140 elif sys.argv[1] == "put":
 141   login()
 142   put(sys.argv[2], sys.argv[3])
 143 elif sys.argv[1] == "get":
 144   login()
 145   get(sys.argv[2], sys.argv[3])
 146 elif sys.argv[1] == "list":
 147   login()
 148   get_children(sys.argv[2])
 149 elif sys.argv[1] == "query":
 150   login()
 151   query(sys.argv[2])

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-09-07 15:17:38, 0.1 KB) [[attachment:1.py]]
  • [get | view] (2011-09-07 15:17:44, 1.0 KB) [[attachment:2.py]]
  • [get | view] (2011-09-07 15:17:52, 1.3 KB) [[attachment:3.py]]
  • [get | view] (2011-09-07 15:18:00, 2.3 KB) [[attachment:4.py]]
  • [get | view] (2011-09-07 15:18:06, 2.8 KB) [[attachment:5.py]]
  • [get | view] (2011-09-07 15:18:12, 3.4 KB) [[attachment:6.py]]
  • [get | view] (2011-09-07 15:18:18, 3.8 KB) [[attachment:7.py]]
  • [get | view] (2011-09-07 15:18:24, 4.1 KB) [[attachment:u1file.py]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.