From dd24315eef4eedef93b63082e38c447a5e89a645 Mon Sep 17 00:00:00 2001 From: wip-abramson Date: Thu, 20 May 2021 15:15:17 +0100 Subject: [PATCH 1/2] attempting to fix dependencies --- aries_cloudcontroller/controllers/base.py | 51 ++--- aries_cloudcontroller/helpers/utils.py | 250 ---------------------- requirements.txt | 37 ++-- setup.py | 2 +- 4 files changed, 41 insertions(+), 299 deletions(-) diff --git a/aries_cloudcontroller/controllers/base.py b/aries_cloudcontroller/controllers/base.py index fc916bd8..eea0d418 100644 --- a/aries_cloudcontroller/controllers/base.py +++ b/aries_cloudcontroller/controllers/base.py @@ -1,20 +1,14 @@ from aiohttp import ( - web, - ClientSession, - ClientRequest, ClientResponse, ClientError, - ClientTimeout, ) -import asyncio import logging import json -from ..helpers.utils import log_msg -EVENT_LOGGER = logging.getLogger("__name__") +LOGGER = logging.getLogger("__name__") class repr_json: @@ -34,24 +28,11 @@ def __init__(self, admin_url: str, client_session): self.color = None self.prefix = None - def log(self, *msg, **kwargs): - self.handle_output(*msg, **kwargs) - @property def prefix_str(self): if self.prefix: return f"{self.prefix:10s} |" - def handle_output(self, *output, source: str = None, **kwargs): - end = "" if source else "\n" - if source == "stderr": - color = "fg:ansired" - elif not source: - color = self.color or "fg:ansiblue" - else: - color = None - log_msg(*output, color=color, prefix=self.prefix_str, end=end, **kwargs) - async def admin_request( self, method, path, json_data=None, text=False, params=None, data=None ) -> ClientResponse: @@ -72,23 +53,23 @@ async def admin_request( async def admin_GET(self, path, text=False, params=None) -> ClientResponse: try: - EVENT_LOGGER.debug("Controller GET %s request to Agent", path) + LOGGER.debug("Controller GET %s request to Agent", path) response = await self.admin_request("GET", path, None, text, params) - EVENT_LOGGER.debug( + LOGGER.debug( "Response from GET %s received: \n%s", path, repr_json(response), ) return response except ClientError as e: - self.log(f"Error during GET {path}: {str(e)}") + LOGGER.error(f"Error during GET {path}: {str(e)}") raise async def admin_POST( self, path, json_data=None, text=False, params=None, data=None ) -> ClientResponse: try: - EVENT_LOGGER.debug( + LOGGER.debug( "Controller POST %s request to Agent%s", path, (" with data: \n{}".format(repr_json(json_data)) if json_data else ""), @@ -96,21 +77,21 @@ async def admin_POST( response = await self.admin_request( "POST", path, json_data, text, params, data ) - EVENT_LOGGER.debug( + LOGGER.debug( "Response from POST %s received: \n%s", path, repr_json(response), ) return response except ClientError as e: - self.log(f"Error during POST {path}: {str(e)}") + LOGGER.error(f"Error during POST {path}: {str(e)}") raise async def admin_PATCH( self, path, json_data=None, text=False, params=None, data=None ) -> ClientResponse: try: - EVENT_LOGGER.debug( + LOGGER.debug( "Controller PATCH %s request to Agent%s", path, (" with data: \n{}".format(repr_json(json_data)) if json_data else ""), @@ -118,19 +99,19 @@ async def admin_PATCH( response = await self.admin_request( "PATCH", path, json_data, text, params, data ) - EVENT_LOGGER.debug( + LOGGER.debug( "Response from PATCH %s received: \n%s", path, repr_json(response) ) return response except ClientError as e: - self.log(f"Error during PATCH {path}: {str(e)}") + LOGGER.error(f"Error during PATCH {path}: {str(e)}") raise async def admin_PUT( self, path, json_data=None, text=False, params=None, data=None ) -> ClientResponse: try: - EVENT_LOGGER.debug( + LOGGER.debug( "Controller PUT %s request to Agent%s", path, (" with data: \n{}".format(repr_json(json_data)) if json_data else ""), @@ -138,25 +119,25 @@ async def admin_PUT( response = await self.admin_request( "PUT", path, json_data, text, params, data ) - EVENT_LOGGER.debug( + LOGGER.debug( "Response from PUT %s received: \n%s", path, repr_json(response) ) return response except ClientError as e: - self.log(f"Error during PUT {path}: {str(e)}") + LOGGER.error(f"Error during PUT {path}: {str(e)}") raise async def admin_DELETE(self, path, text=False, params=None) -> ClientResponse: try: - EVENT_LOGGER.debug("Controller DELETE %s request to Agent", path) + LOGGER.debug("Controller DELETE %s request to Agent", path) response = await self.admin_request("DELETE", path, None, text, params) - EVENT_LOGGER.debug( + LOGGER.debug( "Response from DELETE %s received: \n%s", path, repr_json(response), ) return response except ClientError as e: - self.log(f"Error during DELETE {path}: {str(e)}") + LOGGER.error(f"Error during DELETE {path}: {str(e)}") raise diff --git a/aries_cloudcontroller/helpers/utils.py b/aries_cloudcontroller/helpers/utils.py index dedb06c0..ce1f25fd 100644 --- a/aries_cloudcontroller/helpers/utils.py +++ b/aries_cloudcontroller/helpers/utils.py @@ -1,253 +1,3 @@ -import functools -import json -import os -import sys -from timeit import default_timer - -import prompt_toolkit -from prompt_toolkit.application import run_in_terminal - -# from prompt_toolkit.eventloop.defaults import use_asyncio_event_loop -from prompt_toolkit.patch_stdout import patch_stdout -from prompt_toolkit.shortcuts import ProgressBar - -import pygments -from pygments.filter import Filter -from pygments.lexer import Lexer -from pygments.lexers.data import JsonLdLexer -from prompt_toolkit.formatted_text import FormattedText, PygmentsTokens - - -COLORIZE = bool(os.getenv("COLORIZE", True)) - - -class PrefixFilter(Filter): - def __init__(self, **options): - Filter.__init__(self, **options) - self.prefix = options.get("prefix") - - def lines(self, stream): - line = [] - for ttype, value in stream: - if "\n" in value: - parts = value.split("\n") - value = parts.pop() - for part in parts: - line.append((ttype, part)) - line.append((ttype, "\n")) - yield line - line = [] - line.append((ttype, value)) - if line: - yield line - - def filter(self, lexer, stream): - if isinstance(self.prefix, str): - prefix = ((pygments.token.Generic, self.prefix),) - elif self.prefix: - prefix = self.prefix - else: - prefix = () - for line in self.lines(stream): - yield from prefix - yield from line - - -def print_lexer( - body: str, lexer: Lexer, label: str = None, prefix: str = None, indent: int = None -): - if COLORIZE: - prefix_str = prefix + " " if prefix else "" - if prefix_str or indent: - prefix_body = prefix_str + " " * (indent or 0) - lexer.add_filter(PrefixFilter(prefix=prefix_body)) - tokens = list(pygments.lex(body, lexer=lexer)) - if label: - fmt_label = [("fg:ansimagenta", label)] - if prefix_str: - fmt_label.insert(0, ("", prefix_str)) - print_formatted(FormattedText(fmt_label)) - print_formatted(PygmentsTokens(tokens)) - else: - print_ext(body, label=label, prefix=prefix) - - -def print_json(data, label: str = None, prefix: str = None, indent: int = 2): - if isinstance(data, str): - data = json.loads(data) - data = json.dumps(data, indent=2) - prefix_str = prefix or "" - print_lexer(data, JsonLdLexer(), label=label, prefix=prefix_str, indent=indent) - - -def print_formatted(*args, **kwargs): - prompt_toolkit.print_formatted_text(*args, **kwargs) - - -def print_ext( - *msg, - color: str = None, - label: str = None, - prefix: str = None, - indent: int = None, - **kwargs, -): - prefix_str = prefix or "" - if indent: - prefix_str += " " * indent - if color and COLORIZE: - msg = [(color, " ".join(map(str, msg)))] - if prefix_str: - msg.insert(0, ("", prefix_str + " ")) - if label: - msg.insert(0, ("fg:ansimagenta", label + "\n")) - print_formatted(FormattedText(msg), **kwargs) - return - if label: - print(label, **kwargs) - if prefix_str: - msg = (prefix_str, *msg) - print(*msg, **kwargs) - - -def output_reader(handle, callback, *args, **kwargs): - for line in iter(handle.readline, b""): - if not line: - break - run_in_terminal(functools.partial(callback, line, *args)) - - -def log_msg(*msg, color="fg:ansimagenta", **kwargs): - run_in_terminal(lambda: print_ext(*msg, color=color, **kwargs)) - - -def log_json(data, **kwargs): - run_in_terminal(lambda: print_json(data, **kwargs)) - - -def log_status(status: str, **kwargs): - log_msg(f"\n{status}", color="bold", **kwargs) - - -def flatten(args): - for arg in args: - if isinstance(arg, (list, tuple)): - yield from flatten(arg) - else: - yield arg - - -def prompt_init(): - if hasattr(prompt_init, "_called"): - return - prompt_init._called = True - use_asyncio_event_loop() - - -async def prompt(*args, **kwargs): - prompt_init() - with patch_stdout(): - try: - while True: - tmp = await prompt_toolkit.prompt(*args, async_=True, **kwargs) - if tmp: - break - return tmp - except EOFError: - return None - - -async def prompt_loop(*args, **kwargs): - while True: - option = await prompt(*args, **kwargs) - yield option - - -class DurationTimer: - def __init__(self, label: str = None, callback=None): - self.callback = callback - self.duration = None - self.label = label - self.last_error = None - self.total = 0.0 - self.init_time = self.now() - self.start_time = None - self.stop_time = None - self.running = False - - @classmethod - def now(cls): - return default_timer() - - def start(self): - self.start_time = self.now() - self.running = True - - def stop(self): - if not self.running: - return - self.stop_time = self.now() - self.duration = self.stop_time - self.start_time - self.running = False - self.total += self.duration - if self.callback: - self.callback(self) - - def cancel(self): - self.running = False - - def reset(self): - self.duration = None - self.total = 0.0 - self.last_error = None - restart = False - if self.running: - self.stop() - restart = True - self.start_time = None - self.stop_time = None - if restart: - self.start() - - def __enter__(self): - self.start() - return self - - def __exit__(self, err_type, err_value, err_tb): - self.last_error = err_value - self.stop() - - -def log_timer(label: str, show: bool = True, logger=None, **kwargs): - logger = logger or log_msg - cb = ( - ( - lambda timer: timer.last_error - or logger(timer.label, f"{timer.duration:.2f}s", **kwargs) - ) - if show - else None - ) - return DurationTimer(label, cb) - - -def progress(*args, **kwargs): - return ProgressBar(*args, **kwargs) - - -def require_indy(): - try: - from indy.libindy import _cdll - - _cdll() - except ImportError: - print("python3-indy module not installed") - sys.exit(1) - except OSError: - print("libindy shared library could not be loaded") - sys.exit(1) - - # Used to extract a DID from a schema or credential definition id def extract_did(id): split = id.split(":") diff --git a/requirements.txt b/requirements.txt index 3fc37525..b1b3944c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,13 +1,24 @@ -aiohttp~=3.7.3 -asyncio -black -prompt_toolkit -pygments -PyPubSub -python-dotenv -termcolor -pillow -qrcode -beautifulsoup4 -pytest~=6.2 -pytest_asyncio \ No newline at end of file +aiohttp==3.6.2 +appdirs==1.4.4 +async-timeout==3.0.1 +asyncio==3.4.3 +attrs==21.2.0 +black==21.5b1 +certifi==2020.12.5 +chardet==3.0.4 +click==8.0.1 +idna==3.1 +iniconfig==1.1.1 +multidict==4.7.6 +mypy-extensions==0.4.3 +packaging==20.9 +pathspec==0.8.1 +pluggy==1.0.0.dev0 +py==1.10.0 +pyparsing==3.0.0b2 +Pypubsub==4.0.3 +pytest==6.2.4 +pytest-asyncio==0.15.1 +regex==2021.4.4 +toml==0.10.2 +yarl==1.6.3 diff --git a/setup.py b/setup.py index db7da70d..2ed9cdfb 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def parse_requirements(filename): if __name__ == "__main__": setup( name=PACKAGE_NAME, - version="0.2.0", + version="0.2.3", description="A simple python package for controlling an aries agent through the admin-api interface", long_description=long_description, long_description_content_type="text/markdown", From 03b426183929b62674e5a56f6680453d7332dd56 Mon Sep 17 00:00:00 2001 From: wip-abramson Date: Thu, 20 May 2021 16:41:32 +0100 Subject: [PATCH 2/2] revert aiohttp and bump version --- requirements.txt | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index b1b3944c..40a85531 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -aiohttp==3.6.2 +aiohttp>=3.7.4 appdirs==1.4.4 async-timeout==3.0.1 asyncio==3.4.3 diff --git a/setup.py b/setup.py index 2ed9cdfb..48e488b4 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ def parse_requirements(filename): if __name__ == "__main__": setup( name=PACKAGE_NAME, - version="0.2.3", + version="0.2.4", description="A simple python package for controlling an aries agent through the admin-api interface", long_description=long_description, long_description_content_type="text/markdown",