UbuntuOneFilesNotes11.10
Attachment '4.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 if len(sys.argv) <= 1:
80 print "Need more arguments"
81 sys.exit(1)
82
83 if sys.argv[1] == "login":
84 login()
85 elif sys.argv[1] == "logout":
86 logout()
87 elif sys.argv[1] == "create-volume":
88 login()
89 create_volume(sys.argv[2])
90 elif sys.argv[1] == "put":
91 login()
92 put(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.You are not allowed to attach a file to this page.