Package: pubnub-python-tools
pubnub-python-toolsQuickly interact with PubNub using the Python SDK.Pre-requisitesFor MacOS usehomebrewto install packages or the followingPubNub Ansible Role Python SDKthat setseverythingfor you.python3python-tkInstallTo installpubnub-python-tools:Install using pipInstall fromPypi:pipinstallpubnub-python-toolsTo install the testing versionhttps://test.pypi.org/project/pubnub-python-tools/:python-mpipinstall-ihttps://test.pypi.org/simple/pubnub-python-toolsTesting version might be outdated or unstable.Automatic build and installIt is recommended to setup avirtual environment.To install locally the latest git version:gitclonehttps://github.com/sergio-munoz/pubnub-python-tools
chmod+x./scripts/build_install.sh
./scripts/build_install.shManually build and installSetup a new python virtual environment and install packages inrequirements_build.txt:python-mvenvbuild_venvsourcebuild_venv/bin/activate(build_venv)$pipinstall-rbuild_requirements.txt(build_venv)$hatchbuild(build_venv)$pipinstalldist/pubnub_python_tools-${VERSION}.tar.gzInstall into a Jupyter NotebookTo install the into the Jupyter Notebook kernel:importsys
!{sys.executable}-mpipinstall-Upubnub
!{sys.executable}-mpipinstall-U--index-urlhttps://test.pypi.org/simple/--no-depspubnub-python-toolsPubNub Auth SettingsAlways set the UUID (USER_ID) to uniquely identify the user or device that connects to PubNub. This UUID should be persisted, and should remain unchanged for the lifetime of the user or the device. Not setting the UUID can significantly impact your billing.To runpubnubcommands you need to setup your PubNub credentials using any of the following ways:AutomaticallyRun thesetup.shscript with your PubNub subscribe key, publish key and user_id:$./scripts/install.sh$PN_SUBSCRIBE_KEY$PN_PUBLISH_KEY$PN_USER_IDUsing an .env fileRequirespython-dotenv. Install using:pip install python-dotenvIf you can't see your.envfile try using:ls -laCreate an.envfile to avoid typing your credentials each time.Copy the env file sampleenvfile into your own.env(watch the dot'.') file and replace with your PubNub keys.$cpenv.envA minimal.envfile to work properly needs at least your PubNub'ssubscribe key,publish keyand a chosenuser id:$cat.envPN_SUBSCRIBE_KEY=sub-xxx-xxxPN_PUBLISH_KEY=pub-xxx-xxxPN_USER_ID=UUIDManually via CLIThis has the highest precedenceManually set cli variables before each command:-sk$PN_SUBSCRIBE_KEY-pk$PN_PUBLISH_KEY-u$PN_USER_IDCLI Args:-sk,--subscribe-key- PubNub subscribe key.-pk,--publish-key- PubNub publish key.-u,--user-id- User ID (UUID).Overriding Global variablesThis has the lowest precedenceNOTE: It is not recommended to hard-code your credentials due to security purposes.Override the global variables manually in filepubnub_python_tools/config/module_config.py:# OVERRIDE GLOBAL VARIABLESSUBSCRIBE_KEY="pub-xxx-xxx"PUBLISH_KEY="sub-xxx-xxx"USER_ID="UUID"Sample UseRun PubNub using the script:run_pn.py.$python./scripts/run_pn.py-s"Space01"-p"Space01"-m"Hello from MySpace01"Or if installed, run PubNub using the command:pubnub-python-tools.$pubnub-python-tools-s"Space01"-p"Space01"-m"Hello from MySpace01"Get help using the--helpflag.$python./scripts/run_pn.py--help
$pubnub-python-tools--helpUsage ExamplesRemember to always set up your UUIDUSER_IDwith-u.SubscribeSubscribe to a channel forever.pythonrun_pn.py-s"Space"PublishPublish one message to a space.python-pubnub-tools-p"Space"-m"payload"Publish multiple messages to a space.python-pubnub-tools-p"Space"-mm"payload1""payload2""payloadN"Publish one message to multiple spaces.python-pubnub-tools-p"SpaceA""SpaceB""SpaceN"-m"payload"Publish multiple messages to multiple spaces.python-pubnub-tools-pm"SpaceA""SpaceB","SpaceN"-mm"payload1""payload2""payloadN"For some reason the pubnub python sdk fails to publish when instantiated without a subscribe key, so pass it to avoid issues.PresenceSubscribe to a channel with Presence forever.pubnub-python-tools-s"Space"-presHereNowCallHere Nowon a channel.pubnub-python-tools-here"Space"For advancedHereNowtopics see:cache busting information.NOTE: TBD SOONUnsubscribeSend a leave event to a channel subscribed with Presence.pythonrun_pn.py-us"Space"CLI Commands ReferenceFrompubnub-python-tools --help:-h, --help            show this help message and exit
  --stop-on-fail        Stop batch operations if something goes wrong.
  --version             Current pubnub-python-tools Version
  -a ASYNC_CMD, --async-cmd ASYNC_CMD
                        Run command asynchronously using Asyncio
  -dm DEV_MAN, --dev-man DEV_MAN
                        Attach a Device Manager to file
  -here HERE_NOW, --here-now HERE_NOW
                        Here now on a Channel
  -m MESSAGE, --message MESSAGE
                        Message to publish
  -mm MULTIPLE_MESSAGES [MULTIPLE_MESSAGES ...], --multiple-messages MULTIPLE_MESSAGES [MULTIPLE_MESSAGES ...]
                        Messages to publish
  -p PUBLISH, --publish PUBLISH
                        Publish a message to a Channel
  -pk PUBLISH_KEY, --publish-key PUBLISH_KEY
                        PubNub PublishKey
  -pm PUBLISH_MULTIPLE_CHANNELS [PUBLISH_MULTIPLE_CHANNELS ...], --publish-multiple-channels PUBLISH_MULTIPLE_CHANNELS [PUBLISH_MULTIPLE_CHANNELS ...]
                        Publish to multiple Channels
  -pres, --presence     Subscribe with Presence
  -regex REGEX_ACL, --regex-acl REGEX_ACL Replace regex pattern into a PubNub regex
  -s SUBSCRIBE, --subscribe SUBSCRIBE
                        Subscribe to a Channel
  -sk SUBSCRIBE_KEY, --subscribe-key SUBSCRIBE_KEY
                        PubNub SubscribeKey
  -u UUID, --uuid UUID  PubNub UUID
  -us UNSUBSCRIBE, --unsubscribe UNSUBSCRIBEUse CasesSubscribe and PublishBecause subscribe and publish happen so fast the subscribe might not be listening when the publish was made and thus it will not be shown, but it will be published, if other devices are listening, they will reflect the changes. This is classic pub/sub behavior.Open one terminal and run:(venv)$pythonrun_pn.py-s"Space"-p"Space"-m"payload 1"Open another terminal and run that command modified to show that the user is still subscribed the channel:(venv)$pythonrun_pn.py-s"Space"-p"Space"-m"payload 2"Dig into thelogger/main_log.logto see more information.TestsUnit TestsTo all run tests useunittest:(venv)$python-munittesttests/test_*----------------------------------------------------------------------
