Source

cloudster / api / storages / adapters / skeleton.py

Full commit
import abc
from api.serializers import json_types
from functools import wraps

__accounts_field_registry__ = {}

def account_field(cls,name, label, type):

	def inner(f):

		if cls in __accounts_field_registry__:
			__accounts_field_registry__[cls].append(AccountFieldDescriptor(name, label,type))
		else:
			__accounts_field_registry__[cls]=[AccountFieldDescriptor(name, label,type)]

		def wrapped(*args, **kwargs):
			return f(*args, **kwargs)
		return wraps(f)(wrapped)

	return inner

class StorageAdapter(object):

	__metaclass__ = abc.ABCMeta

	@staticmethod
	@abc.abstractmethod
	def registry_metadata():
		pass

	def __init__(self):
		pass

	@abc.abstractmethod
	def read(self, full_remote_path, target_file_path=None):
		pass

	@abc.abstractmethod
	def write(self, target_full_path, source_file, overwrite=True):
		pass

	@abc.abstractmethod
	def move(self):
		pass

	@abc.abstractmethod
	def list(self, path):
		pass

	@abc.abstractmethod
	def connect(self):
		pass

	@abc.abstractmethod
	def obtain_access_token(self, session_handler):
		pass

	@abc.abstractmethod
	def grant_client_access(self,session_handler=None):
		pass

	@abc.abstractmethod
	def about(self):
		pass

	@abc.abstractmethod
	def remove(self, full_path):
		pass

	@abc.abstractmethod
	def events(self, history_pointer = None):
		pass


class StorageAccountData(json_types.JSonizable):

	UNIQUE_ID_FIELD = 'unique_id'

	def __init__(self, account_data):
		self.__unique_id = account_data[self.UNIQUE_ID_FIELD]

	def parse(self,account_data):
		return json_types.JSonizable.fromJSON(self,account_data)

	def save(self,target_path):

		file = open(target_path,'w')
		file.write(self.toJSON())


	def unique_id(self):
		return self.__unique_id

	@staticmethod
	@abc.abstractmethod
	def get_configuration_fields():
		pass

	@staticmethod
	def _list_account_field(account_id):
		fields_registry =  __accounts_field_registry__[account_id]
		fields_registry.append(AccountFieldDescriptor(StorageAccountData.UNIQUE_ID_FIELD,'Account Name'))

		return fields_registry

	def __str__(self):
		return self.toJSON()


class AccountFieldDescriptor():

	FIELD_TYPE_STRING=1

	def __init__(self,name,label=None,type=FIELD_TYPE_STRING):
		self.__name=name
		self.__type=type
		self.__label=label

	def name(self):
		return self.__name

	def label(self):
		if self.__label is None:
			return self.__name

		return self.__label

	def type(self):
		return self.__type

	def __str__(self):
		return '{name: %s, label: %s, type: %i}'% (self.name(), self.label(),self.type())