cloudster / api / storages / adapters / dropbox /

from api.shelf.registry.metadata import ClientRegistryMetadata
from api.storages.adapters import skeleton
from api.storages.adapters.dropbox import events
from api.storages.adapters import elements

from api.storages.clients.dropbox import client, session

ACCESS_TYPE_FOLDER = 'app_folder'  # should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE_FULL = 'dropbox'

class DropboxClient(skeleton.StorageAdapter):

	def registry_metadata():

		metadata = ClientRegistryMetadata('dropbox', DropboxAccountData)

		return metadata

	def __init__(self, account_data):

		self._account_data = account_data

	def obtainAccessToken(self, sess):

		request_token =  sess.obtain_request_token()

		print 'You should redirect user to ' + sess.build_authorize_url(request_token)


		return sess.obtain_access_token(request_token)

	#Mirar el metodo session.obtain_request_token(self) para mas informacion de como hacerlo en el futuro
	def connect(self):

		#TODO account should be type safe

		sess = session.DropboxSession(self._account_data.appKey(), self._account_data.appSecret(), self._account_data.accessType())		

		if not self._account_data.isAccessGranted():
			sess.set_token(self._account_data.oauthToken(), self._account_data.oauthTokenSecret())
			print 'We are already granted ' + str(sess.token)

		self._client = client.DropboxClient(sess)

	#TODO We need to check if we actually have an active session opened

	def list(self, path):

		#This is not a recursive call, only returns those elements that belong to the specified path
		remote_info = self._client.metadata(path)

		content = []

		for remote_element in remote_info['contents']:
			if remote_element['is_dir']:

		return content

	def _toStorageFile(self,source):
		return elements.StorageFile(source['path'],source['modified'],source['rev'])

	def _toStorageFolder(self, source):
		return elements.StorageFolder(source['path'],source['modified'],source['rev'])

	def about(self):return self._client.account_info()

	def write(self, target_full_path, source_file, overwrite=True):
		self._client.put_file(target_full_path, source_file, overwrite)

	def read(self, full_remote_path, target_file_path=None):

		remote_file = self._client.get_file(full_remote_path)

		if target_file_path is not None:

			f = open(target_file_path,'wb')

			file_size = 0

			for header_name, header_value in [(header_content.split(":")[0],header_content.split(":")[1]) for header_content in remote_file.msg.headers]:
				if header_name.lower() == 'content-length':
					file_size = int(header_value)

			print "Downloading: %s Bytes: %s" % (target_file_path, file_size)

			file_size_dl = 0
			block_sz = 1024*1024

			while True:
				buffer =

				if not buffer:

				file_size_dl += len(buffer)

				status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
				status += chr(8)*(len(status)+1)

				print status

			return remote_file

	def remove(self, full_path):

	def events(self, history_pointer = None):

		iterator = events.DropboxEventsIterator(self._client, history_pointer)
		translator = events.DropboxEventTranslator()

		return events.DropboxEventsBrowser(iterator, translator)

class DropboxAccountData(skeleton.StorageAccountData):

	def __init__(self, account_data):


		parsed_data = skeleton.StorageAccountData.parse(self, account_data)

		self._appKey = parsed_data["appKey"]
		self._appSecret = parsed_data["appSecret"]
		self._accessType = parsed_data["accessType"]

		if "OAuthToken" in parsed_data:
			self._oauth_token = parsed_data["OAuthToken"]

		if "OAuthTokenSecret" in parsed_data:
			self._oauth_token_secret = parsed_data["OAuthTokenSecret"]
			self._oauth_token_secret = None

	def assignAccessToken(self, access_token):
		self._oauth_token = access_token.key
		self._oauth_token_secret = access_token.secret

	@skeleton.account_field('dropbox','appKey', 'Application key', skeleton.AccountFieldDescriptor.FIELD_TYPE_STRING)
	def appKey(self):
		return self._appKey

	def appSecret(self):
		return self._appSecret

	def accessType(self):
		return self._accessType

	def oauthToken(self):
		return self._oauth_token

	def oauthTokenSecret(self):
		return self._oauth_token_secret

	def isAccessGranted(self):
		return self._oauth_token_secret is not None and self._oauth_token is not None

	def jsonize(self):
		return dict({'appKey': self._appKey, 'appSecret': self._appSecret, 'accessType': self._accessType, 'OAuthToken' : self._oauth_token, 'OAuthTokenSecret': self._oauth_token_secret})
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.