Seperate commands specs and connection packages
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 28 Aug 2012 14:22:10 +0000 (17:22 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 28 Aug 2012 14:22:10 +0000 (17:22 +0300)
kamaki/cli.py
kamaki/clients/__init__.py
kamaki/clients/connection/__init__.py [new file with mode: 0644]
kamaki/commands/__init__.py [new file with mode: 0644]
kamaki/commands/astakos_cli.py [moved from kamaki/clients/astakos_cli.py with 96% similarity]
kamaki/commands/cli_utils.py [moved from kamaki/clients/cli_utils.py with 73% similarity]
kamaki/commands/cyclades_cli.py [moved from kamaki/clients/cyclades_cli.py with 99% similarity]
kamaki/commands/image_cli.py [moved from kamaki/clients/image_cli.py with 99% similarity]
kamaki/commands/pithos_cli.py [moved from kamaki/clients/pithos_cli.py with 99% similarity]
kamaki/commands/pithos_sh_cli.py [moved from kamaki/clients/pithos_sh_cli.py with 96% similarity]

index 34709bd..0546308 100644 (file)
@@ -64,7 +64,7 @@ from .config import Config
 _commands = OrderedDict()
 
 GROUPS = {}
-CLI_LOCATIONS = ['', 'kamaki', 'kamaki.clients', 'kamaki.clis']
+CLI_LOCATIONS = ['', 'kamaki', 'kamaki.commands']
 
 class CLIError(Exception):
     def __init__(self, message, status=0, details='', importance=0):
@@ -165,7 +165,6 @@ def main():
 
     def load_groups(config):
         """load groups and import CLIs and Modules"""
-        #apis = set(['config']+config.apis())
         loaded_modules = {}
         for api in config.apis():
             api_cli = config.get(api, 'cli')
index 42937cc..aec7c18 100644 (file)
@@ -53,14 +53,37 @@ class ClientError(Exception):
         self.status = status
         self.details = details
 
+class ClientResponse(object):
+
+    def __init__(self):
+        #Dict of response headers
+        self.headers = {}
+        #CLear text in the response
+        self.text = ''
+        #Json formated response
+        self.json = None
+        #xml formated response
+        self.xml = None
+        #Content in byte form
+        self.content = None
+
+    def load_request_object(self, request):
+        r.headers = request.headers
+        r.text = request.text
+        r.json = request.json
+        r.xml = request.xml
+        r.content = request.content
+
 class Client(object):
-    def __init__(self, base_url, token):
+
+    def __init__(self, base_url, token, http_client=None):
         self.base_url = base_url
         self.token = token
         self.headers = {}
         self.DATE_FORMATS = ["%a %b %d %H:%M:%S %Y",
             "%A, %d-%b-%y %H:%M:%S GMT",
             "%a, %d %b %Y %H:%M:%S GMT"]
+        self.http_client = http_client
 
     def raise_for_status(self, r):
         message = "%d %s" % (r.status_code, r.status)
@@ -147,9 +170,3 @@ class Client(object):
     def move(self, path, **kwargs):
         return self.request('move', path, **kwargs)
 
-from .compute import ComputeClient as compute
-from .image import ImageClient as image
-from .storage import StorageClient as storage
-from .cyclades import CycladesClient as cyclades
-from .pithos import PithosClient as pithos
-from .astakos import AstakosClient as astakos
diff --git a/kamaki/clients/connection/__init__.py b/kamaki/clients/connection/__init__.py
new file mode 100644 (file)
index 0000000..847f4b4
--- /dev/null
@@ -0,0 +1,53 @@
+# Copyright 2011-2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, self.list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, self.list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+class ClientResponse(object):
+
+    def __init__(self):
+        #Dict of response headers
+        self.headers = {}
+        #CLear text in the response
+        self.text = ''
+        #Json formated response
+        self.json = None
+        #xml formated response
+        self.xml = None
+        #Content in byte form
+        self.content = None
+
+    def load_request_object(self, request):
+        r.headers = request.headers
+        r.text = request.text
+        r.json = request.json
+        r.xml = request.xml
+        r.content = request.content
\ No newline at end of file
diff --git a/kamaki/commands/__init__.py b/kamaki/commands/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
similarity index 96%
rename from kamaki/clients/astakos_cli.py
rename to kamaki/commands/astakos_cli.py
index 018ff86..46d389f 100644 (file)
@@ -34,7 +34,7 @@
 
 from kamaki.cli import command, set_api_description
 set_api_description('astakos', 'Astakos API commands')
-from .astakos import AstakosClient, ClientError
+from kamaki.clients.astakos import AstakosClient, ClientError
 from .cli_utils import raiseCLIError
 from kamaki.utils import print_dict
 
similarity index 73%
rename from kamaki/clients/cli_utils.py
rename to kamaki/commands/cli_utils.py
index 4af3561..9738c96 100644 (file)
@@ -31,9 +31,9 @@
 # interpreted as representing official policies, either expressed
 # or implied, of GRNET S.A.command
 
-from . import ClientError
+from kamaki.clients import ClientError
 from kamaki.cli import CLIError
-from inspect import getargspec
+#from inspect import getargspec
 
 def raiseCLIError(err, importance = -1):
        if importance < 0:
@@ -47,3 +47,23 @@ def raiseCLIError(err, importance = -1):
                        importance = 3
        raise CLIError(err.message, err.status, err.details, importance)
 
+def dict2file(d, f, depth = 0):
+    for k, v in d.items():
+        f.write('%s%s: '%('\t'*depth, k))
+        if type(v) is dict:
+            f.write('\n')
+            dict2file(v, f, depth+1)
+        elif type(v) is list:
+            f.write('\n')
+            list2file(v, f, depth+1)
+        else:
+            f.write(' %s\n'%unicode(v))
+
+def list2file(l, f, depth = 1):
+    for item in l:
+        if type(item) is dict:
+            dict2file(item, f, depth+1)
+        elif type(item) is list:
+            list2file(item, f, depth+1)
+        else:
+            f.write('%s%s\n'%('\t'*depth, unicode(item)))
\ No newline at end of file
similarity index 99%
rename from kamaki/clients/cyclades_cli.py
rename to kamaki/commands/cyclades_cli.py
index de82dda..a613268 100644 (file)
@@ -37,7 +37,7 @@ set_api_description('server', "Compute/Cyclades API server commands")
 set_api_description('flavor', "Compute/Cyclades API flavor commands")
 set_api_description('image', "Compute/Cyclades or Glance API image commands")
 set_api_description('network', "Compute/Cyclades API network commands")
-from .cyclades import CycladesClient, ClientError
+from kamaki.clients.cyclades import CycladesClient, ClientError
 from .cli_utils import raiseCLIError
 
 from colors import yellow
similarity index 99%
rename from kamaki/clients/image_cli.py
rename to kamaki/commands/image_cli.py
index 30f959c..b50a1ea 100644 (file)
@@ -34,7 +34,7 @@
 from kamaki.cli import command, set_api_description
 from kamaki.utils import print_dict, print_items
 set_api_description('image', "Compute/Cyclades or Glance API image commands")
-from .image import ImageClient, ClientError
+from kamaki.clients.image import ImageClient, ClientError
 from .cli_utils import raiseCLIError
 
 class _init_image(object):
similarity index 99%
rename from kamaki/clients/pithos_cli.py
rename to kamaki/commands/pithos_cli.py
index 9f21aa1..d7cbfbf 100644 (file)
@@ -35,7 +35,7 @@ from kamaki.cli import command, set_api_description, CLIError
 from kamaki.clients.utils import filter_in
 from kamaki.utils import format_size
 set_api_description('store', 'Pithos+ storage commands')
-from .pithos import PithosClient, ClientError
+from kamaki.clients.pithos import PithosClient, ClientError
 from .cli_utils import raiseCLIError
 from kamaki.utils import print_dict, pretty_keys, print_list
 from colors import bold
similarity index 96%
rename from kamaki/clients/pithos_sh_cli.py
rename to kamaki/commands/pithos_sh_cli.py
index 42b91c7..79476e3 100644 (file)
@@ -35,7 +35,7 @@
 
 from kamaki.cli import command, CLIError
 from kamaki.utils import print_list
-from .utils import dict2file, list2file
+from .cli_utils import dict2file, list2file
 from .pithos_cli import _store_container_command, _store_account_command
 from colors import bold
 
@@ -45,9 +45,9 @@ from colors import bold
 from sys import stdout#argv, exit, stdin, stdout
 #from datetime import datetime
 
-from .pithos_sh_lib.client import Pithos_Client, Fault
+from kamaki.clients.pithos_sh_lib.client import Pithos_Client, Fault
 #from .pithos_sh_lib.util import get_user, get_auth, get_url
-from .pithos_sh_lib.transfer import download, cat#,upload
+from kamaki.clients.pithos_sh_lib.transfer import download, cat#,upload
 
 #import json
 #import logging