-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog_commodity.py
500 lines (359 loc) · 18.2 KB
/
dialog_commodity.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# sorting by locale
import locale
locale.setlocale(locale.LC_ALL, "")
#junk.sort(key=locale.strxfrm)
from gi.repository import Gtk
import pdb
import traceback
import gnucash
def N_(msg):
return msg
from gnc_builder import GncBuilder
import gnc_utils
import sw_app_utils
# hmm - question is which is the primary object to choose as the
# defining class - DialogCommodity or SelectCommodityWindow
# also are the objects or GObjects??
class DialogCommodity(object):
DIAG_COMM_CURRENCY = 0 # /**< Dialog box should only allow selection
# of a currency. */
DIAG_COMM_NON_CURRENCY = 1 # /**< Dialog box should allow selection of
# anything but a currency. */
DIAG_COMM_ALL = 2 # /**< Dialog box should allow selection of
# anything. */
def __init__ (self, orig_sel, parent, mode, user_message=None, cusip=None, fullname=None, mnemonic=None):
# this function split out into this init function and a run function
# as per Gtk.Dialog - in C this function sets up the dialog and runs it,
# returning the selected item - python cant return from __init__
retval = None
self.win = SelectCommodityWindow.gnc_ui_select_commodity_create(orig_sel, mode)
self.win.default_cusip = cusip
self.win.default_fullname = fullname
self.win.default_mnemonic = mnemonic
self.win.default_user_symbol = ""
if parent:
self.win.dialog.set_transient_for(parent)
if user_message:
initial = user_message
elif cusip or fullname or mnemonic:
initial = N_("\nPlease select a commodity to match:")
else:
initial = ""
user_prompt_text = "%s%s%s%s%s%s%s"%( \
initial,
N_("\nCommodity: ") if fullname else "",
fullname if fullname else "",
N_("\nExchange code (ISIN, CUSIP or similar): ") if cusip else "",
cusip if cusip else "",
N_("\nMnemonic (Ticker symbol or similar): ") if mnemonic else "",
mnemonic if mnemonic else "")
self.win.select_user_prompt.set_text(user_prompt_text)
def run (self):
done = False
while not done:
value = self.win.dialog.run()
if value == Gtk.ResponseType.OK:
# protect against missing builder handler setup
try:
retval = self.win.selection
except Exception as errexc:
traceback.print_exc()
retval = None
done = True
elif value == gnc_utils.RESPONSE_NEW:
self.win.gnc_ui_select_commodity_new_cb()
else:
retval = None
done = True
self.win.dialog.destroy()
return retval
@classmethod
def gnc_ui_select_commodity_modal_full (cls, orig_sel, parent, mode, user_message=None, cusip=None, fullname=None, mnemonic=None):
newobj = cls(orig_sel, parent, mode, user_message=user_message, cusip=cusip, fullname=fullname, mnemonic=mnemonic)
return newobj
@classmethod
def gnc_ui_select_commodity_modal (cls, orig_sel, parent, mode):
return cls.gnc_ui_select_commodity_modal_full(orig_sel, parent, mode)
class SelectCommodityWindow(object):
def __init__ (self, orig_sel, mode):
#retval = SelectCommodityWindow()
builder = GncBuilder()
builder.add_from_file("dialog-commodity.glade", "liststore1")
builder.add_from_file("dialog-commodity.glade", "liststore2")
builder.add_from_file("dialog-commodity.glade", "security_selector_dialog")
self.builder_handlers = { \
# 'onDeleteWindow' : Gtk.main_guit,
'gnc_ui_select_commodity_changed_cb' : self.gnc_ui_select_commodity_changed_cb,
'gnc_ui_select_commodity_namespace_changed_cb' : self.gnc_ui_select_commodity_namespace_changed_cb,
}
builder.connect_signals(self.builder_handlers)
builder.connect_signals({})
self.dialog = builder.get_object("security_selector_dialog")
namespace_combo = builder.get_object("ss_namespace_cbwe")
self.namespace_combo = NamespacePicker(namespace_combo)
commodity_combo = builder.get_object("ss_commodity_cbwe")
self.commodity_combo = CommodityPicker(commodity_combo)
self.select_user_prompt = builder.get_object("select_user_prompt")
self.ok_button = builder.get_object("ss_ok_button")
label = builder.get_object("item_label")
self.namespace_combo.require_list_item()
self.commodity_combo.require_list_item()
self.select_user_prompt.set_text("")
if mode == DialogCommodity.DIAG_COMM_ALL:
title = N_("Select security/currency")
text = N_("_Security/currency:")
elif mode == DialogCommodity.DIAG_COMM_NON_CURRENCY:
title = N_("Select security")
text = N_("_Security:")
#elif mode == DialogCommodity.DIAG_COMM_CURRENCY:
else:
title = N_("Select currency")
text = N_("Cu_rrency:")
button = builder.get_object("ss_new_button")
button.destroy()
#pdb.set_trace()
self.dialog.set_title(title)
label.set_text_with_mnemonic(text)
#gnc_ui_update_namespace_picker(self.namespace_combo,orig_sel.get_namespace(),mode)
#namespace = gnc_ui_namespace_picker_ns(self.namespace_combo)
#gnc_ui_update_commodity_picker(self.commodity_combo,namespace,orig_sel.get_printname())
self.namespace_combo.gnc_ui_update_namespace_picker(orig_sel.get_namespace(),mode)
namespace = self.namespace_combo.gnc_ui_namespace_picker_ns()
self.commodity_combo.gnc_ui_update_commodity_picker(namespace,orig_sel.get_printname())
@classmethod
def gnc_ui_select_commodity_create (cls, orig_sel, mode):
return cls(orig_sel, mode)
def gnc_ui_select_commodity_new_cb (self, *args):
print("gnc_ui_select_commodity_new_cb",args)
#w = args[1]
namespace = self.namespace_combo.gnc_ui_namespace_picker_ns()
new_commodity = gnc_ui_new_commodity_modal_full(namespace,
self.dialog,
self.default_cusip,
self.default_fullname,
self.default_mnemonic,
self.default_user_symbol,
self.default_fraction)
if new_commodity:
self.namespace_combo.gnc_ui_update_namespace_picker(new_commodity.get_namespace(),DialogCommodity.DIAG_COMM_ALL)
self.commodity_combo.gnc_ui_update_commodity_picker(new_commodity.get_namespace(),new_commodity.get_printname())
def gnc_ui_select_commodity_namespace_changed_cb (self, *args):
print("gnc_ui_select_commodity_namespace_changed_cb",args)
#ENTER("cbwe=%p, user_data=%p", cbwe, user_data)
# this is actual Gtk ComboBox object
combobox = args[0]
#DEBUG("namespace=%s", namespace)
namespace = self.namespace_combo.gnc_ui_namespace_picker_ns()
self.commodity_combo.gnc_ui_update_commodity_picker(namespace,None)
def gnc_ui_select_commodity_changed_cb (self, *args):
print(".gnc_ui_select_commodity_changed_cb",args)
#pdb.set_trace()
#ENTER("cbwe=%p, user_data=%p", cbwe, user_data)
# this is actual Gtk ComboBox object
combobox = args[0]
#DEBUG("namespace=%s", namespace)
namespace = self.namespace_combo.gnc_ui_namespace_picker_ns()
fullname = self.commodity_combo.commodity_combo.get_child().get_text()
table = sw_app_utils.get_current_book().get_table()
try:
#self.selection = table.lookup(namespace,fullname)
self.selection = table.find_full(namespace,fullname)
except RuntimeError as errexc:
# fail to find it
self.selection = None
#pdb.set_trace()
ok = self.selection != None
self.dialog.set_default_response(0 if ok else 2)
#LEAVE("sensitive=%d, default = %d", ok, ok ? 0 : 2)
class NamespacePicker(object):
def __init__ (self, namespace_combo):
# nice idea but wont work here - namespace_combo not defined using class
#newsubcls = type("ComboBoxWithUtils",(namespace_combo.__class__,gnc_utils.GncCBWEMixin),{})
#namespace_combo.__class__ = newsubcls
self.namespace_combo = namespace_combo
gnc_utils.add_utils(self.namespace_combo)
def require_list_item(self):
self.namespace_combo.require_list_item()
def gnc_ui_update_namespace_picker (self, init_string, mode):
model = self.namespace_combo.get_model()
model.clear()
self.namespace_combo.set_active(-1)
if mode == DialogCommodity.DIAG_COMM_ALL:
# get_namespaces crashes
# not clear what the difference is
#namespaces = sw_app_utils.get_current_commodities().get_namespaces()
namelst = sw_app_utils.get_current_commodities().get_namespaces_list()
namespaces = [ (x.get_name(),x) for x in namelst ]
elif mode == DialogCommodity.DIAG_COMM_NON_CURRENCY:
#namespaces = sw_app_utils.get_current_commodities().get_namespaces()
namelst = sw_app_utils.get_current_commodities().get_namespaces_list()
#namespaces = [ (x.get_name(),x) for x in namelst ]
namespaces = []
#node = g_list_find_custom(namespaces, 'CURRENCY', collate)
#if node:
# namespaces = g_list_remove_link(namespaces, node)
for x in namelst:
if x.get_name() == 'CURRENCY':
continue
namespaces.append((x.get_name(),x))
if gnc_commodity_namespace_is_iso(init_string):
init_string = None
#elif mode == DialogCommodity.DIAG_COMM_CURRENCY:
else:
#namespaces = g_list_prepend(None, 'CURRENCY')
namespacecur = sw_app_utils.get_current_commodities().find_namespace('CURRENCY')
namespaces = [ ('CURRENCY',namespacecur) ]
pdb.set_trace()
#namespaces = g_list_sort(namespaces, collate)
#namespaces.sort(cmp=lambda x,y: locale.strcoll(x[0],y[0]))
namespaces.sort(key=lambda x: locale.strxfrm(x[0]))
current = 0
match = 0
#for node in namespaces:
# if g_utf8_collate(node.data, "GNC_LEGACY_CURRENCIES") == 0:
# continue
# if g_utf8_collate(node.data, "template") != 0:
# model.append((node.data,))
# if g_utf8_collate(node.data, init_string) == 0:
# match = current
# current += 1
for current,node in enumerate(namespaces):
if node[0] == "GNC_LEGACY_CURRENCIES":
continue
if node[0] != "template":
model.append((node[0],))
if node[0] == init_string:
match = current
# current += 1
self.namespace_combo.set_active(match)
def gnc_ui_namespace_picker_ns (self):
namespace = self.namespace_combo.get_child().get_text()
if namespace == 'ISO4217':
return 'CURRENCY'
return namespace
class CommodityPicker(object):
def __init__ (self, commodity_combo):
self.commodity_combo = commodity_combo
gnc_utils.add_utils(self.commodity_combo)
def require_list_item(self):
self.commodity_combo.require_list_item()
def gnc_ui_update_commodity_picker (self, namespace, init_string):
#pdb.set_trace()
model = self.commodity_combo.get_model()
model.clear()
entry = self.commodity_combo.get_child()
entry.delete_text(0,-1)
self.commodity_combo.set_active(-1)
table = sw_app_utils.get_current_book().get_table()
commodities = table.get_commodities(namespace)
commodity_items = []
for commod in commodities:
commodity_items.append(commod.get_printname())
#commodity_items.sort(cmp=lambda x,y: locale.strcoll(x[0],y[0]))
commodity_items.sort(key=lambda x: locale.strxfrm(x[0]))
match = 0
for current,commod in enumerate(commodity_items):
model.append((commod,))
if commod == init_string:
match = current
self.commodity_combo.set_active(match)
class CommonCommodityModal(object):
def __init__ (self,commodity,parent,namespace,cusip,fullname,mnemonic,user_symbol,fraction):
# as before splitting the C code into this init and a run function
# this is equivalent of gnc_ui_common_commodity_modal
#ENTER(" ")
retval = None
if commodity:
namespace = commodity.get_namespace()
fullname = commodity.get_fullname()
mnemonic = commodity.get_mnemonic()
user_symbol = commodity.get_user_symbol()
cusip = commodity.get_cusip()
fraction = commodity.get_fraction()
else:
if sw_app_utils.gnc_commodity_namespace_is_iso(namespace):
namespace = None
self.win = gnc_ui_build_commodity_dialog(namespace, parent, fullname,
mnemonic, user_symbol, cusip,
fraction, commodity != None)
self.win.update_quote_info(commodity)
self.win.edit_commodity = commodity
#self.win.gnc_ui_commodity_quote_info_cb(self.win.get_quote_check)
#LEAVE(" ")
def run (self):
done = False
while not done:
value = self.win.dialog.run()
if value == Gtk.ResponseType.OK:
#DEBUG("case OK")
done = self.win.gnc_ui_commodity_dialog_to_object()
retval = self.win.edit_commodity
elif value == Gtk.ResponseType.HELP:
#DEBUG("case HELP")
if help_callback:
help_callback()
else:
#DEBUG("default: %d", value)
retval = None
done = True
self.win.dialog.destroy()
return retval
@classmethod
def gnc_ui_new_commodity_modal_full (cls,namespace,parent,cusip,fullname,mnemonic,user_symbol,fraction):
#return gnc_ui_new_commodity_modal_full(None,parent,namespace,cusip,fullname,mnemonic,user_symbol,fraction)
return CommonCommodityModal(None,parent,namespace,cusip,fullname,mnemonic,user_symbol,10000)
class QuoteSourceType(object):
SOURCE_SINGLE = 0 # /**< This quote source pulls from a single
# * specific web site. For example, the
# * yahoo_australia source only pulls from
# * the yahoo web site. */
SOURCE_MULTI = 1 # /**< This quote source may pull from multiple
# * web sites. For example, the australia
# * source may pull from ASX, yahoo, etc. */
SOURCE_UNKNOWN = 2 # /**< This is a locally installed quote source
# * that gnucash knows nothing about. May
# * pull from single or multiple
# * locations. */
SOURCE_MAX = 3
SOURCE_CURRENCY = SOURCE_MAX #/**< The special currency quote source. */
class CommodityWindow(object):
def __init__ (self, namespace, parent, fullname,mnemonic, user_symbol, cusip, fraction, edit):
#ENTER("widget=%p, selected namespace=%s, fullname=%s, mnemonic=%s",
# parent, selected_namespace, fullname, mnemonic);
builder = GncBuilder()
builder.add_from_file ("dialog-commodity.glade", "liststore2")
builder.add_from_file ("dialog-commodity.glade", "adjustment1")
builder.add_from_file ("dialog-commodity.glade", "security_dialog")
self.connect_signals({})
self.dialog = builder.get_object("security_dialog")
if parent != None:
self.dialog.set_transient_for(parent)
self.edit_commodity = None
self.help_button = builder.get_object("help_button")
if not help_callback:
help_button.hide()
self.source_button = [None,None,None]
self.fullname_entry = builder.get_object("fullname_entry")
self.mnemonic_entry = builder.get_object("mnemonic_entry")
self.user_symbol_entry = builder.get_object("user_symbol_entry")
self.namespace_combo = builder.get_object("namespace_cbwe")
self.code_entry = builder.get_object("code_entry")
self.fraction_spinbutton = builder.get_object("fraction_spinbutton")
self.ok_button = builder.get_object("ok_button")
self.get_quote_check = builder.get_object("get_quote_check")
self.source_label = get_object("source_label")
self.source_button[QuoteSourceType.SOURCE_SINGLE] = builder.get_object("single_source_button")
self.source_button[QuoteSourceType.SOURCE_MULTI] = builder.get_object("multi_source_button")
self.quote_tz_label = builder.get_object("quote_tz_label")
self.table = builder.get_object("edit_table")
sec_label = builder.get_object("security_label")
self.comm_section_top = self.table.child_get(sec_label, "bottom-attach")
widget = builder.get_object("quote_label")
self.comm_section_bottom = self.table.child_get(widget, "top-attach")
self.comm_symbol_line = self.table.child_get(self.user_symbol_entry, "top-attach")
# theres more!!
@classmethod
def gnc_ui_build_commodity_dialog(cls,namespace, parent, fullname,
mnemonic, user_symbol, cusip,
fraction, edit):
return cls(namespace, parent, fullname,mnemonic, user_symbol, cusip, fraction, edit)