Skip to content

Latest commit

 

History

History
131 lines (98 loc) · 4.24 KB

USAGE.rst

File metadata and controls

131 lines (98 loc) · 4.24 KB

Usage

To develop on notifierlib:

# The following commands require pipenv as a dependency

# To lint the project
_CI/scripts/lint.py

# To execute the testing
_CI/scripts/test.py

# To create a graph of the package and dependency tree
_CI/scripts/graph.py

# To build a package of the project under the directory "dist/"
_CI/scripts/build.py

# To see the package version
_CI/scripts/tag.py

# To bump semantic versioning [--major|--minor|--patch]
_CI/scripts/tag.py --major|--minor|--patch

# To upload the project to a pypi repo if user and password are properly provided
_CI/scripts/upload.py

# To build the documentation of the project
_CI/scripts/document.py

To use notifierlib in a project:

from notifierlib.channels import Email
from notifierlib.channels import Stdout
from notifierlib import Notifier, Group

# import logging and set it to debug so we can see what is going on
import logging
logging.basicConfig(level=logging.DEBUG)

# instantiate the main object
notifier=Notifier()


# instantiate channels for email and stdout
# the first argument is the name of the channel since we can have many
# channels of the same type if we only give them distinct names
email=Email('email',
            sender='[email protected]',
            recipient='[email protected]',
            smtp_address='smtp.domain.com',
            username='smtp_username',
            password='smtp_password',
            tls=True,
            ssl=False,
            port=587)

stdout=Stdout('stdout')

# register the channels to notifier
notifier.register(stdout, email)

# or just one by one
notifier.register(stdout, email)

# this attribute shows us the names of the registered channels
notifier.channels
 # >>> ['stdout', 'email']

 # from here on we have access to a "broadcast" method than will propagate
 # the supplied arguments to all registered channels
notifier.broadcast(subject='yay!!', message='this is a message')

# this is the debug output of the above
DEBUG:notifierlib.Group:Sending notification using channel: stdout with args:{'message': 'this is a message', 'subject': 'yay!!'}
DEBUG:notifierlib.Group:Sending notification using channel: email with args:{'message': 'this is a message', 'subject': 'yay!!'}
DEBUG:emaillib.emaillib:Trying to connect via SMTP
DEBUG:notifierlib.Group:Waiting for results
Subject :yay!!
Message :this is a message
()
INFO:emaillib.emaillib:Got smtp connection
INFO:emaillib.emaillib:Logging in
DEBUG:emaillib.emaillib:Done
DEBUG:notifierlib.Group:Result of notification: [{'stdout': True}, {'email': True}]

# we can fine tune the channel collections by creating groups
# again the first argument is the group name and all other arguments should
# be of type channel
email_group=Group('email', email)

print(email_group.name)
>>> 'email'

# we add the group to the notifier
notifier.add_group(email_group)

# and from here on we have a method with the name of the group that can accept
# our arguments and it will propagate them its the registered channels
notifier.email(subject='test',message='as')

DEBUG:notifierlib.Group:Sending notification using channel: email with args:{'message': 'as', 'subject': 'test'}
DEBUG:notifierlib.Group:Waiting for results
DEBUG:emaillib.emaillib:Trying to connect via SMTP
INFO:emaillib.emaillib:Got smtp connection
INFO:emaillib.emaillib:Logging in
DEBUG:emaillib.emaillib:Done
DEBUG:notifierlib.Group:Result of notification: [{'email': True}]

# and we can remove the method by removing the group from the notifier
notifier.remove_group(email_group)
>>>True

# another group example
debug_group=Group('debug', stdout)
notifier.add_group(debug_group)
notifier.debug(subject='test')
Subject :test
Message :None
()
DEBUG:notifierlib.Group:Sending notification using channel: stdout with args:{'subject': 'test'}
DEBUG:notifierlib.Group:Waiting for results
DEBUG:notifierlib.Group:Result of notification: [{'stdout': True}]