]> git.armaanb.net Git - phrases.git/blob - phrases.py
change to CSV instead of fortune-style database
[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 csv
9
10 # Main program
11 def main(args=sys.argv[1:]):
12     parser = argparse.ArgumentParser(description="Latin famous phrases in the terminal.")
13     parser.add_argument("-e", "--english", action='store_true', help="Print the English translation.")
14     parser.add_argument("-i", "--id", action='store_true', help="Print the id of the phrase.")
15     parser.add_argument("-l", "--latin", action='store_true', help="Print the Latin phrase (default)")
16     parser.add_argument("-m", "--min", type=int, default=0, help="Set the minimum length of the Latin phrase")
17     parser.add_argument("-M", "--max", type=int, default=10000000, help="Set the maximum length of Latin phrase")
18     parser.add_argument("-n", "--notes", action='store_true', help="Print any notes on phrase")
19     args = parser.parse_args()
20
21     id = []
22
23     with open('/usr/share/phrases/phrases.csv') as f:
24         reader = csv.reader(f)
25         for row in reader:
26             if args.max >= int(row[len(row) - 1]) >= args.min:
27                 id.append(row[0])
28         chosen = id[random.randint(0, len(id))]
29
30     with open('/usr/share/phrases/phrases.csv') as f:
31         reader = csv.reader(f)
32         for row in reader:
33             if row[0] == chosen:
34                 if args.id:
35                     print(row[0])
36                 if args.latin:
37                     print(row[1])
38                 if args.english:
39                     print(row[2])
40                 if args.notes:
41                     print(row[3])
42                 if not len(sys.argv) > 1:
43                     print(row[1])
44
45 if __name__ == "__main__":
46     main()