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