From cb84351270041449ee8a8984758e7e3ef86ea590 Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 12 Dec 2019 23:15:45 +0100 Subject: test code and small init corrections + added pytest test cases * fixed TSGroupAssigner module init file --- setup.py | 1 - 1 file changed, 1 deletion(-) (limited to 'setup.py') diff --git a/setup.py b/setup.py index f296015..6136fdc 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,6 @@ from setuptools import setup, find_packages with open("README.md", "r") as fh: long_description = fh.read() - setup( name='TSGroupAssigner', version='0.0.1', -- cgit v1.2.3-54-g00ecf From 9b964690e79b5a4366e43fde01c6594de693be4d Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 19 Dec 2019 18:15:47 +0100 Subject: package finish up * update setup.py * better format the main __init__ file * move exceptions to separate file for maintainability * small corrections --- TSGroupAssigner/__init__.py | 8 +++++++- TSGroupAssigner/exceptions.py | 8 ++++++++ TSGroupAssigner/group_assign.py | 17 ++++++++--------- setup.py | 24 +++++++++++++++--------- tests/test_group_assign.py | 6 +++--- 5 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 TSGroupAssigner/exceptions.py (limited to 'setup.py') diff --git a/TSGroupAssigner/__init__.py b/TSGroupAssigner/__init__.py index 04fc467..e434073 100644 --- a/TSGroupAssigner/__init__.py +++ b/TSGroupAssigner/__init__.py @@ -1,4 +1,10 @@ # -*- coding: utf-8 -*- +# version +__version__ = "0.0.1" + +# modules from .group_assign import GroupAssigner -from .group_assign import DateException + +# utils +from .exceptions import DateException diff --git a/TSGroupAssigner/exceptions.py b/TSGroupAssigner/exceptions.py new file mode 100644 index 0000000..a0bb55b --- /dev/null +++ b/TSGroupAssigner/exceptions.py @@ -0,0 +1,8 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +__all__ = ['DateException'] + + +class DateException(Exception): + """Exceptions thrown if configured date is out of range""" diff --git a/TSGroupAssigner/group_assign.py b/TSGroupAssigner/group_assign.py index 9f43d35..1f9fdbb 100644 --- a/TSGroupAssigner/group_assign.py +++ b/TSGroupAssigner/group_assign.py @@ -8,9 +8,9 @@ from contextlib import suppress import ts3 +from .exceptions import DateException -class DateException(Exception): - """raise this if the date delta exceeds the configured range""" +__all__ = ['GroupAssigner'] class GroupAssigner: @@ -107,7 +107,7 @@ class GroupAssigner: # calculate remaining time delta remaindelta = starttime - now - logging.debug('target date will be reached in {sec} seconds -- sleeping'.format(sec=remaindelta.seconds)) + logging.debug(f'target date will be reached in {remaindelta.seconds} seconds -- sleeping') time.sleep(remaindelta.seconds + 1) else: @@ -119,9 +119,6 @@ class GroupAssigner: if data['client_type'] != '0' or data['reasonid'] != '0': return - # check if the current date is still eligible - self.__datecheck() - cldbid = data['client_database_id'] user_grps = data['client_servergroups'].split(sep=',') @@ -133,8 +130,7 @@ class GroupAssigner: try: # Usage: servergroupaddclient sgid={groupID} cldbid={clientDBID} - # cmd = self.conn.servergroupaddclient(sgid=self.gid, cldbid=cldbid) - cmd = self.conn.clientdbinfo(cldbid=cldbid) + cmd = self.conn.servergroupaddclient(sgid=self.gid, cldbid=cldbid) if cmd.error['id'] != '0': logging.error(cmd.data[0].decode("utf-8")) @@ -142,7 +138,7 @@ class GroupAssigner: # log process logging.info('{client_nickname}:{client_database_id} added to {gid}'.format(**data, gid=self.gid)) - # log possible key errors while the teamspeak 5 client is not fully working + # log possible key errors while the teamspeak 5 client is not fully released except KeyError as err: logging.error([err, data]) @@ -150,6 +146,9 @@ class GroupAssigner: """ event handler which separates events to their specific handlers """ + # check if event is still eligible + self.__datecheck() + # client enter events if event == "notifycliententerview": self.__notifycliententerview(data) diff --git a/setup.py b/setup.py index 6136fdc..3fbb8aa 100644 --- a/setup.py +++ b/setup.py @@ -1,25 +1,31 @@ # -*- coding: utf-8 -*- from setuptools import setup, find_packages +from TSGroupAssigner import __version__ + with open("README.md", "r") as fh: long_description = fh.read() setup( name='TSGroupAssigner', - version='0.0.1', - packages=find_packages(exclude=['tests', 'tests.*']), - keywords='automation TeamSpeak teamspeak ts3 ts3server ts', + version=__version__, url='https://github.com/mightyBroccoli/TSGroupAssigner', - license='GPLv3', author='nico wellpott', author_email='nico@magicbroccoli.de', - description='date based TeamSpeak Group Assigner', - long_description=long_description, - python_requires='>=3.7', classifiers=[ 'Programming Language :: Python :: 3', 'Intended Audience :: System Administrators', 'Operating System :: Unix', - 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)' - ] + 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', + "Topic :: Communications", + "Topic :: Internet" + ], + license='GPLv3', + description='date based TeamSpeak Group Assigner', + long_description=long_description, + long_description_content_type='text/markdown', + keywords='automation TeamSpeak teamspeak ts3 ts3server ts', + install_requires='ts3>=1.0.11', + packages=find_packages(exclude=("tests",)), + python_requires='>=3.6' ) diff --git a/tests/test_group_assign.py b/tests/test_group_assign.py index c3529a0..150aa7b 100644 --- a/tests/test_group_assign.py +++ b/tests/test_group_assign.py @@ -32,7 +32,7 @@ class TestGroupAssigner: duration = dt.timedelta() with pytest.raises(DateException): - GroupAssigner(date=startdate, delta=duration,**creds).start() + GroupAssigner(date=startdate, delta=duration, **creds).start() def test_datecheck_enddate(self): # this should produce a exit code 0 SystemExit as the end date is in the past @@ -42,7 +42,7 @@ class TestGroupAssigner: duration = dt.timedelta() with pytest.raises(SystemExit): - GroupAssigner(date=startdate, delta=duration,**creds).start() + GroupAssigner(date=startdate, delta=duration, **creds).start() def test_connect_noconnection(self): # connect should fail with ConnectionRefusedError @@ -52,4 +52,4 @@ class TestGroupAssigner: duration = dt.timedelta() with pytest.raises(ConnectionRefusedError): - GroupAssigner(date=startdate, delta=duration,**creds).start() + GroupAssigner(date=startdate, delta=duration, **creds).start() -- cgit v1.2.3-54-g00ecf From a604e18853c3be192e439889b30a372f18740230 Mon Sep 17 00:00:00 2001 From: nico Date: Sun, 22 Dec 2019 04:05:41 +0100 Subject: setup is difficult * inversed checkdate to simplify * finished setup.py + add MANIFEST file --- MANIFEST.in | 3 +++ TSGroupAssigner/__init__.py | 2 +- TSGroupAssigner/group_assign.py | 45 ++++++++++++++++++++++------------------- setup.py | 28 +++++++++++++++++-------- 4 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 MANIFEST.in (limited to 'setup.py') diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..4ac345f --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,3 @@ +include README.md +include LICENCE.md +include requirements.txt \ No newline at end of file diff --git a/TSGroupAssigner/__init__.py b/TSGroupAssigner/__init__.py index e434073..6feef67 100644 --- a/TSGroupAssigner/__init__.py +++ b/TSGroupAssigner/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # version -__version__ = "0.0.1" +__version__ = "0.1" # modules from .group_assign import GroupAssigner diff --git a/TSGroupAssigner/group_assign.py b/TSGroupAssigner/group_assign.py index c3ae371..c39711d 100644 --- a/TSGroupAssigner/group_assign.py +++ b/TSGroupAssigner/group_assign.py @@ -21,16 +21,18 @@ class GroupAssigner: self.port = port self.user = user self.pw = password - self.sid = sid - # group + # server and group id + self.sid = sid self.gid = gid # start date and delta self.sleepstart = date - dt.timedelta(days=1) self.startdate = date self.enddate = date + delta - self.delta = delta + + # init connection handler + self.conn = None def __connect(self): """ establish query connection and return connection handler """ @@ -54,9 +56,9 @@ class GroupAssigner: pass # broad exception if something unexpected happens - except ts3.TS3Error as TS3Error: + except ts3.TS3Error as err: # log exception - logging.error(TS3Error) + logging.error(err) # exit sys.exit() @@ -65,15 +67,15 @@ class GroupAssigner: """ method to check if the current date is still in the configured date range """ now = dt.date.today() - # check if target date is in the configured range - if self.startdate <= now <= self.enddate: - logging.debug('target date within configured date range') - return True - + # check if still in the configured range + if not self.startdate <= now <= self.enddate: # if date range is exceeded shutdown gracefully logging.info('the current date exceeds the configured date range -- exiting') self.__disconnect() + # else continue + logging.debug('heartbeat - target date within configured date range') + def __start_sleepstart(self): """ method to check if the process is eligible to sleepstart """ now = dt.date.today() @@ -144,19 +146,20 @@ class GroupAssigner: self.__start_sleepstart() # proceed only if target date is inside the date range - if self.__checkdate(): - try: - # init connection - self.__connect() + self.__checkdate() + + try: + # init connection + self.__connect() - # break if credentials are invalid - except ts3.query.TS3QueryError as err: - # log error - logging.error(err) - self.__disconnect() + # break if credentials are invalid + except ts3.query.TS3QueryError as err: + # log error + logging.error(err) + self.__disconnect() - # start processing - self.__main() + # start processing + self.__main() def __main(self): """ bots main loop """ diff --git a/setup.py b/setup.py index 3fbb8aa..2bccdd9 100644 --- a/setup.py +++ b/setup.py @@ -3,6 +3,7 @@ from setuptools import setup, find_packages from TSGroupAssigner import __version__ +# long readme with open("README.md", "r") as fh: long_description = fh.read() @@ -13,19 +14,30 @@ setup( author='nico wellpott', author_email='nico@magicbroccoli.de', classifiers=[ - 'Programming Language :: Python :: 3', 'Intended Audience :: System Administrators', - 'Operating System :: Unix', + 'Natural Language :: English', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', - "Topic :: Communications", - "Topic :: Internet" - ], + 'Operating System :: Unix', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: Implementation :: CPython', + 'Topic :: Communications', + 'Topic :: Internet' + ], license='GPLv3', description='date based TeamSpeak Group Assigner', long_description=long_description, long_description_content_type='text/markdown', keywords='automation TeamSpeak teamspeak ts3 ts3server ts', - install_requires='ts3>=1.0.11', - packages=find_packages(exclude=("tests",)), - python_requires='>=3.6' + install_requires=[ + 'ts3>=1.0.11,<2' + ], + packages=find_packages(exclude=('tests',)), + python_requires='>=3.6', + project_urls={ + 'Source': 'https://github.com/mightyBroccoli/TSGroupAssigner', + 'Issue-Tracker': 'https://github.com/mightyBroccoli/TSGroupAssigner/issues' + } ) -- cgit v1.2.3-54-g00ecf