]> git.armaanb.net Git - phrases.git/blob - phrases.py
Make manconvert smarter
[phrases.git] / phrases.py
1 #!/usr/bin/env python3
2 # Display Latin famous phrases in the terminal
3 # Armaan Bhojwani 2021
4
5 import argparse
6 from random import randint
7 import sqlite3
8 from sys import exit
9 from os import path
10
11
12 def parse_args():
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("-v", "--version",
28                         action='version',
29                         version="phrases 1.0.3")
30     parser.add_argument("-m", "--min",
31                         default=0,
32                         type=int,
33                         help="set the minimum length of the Latin phrase")
34     parser.add_argument("-M", "--max",
35                         default=10000000,
36                         type=int,
37                         help="set the maximum length of Latin phrase")
38     parser.add_argument("-p", "--num",
39                         action='store_true',
40                         help="print number of possible phrases")
41     parser.add_argument("-f", "--file",
42                         help="set the location of the phrase database")
43     return parser.parse_args()
44
45
46 def output():
47     data = c.fetchall()
48     row = list(data[randint(0, len(data) - 1)])
49
50     if not (args.id
51             or args.latin
52             or args.english
53             or args.notes
54             or args.num):
55         print(row[1])
56         exit(0)
57     else:
58         if args.id:
59             print(row[0])
60         if args.latin:
61             print(row[1])
62         if args.english:
63             print(row[2])
64         if args.notes:
65             print(row[3])
66         if args.num:
67             print(len(data))
68         exit(0)
69
70
71 def find_file():
72     if args.file:
73         return args.file
74     if path.isfile("phrases.db"):
75         return "phrases.db"
76     elif path.isfile("/usr/local/share/phrases/phrases.db"):
77         return "/usr/local/share/phrases/phrases.db"
78     else:
79         exit("cannot find the phrase database!")
80
81
82 def get_rand():
83     c.execute("SELECT * FROM phrases WHERE length <= (?) AND length >= (?)",
84               (args.max, args.min))
85
86
87 def main():
88     get_rand()
89     output()
90
91
92 if __name__ == "__main__":
93     args = parse_args()
94     c = sqlite3.connect(find_file()).cursor()
95     main()