From 76b471e84c4f8c64d0b713934df217c4d82fa977 Mon Sep 17 00:00:00 2001 From: davebshow <davebshow@gmail.com> Date: Sat, 27 Jun 2015 16:29:04 -0400 Subject: [PATCH] refactored tests, working on API, started real docs --- aiogremlin/client.py | 205 +++++++++++------- aiogremlin/connector.py | 76 +++---- aiogremlin/contextmanager.py | 23 +- aiogremlin/response.py | 2 +- aiogremlin/subprotocol.py | 15 +- docs/Makefile | 192 +++++++++++++++++ docs/aiogremlin.rst | 58 +++++ docs/conf.py | 288 +++++++++++++++++++++++++ docs/getting_started.rst | 2 + docs/index.rst | 23 ++ docs/modules.rst | 8 + docs/usage.rst | 2 + setup.py | 4 +- tests/tests.py | 407 +++++++++++------------------------ 14 files changed, 888 insertions(+), 417 deletions(-) create mode 100644 docs/Makefile create mode 100644 docs/aiogremlin.rst create mode 100644 docs/conf.py create mode 100644 docs/getting_started.rst create mode 100644 docs/index.rst create mode 100644 docs/modules.rst create mode 100644 docs/usage.rst diff --git a/aiogremlin/client.py b/aiogremlin/client.py index 1297a25..93a0d58 100644 --- a/aiogremlin/client.py +++ b/aiogremlin/client.py @@ -1,6 +1,7 @@ """Client for the Tinkerpop 3 Gremlin Server.""" import asyncio +import uuid import aiohttp @@ -10,49 +11,84 @@ from aiogremlin.log import logger, INFO from aiogremlin.connector import GremlinConnector from aiogremlin.subprotocol import gremlin_response_parser, GremlinWriter -__all__ = ("GremlinClient", "GremlinClientSession") +__all__ = ("submit", "SimpleGremlinClient", "GremlinClient", + "GremlinClientSession") -@asycnio.coroutine -def submit(gremlin, *, - url='ws://localhost:8182/', - bindings=None, - lang="gremlin-groovy", - op="eval", - processor="", - connector=None): +class BaseGremlinClient: - connector = aiohttp.TCPConnector(force_close=True) + def __init__(self, *, lang="gremlin-groovy", op="eval", processor="", + loop=None, verbose=False): + self._lang = lang + self._op = op + self._processor = processor + self._loop = loop or asyncio.get_event_loop() + self._closed = False + if verbose: + logger.setLevel(INFO) - client_session = aiohttp.ClientSession( - connector=connector, ws_response_class=GremlinClientWebSocketResponse) + @property + def loop(self): + return self._loop - gremlin_client = GremlinClient(url=url, connector=client_session) + @property + def op(self): + return self._op - try: - resp = yield from gremlin_client.submit( - gremlin, bindings=bindings, lang=lang, op=op, processor=processor) + @property + def processor(self): + return self._processor - return resp + @property + def lang(self): + return self._lang - finally: - gremlin_client.detach() - client_session.detach() + def submit(self): + raise NotImplementedError + + @asyncio.coroutine + def execute(self, gremlin, *, bindings=None, lang=None, + op=None, processor=None, binary=True): + """ + """ + lang = lang or self.lang + op = op or self.op + processor = processor or self.processor + resp = yield from self.submit(gremlin, bindings=bindings, lang=lang, + op=op, processor=processor, + binary=binary) + + return (yield from resp.get()) -class SimpleGremlinClient: +class SimpleGremlinClient(BaseGremlinClient): - def __init__(self, connection, *, loop=None, verbose=False): + def __init__(self, connection, *, lang="gremlin-groovy", op="eval", + processor="", loop=None, verbose=False): """This class is primarily designed to be used in the context `manager""" - self._loop = loop or asyncio.get_event_loop() + super().__init__(lang=lang, op=op, processor=processor, loop=loop, + verbose=verbose) self._connection = connection - if verbose: - logger.setLevel(INFO) + + @asyncio.coroutine + def close(self): + if self._closed: + return + self._closed = True + try: + yield from self._connection.release() + finally: + self._connection = None + + @property + def closed(self): + return (self._closed or self._connection.closed or + self._connection is None) @asyncio.coroutine def submit(self, gremlin, *, bindings=None, lang="gremlin-groovy", - op="eval", processor=""): + op="eval", processor="", session=None, binary=True): """ """ writer = GremlinWriter(self._connection) @@ -62,36 +98,33 @@ class SimpleGremlinClient: binary=binary) return GremlinResponse(self._connection, - pool=self._pool, session=session, loop=self._loop) -class GremlinClient: +class GremlinClient(BaseGremlinClient): - def __init__(self, url='ws://localhost:8182/', loop=None, + def __init__(self, *, url='ws://localhost:8182/', loop=None, protocols=None, lang="gremlin-groovy", op="eval", - processor="", timeout=None, verbose=False, - session=None, connector=None): + processor="", timeout=None, verbose=False, connector=None): """ """ - # Maybe getter setter for some of these: url, session, lang, op - self.url = url - self._loop = loop or asyncio.get_event_loop() - self.lang = lang or "gremlin-groovy" - self.op = op or "eval" - self.processor = processor or "" + super().__init__(lang=lang, op=op, processor=processor, loop=loop, + verbose=verbose) + self._url = url self._timeout = timeout - self._session = session - if verbose: - logger.setLevel(INFO) + self._session = None if connector is None: - connector = GremlinConnector() + connector = GremlinConnector(loop=self._loop) self._connector = connector @property - def loop(self): - return self._loop + def url(self): + return self._url + + @url.setter + def url(self, value): + self._url = value @property def closed(self): @@ -99,12 +132,9 @@ class GremlinClient: @asyncio.coroutine def close(self): - if self._closed: return - self._closed = True - try: yield from self._connector.close() finally: @@ -133,51 +163,44 @@ class GremlinClient: return GremlinResponse(ws, session=self._session, loop=self._loop) - @asyncio.coroutine - def execute(self, gremlin, *, bindings=None, lang=None, - op=None, processor=None, binary=True): - """ - """ - lang = lang or self.lang - op = op or self.op - processor = processor or self.processor - - resp = yield from self.submit(gremlin, bindings=bindings, lang=lang, - op=op, processor=processor, - binary=binary) - - return (yield from resp.get()) - class GremlinClientSession(GremlinClient): - def __init__(self, url='ws://localhost:8182/', loop=None, + def __init__(self, *, url='ws://localhost:8182/', loop=None, protocols=None, lang="gremlin-groovy", op="eval", - processor="", timeout=None, verbose=False, - session=None, connector=None): - - super().__init__(url=url, loop=loop, protocols=protocols, lang=lang, - op=op, processor=processor, timeout=timeout, + processor="session", session=None, timeout=None, + verbose=False, connector=None): + """ + """ + super().__init__(url=url, protocols=protocols, lang=lang, op=op, + processor=processor, loop=loop, timeout=timeout, verbose=verbose, connector=connector) if session is None: - session = str(uuid4.uuid4()) + session = str(uuid.uuid4()) self._session = session - def set_session(self): - pass + @property + def session(self): + return self._session + + @session.setter + def session(self, value): + self._session = value - def change_session(self): - pass + def reset_session(self, session=None): + if session is None: + session = str(uuid.uuid4()) + self._session = session + return self._session class GremlinResponse: def __init__(self, ws, *, session=None, loop=None): - # Add timeout for read self._loop = loop or asyncio.get_event_loop() self._session = session - self._stream = GremlinResponseStream(ws, oop=self._loop) + self._stream = GremlinResponseStream(ws, loop=self._loop) @property def stream(self): @@ -224,4 +247,38 @@ class GremlinResponseStream: message = yield from self._stream.read() except RequestError: yield from self._ws.release() + raise return message + + +@asyncio.coroutine +def submit(gremlin, *, + url='ws://localhost:8182/', + bindings=None, + lang="gremlin-groovy", + op="eval", + processor="", + connector=None, + loop=None): + + if loop is None: + loop = asyncio.get_event_loop() + + connector = aiohttp.TCPConnector(force_close=True, loop=loop) + + client_session = aiohttp.ClientSession( + connector=connector, loop=loop, + ws_response_class=GremlinClientWebSocketResponse) + + gremlin_client = GremlinClient(url=url, loop=loop, + connector=client_session) + + try: + resp = yield from gremlin_client.submit( + gremlin, bindings=bindings, lang=lang, op=op, processor=processor) + + return resp + + finally: + gremlin_client.detach() + client_session.detach() diff --git a/aiogremlin/connector.py b/aiogremlin/connector.py index fb149ab..e623e48 100644 --- a/aiogremlin/connector.py +++ b/aiogremlin/connector.py @@ -1,5 +1,7 @@ import asyncio +from contextlib import contextmanager + from aiowebsocketclient import WebSocketConnector from aiogremlin.response import GremlinClientWebSocketResponse @@ -11,41 +13,41 @@ __all__ = ("GremlinConnector",) class GremlinConnector(WebSocketConnector): - def __init__(self, *args, **kwargs): - kwargs["ws_response_class"] = GremlinClientWebSocketResponse - super().__init__(*args, **kwargs) - - def create_client(self, *, url='ws://localhost:8182/', loop=None, - protocol=None, lang="gremlin-groovy", op="eval", - processor="", verbose=False): - - return GremlinClient(url=url, - loop=loop, - protocol=protocol, - lang=lang, - op=op, - processor=processor, - connector=self - verbose=verbose) - - def create_client_session(self, *, url='ws://localhost:8182/', loop=None, - protocol=None, lang="gremlin-groovy", op="eval", - processor="", connector=self, verbose=False): - - return GremlinClientSession(url=url, - loop=loop, - protocol=protocol, - lang=lang, - op=op, - processor=processor, - connector=self - verbose=verbose) - - # # Something like - # @contextmanager - # @asyncio.coroutine - # def connect(self, url, etc): - # pass + def __init__(self, *, conn_timeout=None, force_close=False, limit=1024, + client_session=None, loop=None): + """ + :param float conn_timeout: timeout for establishing connection + (optional). Values ``0`` or ``None`` + mean no timeout + :param bool force_close: close underlying sockets after + releasing connection + :param int limit: limit for total open websocket connections + :param aiohttp.client.ClientSession client_session: Underlying HTTP + session used to + to establish + websocket + connections + :param loop: `event loop` + used for processing HTTP requests. + If param is ``None``, `asyncio.get_event_loop` + is used for getting default event loop. + (optional) + :param ws_response_class: WebSocketResponse class implementation. + ``ClientWebSocketResponse`` by default + """ + super().__init__(conn_timeout=conn_timeout, force_close=force_close, + limit=limit, client_session=client_session, loop=loop, + ws_response_class=GremlinClientWebSocketResponse) + + @contextmanager + @asyncio.coroutine + def connection(self, url, *, + protocols=(), + timeout=10.0, + autoclose=True, + autoping=True): + ws = yield from self.ws_connect(url='ws://localhost:8182/') + return ConnectionContextManager(ws) # aioredis style def __enter__(self): @@ -56,5 +58,5 @@ class GremlinConnector(WebSocketConnector): pass def __iter__(self): - conn = yield from self.ws_connect(url='ws://localhost:8182/') - return ConnectionContextManager(client) + ws = yield from self.ws_connect(url='ws://localhost:8182/') + return ConnectionContextManager(ws) diff --git a/aiogremlin/contextmanager.py b/aiogremlin/contextmanager.py index 04cc3ed..6216ba4 100644 --- a/aiogremlin/contextmanager.py +++ b/aiogremlin/contextmanager.py @@ -1,24 +1,19 @@ -from aiogremlin.client import SimpleGremlinClient - - class ConnectionContextManager: - __slots__ = ("_conn") + __slots__ = ("_ws") - def __init__(self, conn): - self._conn = conn - self._client = SimpleGremlinClient(conn) + def __init__(self, ws): + self._ws = ws def __enter__(self): - if self._conn.closed: + if self._ws.closed: raise RuntimeError("Connection closed unexpectedly.") - return self._client + return self._ws def __exit__(self, exception_type, exception_value, traceback): try: - self._conn._close_code = 1000 - self._conn._closing = True - self._conn._close() + self._ws._close_code = 1000 + self._ws._closing = True + self._ws._do_close() finally: - self._conn = None - self._client = None + self._ws = None diff --git a/aiogremlin/response.py b/aiogremlin/response.py index c216f13..b22e0e7 100644 --- a/aiogremlin/response.py +++ b/aiogremlin/response.py @@ -11,7 +11,7 @@ from aiowebsocketclient.connector import ClientWebSocketResponse from aiogremlin.exceptions import SocketClientError from aiogremlin.log import INFO, logger -__all__ = ('GremlinClientWebSocketResponse') +__all__ = ('GremlinClientWebSocketResponse',) class GremlinClientWebSocketResponse(ClientWebSocketResponse): diff --git a/aiogremlin/subprotocol.py b/aiogremlin/subprotocol.py index 4025c4f..dc2dfbf 100644 --- a/aiogremlin/subprotocol.py +++ b/aiogremlin/subprotocol.py @@ -1,6 +1,5 @@ """Implements the Gremlin Server subprotocol.""" -import asyncio import collections import uuid @@ -10,9 +9,8 @@ except ImportError: import json from aiogremlin.exceptions import RequestError, GremlinServerError -from aiogremlin.log import logger -__all__ = ("GremlinWriter",) +__all__ = ("GremlinWriter", "gremlin_response_parser") Message = collections.namedtuple( @@ -86,10 +84,9 @@ class GremlinWriter: "language": lang } } - if processor == "session": - # idk about this autogenerate here with new class - session = session or str(uuid.uuid4()) - message["args"]["session"] = session - logger.info( - "Session ID: {}".format(message["args"]["session"])) + if session is None: + if processor == "session": + raise RuntimeError("session processor requires a session id") + else: + message["args"].update({"session": session}) return message diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..6b407a7 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,192 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext + +help: + @echo "Please use \`make <target>' where <target> is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " applehelp to make an Apple Help Book" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + @echo " coverage to run coverage check of the documentation (if enabled)" + +clean: + rm -rf $(BUILDDIR)/* + +html: + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/aiogremlin.qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/aiogremlin.qhc" + +applehelp: + $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp + @echo + @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." + @echo "N.B. You won't be able to view it unless you put it in" \ + "~/Library/Documentation/Help or install it in your application" \ + "bundle." + +devhelp: + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $$HOME/.local/share/devhelp/aiogremlin" + @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/aiogremlin" + @echo "# devhelp" + +epub: + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +latex: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +latexpdfja: + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +coverage: + $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage + @echo "Testing of coverage in the sources finished, look at the " \ + "results in $(BUILDDIR)/coverage/python.txt." + +xml: + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +pseudoxml: + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/docs/aiogremlin.rst b/docs/aiogremlin.rst new file mode 100644 index 0000000..9e90044 --- /dev/null +++ b/docs/aiogremlin.rst @@ -0,0 +1,58 @@ +API +=== + +aiogremlin.client module +------------------------ + +.. automodule:: aiogremlin.client + :members: + :undoc-members: + :show-inheritance: + +aiogremlin.connector module +--------------------------- + +.. automodule:: aiogremlin.connector + :members: + :undoc-members: + :show-inheritance: + +aiogremlin.contextmanager module +-------------------------------- + +.. automodule:: aiogremlin.contextmanager + :members: + :undoc-members: + :show-inheritance: + +aiogremlin.exceptions module +---------------------------- + +.. automodule:: aiogremlin.exceptions + :members: + :undoc-members: + :show-inheritance: + +aiogremlin.log module +--------------------- + +.. automodule:: aiogremlin.log + :members: + :undoc-members: + :show-inheritance: + +aiogremlin.response module +-------------------------- + +.. automodule:: aiogremlin.response + :members: + :undoc-members: + :show-inheritance: + +aiogremlin.subprotocol module +----------------------------- + +.. automodule:: aiogremlin.subprotocol + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..af3e9c0 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,288 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# aiogremlin documentation build configuration file, created by +# sphinx-quickstart on Sat Jun 27 13:50:06 2015. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import sys +import os +import shlex + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +#sys.path.insert(0, os.path.abspath('.')) + +# -- General configuration ------------------------------------------------ + +# If your documentation needs a minimal Sphinx version, state it here. +#needs_sphinx = '1.0' +sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + 'sphinx.ext.autodoc', + 'sphinx.ext.viewcode', +] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +# source_suffix = ['.rst', '.md'] +source_suffix = '.rst' + +# The encoding of source files. +#source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = 'aiogremlin' +copyright = '2015, David M. Brown' +author = 'David M. Brown' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '0.0.10' +# The full version, including alpha/beta/rc tags. +release = '0.0.10' + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +#today = '' +# Else, today_fmt is used as the format for a strftime call. +#today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = [] + +# The reST default role (used for this markup: `text`) to use for all +# documents. +#default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +#add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +#add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +#show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +#modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +#keep_warnings = False + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + + +# -- Options for HTML output ---------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'alabaster' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +#html_theme_options = {} + +# Add any paths that contain custom themes here, relative to this directory. +#html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# "<project> v<release> documentation". +#html_title = None + +# A shorter title for the navigation bar. Default is the same as html_title. +#html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +#html_logo = None + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +#html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# Add any extra paths that contain custom files (such as robots.txt or +# .htaccess) here, relative to this directory. These files are copied +# directly to the root of the documentation. +#html_extra_path = [] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +#html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +#html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +#html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +#html_additional_pages = {} + +# If false, no module index is generated. +#html_domain_indices = True + +# If false, no index is generated. +#html_use_index = True + +# If true, the index is split into individual pages for each letter. +#html_split_index = False + +# If true, links to the reST sources are added to the pages. +#html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +#html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +#html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a <link> tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +#html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +#html_file_suffix = None + +# Language to be used for generating the HTML full-text search index. +# Sphinx supports the following languages: +# 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' +# 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr' +#html_search_language = 'en' + +# A dictionary with options for the search language support, empty by default. +# Now only 'ja' uses this config value +#html_search_options = {'type': 'default'} + +# The name of a javascript file (relative to the configuration directory) that +# implements a search results scorer. If empty, the default will be used. +#html_search_scorer = 'scorer.js' + +# Output file base name for HTML help builder. +htmlhelp_basename = 'aiogremlindoc' + +# -- Options for LaTeX output --------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +#'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +#'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +#'preamble': '', + +# Latex figure (float) alignment +#'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, +# author, documentclass [howto, manual, or own class]). +latex_documents = [ + (master_doc, 'aiogremlin.tex', 'aiogremlin Documentation', + 'David M. Brown', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +#latex_logo = None + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +#latex_use_parts = False + +# If true, show page references after internal links. +#latex_show_pagerefs = False + +# If true, show URL addresses after external links. +#latex_show_urls = False + +# Documents to append as an appendix to all manuals. +#latex_appendices = [] + +# If false, no module index is generated. +#latex_domain_indices = True + + +# -- Options for manual page output --------------------------------------- + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + (master_doc, 'aiogremlin', 'aiogremlin Documentation', + [author], 1) +] + +# If true, show URL addresses after external links. +#man_show_urls = False + + +# -- Options for Texinfo output ------------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + (master_doc, 'aiogremlin', 'aiogremlin Documentation', + author, 'aiogremlin', 'One line description of project.', + 'Miscellaneous'), +] + +# Documents to append as an appendix to all manuals. +#texinfo_appendices = [] + +# If false, no module index is generated. +#texinfo_domain_indices = True + +# How to display URL addresses: 'footnote', 'no', or 'inline'. +#texinfo_show_urls = 'footnote' + +# If true, do not generate a @detailmenu in the "Top" node's menu. +#texinfo_no_detailmenu = False diff --git a/docs/getting_started.rst b/docs/getting_started.rst new file mode 100644 index 0000000..3ce1d78 --- /dev/null +++ b/docs/getting_started.rst @@ -0,0 +1,2 @@ +Getting Started +=============== diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..ac9c9f6 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,23 @@ +.. aiogremlin documentation master file, created by + sphinx-quickstart on Sat Jun 27 13:50:06 2015. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Welcome to aiogremlin's documentation! +====================================== + +Contents: + +.. toctree:: + :maxdepth: 3 + + getting_started + modules + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/docs/modules.rst b/docs/modules.rst new file mode 100644 index 0000000..0785e37 --- /dev/null +++ b/docs/modules.rst @@ -0,0 +1,8 @@ +aiogremlin +========== + +.. toctree:: + :maxdepth: 4 + + usage + aiogremlin diff --git a/docs/usage.rst b/docs/usage.rst new file mode 100644 index 0000000..b2c0348 --- /dev/null +++ b/docs/usage.rst @@ -0,0 +1,2 @@ +Using aiogremlin +================ diff --git a/setup.py b/setup.py index 93ce9ee..13b44cf 100644 --- a/setup.py +++ b/setup.py @@ -12,8 +12,8 @@ setup( long_description=open("README.txt").read(), packages=["aiogremlin", "tests"], install_requires=[ - "aiohttp==0.16.3", - "aiowebsocketclient==0.0.3" + "aiohttp==0.16.5", + "aiowebsocketclient==0.0.2" ], test_suite="tests", classifiers=[ diff --git a/tests/tests.py b/tests/tests.py index 6d31329..f924506 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2,75 +2,101 @@ """ import asyncio -import itertools import unittest import uuid -import aiohttp -from aiogremlin import (GremlinClient, RequestError, GremlinServerError, - SocketClientError, WebSocketPool, GremlinFactory, - create_client, GremlinWriter, GremlinResponse, - GremlinClientWebSocketResponse) +from aiogremlin import (submit, SimpleGremlinClient, GremlinConnector, + GremlinClient, GremlinClientSession) -class GremlinClientTests(unittest.TestCase): +class SubmitTest(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) - self.gc = GremlinClient("ws://localhost:8182/", loop=self.loop) def tearDown(self): - self.loop.run_until_complete(self.gc.close()) self.loop.close() - def test_connection(self): + def test_submit(self): + @asyncio.coroutine - def conn_coro(): - conn = yield from self.gc._acquire() - self.assertFalse(conn.closed) - return conn - conn = self.loop.run_until_complete(conn_coro()) - # Clean up the resource. - self.loop.run_until_complete(conn.close()) - - def test_sub(self): + def go(): + resp = yield from submit("4 + 4", bindings={"x": 4}, + loop=self.loop) + results = yield from resp.get() + return results + + results = self.loop.run_until_complete(go()) + self.assertEqual(results[0].data[0], 8) + + +class SimpleGremlinClientTest(unittest.TestCase): + + def setUp(self): + self.loop = asyncio.new_event_loop() + asyncio.set_event_loop(None) + self.connector = GremlinConnector(force_close=True, loop=self.loop) + + def tearDown(self): + self.loop.close() + + def test_submit(self): + @asyncio.coroutine def go(): - resp = yield from self.gc.execute("x + x", bindings={"x": 4}) - return resp + ws = yield from self.connector.ws_connect('ws://localhost:8182/') + client = SimpleGremlinClient(ws, loop=self.loop) + resp = yield from client.submit("4 + 4", bindings={"x": 4}) + results = yield from resp.get() + return results + results = self.loop.run_until_complete(go()) self.assertEqual(results[0].data[0], 8) + def test_close(self): + + @asyncio.coroutine + def go(): + ws = yield from self.connector.ws_connect('ws://localhost:8182/') + client = SimpleGremlinClient(ws, loop=self.loop) + yield from client.close() + self.assertTrue(client.closed) + self.assertTrue(ws.closed) + self.assertIsNone(client._connection) + + results = self.loop.run_until_complete(go()) + -class GremlinClientPoolTests(unittest.TestCase): +class GremlinClientTest(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) - pool = WebSocketPool("ws://localhost:8182/", loop=self.loop) - self.gc = GremlinClient(url="ws://localhost:8182/", - factory=GremlinFactory(loop=self.loop), - pool=pool, - loop=self.loop) + self.gc = GremlinClient(url="ws://localhost:8182/", loop=self.loop) def tearDown(self): self.loop.run_until_complete(self.gc.close()) self.loop.close() def test_connection(self): + @asyncio.coroutine - def conn_coro(): - conn = yield from self.gc._acquire() - self.assertFalse(conn.closed) - return conn - conn = self.loop.run_until_complete(conn_coro()) - # Clean up the resource. - self.loop.run_until_complete(conn.close()) - - def test_sub(self): - execute = self.gc.execute("x + x", bindings={"x": 4}) - results = self.loop.run_until_complete(execute) + def go(): + ws = yield from self.gc._connector.ws_connect(self.gc.url) + self.assertFalse(ws.closed) + yield from ws.close() + + self.loop.run_until_complete(go()) + + def test_execute(self): + + @asyncio.coroutine + def go(): + resp = yield from self.gc.execute("x + x", bindings={"x": 4}) + return resp + + results = self.loop.run_until_complete(go()) self.assertEqual(results[0].data[0], 8) def test_sub_waitfor(self): @@ -98,14 +124,6 @@ class GremlinClientPoolTests(unittest.TestCase): self.assertEqual(results[0].data[0], 8) self.loop.run_until_complete(stream_coro()) - def test_resp_get(self): - @asyncio.coroutine - def get_coro(): - conn = yield from self.gc.submit("x + x", bindings={"x": 4}) - results = yield from conn.get() - self.assertEqual(results[0].data[0], 8) - self.loop.run_until_complete(get_coro()) - def test_execute_error(self): execute = self.gc.execute("x + x g.asdfas", bindings={"x": 4}) try: @@ -115,122 +133,89 @@ class GremlinClientPoolTests(unittest.TestCase): error = True self.assertTrue(error) - def test_session_gen(self): - execute = self.gc.execute("x + x", processor="session", - bindings={"x": 4}) - results = self.loop.run_until_complete(execute) - self.assertEqual(results[0].data[0], 8) - - def test_session(self): - @asyncio.coroutine - def stream_coro(): - session = str(uuid.uuid4()) - resp = yield from self.gc.submit("x + x", bindings={"x": 4}, - session=session) - while True: - f = yield from resp.stream.read() - if f is None: - break - self.assertEqual(resp.session, session) - self.loop.run_until_complete(stream_coro()) - -class WebSocketPoolTests(unittest.TestCase): +class GremlinClientSessionTest(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) - self.pool = WebSocketPool("ws://localhost:8182/", - poolsize=2, - timeout=1, - loop=self.loop, - factory=GremlinFactory(loop=self.loop)) + self.gc = GremlinClientSession(url="ws://localhost:8182/", + loop=self.loop) + self.script1 = """graph = TinkerFactory.createModern() + g = graph.traversal(standard())""" + + self.script2 = "g.V().has('name','marko').out('knows').values('name')" def tearDown(self): - self.loop.run_until_complete(self.pool.close()) + self.loop.run_until_complete(self.gc.close()) self.loop.close() - def test_connect(self): + def test_session(self): @asyncio.coroutine - def conn(): - conn = yield from self.pool.acquire() - self.assertFalse(conn.closed) - self.pool.release(conn) - self.assertEqual(self.pool.num_active_conns, 0) + def go(): + yield from self.gc.execute(self.script1) + results = yield from self.gc.execute(self.script2) + return results - self.loop.run_until_complete(conn()) + results = self.loop.run_until_complete(go()) + self.assertTrue(len(results[0].data), 2) - def test_multi_connect(self): + def test_session_reset(self): @asyncio.coroutine - def conn(): - conn1 = yield from self.pool.acquire() - conn2 = yield from self.pool.acquire() - self.assertFalse(conn1.closed) - self.assertFalse(conn2.closed) - self.pool.release(conn1) - self.assertEqual(self.pool.num_active_conns, 1) - self.pool.release(conn2) - self.assertEqual(self.pool.num_active_conns, 0) + def go(): + yield from self.gc.execute(self.script1) + self.gc.reset_session() + results = yield from self.gc.execute(self.script2) + return results - self.loop.run_until_complete(conn()) + results = self.loop.run_until_complete(go()) + self.assertIsNone(results[0].data) - def test_timeout(self): + def test_session_manual_reset(self): @asyncio.coroutine - def conn(): - conn1 = yield from self.pool.acquire() - conn2 = yield from self.pool.acquire() - try: - conn3 = yield from self.pool.acquire() - timeout = False - except asyncio.TimeoutError: - timeout = True - self.assertTrue(timeout) + def go(): + yield from self.gc.execute(self.script1) + new_sess = str(uuid.uuid4()) + sess = self.gc.reset_session(session=new_sess) + self.assertEqual(sess, new_sess) + self.assertEqual(self.gc.session, new_sess) + results = yield from self.gc.execute(self.script2) + return results - self.loop.run_until_complete(conn()) + results = self.loop.run_until_complete(go()) + self.assertIsNone(results[0].data) - def test_socket_reuse(self): + def test_session_set(self): @asyncio.coroutine - def conn(): - conn1 = yield from self.pool.acquire() - conn2 = yield from self.pool.acquire() - try: - conn3 = yield from self.pool.acquire() - timeout = False - except asyncio.TimeoutError: - timeout = True - self.assertTrue(timeout) - self.pool.release(conn2) - conn3 = yield from self.pool.acquire() - self.assertFalse(conn1.closed) - self.assertFalse(conn3.closed) - self.assertEqual(conn2, conn3) - - self.loop.run_until_complete(conn()) - - def test_socket_repare(self): + def go(): + yield from self.gc.execute(self.script1) + new_sess = str(uuid.uuid4()) + self.gc.session = new_sess + self.assertEqual(self.gc.session, new_sess) + results = yield from self.gc.execute(self.script2) + return results + + results = self.loop.run_until_complete(go()) + self.assertIsNone(results[0].data) + + def test_resp_session(self): @asyncio.coroutine - def conn(): - conn1 = yield from self.pool.acquire() - conn2 = yield from self.pool.acquire() - self.assertFalse(conn1.closed) - self.assertFalse(conn2.closed) - yield from conn1.close() - yield from conn2.close() - self.assertTrue(conn2.closed) - self.assertTrue(conn2.closed) - self.pool.release(conn1) - self.pool.release(conn2) - conn1 = yield from self.pool.acquire() - conn2 = yield from self.pool.acquire() - self.assertFalse(conn1.closed) - self.assertFalse(conn2.closed) - - self.loop.run_until_complete(conn()) + def go(): + session = str(uuid.uuid4()) + self.gc.session = session + resp = yield from self.gc.submit("x + x", bindings={"x": 4}) + while True: + f = yield from resp.stream.read() + if f is None: + break + self.assertEqual(resp.session, session) + + self.loop.run_until_complete(go()) class ContextMngrTest(unittest.TestCase): @@ -238,165 +223,27 @@ class ContextMngrTest(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) - self.pool = WebSocketPool("ws://localhost:8182/", - poolsize=1, - loop=self.loop, - factory=GremlinFactory(loop=self.loop), - max_retries=0) + self.connector = GremlinConnector(loop=self.loop) def tearDown(self): - self.loop.run_until_complete(self.pool.close()) + self.loop.run_until_complete(self.connector.close()) self.loop.close() - @asyncio.coroutine - def _check_closed(self): - conn = self.pool._pool.get_nowait() - self.assertTrue(conn.closed) - writer = GremlinWriter(conn) - try: - conn = yield from writer.write("1 + 1") - error = False - except RuntimeError: - error = True - self.assertTrue(error) - def test_connection_manager(self): results = [] @asyncio.coroutine def go(): - with (yield from self.pool) as conn: - writer = GremlinWriter(conn) - conn = writer.write("1 + 1") - resp = GremlinResponse(conn, pool=self.pool, loop=self.loop) + with (yield from self.connector) as conn: + client = SimpleGremlinClient(conn, loop=self.loop) + resp = yield from client.submit("1 + 1") while True: mssg = yield from resp.stream.read() if mssg is None: break results.append(mssg) - # Test that connection was closed - yield from self._check_closed() - self.loop.run_until_complete(go()) - - def test_connection_manager_with_client(self): - @asyncio.coroutine - def go(): - with (yield from self.pool) as conn: - gc = GremlinClient(connection=conn, loop=self.loop) - resp = yield from gc.submit("1 + 1") - self.assertEqual(conn, resp.stream._conn) - result = yield from resp.get() - self.assertEqual(result[0].data[0], 2) - - self.pool.release(conn) - # Test that connection was closed - yield from self._check_closed() - self.loop.run_until_complete(go()) - - def test_connection_manager_with_client_closed_conn(self): - @asyncio.coroutine - def go(): - with (yield from self.pool) as conn: - conn._closing = True - conn._close() - gc = GremlinClient(connection=conn, loop=self.loop) - resp = yield from gc.submit("1 + 1") - self.assertNotEqual(conn, resp.stream._conn) - result = yield from resp.get() - self.assertEqual(result[0].data[0], 2) - yield from resp.stream._conn.close() - # Test that connection was closed self.loop.run_until_complete(go()) - def test_connection_manager_error(self): - results = [] - - @asyncio.coroutine - def go(): - with (yield from self.pool) as conn: - writer = GremlinWriter(conn) - conn = writer.write("1 + 1 sdalfj;sd") - resp = GremlinResponse(conn, pool=self.pool, loop=self.loop) - try: - while True: - mssg = yield from resp.stream.read() - if mssg is None: - break - results.append(mssg) - except: - self.pool.release(conn) - raise - try: - self.loop.run_until_complete(go()) - err = False - except: - err = True - self.assertTrue(err) - self.loop.run_until_complete(self._check_closed()) - - -class GremlinClientPoolSessionTests(unittest.TestCase): - - def setUp(self): - self.loop = asyncio.new_event_loop() - asyncio.set_event_loop(None) - pool = WebSocketPool( - "ws://localhost:8182/", - loop=self.loop, - factory=aiohttp.ClientSession( - loop=self.loop, - ws_response_class=GremlinClientWebSocketResponse)) - self.gc = GremlinClient("ws://localhost:8182/", - pool=pool, - loop=self.loop) - - def tearDown(self): - self.gc._pool._factory.close() - self.loop.run_until_complete(self.gc.close()) - self.loop.close() - - def test_connection(self): - @asyncio.coroutine - def conn_coro(): - conn = yield from self.gc._acquire() - self.assertFalse(conn.closed) - return conn - conn = self.loop.run_until_complete(conn_coro()) - # Clean up the resource. - self.loop.run_until_complete(conn.close()) - - def test_sub(self): - execute = self.gc.execute("x + x", bindings={"x": 4}) - results = self.loop.run_until_complete(execute) - self.assertEqual(results[0].data[0], 8) - - def test_sub_waitfor(self): - sub1 = self.gc.execute("x + x", bindings={"x": 1}) - sub2 = self.gc.execute("x + x", bindings={"x": 2}) - sub3 = self.gc.execute("x + x", bindings={"x": 4}) - coro = asyncio.gather(*[asyncio.async(sub1, loop=self.loop), - asyncio.async(sub2, loop=self.loop), - asyncio.async(sub3, loop=self.loop)], - loop=self.loop) - # Here I am looking for resource warnings. - results = self.loop.run_until_complete(coro) - self.assertIsNotNone(results) - - -class CreateClientTests(unittest.TestCase): - - def test_pool_init(self): - @asyncio.coroutine - def go(loop): - gc = yield from create_client(poolsize=10, loop=loop) - self.assertEqual(gc._pool._pool.qsize(), 10) - yield from gc.close() - - loop = asyncio.new_event_loop() - asyncio.set_event_loop(None) - loop.run_until_complete(go(loop)) - loop.close() - if __name__ == "__main__": unittest.main() -- GitLab