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