]> git.armaanb.net Git - phrases.git/blob - phrases.py
46db7829c2abe8dabf482c6387545846978f69c4
[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 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 possible phrases.")
38     parser.add_argument("-f", "--file",
39                         help="set the location of the phrase database.")
40     parser.add_argument("-o", "--open",
41                         type=int,
42                         help="specify the id of a specific phrase to print.")
43     args = parser.parse_args()
44
45     # find phrase file
46     if args.file:
47         phrase_file = args.file
48     if os.path.isfile("phrases.db"):
49         phrase_file = "phrases.db"
50     elif os.path.isfile("/usr/local/share/phrases/phrases.db"):
51         phrase_file = "/usr/local/share/phrases/phrases.db"
52     else:
53         sys.exit("cannot find the phrase database!")
54     
55     conn = sqlite3.connect(phrase_file)
56     c = conn.cursor()
57     c.execute("SELECT * FROM phrases WHERE length <= (?) AND length >= (?)",
58               (args.max, args.min))
59     data = c.fetchall()
60     rown = randint(0, len(data))
61     row = list(data[rown]) 
62
63     # Output as specified in flags
64     if not (args.id
65             or args.latin
66             or args.english
67             or args.notes
68             or args.num):
69         print(row[1])
70         sys.exit(0)
71     else:
72         if args.id:
73             print(row[0])
74         if args.latin:
75             print(row[1])
76         if args.english:
77             print(row[2])
78         if args.notes:
79             print(row[3])
80         if args.num:
81             print(len(data))
82         sys.exit(0)
83
84 if __name__ == "__main__":
85     main()