feat: Implement optparse

This commit is contained in:
PapaTutuWawa 2020-09-17 18:32:00 +02:00
parent 1f3e5b28fe
commit 95b227504e

View File

@ -22,6 +22,7 @@ import configparser
import logging import logging
import asyncio import asyncio
from collections import namedtuple from collections import namedtuple
from optparse import OptionParser
import aioxmpp import aioxmpp
from aioxmpp.structs import PresenceShow from aioxmpp.structs import PresenceShow
@ -104,7 +105,7 @@ class WarningBot:
This class represents the actual bot. The only things This class represents the actual bot. The only things
to be done is call connect() after creating an instance. to be done is call connect() after creating an instance.
''' '''
def __init__(self): def __init__(self, config_file):
self._warnings0 = [] self._warnings0 = []
self._warnings1 = [] self._warnings1 = []
self._client = None self._client = None
@ -116,7 +117,7 @@ class WarningBot:
self._client_store = '' self._client_store = ''
self._warning_store = '' self._warning_store = ''
self._load_config() self._load_config(config_file)
async def connect(self): async def connect(self):
''' '''
@ -126,12 +127,13 @@ class WarningBot:
self._jid, self._jid,
aioxmpp.make_security_layer(self._password)) aioxmpp.make_security_layer(self._password))
log.debug('Connecting to server')
async with self._client.connected(): async with self._client.connected():
log.info('Client connected to server') log.info('Client connected to server')
# In case you want a nice avatar # In case you want a nice avatar
if self._avatar: if self._avatar:
log.info('Setting avatar') log.debug('Setting avatar')
with open(self._avatar, 'rb') as avatar_file: with open(self._avatar, 'rb') as avatar_file:
image_data = avatar_file.read() image_data = avatar_file.read()
@ -139,6 +141,7 @@ class WarningBot:
avatar_set.add_avatar_image('image/png', image_bytes=image_data) avatar_set.add_avatar_image('image/png', image_bytes=image_data)
await (self._client.summon(aioxmpp.avatar.AvatarService) await (self._client.summon(aioxmpp.avatar.AvatarService)
.publish_avatar_set(avatar_set)) .publish_avatar_set(avatar_set))
log.info('Avatar set')
# Set some presence information # Set some presence information
self._client.set_presence( self._client.set_presence(
@ -311,27 +314,27 @@ help - Gebe diese Hilfe aus'''
msg.body[None] = body msg.body[None] = body
await self._client.send(msg) await self._client.send(msg)
def _load_config(self): def _load_config(self, config_file):
# Load config # Load config
config_path = sys.argv[1] if len(sys.argv) == 2 else '/etc/janine/janine.conf'
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(config_path) config.read(config_file)
# Configure sources # Configure sources
self._sources = sources_from_config(config) self._sources = sources_from_config(config)
self._data_dir = config['General'].get('DataDir', '/etc/janine') self._data_dir = config['General'].get('DataDir', '/etc/janine/data')
self._recipients = config['General']['Recipients'].split(',') self._recipients = config['General']['Recipients'].split(',')
self._refresh_timeout = int(config['General']['Timeout']) self._refresh_timeout = int(config['General']['Timeout'])
self._same_domain = config['General'].get('SameDomain', 'True') == 'True' self._same_domain = config['General'].get('SameDomain', 'True') == 'True'
# Persistent data # Persistent data
# Subscribed clients ## Subscribed clients
self._client_store = os.path.join(self._data_dir, 'clients.json') self._client_store = os.path.join(self._data_dir, 'clients.json')
if os.path.exists(self._client_store): if os.path.exists(self._client_store):
with open(self._client_store, 'r') as clients_file: with open(self._client_store, 'r') as clients_file:
self._warn_clients = json.loads(clients_file.read()) self._warn_clients = json.loads(clients_file.read())
log.info('Clients read from disk') log.info('Clients read from disk')
## Warnings
self._warnings_file = os.path.join(self._data_dir, 'warnings.json') self._warnings_file = os.path.join(self._data_dir, 'warnings.json')
if os.path.exists(self._warnings_file): if os.path.exists(self._warnings_file):
with open(self._warnings_file, 'r') as warnings_file: with open(self._warnings_file, 'r') as warnings_file:
@ -339,9 +342,8 @@ help - Gebe diese Hilfe aus'''
json.loads(warnings_file.read()))) json.loads(warnings_file.read())))
log.info('Warnings read from disk') log.info('Warnings read from disk')
# Landkreise ## Landkreise
self._channels = [] self._channels = []
channels = {}
self._channel_file = os.path.join(self._data_dir, 'channels.json') self._channel_file = os.path.join(self._data_dir, 'channels.json')
if not os.path.exists(self._channel_file): if not os.path.exists(self._channel_file):
log.info('Requesting search channels') log.info('Requesting search channels')
@ -371,7 +373,19 @@ def main():
''' '''
Main function. Main function.
''' '''
bot = WarningBot() parser = OptionParser()
parser.add_option('-d', '--debug', action='store_true', dest='debug', default=False)
(options, args) = parser.parse_args()
if options.debug:
log.setLevel(logging.DEBUG)
if len(args) != 0:
config_file = args[0]
else:
config_file = '/etc/janine/janine.conf'
bot = WarningBot(config_file)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.run_until_complete(bot.connect()) loop.run_until_complete(bot.connect())
loop.close() loop.close()