diff --git a/papersurfer/papersurfer.py b/papersurfer/papersurfer.py index 03145f887c54559c27fe5e18a8af21b6d2865c10..89cb9ee5b34c8a0e9fe655be84cb357c96216722 100644 --- a/papersurfer/papersurfer.py +++ b/papersurfer/papersurfer.py @@ -34,6 +34,9 @@ class PostDTO: reporter: str doi: str + def __str__(self): + return self.message + @dataclass class PaperDTO: @@ -522,55 +525,6 @@ def interactive_configuration(): return url, channel, username, password -def parse_args(): - """Parse command line arguments and config file.""" - parser = configargparse.ArgParser() - parser._default_config_files = get_config_file_paths() - parser.add("-w", "--write-out-config-file", - help="takes the current command line args and writes them out " - "to a config file at the given path", - is_write_out_config_file_arg=True) - parser.add('-c', '--my-config', required=False, is_config_file=True, - help='config file path') - parser.add('--url', required=False, help='Mattermost url') - parser.add('--channel', required=False, help='Mattermost channel') - parser.add('-u', '--username', required=False, help='Mattermost username') - parser.add('-p', '--password', required=False, help='Mattermost password') - options = parser.parse_args() - - if not options.url: - start_interactive = input( - "Could not load config file or read command line arguments, do you" - " wish to start the interactive configuration assistant? (y/n) ") - if start_interactive == "y": - url, channel, username, password = interactive_configuration() - try: - Mattermost(url, channel, username, password) - except ConfigError: - print("Failed to validate configuration, exiting.") - exit(1) - - options.url = url - options.channel = channel - options.username = username - options.password = password - - configfile = "papersurfer.conf" - with open(configfile, "w") as file: - file.write(f"url = {url}\n") - file.write(f"channel = {channel}\n") - file.write(f"username = {username}\n") - file.write(f"password = {password}\n") - print(f"Configfile {configfile} written.") - - time.sleep(2) - else: - parser.print_help() - exit(0) - - return options.url, options.channel, options.username, options.password - - class PostDialog(urwid.WidgetWrap): """ UI: @@ -646,9 +600,80 @@ class PostDialog(urwid.WidgetWrap): self.doi_result.set_text("invalid doi") +def parse_args(): + """Parse command line arguments and config file.""" + parser = configargparse.ArgParser() + parser._default_config_files = get_config_file_paths() + parser.add("-w", "--write-out-config-file", + help="takes the current command line args and writes them out " + "to a config file at the given path", + is_write_out_config_file_arg=True) + parser.add('-c', '--my-config', required=False, is_config_file=True, + help='config file path') + parser.add('--url', required=False, help='Mattermost url') + parser.add('--channel', required=False, help='Mattermost channel') + parser.add('-u', '--username', required=False, help='Mattermost username') + parser.add('-p', '--password', required=False, help='Mattermost password') + parser.add('--dump-posts', action='store_true', + help="Dump mattermost paper posts to stdout and exit") + parser.add('--dump-bibtex', action='store_true', + help="Dump mattermost paper posts to stdout and exit") + options = parser.parse_args() + + if not options.url: + start_interactive = input( + "Could not load config file or read command line arguments, do you" + " wish to start the interactive configuration assistant? (y/n) ") + if start_interactive == "y": + url, channel, username, password = interactive_configuration() + try: + Mattermost(url, channel, username, password) + except ConfigError: + print("Failed to validate configuration, exiting.") + exit(1) + + options.url = url + options.channel = channel + options.username = username + options.password = password + + configfile = "papersurfer.conf" + with open(configfile, "w") as file: + file.write(f"url = {url}\n") + file.write(f"channel = {channel}\n") + file.write(f"username = {username}\n") + file.write(f"password = {password}\n") + print(f"Configfile {configfile} written.") + + time.sleep(2) + else: + parser.print_help() + exit(0) + + return options + + +def just_papers(url, channel, username, password): + """Fuck off with all this interactive shit.""" + posts = Mattermost(url, channel, username, password).retrieve() + for post in posts: + print(post) + + +def just_bibtex(url, channel, username, password): + posts = Mattermost(url, channel, username, password).retrieve() + dois = [post.doi for post in posts] + print(Bibtex().bib_from_dois(dois)) + + def main(): - URL, CHANNEL, USERNAME, PASSWORD = parse_args() - Papersurfer(URL, CHANNEL, USERNAME, PASSWORD) + opt = parse_args() + if opt.dump_posts: + just_papers(opt.url, opt.channel, opt.username, opt.password) + if opt.dump_bibtex: + just_bibtex(opt.url, opt.channel, opt.username, opt.password) + else: + Papersurfer(opt.url, opt.channel, opt.username, opt.password) if __name__ == "__main__":