Skip to content

Commit

Permalink
Merge pull request #2 from Nasdaq/docs
Browse files Browse the repository at this point in the history
docs: initial infra for docs
  • Loading branch information
SamDanielThangarajan authored Jan 6, 2024
2 parents f2512ed + ca52279 commit 6aa8be4
Show file tree
Hide file tree
Showing 22 changed files with 728 additions and 90 deletions.
13 changes: 13 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[FORMAT]
max-line-length=180

[MASTER]
disable=
C0114, #missing-module-docstring
C0115, #missing-class-docstring
C0116, #missing-function-docstring
W0223, #abstract-method
E1101, #no-member
R0903, #too-few-public-methods
R0902, #too-many-instance-attributes
W0707, #too-many-statements
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Nasdaq
Copyright (c) 2024 Nasdaq

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
9 changes: 9 additions & 0 deletions docs/api_reference.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
API Reference
=============

.. toctree::
:maxdepth: 1
:caption: Contents:

common_api_reference
soup_api_reference
10 changes: 10 additions & 0 deletions docs/common_api_reference.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Common api reference
====================

.. automodule:: nasdaq_protocols.common.message_queue

.. automodule:: nasdaq_protocols.common.session

.. automodule:: nasdaq_protocols.common.types

.. automodule:: nasdaq_protocols.common.utils
38 changes: 38 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os
import sys
sys.path.insert(0, os.path.abspath('../src'))

# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'Nasdaq Protocols Python Library'
copyright = 'Copyright (c) 2024 Nasdaq'
author = 'Sam Daniel Thangarajan'
release = '0.0.1'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
extensions = ['sphinx.ext.autodoc',]
templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']


autodoc_member_order = 'bysource'
autodoc_default_options = {
'members': True,
'member-order': 'bysource',
'show-inheritance': True,
}
autodoc_typehints = 'description'

# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
# https://www.sphinx-doc.org/en/master/usage/theming.html [themes]
html_theme = 'agogo'
html_static_path = ['_static']
25 changes: 25 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. nasdaq-protocols documentation master file, created by
sphinx-quickstart on Thu Jan 4 23:40:15 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to nasdaq-protocols's documentation!
============================================

.. automodule:: nasdaq_protocols

.. toctree::
:maxdepth: 1
:caption: Contents:

install
api_reference
user_guide


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
8 changes: 8 additions & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Installation
============

To use nasdaq-protocols, install it using pip:

.. code-block:: console
(.venv) $ pip install nasdaq-protocols
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
24 changes: 24 additions & 0 deletions docs/soup_api_reference.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
SOUP api reference
==================

Soup
----
.. automodule:: nasdaq_protocols.soup


Soup Session
^^^^^^^^^^^^
.. autoclass:: nasdaq_protocols.soup.session.SoupSessionId
:show-inheritance: True

.. autoclass:: nasdaq_protocols.soup.session.SoupClientSession
:show-inheritance: True
:undoc-members: ['send_heartbeat']

.. autoclass:: nasdaq_protocols.soup.session.SoupServerSession
:show-inheritance: True


Soup Messages
^^^^^^^^^^^^^
.. automodule:: nasdaq_protocols.soup.core
35 changes: 35 additions & 0 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
User Guide
==========

SOUP
____

Connect to Soup session
-----------------------
Example of connecting to a Soup session and receiving messages.

.. code-block:: python
import asyncio
from nasdaq_protocols import Soup
stopped = asyncio.Event()
async def on_msg(msg):
print(msg)
async def on_close():
stopped.set()
print('closed')
async def main():
session = await soup.connect_async(
('host', port), 'user', 'password',
sequence=1, on_msg_coro=on_msg, on_close_coro=on_close
)
await stopped.wait()
if __name__ == '__main__':
asyncio.run(main())
*A simple soup tail program*
9 changes: 5 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
attrs==23.1.0
pytest==7.4.3
pytest-cov==4.1.0
pytest-asyncio==0.23.0
attrs>=23.1
pytest>=7.4
pytest-cov>=4.1
pytest-asyncio>=0.23
sphinx
4 changes: 4 additions & 0 deletions src/nasdaq_protocols/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
nasdaq-protocols contains client side implementations of the various
publicly available protocols used by Nasdaq ecosystem.
"""
44 changes: 39 additions & 5 deletions src/nasdaq_protocols/common/message_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ def __attrs_post_init__(self):
self._msg_queue = asyncio.Queue()
self.start_dispatching(self.on_msg_coro)

async def put(self, msg: Any):
async def put(self, msg: Any) -> None:
"""
put an entry into the queue.
:param msg: Any
"""
await self._msg_queue.put(msg)

async def get(self):
Expand All @@ -51,9 +55,17 @@ async def get(self):
return msg if msg else await self._blocking_read()

def put_nowait(self, msg: Any):
"""
put an entry into the queue.
:param msg: Any
"""
self._msg_queue.put_nowait(msg)

def get_nowait(self):
def get_nowait(self) -> Any | None:
"""
get an entry from the queue. This is a non-blocking call.
:return: entry from the queue or None if queue is empty.
"""
if self._dispatcher_task:
raise StateError(f'{self.session_id}-dispatcher, Dispatcher is running, cannot use get_no_wait')
msg = None
Expand All @@ -66,6 +78,17 @@ def get_nowait(self):

@asynccontextmanager
async def pause_dispatching(self):
"""
This is a context manager that pauses the dispatcher::
queue = DispatchableMessageQueue(session_id, on_msg_coro)
queue.get() # will raise an exception
async with queue.pause_dispatching():
queue.get() # will not raise an exception
"""
if not self._dispatcher_task:
raise StateError('Dispatcher is not running, cannot pause')
self._dispatcher_task = await stop_task(self._dispatcher_task)
Expand All @@ -76,21 +99,32 @@ async def pause_dispatching(self):
self._dispatcher_task = asyncio.create_task(self._start_dispatching(), name=f'{self.session_id}-dispatcher')
self.log.debug('%s> queue dispatcher resumed.', self.session_id)

def start_dispatching(self, on_msg_coro: DispatcherCoro):
def start_dispatching(self, on_msg_coro: DispatcherCoro) -> None:
"""
Start dispatching messages from the queue to the coro.
:param on_msg_coro:
"""
if self._dispatcher_task:
raise StateError('Dispatcher is already running, cannot start')
if on_msg_coro:
self.on_msg_coro = on_msg_coro
self._dispatcher_task = asyncio.create_task(self._start_dispatching(), name=f'{self.session_id}-dispatcher')
self.log.debug('%s> queue dispatcher started.', self.session_id)

async def stop(self):
async def stop(self) -> None:
"""
Stop the queue.
"""
if not self._closed:
self._closed = True
self._dispatcher_task = await stop_task(self._dispatcher_task)
self._recv_task = await stop_task(self._recv_task)

def is_stopped(self):
def is_stopped(self) -> bool:
"""
:return: True if the queue is stopped.
"""
return self._closed

async def _start_dispatching(self):
Expand Down
Loading

0 comments on commit 6aa8be4

Please sign in to comment.