-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaglines
executable file
·194 lines (151 loc) · 4.81 KB
/
Taglines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Preamble {{{1
""" Entry point for the Taglines program. """
import os
import sys
from taglines.argparser import parse_arguments
from taglines.database import Database
from taglines.shell_ui import ShellUI
def init_database(filepath): # {{{1
""" Create a new sqlite database file. """
if os.path.exists(filepath):
ok = ShellUI.get_input(
"Warning: "+filepath+" already exists. Overwrite? [y/N] ")
if ok and "yes".startswith(ok.lower()):
try:
os.remove(filepath)
except OSError as error:
sys.exit(
f"Error: could not delete old file: {error.args[1]}. "
"Exiting.")
else:
print("good bye")
sys.exit(1)
try:
db = Database()
if db:
db.initialise_file(filepath)
return True
except Database.DatabaseError as error:
print("Error initialising database: {error.args[0]}.")
return False
def get_random_item(_args): # {{{1
""" Retrieve one random tagline. """
db = Database(_args.file)
if db:
db.parse_arguments(_args)
tagline = db.random_tagline()
if tagline:
print(tagline)
return True
return False
def list_items(_args): # {{{1
""" Show list of taglines. """
db = Database(_args.file)
if db:
db.parse_arguments(_args)
first = True
for row in db.taglines():
if first:
first = False
else:
print("%")
print(row[0])
return True
return False
def show_keywords(filepath): # {{{1
""" Print all keywords, sorted alphabetically. """
db = Database(filepath)
if db:
for keyword in db.keywords(by_name=True):
print(keyword)
return True
return False
def show_authors(filepath): # {{{1
""" Print all authors, sorted alphabetically. """
db = Database(filepath)
if db:
for author in db.authors():
print(author)
return True
return False
def show_stats(filepath): # {{{1
""" Print tabular statistics about the given database file. """
db = Database(filepath)
if db:
stats = db.stats()
labels = [
"Database schema version:",
"Number of taglines:",
"Number of texts:",
"Average text length:",
"Number of keywords:",
"Number of keyword assignments:",
"Number of authors:",
"Used languages:",
]
values = [
"unknown" if stats["schema version"] is None else
"{:6d}".format(stats["schema version"]),
"{:6d}".format(stats["tagline count"],),
"{:6d} (ø {:5.2f} per tagline)".format(
stats["line count"],
stats["line count"]/stats["tagline count"] if
stats["line count"] != 0 else 0),
"{:8.1f}".format(stats["avg tagline length"],),
"{:6d}".format(stats["keyword count"],),
"{:6d} (ø {:5.2f} per tagline)".format(
stats["keyword assignments"],
stats["keyword assignments"]/stats["tagline count"] if
stats["tagline count"] != 0 else 0),
"{:6d}".format(stats["author count"],),
"{:6d}".format(stats["language count"],),
]
maxlabelwidth = max(len(label) for label in labels)
for keyval in zip(labels, values):
print(f"{keyval[0]:{maxlabelwidth}} {keyval[1]}")
return True
return False
def interactive_menu(filepath, editor): # {{{1
""" Start interactive console menu mode and exit at the end. """
try:
db = Database(filepath)
if db:
shell = ShellUI(db, editor)
result = shell.main_menu()
except Exception:
raise
print(sys.exc_info()[1])
result = False
return result
def main(): # {{{1
args = parse_arguments()
result = None
try:
if args.init:
result = init_database(args.file)
if args.random:
result = get_random_item(args)
if args.list:
result = list_items(args)
if args.show_keywords:
result = show_keywords(args.file)
if args.show_authors:
result = show_authors(args.file)
if args.stats:
result = show_stats(args.file)
if args.interactive:
result = interactive_menu(args.file, args.editor)
except Exception as error:
raise
print(error, file=sys.stderr)
result = False
if result is None:
sys.exit(-1)
elif result:
sys.exit(0)
else:
sys.exit(1)
if __name__ == "__main__": # {{{1
main()