Commit 70cff148 authored by Stavros Sachtouris's avatar Stavros Sachtouris Committed by Giorgos Korfiatis

Generalize and enhance PithosLocationArgument

* Move the class from kamaki.cli.cmds to kamaki.cli.argument
* Change the name of object path from "path" to "object"
    throughout the class
* Add the dict and tuple properties, which return the value
    in dict {user_uuid: .., container: .., object: ..} or
    tuple (user_uuid, container, object) format
parent 80f20e72
......@@ -552,6 +552,63 @@ class ProgressBarArgument(FlagArgument):
mybar.finish()
class PithosLocationArgument(ValueArgument):
"""Resolve pithos URI, return in the form pithos://uuid/container[/object]
UPDATE: URLs without an object are also resolvable. Therefore, caller
methods should check if there is an object or not
"""
def __init__(
self, help=None, parsed_name=None, default=None, user_uuid=None):
super(PithosLocationArgument, self).__init__(
help=help, parsed_name=parsed_name, default=default)
self.uuid, self.container, self.object = user_uuid, None, None
def setdefault(self, term, value):
if not getattr(self, term, None):
setattr(self, term, value)
@property
def dict(self):
""":returns: location as {user_uuid: .., container: .., object: ..}"""
return dict(
user_uuid=self.uuid, container=self.container, object=self.object)
@property
def tuple(self):
"""returns: location as (user_uuid, container, object)"""
return (self.uuid, self.container, self.object)
@property
def value(self):
object_ = ('/%s' % self.object) if self.object else ''
return 'pithos://%s/%s%s' % (self.uuid, self.container, object_)
@value.setter
def value(self, location):
if location:
from kamaki.cli.cmds.pithos import _PithosContainer as pc
try:
uuid, self.container, self.object = pc.resolve_pithos_url(
location)
self.uuid = uuid or self.uuid
assert self.container, 'No container in pithos URI'
except Exception as e:
raise CLIInvalidArgument(
'Invalid Pithos+ location %s (%s)' % (location, e),
details=[
'The image location must be a valid Pithos+',
'location. There are two valid formats:',
' pithos://USER_UUID/CONTAINER[/OBJECT]',
'OR',
' /CONTAINER/OBJECT]',
'To see all containers:',
' [kamaki] container list',
'To list the contents of a container:',
' [kamaki] container list CONTAINER'])
# Initial command line interface arguments
......
......@@ -45,7 +45,7 @@ from kamaki.clients.pithos import PithosClient
from kamaki.clients import ClientError
from kamaki.cli.argument import (
FlagArgument, ValueArgument, RepeatableArgument, KeyValueArgument,
IntArgument, ProgressBarArgument)
IntArgument, ProgressBarArgument, PithosLocationArgument)
from kamaki.cli.cmds.cyclades import _CycladesInit
from kamaki.cli.errors import CLIError, raiseCLIError, CLIInvalidArgument
from kamaki.cli.cmds import (
......@@ -339,52 +339,6 @@ class image_modify(_ImageInit):
self._run(image_id=image_id)
class PithosLocationArgument(ValueArgument):
"""Resolve pithos URI, return in the form pithos://uuid/container[/path]
UPDATE: URLs without a path are also resolvable. Therefore, caller methods
should check if there is a path or not
"""
def __init__(
self, help=None, parsed_name=None, default=None, user_uuid=None):
super(PithosLocationArgument, self).__init__(
help=help, parsed_name=parsed_name, default=default)
self.uuid, self.container, self.path = user_uuid, None, None
def setdefault(self, term, value):
if not getattr(self, term, None):
setattr(self, term, value)
@property
def value(self):
path = ('/%s' % self.path) if self.path else ''
return 'pithos://%s/%s%s' % (self.uuid, self.container, path)
@value.setter
def value(self, location):
if location:
from kamaki.cli.cmds.pithos import _PithosContainer as pc
try:
uuid, self.container, self.path = pc.resolve_pithos_url(
location)
self.uuid = uuid or self.uuid
assert self.container, 'No container in pithos URI'
except Exception as e:
raise CLIInvalidArgument(
'Invalid Pithos+ location %s (%s)' % (location, e),
details=[
'The image location must be a valid Pithos+',
'location. There are two valid formats:',
' pithos://USER_UUID/CONTAINER[/PATH]',
'OR',
' /CONTAINER[/PATH]',
'To see all containers:',
' [kamaki] container list',
'To list the contents of a container:',
' [kamaki] container list CONTAINER'])
@command(image_cmds)
class image_register(_ImageInit, OptionalOutput):
"""(Re)Register an image file to an Image service
......@@ -499,14 +453,14 @@ class image_register(_ImageInit, OptionalOutput):
if self['local_image_path']:
with open(self['local_image_path']) as f:
pithos = self._get_pithos_client(locator)
self._assert_remote_file_not_exist(pithos, locator.path)
self._assert_remote_file_not_exist(pithos, locator.object)
(pbar, upload_cb) = self._safe_progress_bar('Uploading')
if pbar:
hash_bar = pbar.clone()
hash_cb = hash_bar.get_generator('Calculating hashes')
try:
pithos.upload_object(
locator.path, f,
locator.object, f,
hash_cb=hash_cb, upload_cb=upload_cb,
container_info_cache=self.container_info_cache)
finally:
......@@ -521,7 +475,7 @@ class image_register(_ImageInit, OptionalOutput):
if not self['no_metafile_upload']:
# check if metafile exists
pithos = pithos or self._get_pithos_client(locator)
meta_path = '%s.meta' % locator.path
meta_path = '%s.meta' % locator.object
self._assert_remote_file_not_exist(pithos, meta_path)
# register the image
......@@ -534,8 +488,8 @@ class image_register(_ImageInit, OptionalOutput):
details=[
'%s' % ce,
'Does the image file %s exist at container %s ?' % (
locator.path,
locator.container)] + howto_image_file)
locator.object, locator.container)
] + howto_image_file)
raise
r['owner'] += ' (%s)' % self._uuid2username(r['owner'])
self.print_(r, self.print_dict)
......@@ -566,9 +520,9 @@ class image_register(_ImageInit, OptionalOutput):
super(self.__class__, self)._run()
locator = self.arguments['pithos_location']
locator.setdefault('uuid', self.astakos.user_term('id'))
locator.path = locator.path or path.basename(
locator.object = locator.object or path.basename(
self['local_image_path'] or '')
if not locator.path:
if not locator.object:
raise CLIInvalidArgument(
'Missing the image file or object', details=[
'Pithos+ URI %s does not point to a physical image' % (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment