-
Notifications
You must be signed in to change notification settings - Fork 8
/
gedcom_calendar.py
executable file
·352 lines (280 loc) · 11.5 KB
/
gedcom_calendar.py
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python
"""
Provides an interactive date calculator
Inspired by http://www.geditcom.com/DateCalculator/index.html#about
"""
import sys
sys.path.insert(0, "geneaprove/utils")
from geneaprove.utils.date import DateRange, TimeDelta
import gtk
STACK_DEPTH = 20 # Number of lines in the GUI stack. Internally, the
# stack is illimited.
MAX_UNDO = 10 # Number of previous stacks that are saved
class EntryNoFrame(gtk.Entry):
def __init__(self, editable, *args):
super(EntryNoFrame, self).__init__(*args)
self.set_has_frame(False)
self.set_editable(editable)
self.set_can_focus(editable)
class StackMachine(object):
def __init__(self):
self.contents = [] # contents for each level of the stack
# (label, object)
# contents[0] is the "a" level,...
self.last_stacks = [] # List of previous stacks
self.__show_original = False # Whether to show process dates or dates
# as entered by the user
#################
# Stack history #
#################
def save_stack(self):
"""Save the current stack for future reuse"""
self.last_stacks = [self.contents] + self.last_stacks[0:MAX_UNDO]
def prev_stack(self, *args):
"""Replace the current stack with the previous stack"""
if len(self.last_stacks):
self.contents = self.last_stacks[0]
self.last_stacks = self.last_stacks[1:]
self.update()
###########################
# Operations on the stack #
###########################
def enter(self, *args):
"""Add the currently edited widget onto the stack.
This does nothing in non-gui mode.
"""
pass
def push(self, obj, label=""):
"""Inserts a new object on the stack"""
self.save_stack()
self.contents.insert(0, (label, obj))
self.update()
def drop(self, *args):
"""Removes the last element on the stack"""
self.save_stack()
self.contents = self.contents[1:]
self.update()
def dup(self, count=1, *args):
"""Duplicate the count first elements on the stack"""
self.save_stack()
self.contents = self.contents[0:count] + self.contents
self.update()
def set_label(self, label):
"""Set the label for the last element on the stack"""
self.contents[0] = (label, self.contents[0][1])
self.update()
def swap(self, *args):
"""Swap the last two elements on the stack"""
self.save_stack()
self.contents[1], self.contents[0] = self.contents[0], self.contents[1]
self.update()
def first(self):
"""Return the lowest element on the stack"""
return self.contents[0][1]
def unary(self, func):
"""Apply an operator on the first element of the stack, and replace
it with the result. FUNC must always return a result.
"""
self.enter()
if len(self.contents) >= 1:
self.save_stack()
first = self.contents[0][1]
self.contents[0] = (None, func(first))
self.update()
def plus(self, *args):
"""Add the first two levels of the stack"""
self.enter()
if len(self.contents) >= 2:
self.save_stack()
d1 = self.contents[0][1]
d2 = self.contents[1][1]
self.contents = self.contents[2:]
self.contents.insert(0, (None, d2 + d1))
self.update()
def minus(self, *args):
"""Substract the first two levels of the stack"""
self.enter()
if len(self.contents) >= 2:
self.save_stack()
d1 = self.contents[0][1]
d2 = self.contents[1][1]
self.contents = self.contents[2:]
self.contents.insert(0, (None, d2 - d1))
self.update()
########################
# Displaying the stack #
########################
def set_show_original(self, show_original):
self.__show_original = show_original
self.update()
def img(self, idx):
"""Return the text to display for the given level of the stack"""
if idx == 0 or idx > len(self.contents):
return ""
label, obj = self.contents[idx - 1]
if isinstance(obj, DateRange):
dpy = obj.display(original=self.__show_original)
else:
dpy = unicode(obj)
if label:
return f"{label}: {dpy}"
else:
return dpy
def update(self):
"""Update the display of self"""
for idx, d in enumerate(self.contents):
print(f"{chr(ord('a') + idx)}: {self.img(idx)}"
class GUIDateCalculator(StackMachine, gtk.Window):
def __init__(self):
StackMachine.__init__(self)
gtk.Window.__init__(self)
self.stack = [] # (entry, calendar) for the stack.
# stack[0] is the edition widget
# stack[1] is the "a" level, ...
self.set_title("Date calculator")
self.set_size_request(500, 700)
self.connect("destroy", gtk.main_quit)
vbox = gtk.VBox(spacing=0)
self.add(vbox)
stackdisplay = gtk.VBox(homogeneous=True)
vbox.pack_start(stackdisplay, expand=False, fill=True)
gr = gtk.SizeGroup(gtk.SIZE_GROUP_HORIZONTAL)
for stackrow in range(STACK_DEPTH, -2, -1):
hbox = gtk.HBox()
stackdisplay.pack_start(hbox, expand=False, fill=False)
if stackrow == -1:
label = gtk.Label("")
else:
label = gtk.Label(f"{chr(ord('a') + stackrow)}: ")
gr.add_widget(label)
hbox.pack_start(label, expand=False)
ent = EntryNoFrame(editable=stackrow==-1)
hbox.pack_start(ent)
cal = EntryNoFrame(editable=False)
hbox.pack_start(cal, expand=False, fill=False)
self.stack.insert(0, (ent, cal))
self.stack[0][1].set_text("Auto")
self.stack[0][0].grab_focus()
self.stack[0][0].connect("activate", self.enter)
table = gtk.Table(rows=2, columns=4, homogeneous=False)
vbox.pack_start(table)
months = gtk.Table(rows=4, columns=3, homogeneous=True)
table.attach(months, 0, 1, 0, 1, xoptions=gtk.FILL, yoptions=gtk.FILL,
xpadding=5, ypadding=5)
for idx, month in enumerate(("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")):
button = gtk.Button(month)
button.set_can_focus(False)
button.connect("clicked", self.insert, month)
months.attach(button, idx % 3, idx % 3 + 1,
idx / 3, idx / 3 + 1,
xpadding=2, ypadding=2,
xoptions=gtk.FILL, yoptions=gtk.FILL)
calendars = gtk.Table(rows=4, columns=1, homogeneous=True)
table.attach(calendars, 1, 2, 0, 1,
xoptions=gtk.FILL, yoptions=gtk.FILL,
xpadding=5, ypadding=5)
for idx, cal in enumerate(("Greg", "Juln", "Fren")):
button = gtk.Button(cal)
button.set_can_focus(False)
button.connect("clicked", self.set_calendar, cal)
calendars.attach(button, 0, 1,
idx, idx + 1,
xpadding=2, ypadding=2,
xoptions=gtk.FILL, yoptions=gtk.FILL)
operations = gtk.Table(rows=5, columns=2, homogeneous=True)
table.attach(operations, 2, 3, 0, 1,
xoptions=gtk.FILL, yoptions=gtk.FILL,
xpadding=5, ypadding=5)
button = gtk.Button("Enter")
button.connect("clicked", self.enter_or_dup)
operations.attach(button, 0, 2, 0, 1,
xpadding=2, ypadding=2,
xoptions=gtk.FILL, yoptions=gtk.FILL)
for idx, op in enumerate(
(("a+b", self.plus),
("b-a", self.minus),
("a<>b", self.swap),
("->day", self.day_of_week),
("drop", self.drop),
("Last Stack", self.prev_stack))):
button = gtk.Button(op[0])
button.set_can_focus(False)
if op[1]:
button.connect("clicked", op[1])
operations.attach(button, idx % 2, idx % 2 + 1,
idx / 2 + 1, idx / 2 + 2,
xpadding=2, ypadding=2,
xoptions=gtk.FILL, yoptions=gtk.FILL)
qualifiers = gtk.Table(rows=2, columns=5, homogeneous=True)
table.attach(qualifiers, 0, 3, 1, 2,
xoptions=gtk.FILL, yoptions=gtk.FILL,
xpadding=5, ypadding=5)
for idx, qual in enumerate(("From", "Bef", "Aft", "Bet",
"And", "To", "Abt", "Est")):
button = gtk.Button(qual)
button.set_can_focus(False)
button.connect("clicked", self.insert, qual)
qualifiers.attach(button, idx % 5, idx % 5 + 1,
idx / 5, idx / 5 + 1,
xpadding=2, ypadding=2,
xoptions=gtk.FILL, yoptions=gtk.FILL)
button = gtk.ToggleButton("Original dates")
button.set_active(False)
button.connect("toggled", self.toggle_show_original)
table.attach(button, 3, 4, 0, 1, xoptions=gtk.FILL,
yoptions=0, xpadding=5, ypadding=5)
self.show_all()
def toggle_show_original(self, button):
self.set_show_original(button.get_active())
def update(self):
"""Update the display of self"""
for idx, widgets in enumerate(self.stack):
txt = self.img(idx)
ent, cal = widgets
ent.set_text(txt)
cal.set_text("")
def enter_or_dup(self, *args):
if self.stack[0][0].get_text():
self.enter()
else:
self.dup()
def day_of_week(self, *args):
def compute(obj):
if isinstance(obj, DateRange):
return obj.day_of_week()
return obj
self.unary(compute)
def enter(self, *args):
"""Validate the current edit widget"""
ent, cal = self.stack[0]
if ent.get_text():
txt = ent.get_text()
obj = TimeDelta()
txt2 = obj.parse(txt)
if txt2 == txt:
obj = DateRange(txt)
self.push(obj)
ent.grab_focus()
def insert(self, button, text):
"""Insert some text in the current edit widget"""
ent, cal = self.stack[0]
pos = ent.get_position()
txt = ent.get_text()
ent.set_text(f"{txt[0:pos]} {text} {txt[pos:]}")
ent.set_position(pos + len(text) + 2)
def set_calendar(self, button, calendar):
ent, cal = self.stack[0]
cal.set_text(calendar)
win = GUIDateCalculator()
win.push(DateRange("Est JUL 1975"), "estimated")
win.push(DateRange("After JUL 1975"))
win.push(DateRange("~2011"))
win.push(DateRange("<JUL 1975"))
win.push(DateRange("10 vendemiaire XI"))
win.push(DateRange("2011-07-07 - 11 months"), "07jul-11m")
win.push(DateRange("NOV 20, 2011"))
win.push(DateRange("7 JUL 1975"))
win.push(DateRange("2011-10-07"))
win.push(DateRange("from 2011-01-01 to 2011-02-01"))
gtk.main()