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