From: Armaan Bhojwani <3fb650a9-b47e-4604-a282-1dd91953b2ee@anonaddy.me> Date: Tue, 1 Dec 2020 17:36:13 +0000 (-0500) Subject: add argparsing, branding X-Git-Tag: v1.0.0~13 X-Git-Url: https://git.armaanb.net/tall.bot/tree/?p=phrases.git;a=commitdiff_plain;h=a1843720c7444be2e77d41bab8bd135015247002 add argparsing, branding * Add argument parsing and ability to output more than just the latin phrase * Add logo --- diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2d19fc7..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.html diff --git a/branding/logo.png b/branding/logo.png new file mode 100644 index 0000000..b5037e4 Binary files /dev/null and b/branding/logo.png differ diff --git a/branding/logo.xcf b/branding/logo.xcf new file mode 100644 index 0000000..66be6da Binary files /dev/null and b/branding/logo.xcf differ diff --git a/phrases.py b/phrases.py index bcf3f73..39d6f0c 100755 --- a/phrases.py +++ b/phrases.py @@ -2,22 +2,13 @@ # Display Latin famous phrases in the terminal # Armaan Bhojwani 2020 +import argparse import random -import os import sys import re -# Use system random if available, otherwise, use Python's -def _random_int(start, end): - try: - r = random.SystemRandom() - except: - r = random - - return r.randint(start, end) - # Create list of phrases from phrase file -def _read_phrases(phrase_file): +def read_phrases(phrase_file): f = open(phrase_file, 'r') contents = f.read() @@ -26,33 +17,44 @@ def _read_phrases(phrase_file): phrases = [] cur = [] - def save_if_nonempty(buf): - phrase = '\n'.join(buf) - if phrase.strip(): - phrases.append(phrase) - for line in lines: if delim.match(line): - save_if_nonempty(cur) + phrase = '\n'.join(cur) + if phrase.strip(): + phrases.append(phrase) cur = [] continue - cur.append(line) - if cur: - save_if_nonempty(cur) - return phrases -# Return a random phrase from the phrases list -def get_random_phrase(phrase_file): - phrases = list(_read_phrases(phrase_file)) - randomRecord = _random_int(0, len(phrases) - 1) +# Main program +def main(args=sys.argv[1:]): + parser = argparse.ArgumentParser(description="Latin famous phrases in the terminal.") + parser.add_argument("-e", "--english", action='store_true', help="Output English.") + parser.add_argument("-l", "--latin", action='store_true', help="Output Latin (default)") + parser.add_argument("-n", "--notes", action='store_true', help="Output notes on phrase") + parser.add_argument("-m", "--min", type=int, help="Set the minimum length of latin") + parser.add_argument("-M", "--max", type=int, help="Set the maximum length of latin") + args = parser.parse_args() + + phrases = list(read_phrases("/usr/share/phrases/phrases")) + randomRecord = random.randint(0, len(phrases) - 1) randphrase = phrases[randomRecord] - return randphrase.partition('\n')[0] -# Main program -def main(): - print(get_random_phrase("/usr/share/phrases/phrases")) + if args.min: + while True: + randomRecord = random.randint(0, len(phrases) - 1) + randphrase = phrases[randomRecord] + if args.min <= len(randphrase.split('\n')[0]) <= args.max: + break + if args.latin: + print(randphrase.split('\n')[0]) + if args.english: + print(randphrase.split('\n')[1]) + if args.notes: + print(randphrase.split('\n')[2]) + if not len(sys.argv) > 1: + print(randphrase.split('\n')[0]) main()