Commits

Guillermo Szeliga  committed 277c4b1 Draft

- Shell account connect method finished. \n - Shell list method being implemented

  • Participants
  • Parent commits cf1fe10

Comments (0)

Files changed (5)

File api/shelf/configuration/accounts.py

 
 import os
 import sys
+import re
 import simplejson as json
 
 from api.shelf.registry.clients import StorageClientsRegistry
     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()
             raise RuntimeError('Account file %s not found' % filename)
 
     def connect(self, account_data):
-        pass
+        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
+        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)

File api/shelf/configuration/config.py

 	APP_CONFIG_PATH = os.path.join(APP_HOME_FOLDER_PATH, 'config')
 	APP_INSTANCES_PATH = os.path.join(APP_HOME_FOLDER_PATH, 'instances')
 
-	def settle(self):
+	def settleIfNecessary(self):
 		self.__create_app_folder_structure()
 
+	def list_defined_instances(self, name_expression=None):
+
+		instances = os.listdir(self.APP_INSTANCES_PATH)
+
+		if name_expression is not None:
+			return [instance for instance in instances if name_expression.match(instance)]
+		else:
+			return instances
+
+
 	def __create_app_folder_structure(self):
 
 		ConfigurationManager.__create_dir(self.APP_HOME_FOLDER_PATH)

File api/shelf/configuration/test.py

 	def test_001_settle(self):
 		manager = config.ConfigurationManager()
 
-		manager.settle()
+		manager.settleIfNecessary()

File api/shelf/console/shell.py

 
 		elif operation_parts[0] == 'connect':
 
+			#TODO We need to check whether the account is already connected 
+
 			account_unique_id = None
 
 			if len(operation_parts) > 1:
 
 				try:
 					account_data = account_manager.load(account_unique_id)
+					client = account_manager.connect(account_data)
 
-					print account_data
+					print 'Account \'%s\' successfully connected' % account_unique_id
 
 				except RuntimeError, e:
 					print 'Account information could not be loaded. Reason: %s' % e
 			else:
 				print 'Account id cannot be empty'
 
+		elif operation_parts[0] == 'list':
+			
+			for defined_account in account_manager.list():
+				print defined_account
+
+		elif operation_parts[0] == 'delete':
+			pass
 
 	@single_client_command
 	def do_disconnect(self, operation):

File api/storages/adapters/skeleton.py

 	UNIQUE_ID_FIELD = 'unique_id'
 
 	def __init__(self, account_data):
+
+		#TODO Validate unique id name ^\w*$
+
 		self.__unique_id = account_data[self.UNIQUE_ID_FIELD]
 
 	def parse(self,account_data):