Skip to content
This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
/ accelpy Public archive

Commit

Permalink
1.0.0-beta.21
Browse files Browse the repository at this point in the history
  • Loading branch information
JGoutin committed Aug 19, 2019
1 parent c98405f commit 2cfe972
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 97 deletions.
2 changes: 1 addition & 1 deletion accelpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
__version__ = '1.0.0-beta.20'
__version__ = '1.0.0-beta.21'
__copyright__ = "Copyright 2019 Accelize"
__licence__ = "Apache 2.0"

Expand Down
85 changes: 71 additions & 14 deletions accelpy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,66 @@ def _yaml_completer(prefix, parsed_args, **__):
yield path


def _get_cached_app(prefix, name, after, getter):
"""
Get from cache if available, else get from web server.
Args:
prefix (str): Application prefix to filter.
name (str): Cache name to use.
after (iterable): Iterable after which chain .
getter (function): Function ot use to get from web server.
Returns:
iterable of str:
"""
from itertools import chain
from accelpy._common import get_cli_cache, set_cli_cache

cached = f'{name}|{prefix}'
values = get_cli_cache(cached, recursive=True)

# If no cached values, get from web server then cache values
if not values:
values = set_cli_cache(cached, getter(prefix))

# If cached values, filter before return
else:
values = (value for value in values if value.startswith(prefix))

return chain(after, values)


def _get_product_ids(prefix):
"""
Get products IDs from web server.
Args:
prefix (str): Application prefix to filter.
Returns:
list of str: Product ids.
"""
from accelpy._application import Application
return Application.list(prefix)


def _get_versions(prefix):
"""
Get versions from web server.
Args:
prefix (str): Application prefix to filter.
Returns:
list of str: Versions.
"""
from accelpy._application import Application
product_id, version_prefix = prefix.split(':', 1)
return (f"{product_id}:{version}" for version in
Application.list_versions(product_id, version_prefix))


def _application_completer(prefix, parsed_args, **__):
"""
Autocomplete "accelpy init --application"
Expand All @@ -273,32 +333,29 @@ def _application_completer(prefix, parsed_args, **__):
# than 2 "/"
if (not prefix.startswith('.') and not prefix.startswith('/') and
prefix.count('/') <= 2):
from itertools import chain
from accelpy._application import Application

from accelpy.exceptions import (
AuthenticationException, AccelizeException)
AuthenticationException, WebServerException)

try:
# "product_id:version" formatted
if ':' in prefix:
# TODO: Enable result caching
product_id, prefix = prefix.split(':', 1)

return chain(yaml_applications, (
f"{product_id}:{version}" for version in
Application.list_versions(product_id, prefix)))
name = 'version'
getter = _get_versions

# "product_id" formatted
else:
# TODO: Enable result caching
return chain(yaml_applications, Application.list(prefix))
name = 'product'
getter = _get_product_ids

return _get_cached_app(prefix, name, yaml_applications, getter)

except AuthenticationException as exception:
_completer_warn(
'"--application"/"-a" argument autocompletion require '
f'Accelize authentication: {exception}')

except AccelizeException:
except WebServerException:
# Skip silently any other Accelize web service error.
pass

Expand Down Expand Up @@ -327,7 +384,7 @@ def _provider_completer(prefix, parsed_args, **_):
from accelpy._common import get_cli_cache, set_cli_cache

application = abspath(application) if isfile(application) else application
cached = f'{application}_providers'
cached = f'providers|{application}'
providers = get_cli_cache(cached)

# Else get providers from application and cache them
Expand Down Expand Up @@ -525,7 +582,7 @@ def _run_command():
parser.error('\n'.join(message))
raise

except KeyboardInterrupt:
except KeyboardInterrupt: # pragma: no cover
parser.exit(status=1, message="Interrupted by user\n")


Expand Down
20 changes: 11 additions & 9 deletions accelpy/_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from os import fsdecode
from re import fullmatch

from accelpy._common import request
from accelpy._common import accelize_ws_session
from accelpy._yaml import yaml_read, yaml_write
from accelpy.exceptions import ConfigurationException

Expand Down Expand Up @@ -155,8 +155,8 @@ def _get_definition(application):
params['product_id'], params['version'] = application.split(':', 1)
except ValueError:
params['product_id'] = application
return request.query('/auth/objects/productconfiguration/',
params=params)['results'][0]
return accelize_ws_session.request(
'/auth/objects/productconfiguration/', params=params)['results'][0]

@staticmethod
def list(prefix=''):
Expand All @@ -169,7 +169,7 @@ def list(prefix=''):
Returns:
list of str: products.
"""
return request.query(
return accelize_ws_session.request(
'/auth/objects/productconfigurationlistproduct/', params=dict(
product_id__startswith=prefix) if prefix else None)['results']

Expand All @@ -188,14 +188,15 @@ def list_versions(product_id, prefix=''):
params = dict(product_id=product_id)
if prefix:
params['version__startswith'] = prefix
return request.query('/auth/objects/productconfigurationlistversion/',
params=params)['results']
return accelize_ws_session.request(
'/auth/objects/productconfigurationlistversion/',
params=params)['results']

def push(self):
"""
Push application definition on Accelize web service.
"""
self._configuration_id = request.query(
self._configuration_id = accelize_ws_session.request(
'/auth/objects/productconfiguration/',
data=dumps(self._clean_definition),
method='post')['application']['configuration_id']
Expand All @@ -216,8 +217,9 @@ def delete(cls, application):
application = cls._get_definition(
application)['application']['configuration_id']

request.query(f'/auth/objects/productconfiguration/{application}/',
method='delete')
accelize_ws_session.request(
f'/auth/objects/productconfiguration/{application}/',
method='delete')

@property
def configuration_id(self):
Expand Down
Loading

0 comments on commit 2cfe972

Please sign in to comment.