Skip to content

Commit

Permalink
Change mapping
Browse files Browse the repository at this point in the history
The mapping is now a little clearer about what exactly is being
changed. It uses a jmespath expression to the operation that is being
updated as a key, and the new parameter to be injected as the value.
  • Loading branch information
stealthycoin committed Jul 13, 2018
1 parent 6fab8a6 commit b5aa42f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .changes/next-release/enhancement-rekognition-66230.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "enhancement",
"category": "rekognition",
"description": "Added top level parameters to rekognition to make it possible\nto supply images to the operations that require bytes."
"description": "Added top level parameters to rekognition to make it possible to supply images to the operations that require bytes."
}
61 changes: 30 additions & 31 deletions awscli/customizations/rekognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import re

from awscli.arguments import CustomArgument


Expand All @@ -19,56 +21,45 @@
'instead.</p>')


class ParameterReplacement(object):
def __init__(self, source_param, target_param):
self.source_param = source_param
self.target_param = target_param


FILE_PARAMETER_UPDATES = {
'compare-faces': [
ParameterReplacement('source-image', 'SourceImage'),
ParameterReplacement('target-image', 'TargetImage')
],
'detect-faces': [ParameterReplacement('image', 'Image')],
'detect-labels': [ParameterReplacement('image', 'Image')],
'detect-moderation-labels': [ParameterReplacement('image', 'Image')],
'detect-text': [ParameterReplacement('image', 'Image')],
'index-faces': [ParameterReplacement('image', 'Image')],
'recognize-celebrities': [ParameterReplacement('image', 'Image')],
'search-faces-by-image': [ParameterReplacement('image', 'Image')],
'compare-faces.source-image': 'source-image-bytes',
'compare-faces.target-image': 'target-image-bytes',
'*.image': 'image-bytes',
}


def register_rekognition_detect_labels(cli):
for operation, params_to_update in FILE_PARAMETER_UPDATES.items():
for target, new_param in FILE_PARAMETER_UPDATES.items():
operation, old_param = target.split('.')
cli.register('building-argument-table.rekognition.%s' % operation,
ImageArgUpdater(params_to_update))
ImageArgUpdater(old_param, new_param))


class ImageArgUpdater(object):
def __init__(self, params_to_update):
self._params_to_update = params_to_update
def __init__(self, source_param, new_param):
self._source_param = source_param
self._new_param = new_param

def __call__(self, session, argument_table, **kwargs):
for param_to_update in self._params_to_update:
self._update_param(param_to_update, argument_table)
if not self._source_param in argument_table:
return
self._update_param(
argument_table, self._source_param, self._new_param)

def _update_param(self, param_replacement, argument_table):
param = param_replacement.source_param
new_param = '%s-file' % param
def _update_param(self, argument_table, source_param, new_param):
argument_table[new_param] = ImageArgument(
new_param, param_replacement.target_param,
new_param, source_param,
help_text=IMAGE_FILE_DOCSTRING, cli_type_name='blob')
argument_table[param].required = False
argument_table[source_param].required = False
doc_addendum = IMAGE_DOCSTRING_ADDENDUM % new_param
argument_table[param].documentation += doc_addendum
argument_table[source_param].documentation += doc_addendum


class ImageArgument(CustomArgument):
def __init__(self, name, parameter_to_overwrite, **kwargs):
def __init__(self, name, source_param, **kwargs):
super(ImageArgument, self).__init__(name, **kwargs)
self._parameter_to_overwrite = parameter_to_overwrite
self._parameter_to_overwrite = reverse_xform_name(source_param)
print(source_param, self._parameter_to_overwrite)

def add_to_params(self, parameters, value):
if value is None:
Expand All @@ -78,3 +69,11 @@ def add_to_params(self, parameters, value):
parameters[self._parameter_to_overwrite].update(image_file_param)
else:
parameters[self._parameter_to_overwrite] = image_file_param


def _upper(match):
return match.group(1).lstrip('-').upper()


def reverse_xform_name(name):
return re.sub(r'(^.|-.)', _upper, name)
18 changes: 9 additions & 9 deletions tests/functional/rekognition/test_image_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def test_image_file_does_populate_bytes_param(self):
second_file_bytes = open(second_temp_file, 'rb').read()

cmdline = self.prefix
cmdline += ' --source-image-file fileb://%s' % self.temp_file
cmdline += ' --target-image-file fileb://%s' % second_temp_file
cmdline += ' --source-image-bytes fileb://%s' % self.temp_file
cmdline += ' --target-image-bytes fileb://%s' % second_temp_file
result = {
'SourceImage': {'Bytes': self.temp_file_bytes},
'TargetImage': {'Bytes': second_file_bytes},
Expand All @@ -62,7 +62,7 @@ class TestDetectFaces(BaseRekognitionTest):

def test_image_file_does_populate_bytes_param(self):
cmdline = self.prefix
cmdline += ' --image-file fileb://%s' % self.temp_file
cmdline += ' --image-bytes fileb://%s' % self.temp_file
result = {
'Image': {'Bytes': self.temp_file_bytes}
}
Expand All @@ -83,7 +83,7 @@ class TestDetectLabels(BaseRekognitionTest):

def test_image_file_does_populate_bytes_param(self):
cmdline = self.prefix
cmdline += ' --image-file fileb://%s' % self.temp_file
cmdline += ' --image-bytes fileb://%s' % self.temp_file
result = {
'Image': {'Bytes': self.temp_file_bytes}
}
Expand All @@ -104,7 +104,7 @@ class TestDetectModerationLabels(BaseRekognitionTest):

def test_image_file_does_populate_bytes_param(self):
cmdline = self.prefix
cmdline += ' --image-file fileb://%s' % self.temp_file
cmdline += ' --image-bytes fileb://%s' % self.temp_file
result = {
'Image': {'Bytes': self.temp_file_bytes}
}
Expand All @@ -125,7 +125,7 @@ class TestDetectText(BaseRekognitionTest):

def test_image_file_does_populate_bytes_param(self):
cmdline = self.prefix
cmdline += ' --image-file fileb://%s' % self.temp_file
cmdline += ' --image-bytes fileb://%s' % self.temp_file
result = {
'Image': {'Bytes': self.temp_file_bytes}
}
Expand All @@ -147,7 +147,7 @@ class TestIndexFaces(BaseRekognitionTest):
def test_image_file_does_populate_bytes_param(self):
cmdline = self.prefix
cmdline += ' --collection-id foobar'
cmdline += ' --image-file fileb://%s' % self.temp_file
cmdline += ' --image-bytes fileb://%s' % self.temp_file
result = {
'CollectionId': 'foobar',
'Image': {'Bytes': self.temp_file_bytes}
Expand All @@ -171,7 +171,7 @@ class TestRecognizeCelebrities(BaseRekognitionTest):

def test_image_file_does_populate_bytes_param(self):
cmdline = self.prefix
cmdline += ' --image-file fileb://%s' % self.temp_file
cmdline += ' --image-bytes fileb://%s' % self.temp_file
result = {
'Image': {'Bytes': self.temp_file_bytes}
}
Expand All @@ -193,7 +193,7 @@ class TestSearchFacesByImage(BaseRekognitionTest):
def test_image_file_does_populate_bytes_param(self):
cmdline = self.prefix
cmdline += ' --collection-id foobar'
cmdline += ' --image-file fileb://%s' % self.temp_file
cmdline += ' --image-bytes fileb://%s' % self.temp_file
result = {
'CollectionId': 'foobar',
'Image': {'Bytes': self.temp_file_bytes}
Expand Down

0 comments on commit b5aa42f

Please sign in to comment.