refactor/fix: Fix module name collision

This commit is contained in:
PapaTutuWawa 2021-06-13 13:17:39 +02:00
parent 3fcf5d52b3
commit e953486494
5 changed files with 75 additions and 24 deletions

0
papatutuwawa/__init__.py Normal file
View File

View File

View File

View File

@ -14,7 +14,6 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
# TODO: Prevent subscriptions when subscribed to 'Alle'
# TODO: remove Alle sollte jede subscription entfernen # TODO: remove Alle sollte jede subscription entfernen
# TODO: Deal with weird casing and typos # TODO: Deal with weird casing and typos
# TODO: pollen hilfe <CMD> should print help per command # TODO: pollen hilfe <CMD> should print help per command
@ -63,8 +62,8 @@ class PollenModule(BaseModule):
super().__init__(base, **kwargs) super().__init__(base, **kwargs)
PollenModule.__instance = self PollenModule.__instance = self
self._subcommand_table = { self._subcommand_table = {
'add': self._subscribe, 'subscribe': self._subscribe,
'remove': self._unsubscribe, 'unsubscribe': self._unsubscribe,
'hilfe': self._help 'hilfe': self._help
} }
self._sleep_duration = self.get_option('sleep_duration', 12 * 3600) self._sleep_duration = self.get_option('sleep_duration', 12 * 3600)
@ -84,11 +83,18 @@ class PollenModule(BaseModule):
''' '''
msg = '{} (*{}*)\n'.format(date, plz) msg = '{} (*{}*)\n'.format(date, plz)
if allergies == ['Alle']: if 'Alle' in [x['type'] for x in allergies]:
allergies = list(self._pollen_data[plz][date].keys) allergies = list(self._pollen_data[plz][date].keys)
count = 0
for allergy in allergies: for allergy in allergies:
msg += '{}: {}'.format(allergy, intensity_str(self._pollen_data[plz][date][allergy])) intensity = self._pollen_data[plz][date][allergy['type']]
if intensity >= allergy['level']:
msg += '{}: {}'.format(allergy['type'], intensity_str(intensity))
count += 1
if count == 0:
return ''
return msg return msg
def _is_notified(self, jid, date): def _is_notified(self, jid, date):
@ -121,7 +127,10 @@ class PollenModule(BaseModule):
only the data for the pollen types in filter_allergies will be sent. only the data for the pollen types in filter_allergies will be sent.
''' '''
for plz, data in self._sum.get_subscriptions_for(jid).items(): for plz, data in self._sum.get_subscriptions_for(jid).items():
allergies = data['data'] if not filter_allergies else filter_allergies if filter_allergies:
allergies = filter_allergies
else:
allergies = data['data']
notified = [] notified = []
for date in self._pollen_data[plz]: for date in self._pollen_data[plz]:
@ -129,10 +138,12 @@ class PollenModule(BaseModule):
continue continue
notified.append(date) notified.append(date)
self.send_message(aioxmpp.JID.fromstr(jid), msg = self.notification_body(plz,
self.notification_body(plz, date,
date, allergies)
allergies)) if msg:
self.send_message(aioxmpp.JID.fromstr(jid),
msg)
if notified: if notified:
self._set_dates_notified(jid, notified) self._set_dates_notified(jid, notified)
@ -182,25 +193,40 @@ class PollenModule(BaseModule):
await asyncio.sleep(self._sleep_duration) await asyncio.sleep(self._sleep_duration)
async def _subscribe(self, cmd, msg): async def _subscribe(self, cmd, msg):
if len(cmd) != 3: if len(cmd) < 3 or len(cmd) > 4:
self.send_message(msg.from_, 'Verwendung: pollen add <PLZ> <Allergen>') self.send_message(msg.from_, 'Verwendung: pollen add <PLZ> <Allergen> [<Level>]')
return return
plz = cmd[1] plz = cmd[1]
allergy = cmd[2] allergy = cmd[2]
jid = str(msg.from_.bare()) jid = str(msg.from_.bare())
level = int(cmd[3]) if len(cmd) == 4 else 0
if self._sum.is_subscribed_to_data(jid, plz, allergy): if self._sum.is_subscribed_to_data(jid, plz, 'Alle'):
self.send_message(msg.from_, 'Du hast das schon aboniert') self.send_message(msg.from_, 'Du hast schon alle Allergene abonniert')
return return
self._sum.append_data_for_subscription(jid, plz, allergy) if self._sum.is_subscribed_to_data(jid, plz, allergy):
self.send_message(msg.from_, 'OK') self.send_message(msg.from_, 'Du hast das schon abonniert')
return
if level < 0 or level > 3:
self.send_message(msg.from_, 'Das Level muss zwischen 0 und 3 liegen')
return
self._sum.append_data_for_subscription(jid, plz, {
'type': allergy,
'level': level
})
self.send_message(msg.from_, 'Du erhälst nun Pollenmeldungen zu %s' % allergy)
# Just some bandwidth saving measure # Just some bandwidth saving measure
if plz not in self._pollen_data: if plz not in self._pollen_data:
await self._request_pollen_data(False) await self._request_pollen_data(False)
self._broadcast_jid(jid, [allergy]) self._broadcast_jid(jid, [{
'type': allergy,
'level': level
}])
async def _unsubscribe(self, cmd, msg): async def _unsubscribe(self, cmd, msg):
if len(cmd) != 3: if len(cmd) != 3:
@ -211,20 +237,28 @@ class PollenModule(BaseModule):
allergy = cmd[2] allergy = cmd[2]
jid = str(msg.from_.bare()) jid = str(msg.from_.bare())
if not self._sum.is_subscribed_to_data(jid, plz, allergy): if allergy == 'Alle':
self.send_message(msg.from_, 'Du hast das nicht aboniert') self._sum.filter_items_for_subscriptions(jid, plz, lambda x: False)
self.send_message(msg.from_, 'Du erhälst keine Pollenmeldungen mehr')
return return
self._sum.remove_data_for_subscription(str(msg.from_.bare()), plz, allergy) if not self._sum.is_subscribed_to_data_one(jid, plz, lambda x: x['type'] == allergy):
self.send_message(msg.from_, 'OK') self.send_message(msg.from_, 'Du hast %s nicht abonniert' % (allergy))
return
self._sum.filter_items_for_subscription(str(msg.from_.bare()),
plz,
lambda x: x['type'] != allergy)
self.send_message(msg.from_, 'Du erhälst nun keine Pollenmeldungen zu %s mehr' % (allergy))
async def _help(self, cmd, msg): async def _help(self, cmd, msg):
body = '''Verfügbare Befehle: body = '''Verfügbare Befehle:
pollen add <PLZ> <Typ> pollen subscribe <PLZ> <Typ> [<Min. Level>]
pollen remove <PLZ> <Typ> pollen unsubscribe <PLZ> <Typ>
pollen hilfe pollen hilfe
Pollentypen: Ambrosia, Ampfer, Beifuß, Birke, Buche, Eiche, Erle, Esche, Gräser, Hasel, Pappel, Roggen, Ulme, Wegerich, Weide, Alle Pollentypen: Ambrosia, Ampfer, Beifuß, Birke, Buche, Eiche, Erle, Esche, Gräser, Hasel, Pappel, Roggen, Ulme, Wegerich, Weide, Alle (Alias für alle Pollentypen)
Level: 0 (Kein Pollenflug) - 3 (Starker Pollenflug)
''' '''
self.send_message(msg.from_, body) self.send_message(msg.from_, body)

17
setup.py Normal file
View File

@ -0,0 +1,17 @@
from setuptools import setup, find_packages
setup(
name = 'mira-modules',
version = '0.1.0',
description = 'Various modules for mira',
url = 'https://git.polynom.me/PapaTutuWawa/mira-modules',
author = 'Alexander "PapaTutuWawa"',
author_email = 'papatutuwawa <at> polynom.me',
license = 'GPLv3',
packages = find_packages(),
install_requires = [
'mira>=0.1.0',
'requests>=2.23.0'
],
zip_safe=True
)