Ran 20 tests in 30.680s

OKCoverage(venv)$python-mcoveragerun-munittestdiscover(venv)$python-mcoveragereport-iName                                                       Stmts   Miss  Cover
------------------------------------------------------------------------------
src/pubnub_python_tools/app/device_manager.py                 74     16    78%
src/pubnub_python_tools/app/main_app.py                       46     35    24%
src/pubnub_python_tools/app/pubnub_config.py                  16      0   100%
src/pubnub_python_tools/app/pubnub_handle_disconnects.py      29     20    31%
src/pubnub_python_tools/app/pubnub_here_now_callback.py       45      9    80%
src/pubnub_python_tools/app/pubnub_listener.py               133     73    45%
src/pubnub_python_tools/app/pubnub_manager.py                 89     33    63%
src/pubnub_python_tools/app/pubnub_manager_asyncio.py         48     11    77%
src/pubnub_python_tools/app/pubnub_on_request.py               3      0   100%
src/pubnub_python_tools/app/pubnub_publish.py                 15     11    27%
src/pubnub_python_tools/cli/v1.py                             18      0   100%
src/pubnub_python_tools/config/module_config.py               20      3    85%
src/pubnub_python_tools/logger/logging_config.py              32      9    72%
tests/__init__.py                                              4      0   100%
tests/test_device_manager.py                                  67      0   100%
tests/test_main_app.py                                         9      0   100%
tests/test_pubnub_manager.py                                  79      0   100%
tests/test_pubnub_manager_asyncio.py                         102     10    90%
tests/test_request_function.py                                15      0   100%
tests/test_v1.py                                              32      0   100%
------------------------------------------------------------------------------
TOTAL                                                        876    230    74%

6 empty files skipped.