cloudster / api / shelf / configuration / accounts.py

__author__ = 'guillermo'

import os
import sys
import re
import simplejson as json

from api.shelf.registry.clients import StorageClientsRegistry
from api.shelf.configuration import config



class AccountsManager():

    def __init__(self, client_class):
        self.__client_class = client_class
        self.__account_type_class = self.__client_class.registry_metadata().account_type_class()
        self.__config_manager = config.ConfigurationManager()

    def get_configuration_descriptor(self):
        return self.__account_type_class.get_configuration_fields()

    def create(self, account_config_values):
        return self.__account_type_class(json.dumps(account_config_values))

    def authorize(self, account_data):
        client = self.__client_class(account_data)
        client.grant_client_access()

        return client,account_data

    def load(self, unique_account_id):

        filename = self.__build_account_filename(unique_account_id)
        full_account_file_path = os.path.join(config.ConfigurationManager.APP_INSTANCES_PATH, filename)

        if os.path.exists(full_account_file_path):
            account_file = open(full_account_file_path,'r')
            account_file_content = account_file.read()

            account_file.close()

            return self.__account_type_class(account_file_content)
        else:
            #TODO Use a more specific exception
            raise RuntimeError('Account file %s not found' % filename)

    def connect(self, account_data):
        client = self.__client_class(account_data)
        client.connect()

        return client

    def __build_account_filename(self, unique_account_id):
        return self.__client_class.registry_metadata().unique_id() + "_" + unique_account_id + ".json"

    def save(self, account_data):

        self.__config_manager.settleIfNecessary()

        filename= self.__build_account_filename(account_data.unique_id())
        account_data.save(os.path.join(config.ConfigurationManager.APP_INSTANCES_PATH, filename))

    def delete(self, id=None):
        pass

    def list(self):

        name_pattern = re.compile('^' + self.__client_class.registry_metadata().unique_id() + "_")

        #TODO Extract account name only (i.e ^box_\w*.json$)

        return self.__config_manager.list_defined_instances(name_pattern)
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 ProjectModifiedEvent.java.
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.