docs #6

Merged
dscharnitzky merged 7 commits from docs into master 2020-04-08 23:05:32 +02:00
17 changed files with 157 additions and 64 deletions

View File

@ -39,10 +39,9 @@ steps:
- name: make_docs - name: make_docs
image: python:3.8 image: python:3.8
commands: commands:
- pip3 install Sphinx - pip3 install Sphinx sphinx_rtd_theme
- pip3 install -r requirements.txt - pip3 install -r requirements.txt
- cd docs - cd docs
- sphinx-apidoc -o source/ ../
- make html - make html
- name: build_docs - name: build_docs

1
.gitignore vendored
View File

@ -131,4 +131,3 @@ dmypy.json
#Pycharm #Pycharm
.idea/ .idea/
*.iml *.iml
docs/source/

7
README.rst Normal file
View File

@ -0,0 +1,7 @@
============
P2P Producer
============
Produced by GoldenPogácsa Inc.
TODO

14
app.py
View File

@ -1,5 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
"""
Main entry point, This module builds the producer from the submodules.
"""
import os import os
import random import random
import uuid import uuid
@ -10,10 +14,6 @@ from communicator import Communicator
from consumerlocator import ConsumerLocator from consumerlocator import ConsumerLocator
from messagesender import MessageSender from messagesender import MessageSender
"""
Main entry point, This module builds the producer from the submodules.
"""
__author__ = "@tormakris" __author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team" __copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "app" __module_name__ = "app"
@ -29,12 +29,12 @@ LOGGER = logging.getLogger(__name__)
KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1') KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
if __name__ == "__main__":
""" """
This is the producers entry point, initializes all the components (:class:'communicator.Communicator', This is the producers entry point, initializes all the components (:class:`communicator.Communicator`,
:class:'consumerlocator.ConsumerLocator' and :class:'messagesender.MessageSender') and sends infinite random :class:`consumerlocator.ConsumerLocator` and :class:`messagesender.MessageSender`) and sends infinite random
messages. messages.
""" """
if __name__ == "__main__":
LOGGER.info("Producer started") LOGGER.info("Producer started")
generateduuid = str(uuid.uuid4()) generateduuid = str(uuid.uuid4())
communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid) communicator = Communicator(currentconsumer=KNOWNCONSUMER, uuid=generateduuid)

View File

@ -1,11 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
import logging
import requests
""" """
Communicator module Communicator module
""" """
import logging
import requests
__author__ = "@tormakris" __author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team" __copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "messagesender" __module_name__ = "messagesender"
@ -20,7 +21,9 @@ class Communicator:
""" """
def __init__(self, currentconsumer: str, uuid: str): def __init__(self, currentconsumer: str, uuid: str):
"""Initialize object """**Constructor:**
Initializes the object.
:param consumerlocator: the current consumer's IP address as a string :param consumerlocator: the current consumer's IP address as a string
:param uuid: string typed UUID. :param uuid: string typed UUID.
""" """
@ -29,6 +32,7 @@ class Communicator:
def sendmessage(self, message: str) -> None: def sendmessage(self, message: str) -> None:
"""Send message to the current consumer. Logs the process. """Send message to the current consumer. Logs the process.
:param message: the message of type string that will be sent. :param message: the message of type string that will be sent.
:return: None :return: None
""" """
@ -39,6 +43,7 @@ class Communicator:
def discoveravailableconsumers(self) -> list: def discoveravailableconsumers(self) -> list:
"""Get the list of available consumer from the current primary consumer. Logs the received list. """Get the list of available consumer from the current primary consumer. Logs the received list.
:return: list of consumers' IP addresses :return: list of consumers' IP addresses
""" """
try: try:
@ -53,6 +58,7 @@ class Communicator:
def isconsumeravailable(self) -> bool: def isconsumeravailable(self) -> bool:
"""Readiness probe current consumer. Logs the result. """Readiness probe current consumer. Logs the result.
:return: True if available, False otherwise :return: True if available, False otherwise
""" """
currentconsumer = self.currenctconsumer currentconsumer = self.currenctconsumer
@ -67,6 +73,7 @@ class Communicator:
def checkconsumer(self, consumer: str) -> bool: def checkconsumer(self, consumer: str) -> bool:
"""Readiness probe of a particular consumer. Logs the result. """Readiness probe of a particular consumer. Logs the result.
:param consumer: the consumer's IP address :param consumer: the consumer's IP address
:return: True if available, False otherwise :return: True if available, False otherwise
""" """
@ -81,6 +88,7 @@ class Communicator:
def set_currentconsumer(self,currenctconsumer) -> None: def set_currentconsumer(self,currenctconsumer) -> None:
"""Set current consumer """Set current consumer
:param currenctconsumer: the consumer's IP address :param currenctconsumer: the consumer's IP address
:return: None :return: None
""" """

