UbuntuOneFilesNotes11.10
Attachment '6.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 if len(sys.argv) <= 1:
116 print "Need more arguments"
117 sys.exit(1)
118
119 if sys.argv[1] == "login":
120 login()
121 elif sys.argv[1] == "logout":
122 logout()
123 elif sys.argv[1] == "create-volume":
124 login()
125 create_volume(sys.argv[2])
126 elif sys.argv[1] == "put":
127 login()
128 put(sys.argv[2], sys.argv[3])
129 elif sys.argv[1] == "get":
130 login()
131 get(sys.argv[2], sys.argv[3])
132 elif sys.argv[1] == "list":
133 login()
134 get_children(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.You are not allowed to attach a file to this page.