]> git.armaanb.net Git - phrases.git/blob - phrases.py
Refactor
[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 from random import randint
7 import sqlite3
8 from sys import exit
9 from os import 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("-v", "--version",
27                         action='store_true',
28                         help="print version.")
29     parser.add_argument("-m", "--min",
30                         default=0,
31                         type=int,
32                         help="set the minimum length of the Latin phrase.")
33     parser.add_argument("-M", "--max",
34                         default=10000000,
35                         type=int,
36                         help="set the maximum length of Latin phrase.")
37     parser.add_argument("-p", "--num",
38                         action='store_true',
39                         help="print number of possible phrases.")
40     parser.add_argument("-f", "--file",
41                         help="set the location of the phrase database.")
42     return parser.parse_args()
43
44 def output():
45     data = c.fetchall()
46     row = list(data[randint(0, len(data) - 1)])
47
48     if not (args.id
49             or args.latin
50             or args.english
51             or args.notes
52             or args.num
53             or args.version):
54         print(row[1])
55         exit(0)
56     else:
57         if args.version:
58             print(version)
59         if args.id:
60             print(row[0])
61         if args.latin:
62             print(row[1])
63         if args.english:
64             print(row[2])
65         if args.notes:
66             print(row[3])
67         if args.num:
68             print(len(data))
69         exit(0)
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 def get_rand():
82     c.execute("SELECT * FROM phrases WHERE length <= (?) AND length >= (?)",
83               (args.max, args.min))
84
85 def main():
86     get_rand()
87     output()
88
89 if __name__ == "__main__":
90     version = "phrases 1.0.1"
91     args = parse_args()
92     c = sqlite3.connect(find_file()).cursor()
93     main()