View File

@ -1,13 +1,13 @@
#!/usr/bin/env python #!/usr/bin/env python
import datetime
from communicator import Communicator
import os
""" """
Consumer locator module, that manages the list of consumers. Consumer locator module, that manages the list of consumers.
""" """
import datetime
from communicator import Communicator
import os
__author__ = "@dscharnitzky" __author__ = "@dscharnitzky"
__copyright__ = "Copyright 2020, GoldenPogácsa Team" __copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "consumerlocator" __module_name__ = "consumerlocator"
@ -18,15 +18,17 @@ KNOWNCONSUMER = os.getenv("PRODUCER_KNOWNCONSUMER",'10.69.42.1')
class ConsumerLocator: class ConsumerLocator:
""" """
Component responsible for managing the list of consumers. Requires an instance of :class:'communicator.Communicator' Component responsible for managing the list of consumers. Requires an instance of :class:`communicator.Communicator`
""" """
def __init__(self, uuid: str, communicator: Communicator): def __init__(self, uuid: str, communicator: Communicator):
"""Initializes the object. """**Constructor:**
Initializes the object.
Gets the known consumer's IP address from the PRODUCER_KNOWNCONSUMER envar. Gets the known consumer's IP address from the PRODUCER_KNOWNCONSUMER envar.
:param: uuid: Not used
:param: communicator: the :class:'communicator.Communicator' instance that will be used for the low level :param uuid: Not used
communication. :param communicator: the :class:'communicator.Communicator' instance that will be used for the low level communication.
""" """
self.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}] self.consumerlist = [{"Host": KNOWNCONSUMER, "State": True, "LastOk": datetime.datetime.now()}]
self.currentconsumer = self.consumerlist[0] self.currentconsumer = self.consumerlist[0]
@ -34,9 +36,11 @@ class ConsumerLocator:
def learnconsumerlist(self) -> None: def learnconsumerlist(self) -> None:
""""Learns the list of consumers from the current consumer. """"Learns the list of consumers from the current consumer.
Calls :func:'~communicator.Communicator.didiscoveravailableconsumers', adds the learned consumers to the list
if are not present, and then calls :func:'~consumerlocator.ConsumerLocator.updateconsumerlist' Calls :func:`communicator.Communicator.didiscoveravailableconsumers`, adds the learned consumers to the list
:return: None if are not present, and then calls :func:`consumerlocator.ConsumerLocator.updateconsumerlist`
:returns: None
""" """
recievedconsumerlist = self.communicator.discoveravailableconsumers() recievedconsumerlist = self.communicator.discoveravailableconsumers()
if recievedconsumerlist is None: if recievedconsumerlist is None:
@ -54,8 +58,10 @@ class ConsumerLocator:
def updateconsumerlist(self) -> None: def updateconsumerlist(self) -> None:
""" Updates the consumer list based on their availability. """ Updates the consumer list based on their availability.
Marks for each consumer if they are available or not. If a consumer is not available for some time (1 hour), Marks for each consumer if they are available or not. If a consumer is not available for some time (1 hour),
the it will be deleted from the list. the it will be deleted from the list.
:return: None :return: None
""" """
removelist = [] removelist = []
@ -72,9 +78,11 @@ class ConsumerLocator:
def updateconsumer(self): def updateconsumer(self):
"""If the current consumer is not available, checks all the consumers in the list and updates the current one. """If the current consumer is not available, checks all the consumers in the list and updates the current one.
Calls :func:'~consumerlocator.ConsumerLocator.checkcurrentconsumer' and if needed
:func:'~consumerlocator.ConsumerLocator.updateconsumerlist'. Sets the :class:'communicator.Communicator' Calls :func:`consumerlocator.ConsumerLocator.checkcurrentconsumer` and if needed
current instance with :func:'~communicator.Communicator.set_currentconsumer'. :func:`consumerlocator.ConsumerLocator.updateconsumerlist`. Sets the :class:`communicator.Communicator`
current instance with :func:`communicator.Communicator.set_currentconsumer`.
:return: the current consumer or None if there are no available customers at the moment. :return: the current consumer or None if there are no available customers at the moment.
""" """
@ -98,15 +106,15 @@ class ConsumerLocator:
return None return None
def getcurrentconsumer(self) -> str: def getcurrentconsumer(self) -> str:
""" """Returns the currently selected consumer's IP address.
Returns the currently selected consumer's IP address.
:return: the current consumer :return: the current consumer
""" """
return self.currentconsumer["Host"] return self.currentconsumer["Host"]
def checkcurrentconsumer(self) -> bool: def checkcurrentconsumer(self) -> bool:
""" """Check the current consumer's health.
Check the current consumer's health.
:return: True if OK, False if fail :return: True if OK, False if fail
""" """
if self.currentconsumer is None: if self.currentconsumer is None:

