Skip to content
Snippets Groups Projects
Commit f6974d1b authored by Johann Jacobsohn's avatar Johann Jacobsohn
Browse files

cleanup

parent 80011cc4
No related branches found
No related tags found
No related merge requests found
...@@ -16,10 +16,15 @@ from functools import partial ...@@ -16,10 +16,15 @@ from functools import partial
import json import json
import time import time
import requests import requests
from mattermostdriver import Driver import mattermostdriver
import urwid import urwid
import configargparse import configargparse
class ConfigError(Exception):
"""Configuration error."""
@dataclass @dataclass
class PostDTO: class PostDTO:
""""Encapsulate Mattermost Posts.""" """"Encapsulate Mattermost Posts."""
...@@ -31,7 +36,7 @@ class PostDTO: ...@@ -31,7 +36,7 @@ class PostDTO:
@dataclass @dataclass
class PaperDTO: class PaperDTO:
""""Encapsulate Mattermost Posts.""" """"Encapsulate Paper meta data."""
author: str author: str
authors: str authors: str
title: str title: str
...@@ -115,7 +120,7 @@ class Doi: ...@@ -115,7 +120,7 @@ class Doi:
class Mattermost: class Mattermost:
"""Provide a simplified interaction w/ mattermost api.""" """Provide a simplified interaction w/ mattermost api."""
def __init__(self, url, channelname, username, password): def __init__(self, url, channelname, username, password):
self.mattermost = Driver({ self.mattermost = mattermostdriver.Driver({
'url': url, 'url': url,
'login_id': username, 'login_id': username,
'password': password, 'password': password,
...@@ -124,31 +129,34 @@ class Mattermost: ...@@ -124,31 +129,34 @@ class Mattermost:
try: try:
self.mattermost.login() self.mattermost.login()
except: except (mattermostdriver.exceptions.NoAccessTokenProvided,
requests.exceptions.InvalidURL,
requests.exceptions.HTTPError):
print("Failed to log into Mattermost.") print("Failed to log into Mattermost.")
raise ValueError raise ConfigError
try: try:
self.channel = self.get_channel(channelname) self.channel = self.get_channel(channelname)
except: except ConfigError:
print("Couldn't find Mattermost channel.") print("Couldn't find Mattermost channel.")
raise ValueError raise ConfigError
self.reporters = {} self.reporters = {}
def get_channel(self, channelname): def get_channel(self, channelname):
""""Try to find the paper channel by display name.""" """"Try to find the paper channel by display name."""
teams = [team["id"] for team in self.mattermost.teams.get_user_teams("me")] mm = self.mattermost
teams = [team["id"] for team in mm.teams.get_user_teams("me")]
channels = [] channels = []
for team in teams: for team in teams:
teamchannels = [channel for channel teamchannels = [channel for channel
in self.mattermost.channels.get_channels_for_user("me", team) in mm.channels.get_channels_for_user("me", team)
if channel["display_name"] == channelname] if channel["display_name"] == channelname]
channels.extend(teamchannels) channels.extend(teamchannels)
# lets just hope no-one has the same channel name in multiple teams # lets just hope no-one has the same channel name in multiple teams
if len(channels) == 0: if len(channels) == 0:
print(f"Channel {channelname} does not exits") print(f"Channel {channelname} does not exits")
raise ValueError raise ConfigError
return channels[0]["id"] return channels[0]["id"]
def get_reporter(self, id): def get_reporter(self, id):
...@@ -394,6 +402,7 @@ class Papersurfer: ...@@ -394,6 +402,7 @@ class Papersurfer:
def close_details(self, _): def close_details(self, _):
self.mainloop.widget = self.top self.mainloop.widget = self.top
def get_config_file_paths(): def get_config_file_paths():
"""Find, load and parse a config file. """Find, load and parse a config file.
...@@ -406,8 +415,7 @@ def get_config_file_paths(): ...@@ -406,8 +415,7 @@ def get_config_file_paths():
- system path - system path
- XDG_CONFIG_DIRS/gascamcontrol/gascamcontrol.conf (linux only) - XDG_CONFIG_DIRS/gascamcontrol/gascamcontrol.conf (linux only)
>>> c = Conf() >>> type(get_config_file_paths())
>>> type(c.get_config_file_paths())
<class 'list'> <class 'list'>
""" """
...@@ -432,13 +440,16 @@ def get_config_file_paths(): ...@@ -432,13 +440,16 @@ def get_config_file_paths():
paths.extend(xdg_config_dirs) paths.extend(xdg_config_dirs)
return [os.path.join(p, default_filename) for p in paths if p] return [os.path.join(p, default_filename) for p in paths if p]
def interactive_configuration(): def interactive_configuration():
url = input("Mattermost URL (eg. mattermost.example.net): ") url = input("Mattermost URL (eg. mattermost.example.net): ")
channel = input("Channel (eg. Paper Club): ") channel = input("Channel (eg. Paper Club): ")
username = input("Username (eg. JohnDoe): ") username = input("Username (same as mattermost login, "
password = input("Password (eg. SuperSecret1): ") "eg. JohnDoe@example.net): ")
password = input("Password (same as mattermost login, eg. SuperSecret1): ")
return url, channel, username, password return url, channel, username, password
def parse_args(): def parse_args():
"""Parse command line arguments and config file.""" """Parse command line arguments and config file."""
parser = configargparse.ArgParser() parser = configargparse.ArgParser()
...@@ -463,7 +474,7 @@ def parse_args(): ...@@ -463,7 +474,7 @@ def parse_args():
url, channel, username, password = interactive_configuration() url, channel, username, password = interactive_configuration()
try: try:
Mattermost(url, channel, username, password) Mattermost(url, channel, username, password)
except: except ConfigError:
print("Failed to validate configuration, exiting.") print("Failed to validate configuration, exiting.")
exit(1) exit(1)
...@@ -487,9 +498,11 @@ def parse_args(): ...@@ -487,9 +498,11 @@ def parse_args():
return options.url, options.channel, options.username, options.password return options.url, options.channel, options.username, options.password
def main(): def main():
URL, CHANNEL, USERNAME, PASSWORD = parse_args() URL, CHANNEL, USERNAME, PASSWORD = parse_args()
Papersurfer(URL, CHANNEL, USERNAME, PASSWORD) Papersurfer(URL, CHANNEL, USERNAME, PASSWORD)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment