]> git.armaanb.net Git - phrases.git/blob - phrases.py
4c6a749477d1e9985a9d30dd8f112ffa8f77153f
[phrases.git] / phrases.py
1 #!/usr/bin/env python3
2 # Display Latin famous phrases in the terminal - python version
3 # Armaan Bhojwani 2020
4
5 import argparse
6 from random import randint
7 import sqlite3
8 import sys
9 import os.path
10
11 def parse_args():
12     parser = argparse.ArgumentParser(
13         description="Latin famous phrases in the terminal.")
14     parser.add_argument("-i", "--id",
15                         action='store_true',
16                         help="print the id of the phrase.")
17     parser.add_argument("-l", "--latin",
18                         action='store_true',
19                         help="print the Latin phrase (default)")
20     parser.add_argument("-e", "--english",
21                         action='store_true',
22                         help="print the English translation.")
23     parser.add_argument("-n", "--notes",
24                         action='store_true',
25                         help="print any notes on phrase.")
26     parser.add_argument("-m", "--min",
27                         default=0,
28                         type=int,
29                         help="set the minimum length of the Latin phrase.")
30     parser.add_argument("-M", "--max",
31                         default=10000000,
32                         type=int,
33                         help="set the maximum length of Latin phrase.")
34     parser.add_argument("-p", "--num",
35                         action='store_true',
36                         help="print number of possible phrases.")
37     parser.add_argument("-f", "--file",
38                         help="set the location of the phrase database.")
39     return parser.parse_args()
40
41 def output(args, row, numx):
42     if not (args.id
43             or args.latin
44             or args.english
45             or args.notes
46             or args.num):
47         print(row[1])
48         sys.exit(0)
49     else:
50         if args.id:
51             print(row[0])
52         if args.latin:
53             print(row[1])
54         if args.english:
55             print(row[2])
56         if args.notes:
57             print(row[3])
58         if args.num:
59             print(numx)
60         sys.exit(0)
61
62 def find_file(args):
63     if args.file:
64         return args.file
65     if os.path.isfile("phrases.db"):
66         return "phrases.db"
67     elif os.path.isfile("/usr/local/share/phrases/phrases.db"):
68         return "/usr/local/share/phrases/phrases.db"
69     else:
70         sys.exit("cannot find the phrase database!")
71
72 def main(args):
73     c = sqlite3.connect(find_file(args)).cursor()
74     c.execute("SELECT * FROM phrases WHERE length <= (?) AND length >= (?)",
75               (args.max, args.min))
76     data = c.fetchall()
77     output(args, list(data[randint(0, len(data))]), len(data))
78
79 if __name__ == "__main__":
80     main(parse_args())