View File

@ -44,7 +44,9 @@ exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
# The theme to use for HTML and HTML Help pages. See the documentation for # The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes. # a list of builtin themes.
# #
html_theme = 'alabaster' html_theme = 'sphinx_rtd_theme'
autoclass_content = 'both'
# Add any paths that contain custom static files (such as style sheets) here, # 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, # relative to this directory. They are copied after the builtin static files,

View File

@ -10,6 +10,8 @@ Welcome to Producer's documentation!
:maxdepth: 2 :maxdepth: 2
:caption: Contents: :caption: Contents:
readme
source/modules
Indices and tables Indices and tables
@ -18,3 +20,5 @@ Indices and tables
* :ref:`genindex` * :ref:`genindex`
* :ref:`modindex` * :ref:`modindex`
* :ref:`search` * :ref:`search`
* `Git repository <https://git.kmlabz.com/GoldenPogacsa/producer>`_

2
docs/readme.rst Normal file
View File

@ -0,0 +1,2 @@
.. include:: ../README.rst

7
docs/source/app.rst Normal file
View File

@ -0,0 +1,7 @@
app module
==========
.. automodule:: app
:members:
:undoc-members:
:show-inheritance:

View File

@ -0,0 +1,7 @@
communicator module
===================
.. automodule:: communicator
:members:
:undoc-members:
:show-inheritance:

View File

@ -0,0 +1,7 @@
consumerlocator module
======================
.. automodule:: consumerlocator
:members:
:undoc-members:
:show-inheritance:

View File

@ -0,0 +1,7 @@
messagesender module
====================
.. automodule:: messagesender
:members:
:undoc-members:
:show-inheritance:

11
docs/source/modules.rst Normal file
View File

@ -0,0 +1,11 @@
producer
========
.. toctree::
:maxdepth: 4
app
communicator
consumerlocator
messagesender
test

7
docs/source/test.rst Normal file
View File

@ -0,0 +1,7 @@
test module
===========
.. automodule:: test
:members:
:undoc-members:
:show-inheritance:

View File

