2
from oauth import oauth
9
description = "The Ubuntu One cloud service"
11
oauth_consumer_key = "ubuntuone"
12
oauth_consumer_secret = "hammertime"
15
"""Can we deliver information?"""
16
return get_oauth_data() is not None
20
"""Information needed to replicate to a server."""
22
if oauth_data is not None:
27
matches = gnomekeyring.find_items_sync(
28
gnomekeyring.ITEM_GENERIC_SECRET,
29
{'ubuntuone-realm': "https://ubuntuone.com",
30
'oauth-consumer-key': oauth_consumer_key})
32
# parse "a=b&c=d" to {"a":"b","c":"d"}
33
kv_list = [x.split("=", 1) for x in matches[0].secret.split("&")]
34
keys, values = zip(*kv_list)
35
keys = [k.replace("oauth_", "") for k in keys]
36
oauth_data = dict(zip(keys, values))
38
"consumer_key": oauth_consumer_key,
39
"consumer_secret": oauth_consumer_secret,
42
except ImportError, e:
43
logging.info("Can't replicate to Ubuntu One cloud without credentials."
45
except gnomekeyring.NoMatchError:
46
logging.info("This machine hasn't authorized itself to Ubuntu One; "
47
"replication to the cloud isn't possible until it has. See "
48
"'ubuntuone-client-applet'.")
49
except gnomekeyring.NoKeyringDaemonError:
50
logging.error("No keyring daemon found in this session, so we have "
51
"no access to Ubuntu One data.")
53
def get_oauth_token(consumer):
54
"""Get the token from the keyring"""
56
gobject.set_application_name("desktopcouch replication to Ubuntu One")
58
items = gnomekeyring.find_items_sync(
59
gnomekeyring.ITEM_GENERIC_SECRET,
60
{'ubuntuone-realm': "https://one.ubuntu.com",
61
'oauth-consumer-key': consumer.key})
62
except gnomekeyring.NoMatchError:
63
logging.info("No o.u.c key. Maybe there's uo.c key?")
64
items = gnomekeyring.find_items_sync(
65
gnomekeyring.ITEM_GENERIC_SECRET,
66
{'ubuntuone-realm': "https://ubuntuone.com",
67
'oauth-consumer-key': consumer.key})
69
return oauth.OAuthToken.from_string(items[0].secret)
71
def get_oauth_request_header(consumer, access_token, http_url):
72
"""Get an oauth request header given the token and the url"""
73
signature_method = oauth.OAuthSignatureMethod_PLAINTEXT()
74
assert http_url.startswith("https")
75
oauth_request = oauth.OAuthRequest.from_consumer_and_token(
78
oauth_consumer=consumer,
80
oauth_request.sign_request(signature_method, consumer, access_token)
81
return oauth_request.to_header()
87
self.oauth_header = None
90
if self.str is not None:
93
url = "https://one.ubuntu.com/api/account/"
94
if self.oauth_header is None:
95
consumer = oauth.OAuthConsumer(oauth_consumer_key,
96
oauth_consumer_secret)
98
access_token = get_oauth_token(consumer)
99
except gnomekeyring.NoKeyringDaemonError:
100
logging.info("No keyring daemon is running for this session.")
101
raise ValueError("No keyring access")
103
logging.info("Could not get access token from keyring")
104
raise ValueError("No keyring access")
105
self.oauth_header = get_oauth_request_header(consumer, access_token, url)
107
client = httplib2.Http()
108
resp, content = client.request(url, "GET", headers=self.oauth_header)
109
if resp['status'] == "200":
110
document = simplejson.loads(content)
111
if "couchdb_root" not in document:
112
raise ValueError("couchdb_root not found in %s" % (document,))
113
self.str = document["couchdb_root"]
115
logging.error("Couldn't talk to %r. Got HTTP %s", url, resp['status'])
116
raise ValueError("HTTP %s for %r" % (resp['status'], url))
120
# Access to this as a string fires off functions.
121
db_name_prefix = PrefixGetter()
123
if __name__ == "__main__":
124
logging.basicConfig(level=logging.DEBUG, format="%(message)s")
125
print str(db_name_prefix)