UbuntuOneFilesNotes11.10

Attachment '5.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 if len(sys.argv) <= 1:
  97   print "Need more arguments"
  98   sys.exit(1)
  99 
 100 if sys.argv[1] == "login":
 101   login()
 102 elif sys.argv[1] == "logout":
 103   logout()
 104 elif sys.argv[1] == "create-volume":
 105   login()
 106   create_volume(sys.argv[2])
 107 elif sys.argv[1] == "put":
 108   login()
 109   put(sys.argv[2], sys.argv[3])
 110 elif sys.argv[1] == "get":
 111   login()
 112   get(sys.argv[2], sys.argv[3])

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.