-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport_options.py
More file actions
1300 lines (1080 loc) · 52.1 KB
/
report_options.py
File metadata and controls
1300 lines (1080 loc) · 52.1 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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# here we implement the scheme options in python - converting to objects as much as possible
# this file is a combination of the scheme options.scm and options-utilities.scm
# the make functions in options.scm are converted to instantions of an options class
# that implements the options
# eg make-number-range-options gets converted to NumberRangeOption() instantiation
# higher level functions such as the add functions in options-utilties.scm
# are added as functions to the OptionsDB class
import sys
import os
import numbers
import datetime
from operator import itemgetter
import pdb
import traceback
# not apparently needed
# either
#from gi.repository import Gtk
#from gi.repository import GObject
# or
#import gtk
#import gobject
try:
import sw_app_utils
import gnucash
#from gnucash import *
#from _sw_core_utils import gnc_prefs_is_extra_enabled
except Exception as errexc:
print("Failed to import!!", file=sys.stderr)
print(str(errexc), file=sys.stderr)
pdb.set_trace()
import date_utils
import gnucash_log
#pdb.set_trace()
# define a function equivalent to N_ for internationalization
def N_(msg):
return msg
class OptionsDB(object):
# note this is a scheme database in normal gnucash
def __init__ (self,options=None):
self.option_hash = {}
self.options_changed = False
self.changed_hash = {}
self.callback_hash = {}
self.last_callback_id = 0
self.default_section = None
def lookup_name (self, section, option_name):
section_hash = self.option_hash[section]
option = section_hash[option_name]
# apparently options can be renamed in scheme
# theres a load of coding renaming some options
return option
def option_changed (self, section, option_name):
self.options_changed = True
if section not in self.changed_hash:
self.changed_hash[section] = {}
self.changed_hash[section][option_name] = True
def clear_changes (self):
self.options_changed = False
self.changed_hash = {}
def register_callback (self, section, name, callback):
gnucash_log.dbglog("optionsDB register_callback")
self.last_callback_id += 1
self.callback_hash[self.last_callback_id] = [section, name, callback]
return self.last_callback_id
def unregister_callback (self, callback_id):
gnucash_log.dbglog("optionsDB unregister_callback")
if callback_id in self.callback_hash:
del self.callback_hash[self.last_callback_id]
else:
print("unregister callback - nonexistent ID!!", callback_id)
pdb.set_trace()
print("unregister callback - nonexistent ID!!", callback_id)
def run_callbacks (self):
if self.options_changed:
cblist = []
for idky in self.callback_hash:
cblist.append((idky,self.callback_hash[idky]))
cblist.sort(key=itemgetter(1))
for clbitm in cblist:
self.run_callback(clbitm[0],clbitm[1])
self.clear_changes()
def run_callback (self, id, cbdata):
#pdb.set_trace()
section = cbdata[0]
name = cbdata[1]
callback = cbdata[2]
if section == None:
callback()
else:
section_changed_hash = self.changed_hash[section]
if section_changed_hash != None:
if name == None:
callback()
elif name in section_changed_hash:
callback()
def register_option (self, new_option):
name = new_option.name
section = new_option.section
if section in self.option_hash:
self.option_hash[section][name] = new_option
else:
self.option_hash[section]= {}
self.option_hash[section][name] = new_option
# this doesnt make sense in python
# oh maybe it does - we get multiple instances of the lambda each with
# a different section, name variables
new_option.changed_callback = lambda lcl_sect=section,lcl_name=name: self.option_changed(lcl_sect,lcl_name)
def options_for_each (self):
#pdb.set_trace()
for section_hash in self.option_hash:
option_hash = self.option_hash[section_hash]
for name in option_hash:
yield option_hash[name]
def set_default_section (self, section_name):
self.default_section = section_name
def get_default_section (self):
return self.default_section
def add_report_date (self, pagename, optname, sort_tag):
self.register_option(EndDateOption(pagename, optname, sort_tag, N_("Select a date to report on.")))
def add_date_interval (self, pagename, name_from, name_to, sort_tag):
# scheme has a make-date-interval - but that just ends up as 2 DateOptions
# create FromDataOption and use that here
# just do 2 date options here - as we cant call register_option as scheme does
# this is better
date_from = FromDateOption(pagename, name_from, sort_tag+"a", N_("Start of reporting period."))
date_to = EndDateOption(pagename, name_to, sort_tag+"b", N_("End of reporting period."))
self.register_option(date_from)
self.register_option(date_to)
def add_interval_choice (self, pagename, optname, sort_tag, default):
self.register_option(MultiChoiceOption(pagename, optname, sort_tag,
N_("The amount of time between data points."),
default,
[ \
('DayDelta', N_("Day"), N_("One Day.")),
('WeekDelta', N_("Week"), N_("One Week.")),
('TwoWeekDelta', N_("2Week"), N_("Two Weeks.")),
('MonthDelta', N_("Month"), N_("One Month.")),
('QuarterDelta', N_("Quarter"), N_("One Quarter.")),
('HalfYearDelta', N_("HalfYear"), N_("HalfYear.")),
('YearDelta', N_("Year"), N_("One Year.")),
]))
def add_currency (self, pagename, name_report_currency, sort_tag):
self.register_option(CurrencyOption(pagename, name_report_currency, sort_tag,
N_("Select the currency to display the values of this report in."),
sw_app_utils.default_report_currency()))
def add_price_source (self, pagename, optname, sort_tag, default):
self.register_option(MultiChoiceOption(pagename, optname, sort_tag,
N_("The source of price information."),
default,
[ \
('average-cost', N_("Average Cost"), N_("The volume-weighted average cost of purchases.")),
('weighted-average', N_("Weighted Average"), N_("The weighted average of all currency transactions of the past.")),
('weighted-average', N_("Weighted Average"), N_("The weighted average of all currency transactions of the past.")),
('pricedb-latest', N_("Most recent"), N_("The most recent recorded price.")),
('pricedb-nearest', N_("Nearest in time"), N_("The price recorded nearest in time to the report date.")),
]))
def add_plot_size (self, pagename, name_width, name_height, sort_tag, default_width, default_height):
self.register_option(NumberRangeOption(pagename, name_width, sort_tag+"a",
N_("Width of plot in pixels."), default_width,
100, 20000, 0, 5))
self.register_option(NumberRangeOption(pagename, name_height, sort_tag+"b",
N_("Height of plot in pixels."), default_height,
100, 20000, 0, 5))
def add_marker_choice (self, pagename, optname, sort_tag, default):
self.register_option(MultiChoiceOption(pagename, optname, sort_tag,
N_("Choose the marker for each data point."),
default,
[ \
('diamond', N_("Diamond"), N_("Hollow diamond")),
('circle', N_("Circle"), N_("Hollow circle")),
('square', N_("Square"), N_("Hollow square")),
('cross', N_("Cross"), N_("Cross")),
('plus', N_("Plus"), N_("Plus")),
('dash', N_("Dash"), N_("Dash")),
('filleddiamond', N_("Filled Diamond"), N_("Diamond filled with color")),
('filledcircle', N_("Filled circle"), N_("Circle filled with color")),
('filledsquare', N_("Filled square"), N_("Square filled with color")),
]))
def add_account_selection (self, pagename, name_display_depth, name_show_subaccounts, name_accounts, sort_tag, default_depth, default_accounts, default_show_subaccounts):
self.add_account_levels(pagename, name_display_depth, sort_tag+"a",
N_("Show accounts to this depth, overriding any other option."),
default_depth)
self.register_option(SimpleBooleanOption(pagename, name_show_subaccounts, sort_tag+"b",
N_("Override account-selection and show sub-accounts of all selected accounts?"), default_show_subaccounts))
self.register_option(AccountListOption(pagename, name_accounts, sort_tag+"c",
N_("Report on these accounts, if display depth allows."), default_accounts, None, True))
def add_account_levels (self, pagename, name_display_depth, sort_tag, help_string, default_depth):
self.register_option(MultiChoiceOption(pagename, name_display_depth, sort_tag,
help_string,
default_depth,
[ \
('all', N_("All"), N_("All accounts")),
('1', N_("1"), N_("Top-level.")),
('2', N_("2"), N_("Second-level.")),
('3', N_("3"), N_("Second-level.")),
('4', N_("4"), N_("Fourth-level.")),
('5', N_("5"), N_("Fifth-level.")),
('6', N_("6"), N_("Sixth-level.")),
]))
# for the moment try making some classes based on the scheme/C
# these probably will be changed
# python implementation of the scheme options
# we have a class for each type??
# trying this first
# these options are more complicated
# looks like they are stored in some form of hash table
# which also stores the options-changed callback
# oh great there is a further underlying class for options in scheme
class OptionBase(object):
def __init__ (self):
# ;; The category of this option
self.section = None
self.name = None
# ;; The sort-tag determines the relative ordering of options in
# ;; this category. It is used by the gui for display.
self.sort_tag = None
self.type = None
self.documentation_string = None
self.getter = None
# ;; The setter is responsible for ensuring that the value is valid.
self.setter = None
self.default_getter = None
# ;; Restore form generator should generate an ascii representation
# ;; of a function taking one argument. The argument will be an
# ;; option. The function should restore the option to the original
# ;; value.
self.generate_restore_form = None
# ;; the scm->kvp and kvp->scm functions should save and load
# ;; the option to a kvp. The arguments to these function will be
# ;; a kvp-frame and a base key-path list for this option.
self.scm_to_kvp = None
self.kvp_to_scm = None
# ;; Validation func should accept a value and return (#t value)
# ;; on success, and (#f "failure-message") on failure. If #t,
# ;; the supplied value will be used by the gui to set the option.
self.value_validator = None
# ;;; free-form storage depending on type.
self.option_data = None
# ;; If this is a "multiple choice" type of option,
# ;; this should be a vector of the following five functions:
# ;;
# ;; Function 1: taking no arguments, giving the number of choices
# ;;
# ;; Function 2: taking one argument, a non-negative integer, that
# ;; returns the scheme value (usually a symbol) matching the
# ;; nth choice
# ;;
# ;; Function 3: taking one argument, a non-negative integer,
# ;; that returns the string matching the nth choice
# ;;
# ;; Function 4: takes one argument and returns the description
# ;; containing the nth choice
# ;;
# ;; Function 5: giving a possible value and returning the index
# ;; if an option doesn't use these, this should just be a #f
self.option_data_fns = None
# ;; This function should return a list of all the strings
# ;; in the option other than the section, name, (define
# ;; (list-lookup list item) and documentation-string that
# ;; might be displayed to the user (and thus should be
# ;; translated).
self.strings_getter = None
# ;; This function will be called when the GUI representation
# ;; of the option is changed. This will normally occur before
# ;; the setter is called, because setters are only called when
# ;; the user selects "OK" or "Apply". Therefore, this
# ;; callback shouldn't be used to make changes to the actual
# ;; options database.
self.widget_changed_cb = None
# this seems to be an additional callback
# ah - the make-option function defines the callback function as none
# so needs to be explicitly defined in a subclass
self.changed_callback = None
# value storage for the moment
self.option_value = None
# make the getters/setters default to the functions defined in base?
# these are defined in the gnc:make-... functions in scheme
# but generally as lambda functions rather than explict functions
# dont see why lambda needed in python
self.getter = self.get_value
self.default_getter = self.get_default_value
self.setter = self.set_value
# unlike scheme going to make this explicit
self.setter_function_called_cb = None
# now think these functions not defined as scheme not object oriented
# so all the make-option arguments have lambda functions which define
# these functions which are then stored in the variable which are called
# when the variable is accessed
# havent seen where this is defined - probably in scheme somewhere
def get_default_value (self):
return self.default_value
# havent seen where this is defined - probably in scheme somewhere
def get_value (self):
# I think gnucash is storing these in the Kvp database - hence
# those functions
# punting for the moment
if self.option_value == None:
return self.get_default_value()
return self.option_value
# havent seen where this is defined - probably in scheme somewhere
# the getter and setter seemed to be defined as lambda functions in scheme
# we can only do one-line lambdas in python so need to make explicit functions
def set_value (self, value):
# I think gnucash is storing these in the Kvp database - hence
# those functions
# punting for the moment
# this ought to be calling the setter/getter functions
# and the validator functions
# not sorted this yet - we need to allow for a subclass setter
# which must always call the changed_callback function
# we probably could use lambdas like scheme
# which saves the subclass setter function in the lambda function
# and then the lambda function is stored in the setter attribute
self.option_value = value
if callable(self.changed_callback):
self.changed_callback()
def set_changed_callback (self, callback):
# we need to call this function in a subclass to define the callback
self.changed_callback = callback
# these only fill partial values of above
# going with a subclass
class ComplexBooleanOption(OptionBase):
def __init__ (self, section, optname, sort_tag, tool_tip, default_value=None, setter_function_called_cb=None,option_widget_changed_cb=None):
super(ComplexBooleanOption,self).__init__()
self.section = section
self.name = optname
self.type = 'boolean'
self.sort_tag = sort_tag
self.documentation_string = tool_tip # AKA documentation_string
self.default_value = default_value
# need to deal with setter and getter functions
# I think because these are variables containing functions
# we need to do this rather than use super() function
# need to re-evaluate this sometime and make more pythonic
self.super_setter = self.setter
self.setter = self.local_setter
# this is stored via a lambda in scheme
self.widget_changed_cb = option_widget_changed_cb
# this is stored via a lambda in scheme
self.setter_function_called_cb = setter_function_called_cb
# the setter and getter seem to store in the Kvp database
def local_setter (self, x):
# this is not right - in scheme the super_setter calls
# the changed_callback after the setter_function_called_cb
# here its called before
# is the above correct? - I dont see it now - the following
# follows the scheme lambda function order
self.super_setter(x)
if callable(self.setter_function_called_cb):
self.setter_function_called_cb(x)
def value_validator (self, x):
if type(x) == bool:
return [ True, x ]
else:
return [ False, "boolean-option: not a boolean" ]
class SimpleBooleanOption(ComplexBooleanOption):
def __init__ (self, section, optname, sort_tag, tool_tip, default_value=None):
super(SimpleBooleanOption,self).__init__(section, optname, sort_tag, tool_tip, default_value=default_value,setter_function_called_cb=None,option_widget_changed_cb=None)
def get_value_as_html (self):
val = self.getter()
if val:
return "true"
return "false"
class StringOption(OptionBase):
def __init__ (self, section, optname, sort_tag, tool_tip, default_value=None):
super(StringOption,self).__init__()
self.section = section
self.name = optname
self.type = 'string'
self.sort_tag = sort_tag
self.documentation_string = tool_tip # AKA documentation_string
self.default_value = default_value
# we need to define a validator
def value_validator (self, x):
if type(x) == str:
return [True, x]
else:
return [False, "string-option: not a string"]
def get_option_value (self):
# again this is a function for returning value in the python script
# this seems to be viable for strings
return self.getter()
class MultiChoiceCallbackOption(OptionBase):
def __init__ (self, section, optname, sort_tag, tool_tip, default_value=None,ok_values=[],setter_function_called_cb=None,option_widget_changed_cb=None):
super(MultiChoiceCallbackOption,self).__init__()
self.section = section
self.name = optname
self.type = 'multichoice'
self.sort_tag = sort_tag
self.documentation_string = tool_tip # AKA documentation_string
self.option_data = ok_values
self.option_data_fns = [ \
lambda : len(self.option_data),
lambda x: self.option_data[x][0],
lambda x: self.option_data[x][1],
lambda x: self.option_data[x][2],
lambda x: self.lookup(x),
]
self.option_data_dict = {}
for indx,itm in enumerate(self.option_data):
kywd = itm[0]
self.option_data_dict[kywd] = indx
# for the moment check we have a string
if not isinstance(default_value,str):
raise TypeError("wrong type for default value in multi-choice option")
if self.lookup_key(default_value) < 0:
raise KeyError("unknown key symbol for default value in multi-choice option: %s"%default_value)
# we are storing the key string now not the key index
#self.default_value = self.lookup_key(default_value)
self.default_value = default_value
# need to deal with setter and getter functions
# I think because these are variables containing functions
# we need to do this rather than use super() function
# need to re-evaluate this sometime and make more pythonic
self.super_setter = self.setter
self.setter = self.local_setter
#self.super_getter = self.getter
#self.getter = self.local_getter
# the setter and getter seem to store in the Kvp database
#
self.widget_changed_cb = option_widget_changed_cb
self.strings_getter = self.multichoice_strings
self.setter_function_called_cb = setter_function_called_cb
#def local_getter (self):
# pdb.set_trace()
# retval = self.super_getter()
# return retval
def local_setter (self, x):
# this is not right - in scheme the super_setter calls
# the changed_callback after the setter_function_called_cb
# here its called before
# is the above correct? - I dont see it now - the following
# follows the scheme lambda function order
if self.legal(x):
self.super_setter(x)
if callable(self.setter_function_called_cb):
self.setter_function_called_cb(x)
else:
gnucash_log.gnc_error("Illegal Multichoice option set")
def lookup_key (self, x):
# return the index for a key string
#for indx,itm in enumerate(self.option_data):
# if itm[0] == x:
# return indx
if x in self.option_data_dict:
return self.option_data_dict[x]
return -1
def lookup_value (self, x):
# return the value for a key string - new python function
if x in self.option_data_dict:
return self.option_data_dict[x][1]
return None
def lookup (self, x):
# return whole sublist for a key as per scheme
for indx,itm in enumerate(self.option_data):
if itm[0] == x:
return itm
return None
def legal (self, x):
# this checks the key string (index 0) - not the displayed strings (index 1 or 2)
for itm in self.option_data:
if itm[0] == x:
return True
return False
def multichoice_strings (self):
# this returns the displayed strings - the option string and its tool tip
if len(self.option_data) == 0:
return []
return [ (x[1],x[2]) for itm in self.option_data ]
def get_option_value (self):
# again this is a function for returning value in the python script
# note we return the key value as the value in reports (index 0 in list)
# the index 1 value is the string displayed in the options dialog
optmrk = self.getter()
#return self.option_data[optmrk][0]
return optmrk
class MultiChoiceOption(MultiChoiceCallbackOption):
def __init__ (self, section, optname, sort_tag, tool_tip, default_value=None,ok_values=[]):
super(MultiChoiceOption,self).__init__(section, optname, sort_tag, tool_tip, default_value=default_value,ok_values=ok_values,setter_function_called_cb=None,option_widget_changed_cb=None)
class ListOption(OptionBase):
# self.options.register_option(ListOption(N_("Hello, World!"), N_("A list option"),"h",
# N_("This is a list option."),
# ['good'],
# [ \
# [ 'good', N_("The Good"), N_("Good option.") ],
# [ 'bad', N_("The Bad"), N_("Bad option.") ],
# [ 'ugly', N_("The Ugly"), N_("Ugly option.") ],
# ]))
def __init__ (self, section, optname, sort_tag, tool_tip, default_value=None,ok_values=None,option_widget_changed_cb=None):
super(ListOption,self).__init__()
self.section = section
self.name = optname
self.type = 'list'
self.sort_tag = sort_tag
self.documentation_string = tool_tip # AKA documentation_string
self.option_data = ok_values
self.option_data_fns = [ \
lambda : len(self.option_data),
lambda x: self.option_data[x][0],
lambda x: self.option_data[x][1],
lambda x: self.option_data[x][2],
lambda x: self.lookup(x),
]
# create dict of values
# this assumes all keys unique - is this a requirement??
self.option_data_dict = {}
for indx,itm in enumerate(self.option_data):
kywd = itm[0]
self.option_data_dict[kywd] = indx
# store a list of key strings as in scheme
# do a bit of type checking
self.default_value = []
for dfvl in default_value:
if not isinstance(dfvl,str):
raise TypeError("wrong type for default value in list option")
if self.lookup_key(dfvl) < 0:
raise KeyError("unknown key symbol for default value in list option: %s"%dfvl)
self.default_value.append(dfvl)
self.super_setter = self.setter
self.setter = self.local_setter
# the setter and getter seem to store in the Kvp database
#
self.widget_changed_cb = option_widget_changed_cb
self.strings_getter = self.list_strings
#self.setter_function_called_cb = setter_function_called_cb
def local_setter (self, x):
# this is not right - in scheme the super_setter calls
# the changed_callback after the setter_function_called_cb
# here its called before
# is the above correct? - I dont see it now - the following
# follows the scheme lambda function order
if self.legal(x):
self.super_setter(x)
if callable(self.setter_function_called_cb):
self.setter_function_called_cb(x)
else:
gnucash_log.gnc_error("Illegal List option set")
def lookup_key (self, x):
# return the index for a key string
#for indx,itm in enumerate(self.option_data):
# if itm[0] == x:
# return indx
if x in self.option_data_dict:
return self.option_data_dict[x]
return -1
def lookup (self, x):
# return whole sublist for a key as per scheme
#for indx,itm in enumerate(self.option_data):
# if itm[0] == x:
# return itm
if x in self.option_data_dict:
return self.option_data[self.option_data_dict[x]]
return None
def legal (self, x):
# this checks the key string (index 0) - not the displayed strings (index 1 or 2)
#for itm in self.option_data:
# if itm[0] == x:
# return True
for itm in x:
if not itm in self.option_data_dict:
return False
return True
def list_strings (self):
# this returns the displayed strings - the option string and its tool tip
if len(self.option_data) == 0:
return []
return [ (x[1],x[2]) for itm in self.option_data ]
def get_option_value (self):
# again this is a function for returning value in the python script
# can we return multiple values or just one??
optmrk = self.getter()
#vallst = []
#for mrk in optmrk:
# vallst.append(self.option_data[mrk][0])
#return vallst
return optmrk
class AccountListLimitedOption(OptionBase):
def __init__ (self, section, optname, sort_tag, tool_tip, default_value=None,value_validator=None,multiple_selection=None,acct_type_list=[],option_widget_changed_cb=None):
super(AccountListLimitedOption,self).__init__()
self.section = section
self.name = optname
self.type = 'account-list'
self.sort_tag = sort_tag
self.documentation_string = tool_tip # AKA documentation_string
self.option_data = [multiple_selection, acct_type_list]
self.default_value = default_value
self.getter = self.account_list_getter
self.setter = self.account_list_setter
self.default_getter = self.account_list_default_getter
#self.strings_getter = self.multichoice_strings
self.widget_changed_cb = option_widget_changed_cb
# note this function must return a 2 tuple - True/False and a list
# we need to save the passed validator function in python
self.value_validator = self.local_validator
self.save_value_validator = value_validator
# this is a flag which is True of False depending if
# account items are account pointers or guids
self.option_set = None
#self.option_value = map(self.convert_to_guid,self.default_getter())
# in scheme the following functions apparently convert account lists
# into guid lists for self.option_value using the functions convert_to_guid
# and inverse convert_to_account
# for the moment ignoring this
def convert_to_guid (self, itm):
if type(itm) != str:
return itm.GetGUID()
return itm
def convert_to_account (self, itm):
# not valid!!
if type(itm) == str:
account = gnucash.GUID.AccountLookup(acc_guid,curbook)
account = xaccAccountLookup(gnc_get_current_book(),itm)
return account
return itm
def account_list_getter (self):
#pdb.set_trace()
# re-define to deal with lambda definitions
# this seems to be the code in scheme
#
#if option_set:
# return map(self.convert_to_guid,self.account_list_default_getter())
# return self.option_value
#else:
# return self.account_list_default_getter()
if self.option_value == None:
return self.account_list_default_getter()
if callable(self.option_value):
return self.option_value()
else:
return self.option_value
def account_list_setter (self, account_list):
#pdb.set_trace()
if account_list == None or len(account_list) == 0:
account_list = self.default_getter()
# the following maybe a translation of the scheme code
# the usage of filter seems to be to apply the lambda code to each element
# dont see how it actually filters anything
newlst = []
for x in account_list:
if type(x) == str:
xacc = gnucash.GUID.AccountLookup(x,curbook)
xacc = xaccAccountLookup(gnc_get_current_book(),x)
else:
xacc = x
newlst.append(xacc)
account_list = newlst
# list should be validated
(valid, value) = self.local_validator(account_list)
if valid:
#self.option_value = map(self.convert_to_guid,value)
self.option_value = value
self.option_set = True
else:
gnucash_log.gnc_error("Illegal account list value set")
pdb.set_trace()
pass
def account_list_default_getter (self):
#pdb.set_trace()
if callable(self.default_value):
defval = self.default_value()
else:
defval = self.default_value
#return self.convert_to_account(defval)
return defval
def lookup_key (self, x):
pdb.set_trace()
# return the index for a key string
for indx,itm in enumerate(self.option_data):
if itm[0] == x:
return indx
return -1
def lookup (self, x):
pdb.set_trace()
# return whole sublist for a key as per scheme
for indx,itm in enumerate(self.option_data):
if itm[0] == x:
return itm
return None
def local_validator (self, account_list):
#pdb.set_trace()
if self.save_value_validator == None:
return [True, account_list]
else:
#account_list = map(self.convert_to_account,account_list)
return self.save_value_validator(account_list)
def get_option_value (self):
# again this is a function for returning value in the python script
#pdb.set_trace()
acclst = self.getter()
return acclst
class AccountListOption(AccountListLimitedOption):
def __init__ (self, section, optname, sort_tag, tool_tip, default_value=None,value_validator=None,multiple_selection=None):
super(AccountListOption,self).__init__(section, optname, sort_tag, tool_tip, default_value=default_value,value_validator=value_validator,multiple_selection=multiple_selection,acct_type_list=[])
class DateOption(OptionBase):
def __init__ (self, section, optname, sort_tag, tool_tip, default_getter=None,show_time=False,subtype=None,relative_date_list=[]):
super(DateOption,self).__init__()
self.section = section
self.name = optname
self.type = 'date'
self.sort_tag = sort_tag
self.documentation_string = tool_tip # AKA documentation_string
# although this is called the default_getter its actually the default value
# these are lambda functions in scheme but dont know why
# I guess this can either be a callable (lambda in scheme) or plain value
self.default_value = default_getter
#self.generate_restore_form = ??.restore_form_generator(self.option_value)
self.option_data = (subtype, show_time, relative_date_list)
#self.option_data_fns = (lambda : len(relative_date_list),
# lambda x: relative_date_list[x],
# lambda x: self.get_relative_date_string(relative_date_list[x]),
# lambda x: self.get_relative_date_desc(relative_date_list[x]),
# lambda x: relative_date_list.index(x))
self.option_data_fns = (lambda : len(self.option_data[2]),
lambda x: self.option_data[2][x],
lambda x: self.get_relative_date_string(self.option_data[2][x]),
lambda x: self.get_relative_date_desc(self.option_data[2][x]),
lambda x: self.option_data[2].index(x))
# two False slots in addition
self.strings_getter = None
#self.widget_changed_cb = None
# setup local setter
self.super_setter = self.setter
self.setter = self.local_setter
def get_default_value (self):
# re-define to deal with lambda definitions
if callable(self.default_value):
return self.default_value()
else:
return self.default_value
def local_setter (self, date):
#pdb.set_trace()
if self.date_legal(date):
self.super_setter(date)
else:
gnucash_log.gnc_error("Illegal date value set: %s"%date)
# the setter and getter seem to store in the Kvp database
def default_value_validator (self, date):
if self.date_legal(date):
return [True, date]
else:
return [False, "date-option: illegal date"]
def date_legal (self, date):
#pdb.set_trace()
# something not sorted - an absolute date is a pair in scheme
# maybe because date and time??
# date[1] should be a symbol - whatever that is
# punt with str for the moment
if (((type(date) == tuple or type(date) == list)) and \
len(date) == 2) or \
((date[0] == 'relative' and type(date[1]) == str) or \
(date[0] == 'absolute' and type(date[1]) == datetime.datetime)):
return True
pdb.set_trace()
return False
def lookup_string (self, datestr):
# somewhere in scheme the DateOption strings are looked up in the
# databases defined in date-utilities.scm
if datestr in date_utils.relative_date_values:
return date_utils.relative_date_values[datestr]
else:
pdb.set_trace()
print("junk")
def lookup_key (self, x):
# return the index for a key string
for indx,itm in enumerate(self.option_data[2]):
if itm == x:
return indx
return -1
def lookup_index (self, x):
# return the key string for an index
datestr = self.option_data[2][x]
return datestr
def get_option_value (self):
# this is a function for returning suitable python values
# not figured a good name yet
# somehow in gnucash the date formats are selectable
# based on region
optval = self.getter()
if self.option_data[0] == 'absolute':
if optval[0] == 'absolute':
return optval[1]
elif optval[0] == 'relative':
# is this ever possible??
pdb.set_trace()
rettpl = self.lookup_string(optval[1])
fy_period = rettpl[2]()
return fy_period
else:
pdb.set_trace()
print(optval)
elif self.option_data[0] == 'relative':
if optval[0] == 'absolute':
# is this ever possible??
pdb.set_trace()
return optval[1]
elif optval[0] == 'relative':
rettpl = self.lookup_string(optval[1])
fy_period = rettpl[2]()
return fy_period
else:
pdb.set_trace()
print(optval)
else:
#pdb.set_trace()
# still not sure how both version works
# do we ignore self.option_data and just check optval?
if self.option_data[1] != None:
gnucash_log.dbglog("both - absolute defined")
else:
gnucash_log.dblgog("both - relative defined")
if optval[0] == 'absolute':
return optval[1]
elif optval[0] == 'relative':
rettpl = self.lookup_string(optval[1])
fy_period = rettpl[2]()
return fy_period
else:
pdb.set_trace()
# is this ever possible??
rettpl = self.lookup_string(optval[1])
fy_period = rettpl[2]()
return fy_period
class EndDateOption(DateOption):
def __init__ (self, section, optname, sort_tag, tool_tip):
super(EndDateOption,self).__init__ (section, optname, sort_tag, tool_tip,
('relative', 'end-accounting-period'),
False,
'both',
('today',
'end-this-month',
'end-prev-month',
'end-current-quarter',
'end-prev-quarter',
'end-cal-year',
'end-prev-year',
'end-accounting-period',
'end-last-uk-taxyear',
))
class FromDateOption(DateOption):
def __init__ (self, section, optname, sort_tag, tool_tip):
super(FromDateOption,self).__init__(section, optname, sort_tag, tool_tip,
('relative', 'start-accounting-period'),