-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotation-ctrl
More file actions
193 lines (137 loc) · 5.8 KB
/
annotation-ctrl
File metadata and controls
193 lines (137 loc) · 5.8 KB
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
#!/bin/python3
VERSION = '1.0'
help = """
annotation-ctrl is a single button GUI for annotation-mgr that toggles displaying or not
displaying PDF annotations that have been saved by annotation-mgr.
The command 'annotation-ctrl' starts annotation-mgr (if not running) and also
displays a single button GUI for starting and ending the annotation-mgr process.
Closing the annotation-ctrl GUI will also end the annotation-mgr process.
See: annotation-mgr --help
Usage: Typically annotation-ctrl would be started automatically after KDE starts. This
can be achieved using ~/.config/autostart-scripts. The file,
start-annotation-ctrl.sh
is an autostart script. See the comments in that file for how to install it.
Options: -h, --help will print this documentation then exit.
-v, --version will print the version number then exit.
-e, --exclude passed to annotation_mgr. See: annotation-mgr --help
-d, --debug passed to annotation_mgr. See: annotation-mgr --help
"""
from tkinter import *
from tkinter import ttk
import subprocess
import re
import os
from controls_file import *
# -------------------------------------------------------------------------------------------------
home = os.path.expanduser('~') + '/'
dotdir = home + '.annotation-manager/'
controls_file(dotdir + 'controls.dat')
controls_read()
controls_set('status', 'unknown')
controls_write()
# -------------------------------------------------------------------------------------------------
# Options.
import sys
import getopt
try:
(opts, args) = getopt.getopt(sys.argv[1:], "vhp:de:", ["version", "help", "position=", "debug", "exclude="])
except:
print("annotation-ctrl: Unknown option. Quitting.")
quit()
opts_dict = dict(opts)
if ('-v' in opts_dict) or ('--version' in opts_dict):
print(VERSION)
quit()
if ('-h' in opts_dict) or ('--help' in opts_dict):
print(help)
quit()
options = ''
if ('-d' in opts_dict) or ('--debug' in opts_dict):
options = ' -d'
debug = True
else:
debug = False
if ('-e' in opts_dict):
options = options + ' -e ' + opts_dict['-e']
elif ('--exclude' in opts_dict):
options = options + ' -e ' + opts_dict['--exclude']
# -------------------------------------------------------------------------------------------------
# Only run one instance of annotation-ctrl.
nproc = int(subprocess.run('ps -e | grep --count annotation-ctrl', shell=True, capture_output=True, text=True).stdout)
if nproc > 1:
print('annotation-ctrl: Nothing done, already running.')
quit()
# -------------------------------------------------------------------------------------------------
# Location constants.
home = os.path.expanduser('~') + '/'
dotdir = home + '.annotation-manager/'
dirbak = dotdir + 'bak/' # clean pdfs are temporarily backed up here while being managed.
dirann = dotdir + 'ann/' # permanently saved (smallish) annotation files.
# Create if necessary,
os.makedirs(dirbak, exist_ok = True)
os.makedirs(dirann, exist_ok = True)
# -------------------------------------------------------------------------------------------------
# Get the saved position of GUI window.
g = controls_get('geometry')
if g == None:
initial_pos = None
else:
[initial_pos] = re.findall('[\+\-].*', g) # drop the size part, keep from first + or -.
if debug: print('annotation-ctrl: initial_pos =', initial_pos)
# -------------------------------------------------------------------------------------------------
# Path to scripts.
scripts = os.path.dirname(os.path.realpath(__file__))
if debug: print('annotation-ctrl: scripts =', scripts)
# -------------------------------------------------------------------------------------------------
def start_annotation_mgr():
controls_read()
if not controls_get('status') == 'running':
controls_set('status', 'running')
controls_write()
subprocess.run(scripts + '/annotation-mgr' + options + ' &', shell=True)
lab['text'] = "Annotation manager is running."
b0['text'] = 'Stop'
b0['command'] = stop_annotation_mgr
def stop_annotation_mgr():
controls_read()
if not controls_get('status') == 'stopping':
controls_set('status', 'stopping')
controls_write()
lab['text'] = "Annotation manager is not running."
b0['text'] = 'Start'
b0['command'] = start_annotation_mgr
def save_geometry(event):
g = root.geometry()
if debug: print('save_geometry(): event =', event)
if debug: print('save_geometry(): g =', g)
controls_set('geometry', g)
controls_write()
def close_ctrl():
stop_annotation_mgr()
root.destroy()
# -------------------------------------------------------------------------------------------------
# Adapted from: http://tkdocs.com/tutorial/firstexample.html
# https://tkdocs.com/shipman/
root = Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
if debug: print('Screen size =', width, height)
if initial_pos == None: initial_pos = '+' + str(4*width//9) + '+' + str(2*height//5)
if debug: print('annotation-ctrl: Setting geometry =', initial_pos)
root.geometry(initial_pos)
root.title("annotation-ctrl")
frame = ttk.Frame(root)
frame['padding'] = (3, 5, 3, 5) # L,T,R,B
frame.grid()
lab = ttk.Label(frame, text="", width=29, anchor='center')
lab.grid(row=1)
b0 = ttk.Button(frame)
b0.grid(row=2)
for child in frame.winfo_children():
child.grid_configure(padx=5, pady=5)
root.update()
# root.update_idletasks()
root.bind("<Configure>", save_geometry)
start_annotation_mgr()
root.protocol("WM_DELETE_WINDOW", close_ctrl)
root.mainloop()