Further restructuring to allow multiple connectivities with several accounts.

This commit is contained in:
Schimon Jehudah, Adv. 2024-06-02 09:35:07 +03:00
parent f8bdfca972
commit 5ee75572ab
2 changed files with 53 additions and 65 deletions

View file

@ -56,7 +56,6 @@ TODO
# vars and their meanings: # vars and their meanings:
# jid = Jabber ID (XMPP) # jid = Jabber ID (XMPP)
# res = response (HTTP) # res = response (HTTP)
from argparse import ArgumentParser from argparse import ArgumentParser
from getpass import getpass from getpass import getpass
import sys import sys
@ -85,34 +84,9 @@ from slixfeed.version import __version__
# import socks # import socks
# import socket # import socket
account = config.get_values('accounts.toml', 'xmpp') account_xmpp = config.get_values('accounts.toml', 'xmpp')
if not 'mode' in account['settings']:
account_mode = 'client'
print('Key "mode" was not found.\nSetting value to "client".')
# raise Exception('Key type is missing from accounts.toml.')
else:
account_mode = 'component'
match account_mode:
case 'client':
from slixfeed.xmpp.client import Slixfeed
# from slixfeed.config import ConfigClient as ConfigAccount
case 'component':
from slixfeed.xmpp.component import SlixfeedComponent
# from slixfeed.config import ConfigComponent as ConfigAccount
# account = ConfigAccount() # TODO Delete as soon as posible after is no longer needed
class JabberComponent:
def __init__(self, jid, secret, hostname, port, alias=None):
xmpp = SlixfeedComponent(jid, secret, hostname, port, alias)
class JabberClient:
def __init__(self, jid, password, hostname=None, port=None, alias=None):
xmpp = Slixfeed(jid, password, hostname, port, alias)
# account = ConfigAccount() # TODO ~Delete~ Clear as soon as posible after is no longer needed
def main(): def main():
@ -133,13 +107,10 @@ def main():
# # socks.set_default_proxy(socks.SOCKS5, host, port) # # socks.set_default_proxy(socks.SOCKS5, host, port)
# # socket.socket = socks.socksocket # # socket.socket = socks.socksocket
# Setup the command line arguments. #parser = ArgumentParser(description=Slixfeed.__doc__)
match account_mode: parser = ArgumentParser(description='Slixfeed News Bot')
case 'client':
parser = ArgumentParser(description=Slixfeed.__doc__)
case 'component':
parser = ArgumentParser(description=SlixfeedComponent.__doc__)
# Setup the command line arguments.
parser.add_argument('-v', '--version', help='Print version', parser.add_argument('-v', '--version', help='Print version',
action='version', version=__version__) action='version', version=__version__)
@ -175,38 +146,55 @@ def main():
# lognfo = logger.info # lognfo = logger.info
# logwrn = logger.warning # logwrn = logger.warning
# NOTE Temporarily archived
# NOTE Consider removal of arguments jid, password and alias
# # Try configuration file
# jid = account[account_mode]['jid']
# password = account[account_mode]['password']
# alias = account[account_mode]['alias'] if 'alias' in account[account_mode] else None
# hostname = account[account_mode]['hostname'] if 'hostname' in account[account_mode] else None
# port = account[account_mode]['port'] if 'port' in account[account_mode] else None
# # Use arguments if were given
# if args.jid:
# jid = args.jid
# if args.password:
# password = args.password
# if args.alias:
# alias = args.alias
# if args.hostname:
# hostname = args.hostname
# if args.port:
# port = args.port
# # Prompt for credentials if none were given
# if not jid:
# jid = input('JID: ')
# if not password:
# password = getpass('Password: ')
# if not alias:
# alias = (input('Alias: ')) or 'Slixfeed'
# Try configuration file # Try configuration file
jid = account[account_mode]['jid'] if 'client' in account_xmpp:
password = account[account_mode]['password'] from slixfeed.xmpp.client import Slixfeed
alias = account[account_mode]['alias'] if 'alias' in account[account_mode] else None jid = account_xmpp['client']['jid']
hostname = account[account_mode]['hostname'] if 'hostname' in account[account_mode] else None password = account_xmpp['client']['password']
port = account[account_mode]['port'] if 'port' in account[account_mode] else None alias = account_xmpp['client']['alias'] if 'alias' in account_xmpp['client'] else None
hostname = account_xmpp['client']['hostname'] if 'hostname' in account_xmpp['client'] else None
port = account_xmpp['client']['port'] if 'port' in account_xmpp['client'] else None
Slixfeed(jid, password, hostname, port, alias)
# Use arguments if were given if 'component' in account_xmpp:
if args.jid: from slixfeed.xmpp.component import SlixfeedComponent
jid = args.jid jid = account_xmpp['component']['jid']
if args.password: password = account_xmpp['component']['password']
password = args.password alias = account_xmpp['component']['alias'] if 'alias' in account_xmpp['component'] else None
if args.alias: hostname = account_xmpp['component']['hostname'] if 'hostname' in account_xmpp['component'] else None
alias = args.alias port = account_xmpp['component']['port'] if 'port' in account_xmpp['component'] else None
if args.hostname: SlixfeedComponent(jid, password, hostname, port, alias).process()
hostname = args.hostname
if args.port:
port = args.port
# Prompt for credentials if none were given
if not jid:
jid = input('JID: ')
if not password:
password = getpass('Password: ')
if not alias:
alias = (input('Alias: ')) or 'Slixfeed'
match account_mode:
case 'client':
JabberClient(jid, password, hostname=hostname, port=port, alias=alias)
case 'component':
JabberComponent(jid, password, hostname, port, alias=alias)
sys.exit(0) sys.exit(0)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -1,2 +1,2 @@
__version__ = '0.1.70' __version__ = '0.1.71'
__version_info__ = (0, 1, 70) __version_info__ = (0, 1, 71)