]> git.armaanb.net Git - phrases.git/blob - phrases.py
make database-file location more flexible
[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 csv
7 import random
8 import sys
9 import os.path
10
11 def main(args=sys.argv[1:]):
12     # Argument parsing
13     parser = argparse.ArgumentParser(
14         description="Latin famous phrases in the terminal.")
15     parser.add_argument("-i", "--id",
16                         action='store_true',
17                         help="print the id of the phrase.")
18     parser.add_argument("-l", "--latin",
19                         action='store_true',
20                         help="print the Latin phrase (default)")
21     parser.add_argument("-e", "--english",
22                         action='store_true',
23                         help="print the English translation.")
24     parser.add_argument("-n", "--notes",
25                         action='store_true',
26                         help="print any notes on phrase")
27     parser.add_argument("-m", "--min",
28                         default=0,
29                         type=int,
30                         help="set the minimum length of the Latin phrase")
31     parser.add_argument("-M", "--max",
32                         default=10000000,
33                         type=int,
34                         help="set the maximum length of Latin phrase")
35     parser.add_argument("-p", "--num",
36                         action='store_true',
37                         help="print number of possibilities within constraints")
38     parser.add_argument("-f", "--file",
39                         help="set the location of the phrase file")
40     args = parser.parse_args()
41
42     right_length = []
43
44     # find phrase file
45     if args.file:
46         phrase_file = args.file
47     if os.path.isfile("phrases.csv"):
48         phrase_file = "phrases.csv"
49     elif os.path.isfile("/usr/local/share/phrases/phrases.csv"):
50         phrase_file = "/usr/local/share/phrases/phrases.csv"
51     else:
52         sys.exit("cannot fine phrase database!")
53
54     # convert csv file into list
55     with open(phrase_file) as f:
56         reader = csv.reader(f)
57         next(reader, None) # skip header
58         all_lines = list(reader)
59     f.close()
60
61     # iterate through all the phrases
62     for row in all_lines:
63         try: # generate a shortlist of phrases of the right length
64             if args.max >= int(row[4]) >= args.min:
65                 right_length.append(row[0])
66         except: # skip malformed rows without exiting
67             pass
68
69     try: # choose a random id from the shortlist
70         chosen = int(right_length[random.randint(0, len(right_length) - 1)])
71     except:
72         sys.exit("no phrase within the given parameters!")
73
74     # Output as specified in flags
75     for row in all_lines:
76         if int(row[0]) == chosen:
77             if not (args.id
78                     or args.latin
79                     or args.english
80                     or args.notes
81                     or args.num):
82                 print(row[1])
83                 sys.exit(0)
84             else:
85                 if args.id:
86                     print(row[1])
87                 if args.latin:
88                     print(row[1])
89                 if args.english:
90                     print(row[2])
91                 if args.notes:
92                     print(row[3])
93                 if args.num:
94                     print(len(right_length))
95                 sys.exit(0)
96
97 if __name__ == "__main__":
98     main()