]> git.armaanb.net Git - phrases.git/blob - phrases.py
39d6f0c7e59be1d3101d120fb89b57b669e68c5d
[phrases.git] / phrases.py
1 #!/usr/bin/env python3
2 # Display Latin famous phrases in the terminal
3 # Armaan Bhojwani 2020
4
5 import argparse
6 import random
7 import sys
8 import re
9
10 # Create list of phrases from phrase file
11 def read_phrases(phrase_file):
12     f = open(phrase_file, 'r')
13     contents = f.read()
14
15     lines = [line.rstrip() for line in contents.split('\n')]
16     delim = re.compile(r'^%$')
17     phrases = []
18     cur = []
19
20     for line in lines:
21         if delim.match(line):
22             phrase = '\n'.join(cur)
23             if phrase.strip():
24                 phrases.append(phrase)
25             cur = []
26             continue
27         cur.append(line)
28
29     return phrases
30
31 # Main program
32 def main(args=sys.argv[1:]):
33     parser = argparse.ArgumentParser(description="Latin famous phrases in the terminal.")
34     parser.add_argument("-e", "--english", action='store_true', help="Output English.")
35     parser.add_argument("-l", "--latin", action='store_true', help="Output Latin (default)")
36     parser.add_argument("-n", "--notes", action='store_true', help="Output notes on phrase")
37     parser.add_argument("-m", "--min", type=int, help="Set the minimum length of latin")
38     parser.add_argument("-M", "--max", type=int, help="Set the maximum length of latin")
39     args = parser.parse_args()
40
41     phrases = list(read_phrases("/usr/share/phrases/phrases"))
42     randomRecord = random.randint(0, len(phrases) - 1)
43     randphrase = phrases[randomRecord]
44
45     if args.min:
46         while True:
47             randomRecord = random.randint(0, len(phrases) - 1)
48             randphrase = phrases[randomRecord]
49             if args.min <= len(randphrase.split('\n')[0]) <= args.max:
50                 break
51     if args.latin:
52         print(randphrase.split('\n')[0])
53     if args.english:
54         print(randphrase.split('\n')[1])
55     if args.notes:
56         print(randphrase.split('\n')[2])
57     if not len(sys.argv) > 1:
58         print(randphrase.split('\n')[0])
59
60 main()