@ -1,13 +1,14 @@
#!/usr/bin/env python #!/usr/bin/env python
"""
Message sender component.
"""
import logging import logging
import random import random
import string import string
from communicator import Communicator from communicator import Communicator
"""
Message sender component
"""
__author__ = "@kovacsbence" __author__ = "@kovacsbence"
__copyright__ = "Copyright 2020, GoldenPogácsa Team" __copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "messagesender" __module_name__ = "messagesender"
@ -19,18 +20,20 @@ LOGGER = logging.getLogger(__name__)
class MessageSender: class MessageSender:
""" """
Component responsible for sending the messages. Requires an instance of :class:'communicator.Communicator' Component responsible for sending the messages. Requires an instance of :class:`communicator.Communicator`.
""" """
def __init__(self, communicator: Communicator): def __init__(self, communicator: Communicator):
"""Initializes the object. """**Constructor:**
:param: communicator: an instance of :class:'communicator.Communicator' Initializes the object.
:param communicator: an instance of :class:`communicator.Communicator`.
""" """
self.communicator = communicator self.communicator = communicator
def randomstring(self, stringlength: int) -> str: def randomstring(self, stringlength: int) -> str:
""" """Generate a random string of fixed length
Generate a random string of fixed length
:param stringlength: the length of the string :param stringlength: the length of the string
:return: the generated string :return: the generated string
""" """
@ -39,10 +42,12 @@ class MessageSender:
def sendmessage(self, message: str = "") -> None: def sendmessage(self, message: str = "") -> None:
"""Sends the given message. """Sends the given message.
If the message is omitted (empty), then a random message will be generated with length 23 (with If the message is omitted (empty), then a random message will be generated with length 23 (with
:func:'~messagesender.MessageSender.randomstring'. Calls :func:'~communicator.Communicator.sendmessage' :func:`messagesender.MessageSender.randomstring`. Calls :func:`communicator.Communicator.sendmessage`
to send the message. to send the message.
:param message: the message of type string that will be sent
:param message: the message of type string that will be sent.
:return: None :return: None
""" """
if not message: if not message:

49
test.py
View File

@ -1,13 +1,14 @@
#!/usr/bin/env python #!/usr/bin/env python
"""
Unit tests for the producer module.
"""
import re import re
import consumerlocator import consumerlocator
import communicator import communicator
import messagesender import messagesender
"""
Unit tests for producer module.
"""
__author__ = "@tormakris" __author__ = "@tormakris"
__copyright__ = "Copyright 2020, GoldenPogácsa Team" __copyright__ = "Copyright 2020, GoldenPogácsa Team"
@ -19,8 +20,9 @@ generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad'
def test_generate_string(mocker): def test_generate_string(mocker):
""" """
Tests :func:'~messagesender.MessageSender.randomstring'. Tests :func:`messagesender.MessageSender.randomstring`.
:param: mocker:
:param mocker: patches the :class:`communicator.Communicator`.
""" """
mocker.patch('communicator.Communicator') mocker.patch('communicator.Communicator')
comm = communicator.Communicator( comm = communicator.Communicator(
@ -34,7 +36,8 @@ def test_generate_string(mocker):
def test_sendmessage(httpserver): def test_sendmessage(httpserver):
""" """
Tests :func:'~communicator.Communicator.sendmessage'. Tests :func:`communicator.Communicator.sendmessage`.
:param httpserver: simple HTTP server :param httpserver: simple HTTP server
:return: None :return: None
""" """
@ -56,8 +59,9 @@ def test_sendmessage(httpserver):
def test_send_message(mocker): def test_send_message(mocker):
""" """
Tests :func:'~messagesender.MessageSender.sendmessage'. Tests :func:`messagesender.MessageSender.sendmessage`.
:param mocker:
:param mocker: patches the :class:`communicator.Communicator`.
:return: None :return: None
""" """
mocker.patch('communicator.Communicator') mocker.patch('communicator.Communicator')
@ -72,7 +76,8 @@ def test_send_message(mocker):
def test_discoveravailableconsumers(httpserver): def test_discoveravailableconsumers(httpserver):
""" """
Tests :func:'~communicator.Communicator.discoveravailableconsumers' Tests :func:`communicator.Communicator.discoveravailableconsumers`
:param httpserver: simple HTTP server :param httpserver: simple HTTP server
:return: None :return: None
""" """
@ -93,7 +98,8 @@ def test_discoveravailableconsumers(httpserver):
def test_isconsumeravailable(httpserver): def test_isconsumeravailable(httpserver):
""" """
Tests :func:'~communicator.Communicator.isconsumeravailable' Tests :func:`communicator.Communicator.isconsumeravailable`.
:param httpserver: simple HTTP server :param httpserver: simple HTTP server
:return: None :return: None
""" """
@ -126,7 +132,8 @@ def test_isconsumeravailable(httpserver):
def test_checkconsumer(httpserver): def test_checkconsumer(httpserver):
""" """
Tests :func:'~communicator.Communicator.checkconsumer' Tests :func:`communicator.Communicator.checkconsumer`.
:param httpserver: simple HTTP server :param httpserver: simple HTTP server
:return: None :return: None
""" """
@ -159,7 +166,8 @@ def test_checkconsumer(httpserver):
def test_setcurrentconsumer(): def test_setcurrentconsumer():
""" """
Tests :func:'~communicator.Communicator.set_currentconsumer' Tests :func:`communicator.Communicator.set_currentconsumer`
:return: None :return: None
""" """
comm = communicator.Communicator( comm = communicator.Communicator(
@ -171,7 +179,8 @@ def test_setcurrentconsumer():
def test_learnconsumerlist(httpserver): def test_learnconsumerlist(httpserver):
""" """
Tests :func:'~consumerlocator.ConsumerLocator.learnconsumerlist' Tests :func:`consumerlocator.ConsumerLocator.learnconsumerlist`
:param httpserver: simple HTTP server :param httpserver: simple HTTP server
:return: None :return: None
""" """
@ -194,8 +203,9 @@ def test_learnconsumerlist(httpserver):
def test_getcurrentconsumer(mocker): def test_getcurrentconsumer(mocker):
""" """
Tests :func:'~consumerlocator.ConsumerLocator.getcurrentconsumer' Tests :func:`consumerlocator.ConsumerLocator.getcurrentconsumer`
:param mocker:
:param mocker: patches the :class:`communicator.Communicator`.
:return: None :return: None
""" """
mocker.patch('communicator.Communicator') mocker.patch('communicator.Communicator')
@ -209,7 +219,8 @@ def test_getcurrentconsumer(mocker):
def test_checkcurrentconsumer(httpserver): def test_checkcurrentconsumer(httpserver):
""" """
Tests :func:'~consumerlocator.ConsumerLocator.checkcurrentconsumer' Tests :func:`consumerlocator.ConsumerLocator.checkcurrentconsumer`
:param httpserver: simple HTTP server :param httpserver: simple HTTP server
:return: None :return: None
""" """
@ -232,7 +243,8 @@ def test_checkcurrentconsumer(httpserver):
def test_updateconsumer(httpserver): def test_updateconsumer(httpserver):
""" """
Tests :func:'~consumerlocator.ConsumerLocator.updateconsumer' Tests :func:`consumerlocator.ConsumerLocator.updateconsumer`
:param httpserver: simple HTTP server :param httpserver: simple HTTP server
:return: None :return: None
""" """
@ -256,7 +268,8 @@ def test_updateconsumer(httpserver):
def test_updateconsumerlist(httpserver): def test_updateconsumerlist(httpserver):
""" """
Tests :func:'~consumerlocator.ConsumerLocator.updateconsumerlist' Tests :func:`consumerlocator.ConsumerLocator.updateconsumerlist`
:param httpserver: simple HTTP server :param httpserver: simple HTTP server
:return: None :return: None
""" """