File: __init__.py

package info (click to toggle)
libaqbanking 2.2.3-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 14,912 kB
  • ctags: 6,430
  • sloc: ansic: 59,251; cpp: 17,347; sh: 8,930; xml: 6,189; makefile: 3,326; python: 2,806; pascal: 559; perl: 267
file content (1447 lines) | stat: -rw-r--r-- 44,964 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
# -*- encoding: latin1 -*-
# Copyright (C) 2005 by Andreas Degert
# see LICENSE.txt in the top level distribution directory
#
"""aqbanking -- Interface to the homebanking library aqBanking

This module is a ctypes-based wrapper for aqBanking.

The naming corresponds to aqBanking, but common prefixes are deleted,
case adjusted, and a lot of accessor functions are hidden behind
properties. The complicated list operations of aqBanking are
transformed into iterators. There is a subclass of Job for each job
type.

Subclasses of Enum (defined in module enum) are used to define classes
that represent C enumerations. Calling with an int generates an
instance of EnumInstance, which can display the enum name (or the
corresponding number). Undefined int values raise an exception.

One Example:

The C function AB_JobGetTransactions_new() is used in the constructor of the
python class JobGetTransactions.  AB_JobGetTransactions_GetFromTime() and
AB_JobGetTransactions_SetFromTime() are used to define the getter and setter
of the property fromTime. The list returned by
AB_JobGetTransactions_GetTransactions() is accessable as an iterator returned
by the method iterTransactions().

Classes
  BankingBase           abstract base for the main banking class
                        (cf. console.BankingConsole and gtkui.BankingGtk)
  Job                   abstract base class for jobs
  JobGetBalance         get balance for an account
  JobGetTransactions    get the transactions for an account
  JobSingleTransfer     transfer money from your account to another
  JobSingleDebitNote    retrieve money from another account
  JobEuTransfer         transfer money from your account to another, EU-wide
  Transaction           account numbers etc. for the transfer job
  ImExporterContext     container to hold data for im/export

Enumerations
  JobType               job type (corresponds to Job subclasses)
  JobStatus             status of job execution
  AB_Error              error return codes
  LoggerLevel           levels for aqBanking logging
  LogLevel              levels for messages to the user
  PinStatus             status of the PIN (security code)
  TanStatus             status of the TAN (one time security code)

Exceptions
  BankingError          raised when aqBanking returns an error

Functions
  stripText()           return the text or html part of a message
  msg_flags_type()      type of a message
  msg_flags_confirm_button()
                        number of button signaling a "confirm"
  Logger_SetLevel()     set logging level
  Logger_GetLevel()     get logging level

Classes which must not be instanciated directly:
  Value                 a money/currency value
  GWEN_DB_Node          storage tree node
  AccountInfo           imported data for an account
  Account               local bank account
  AccountStatus         fetched with JobGetBalance
  Balance               Value and time of an account balance
  EuTransferInfo        limiting properties of the euro transfer job
  ImExporter            Importer or Exporter for a specific format
  PluginDescription     describes a plugin (e.g. an ImExporter)
  BufferedIO            aqBanking io abstraction
  
"""
#
# Implementation Notes
#
# The goal is to use ctypes to get a "pythonic" interface to
# aqBanking.
#
# - the structures and concepts represented as aqBanking function
#   groups are mapped to classes
#
# - the accessor functions for field values are used to define
#   properties.
#
# - list operations are encapsulated in iterators.
#
# - error returns from functions raise a BankingError exception.
#
# - ini- / fini-functions are called automatically
#
# - callbacks for interactivity are methods of the main banking class
#
# - referenced C structures are owned by the python object (e.g. by
#   using the attach function of the C interface if one exists), or
#   the python object references the object which keeps the owning C
#   structure (e.g. by defining a property with the helper function
#   property_addref). When the python object is finalized it frees the
#   C structure.
#   FIXME: not yet finished
#
# - bad arguments raise exceptions
#   FIXME: not yet finished
#
# The ctypes tutorial should help understanding its usage in this
# module.
#
from _aqtypes import *
import re
from enum import Enum, check_enum
import atexit, weakref

__all__ = ["Transaction", "Job", "JobSingleDebitNote",
           "JobSingleTransfer", "JobEuTransfer",
           "JobGetTransactions", "JobGetBalance",
           "ImExporterContext", "AB_Error", "BankingError", "JobStatus",
           "LoggerLevel", "LogLevel", "Value", "Balance","Split","AccountInfo",
           "Logger_SetLevel","Logger_GetLevel","PathFlags","DBFlags"]


c_int_p = POINTER(c_int)


################################################################
# Constants, Logging
#
class AB_Error(Enum):
    "error values for C function returns"
    (ok,
     generic,
     not_supported,
     not_available,
     bad_config_file,
     invalid,
     network,
     not_found,
     empty,
     user_abort,
     found,
     no_data,
     nofn,
     unknown_account,
     not_init,
     security,
     bad_data,
     unknown,
     aborted,
     default_value,
     ) = range(0,-20,-1)
    (user1,
     user2,
     user3,
     user4,
     ) = range(-128,-132,-1)


class AccountType(Enum):
    (unknown,
     bank,
     creditcard,
     checking,
     savings,
     investment,
     cash
     ) = range(7)

class AccountTypeAdapter(c_int):
    def _check_retval_(i):
        return AccountType(i)
    _check_retval_ = staticmethod(_check_retval_)
def from_param(cls, e):
    check_enum(e, AccountType, 'argument')
    return int(e)
AccountTypeAdapter.from_param = classmethod(from_param)


class LoggerLevel(Enum):
    "Levels for logging system"
    (emergency,
     alert,
     critical,
     error,
     warning,
     notice,
     info,
     debug,
     verbous,
     ) = range(9)

    unknown = 9999

def Logger_SetLevel(domain, level):
    check_enum(level, LoggerLevel, 'level')
    gwen.GWEN_Logger_SetLevel(domain, level)

gwen.GWEN_Logger_SetLevel.argtypes = c_char_p, c_int

def Logger_GetLevel(domain):
    return gwen.GWEN_Logger_GetLevel(domain)

gwen.GWEN_Logger_GetLevel.restype = LoggerLevel
gwen.GWEN_Logger_GetLevel.argtypes = c_char_p,


################################################################
# exceptions
#
class BankingError(Exception):
    """raised when a C function returns an error.

    error is the AB_Error (if its defined).
    nr is the numeric value.

    When the return value was not in AB_Error,
    error should be set to None (just set nr)
    """
    def __init__(self, text, error, nr):
        self.error = error
        self.nr = nr
        Exception.__init__(self, text)


################################################################
# internal helper functions
#
def chk(ret):
    """Check the return value of a C function.

    For all C functions returning an AB_Error value. Can also be
    used as .restype
    """
    if ret == 0:
	return
    try:
        v = AB_Error(ret)
    except ValueError:
	raise BankingError('unknown error (%d)' % ret, None, ret)
    raise BankingError('%s (%d)' % (v, v), v, ret)


################################################################
# iterators as adapters for functions returning list pointers
#
def iterStringlist(sl):
    "Expects a GWEN_STRINGLIST pointer"
    if not sl:
        return
    e = gwen.GWEN_StringList_FirstEntry(sl)
    while e:
        s = gwen.GWEN_StringListEntry_Data(e)
        if s is None:
            s = ''
        yield s
        e = gwen.GWEN_StringListEntry_Next(e)

def iterJobs(jl):
    "Expects a AB_JOB_LIST2 pointer"
    if not jl:
        return
    jit = aqb.AB_Job_List2_First(jl)
    jp = aqb.AB_Job_List2Iterator_Data(jit)
    while jp:
        yield Job._check_retval_(jp)
        jp = aqb.AB_Job_List2Iterator_Next(jit)
    aqb.AB_Job_List2Iterator_free(jit)
    aqb.AB_Job_List2_free(jl)

################################################################
# adapter classes
#
class GWEN_Buffer(c_void_p):
    """Create a writeable GWEN_BUFFER for .argtypes

    Converting to a string reads the buffer contents:

    p = GWEN_Buffer()
    some_c_func(p)
    print p
    some_python_func(str(p))
    """
    def __init__(self, inisize=200):
        c_void_p.__init__(
            self, gwen.GWEN_Buffer_new(None, inisize, 0, False))

    def __str__(self):
        return c_char_p(gwen.GWEN_Buffer_GetStart(self)).value

    usedBytes = property(gwen.GWEN_Buffer_GetUsedBytes)

    def __del__(self):
        gwen.GWEN_Buffer_free(self)


################################################################
# GWEN_DB
#
class PathFlags:
    PATHMUSTEXIST    = 0x00000001
    PATHMUSTNOTEXIST = 0x00000002
    PATHCREATE       = 0x00000004
    NAMEMUSTEXIST    = 0x00000008
    NAMEMUSTNOTEXIST = 0x00000010
    CREATE_GROUP     = 0x00000020
    CREATE_VAR       = 0x00000040
    VARIABLE         = 0x00000080
    ESCAPE           = 0x00000100
    UNESCAPE         = 0x00000100
    TOLERANT_ESCAPE  = 0x00000200
    CONVERT_LAST     = 0x00000400
    CHECKROOT        = 0x00000800
    NO_IDX           = 0x00001000
    RFU1             = 0x00002000

class DBFlags:
    OVERWRITE_VARS      = 0x00010000
    OVERWRITE_GROUPS    = 0x00020000
    QUOTE_VARNAMES      = 0x00040000
    QUOTE_VALUES        = 0x00080000
    WRITE_SUBGROUPS     = 0x00100000
    DETAILED_GROUPS     = 0x00200000
    INDEND              = 0x00400000
    ADD_GROUP_NEWLINES  = 0x00800000
    USE_COLON           = 0x01000000
    STOP_ON_EMPTY_LINE  = 0x02000000
    OMIT_TYPES          = 0x04000000
    APPEND_FILE         = 0x08000000
    ESCAPE_CHARVALUES   = 0x10000000
    UNESCAPE_CHARVALUES = 0x10000000
    LOCKFILE            = 0x20000000


class GWEN_DB_Node(c_void_p):
    """A node in a storage tree.

    iterGroups()   iterates over the Groups
    """
    VALUETYPE_UNKNOWN = 0
    VALUETYPE_CHAR = 1
    VALUETYPE_INT = 2
    VALUETYPE_BIN = 3
    VALUETYPE_PTR = 4

    def _check_retval_(p):
        if not p:
            return None
        if gwen.GWEN_DB_IsGroup(p):
            cls = GWEN_DB_GroupNode
        elif gwen.GWEN_DB_IsVariable(p):
            cls = GWEN_DB_VariableNode
        elif gwen.GWEN_DB_IsValue(p):
            tp = gwen.GWEN_DB_GetValueType(p)
            cls = GWEN_DB_Node._typetable.get(tp)
            if cls is None:
                raise RuntimeError('unknown GWEN_DB_Node value type %d' % tp)
        v = GWEN_DB_Node.__new__(cls)
        c_void_p.__init__(v, p)
        return v
    _check_retval_ = staticmethod(_check_retval_)

    def __init__(self):
        assert False, "class GWEN_DB_Node is abstract"

    def getGroup(self, flags=0, path=None):
        if path is None:
            raise RuntimeError('please specify a path')
        return gwen.GWEN_DB_GetGroup(self, flags, path)

    def iterGroups(self):
	n = gwen.GWEN_DB_GetFirstGroup(self)
	while n:
	    yield n
	    n = gwen.GWEN_DB_GetNextGroup(n)

    def iterVars(self):
        n = gwen.GWEN_DB_GetFirstVar(self)
        while n:
            yield n
            n = gwen.GWEN_DB_GetNextVar(n)

    def iterValues(self):
        n = gwen.GWEN_DB_GetFirstValue(self)
        while n:
            yield n
            n = gwen.GWEN_DB_GetNextValue(n)

    def getIntValue(self, path='', idx=0, defval=-1):
        return gwen.GWEN_DB_GetIntValue(self, path, idx, defval)

    def __repr__(self):
	return "<%s %s>" % (self.__class__.__name__, self.name)

gwen.GWEN_DB_GetFirstValue.restype = GWEN_DB_Node
gwen.GWEN_DB_GetNextValue.restype = GWEN_DB_Node
gwen.GWEN_DB_GetFirstVar.restype = GWEN_DB_Node
gwen.GWEN_DB_GetNextVar.restype = GWEN_DB_Node
gwen.GWEN_DB_GetGroup.restype = GWEN_DB_Node
gwen.GWEN_DB_GetGroup.argtypes = GWEN_DB_Node, c_int, c_char_p
gwen.GWEN_DB_GetFirstGroup.restype = GWEN_DB_Node
gwen.GWEN_DB_GetNextGroup.restype = GWEN_DB_Node

class GWEN_DB_GroupNode(GWEN_DB_Node):

    def __init__(self):
        raise RuntimeError('please add code to create a group node')

    def deleteVar(self, path):
        res = gwen.GWEN_DB_DeleteVar(self, path)
        if res != 0:
            raise ValueError('DeleteVar failed')

    name = property(gwen.GWEN_DB_GroupName)

gwen.GWEN_DB_DeleteVar.argtypes = GWEN_DB_GroupNode, c_char_p
gwen.GWEN_DB_GroupName.restype = c_char_p


class GWEN_DB_VariableNode(GWEN_DB_Node):

    def __init__(self):
        raise RuntimeError('please add code to create a variable node')

    name = property(gwen.GWEN_DB_VariableName)

gwen.GWEN_DB_VariableName.restype = c_char_p


class GWEN_DB_ValueNode(GWEN_DB_Node):

    def __init__(self):
        assert False, "class GWEN_DB_ValueNode is abstract"

    def __repr__(self):
	return "<%s %s>" % (self.__class__.__name__, self.value)


class GWEN_DB_CharValueNode(GWEN_DB_ValueNode):

    def __init__(self):
        raise RuntimeError('please add code to create a value node')

    value = property(gwen.GWEN_DB_GetCharValueFromNode)

gwen.GWEN_DB_GetCharValueFromNode.restype = c_char_p


class GWEN_DB_IntValueNode(GWEN_DB_ValueNode):

    def __init__(self):
        raise RuntimeError('please add code to create a value node')

    value = property(gwen.GWEN_DB_GetIntValueFromNode)

gwen.GWEN_DB_GetIntValueFromNode.restype = c_int


class GWEN_DB_BinValueNode(GWEN_DB_ValueNode):

    def __init__(self):
        raise RuntimeError('please add code to create a value node')

    def value(self):
        size = c_int()
        v = gwen.GWEN_DB_GetBinValueFromNode(self, byref(size))
        return v[:size]
    value = property(value)

gwen.GWEN_DB_GetBinValueFromNode.restype = c_char_p


class GWEN_DB_PtrValueNode(GWEN_DB_ValueNode):

    def __init__(self):
        raise RuntimeError('please add code to create a value node')

    def value(self):
        return gwen.GWEN_DB_GetPtrValue(self, 0, '', None)
    value = property(value)

gwen.GWEN_DB_GetPtrValue.restype = c_void_p


GWEN_DB_Node._typetable = {
    GWEN_DB_Node.VALUETYPE_CHAR: GWEN_DB_CharValueNode,
    GWEN_DB_Node.VALUETYPE_INT: GWEN_DB_IntValueNode,
    GWEN_DB_Node.VALUETYPE_BIN: GWEN_DB_BinValueNode,
    GWEN_DB_Node.VALUETYPE_PTR: GWEN_DB_PtrValueNode,
    }


################################################################
# Account
#
class Provider(c_void_p):
    """aqBanking Provider (like aqhbci)
    """
    def _check_retval_(p):
        if not p:
            chk(AB_Error.not_available)
        v = Provider.__new__(Provider)
        c_void_p.__init__(v, p)
        return v
    _check_retval_ = staticmethod(_check_retval_)

    def __init__(self):
        raise RuntimeError("class Provider can't be instanciated directly")

    name = property(aqb.AB_Provider_GetName)

Provider.from_param = from_cls_param
aqb.AB_Provider_GetName.restype = c_char_p


class Account(c_void_p):
    """local bank account.

    Instances are created by BaseBanking
    """
    def _check_retval_(p):
        if not p:
            chk(AB_Error.unknown_account)
        v = Account.__new__(Account)
        c_void_p.__init__(v, p)
        return v
    _check_retval_ = staticmethod(_check_retval_)
    
    def __init__(self):
        raise RuntimeError("class Account can't be instanciated directly")

    uniqueId = property(aqb.AB_Account_GetUniqueId)

    accountNumber = property(aqb.AB_Account_GetAccountNumber)

    bankCode = property(aqb.AB_Account_GetBankCode)

    provider = property(aqb.AB_Account_GetProvider)

    accountName = property(aqb.AB_Account_GetAccountName)

    currency = property(aqb.AB_Account_GetCurrency)

    ownerName = property(aqb.AB_Account_GetOwnerName)

    accountType = property(
        aqb.AB_Account_GetAccountType,
        aqb.AB_Account_SetAccountType)

    #def checkAvailability(self):
    #    return aqb.AB_Account_CheckAvailability(self)

    def __eq__(self, other):
        if not isinstance(other, Account):
            return False
        return self.uniqueId == other.uniqueId

    def __str__(self):
	return "Account(%d: %s / %s)" % \
            (self.uniqueId, self.accountNumber, self.bankCode)

Account.from_param = from_cls_param

aqb.AB_Account_GetAccountNumber.restype = c_char_p
aqb.AB_Account_GetBankCode.restype = c_char_p
aqb.AB_Account_GetAccountType.restype = AccountTypeAdapter
aqb.AB_Account_SetAccountType.argtypes = Account, AccountTypeAdapter
aqb.AB_Account_GetProvider.restype = Provider
aqb.AB_Account_GetOwnerName.restype = c_char_p
aqb.AB_Account_GetCurrency.restype = c_char_p
aqb.AB_Account_GetAccountName.restype = c_char_p
#aqb.AB_Account_CheckAvailability.restype = AB_Error


################################################################
# AccountStatus
#
class Balance(c_void_p):
    "wraps AB_BALANCE"

    def _check_retval_(p):
	if not p:
	    return None
        v = Balance.__new__(Balance)
        c_void_p.__init__(v, p)
        return v
    _check_retval_ = staticmethod(_check_retval_)

    def __init__(self, value, time):
	bal = aqb.AB_Balance_new(value, time)
        assert bal
	self.__del__ = aqb.AB_Balance_free
	c_void_p.__init__(self, bal)

    value = property(aqb.AB_Balance_GetValue)
    time = property(aqb.AB_Balance_GetTime)

    def __eq__(self, bal):
	return self.value == bal.value and self.time == bal.time

    def __str__(self):
        return "Balance(%s: %s)" % (self.time, self.value)

aqb.AB_Balance_new.argtypes = [Value, GWEN_Time]
aqb.AB_Balance_GetTime.restype = GWEN_Time
aqb.AB_Balance_GetValue.restype = Value


class AccountStatus(c_void_p):
    """account status return by executing JobGetBalance.

    wraps AB_ACCOUNT_STATUS
    """
    def _check_retval_(p):
        if not p:
            return None
        v = AccountStatus.__new__(AccountStatus)
        c_void_p.__init__(v, p)
        return v
    _check_retval_ = staticmethod(_check_retval_)

    def __init__(self):
        raise RuntimeError(
            "class AccountStatus can't be instanciated directly")

    time = property(aqb.AB_AccountStatus_GetTime)
    bankLine = property(aqb.AB_AccountStatus_GetBankLine)
    disposable = property(aqb.AB_AccountStatus_GetDisposable)
    disposed = property(aqb.AB_AccountStatus_GetDisposed)
    bookedBalance = property(aqb.AB_AccountStatus_GetBookedBalance)
    notedBalance = property(aqb.AB_AccountStatus_GetNotedBalance)

AccountStatus.from_param = from_cls_param

aqb.AB_AccountStatus_GetBookedBalance.restype = Balance
aqb.AB_AccountStatus_GetNotedBalance.restype = Balance
aqb.AB_AccountStatus_GetTime.restype = GWEN_Time
aqb.AB_AccountStatus_GetBankLine.restype = Value
aqb.AB_AccountStatus_GetDisposable.restype = Value
aqb.AB_AccountStatus_GetDisposed.restype = Value


################################################################
# Im- / Export
#
class AccountInfo(c_void_p):
    "Imported data for an account"

    def _check_retval_(p):
        if not p:
            return None
        v = AccountInfo.__new__(AccountInfo)
        c_void_p.__init__(v, p)
        return v
    _check_retval_ = staticmethod(_check_retval_)

    def __init__(self):
        p = aqb.AB_ImExporterAccountInfo_new()
        assert p
        c_void_p.__init__(self, p)
        self.__del__ = aqb.AB_ImExporterAccountInfo_free

    bankCode = property(
        aqb.AB_ImExporterAccountInfo_GetBankCode,
        aqb.AB_ImExporterAccountInfo_SetBankCode)

    bankName = property(
        aqb.AB_ImExporterAccountInfo_GetBankName,
        aqb.AB_ImExporterAccountInfo_SetBankName)

    accountNumber = property(
        aqb.AB_ImExporterAccountInfo_GetAccountNumber,
        aqb.AB_ImExporterAccountInfo_SetAccountNumber)

    accountName = property(
        aqb.AB_ImExporterAccountInfo_GetAccountName,
        aqb.AB_ImExporterAccountInfo_SetAccountName)

    owner = property(
        aqb.AB_ImExporterAccountInfo_GetOwner,
        aqb.AB_ImExporterAccountInfo_SetOwner)

    accountType = property(
        aqb.AB_ImExporterAccountInfo_GetType,
        aqb.AB_ImExporterAccountInfo_SetType)

    description = property(
        aqb.AB_ImExporterAccountInfo_GetDescription,
        aqb.AB_ImExporterAccountInfo_SetDescription)

    def iterTransactions(self):
        "iterates over all the transactions of the account"
	tr = aqb.AB_ImExporterAccountInfo_GetFirstTransaction(self)
	while tr:
	    yield Transaction._check_retval_(tr)
	    tr = aqb.AB_ImExporterAccountInfo_GetNextTransaction(self)

    def iterAccountStatus(self):
        as = aqb.AB_ImExporterAccountInfo_GetFirstAccountStatus(self)
        while as:
            yield AccountStatus._check_retval_(as)
            as = aqb.AB_ImExporterAccountInfo_GetNextAccountStatus(self)

    def addTransaction(self, tr):
        aqb.AB_ImExporterAccountInfo_AddTransaction(self, tr)

    def addAccountStatus(self, as):
        aqb.AB_ImExporterAccountInfo_AddAccountStatus(self, as)

AccountInfo.from_param = from_cls_param

aqb.AB_ImExporterAccountInfo_GetAccountNumber.restype = c_char_p
aqb.AB_ImExporterAccountInfo_GetBankCode.restype = c_char_p
aqb.AB_ImExporterAccountInfo_GetAccountNumber.restype = c_char_p
aqb.AB_ImExporterAccountInfo_GetDescription.restype = c_char_p
aqb.AB_ImExporterAccountInfo_SetDescription.argtypes = AccountInfo, c_char_p
aqb.AB_ImExporterAccountInfo_GetType.restype = AccountTypeAdapter
aqb.AB_ImExporterAccountInfo_SetType.argtypes = AccountInfo, AccountTypeAdapter
aqb.AB_ImExporterAccountInfo_GetOwner.restype = c_char_p
aqb.AB_ImExporterAccountInfo_SetOwner.argtypes = AccountInfo, c_char_p
aqb.AB_ImExporterAccountInfo_GetAccountName.restype = c_char_p
aqb.AB_ImExporterAccountInfo_SetAccountName.argtypes = AccountInfo, c_char_p
aqb.AB_ImExporterAccountInfo_GetBankName.restype = c_char_p
aqb.AB_ImExporterAccountInfo_SetBankName.argtypes = AccountInfo, c_char_p
aqb.AB_ImExporterAccountInfo_AddTransaction.argtypes = AccountInfo, Transaction
aqb.AB_ImExporterAccountInfo_AddAccountStatus.argtypes = (
    AccountInfo, AccountStatus)


class ImExporterContext(c_void_p):
    "container to hold data for im/export"

    def __init__(self):
	ctx = aqb.AB_ImExporterContext_new()
        assert ctx
        c_void_p.__init__(self, ctx)

    def __del__(self):
        aqb.AB_ImExporterContext_free(self)

    def iterAccountInfo(self):
        "iterate over data by accounts"
	ai = aqb.AB_ImExporterContext_GetFirstAccountInfo(self)
	while ai:
	    yield AccountInfo._check_retval_(ai)
	    ai = aqb.AB_ImExporterContext_GetNextAccountInfo(self)

    def addAccountInfo(self, ai):
        aqb.AB_ImExporterContext_AddAccountInfo(self, ai)
        del ai.__del__

aqb.AB_ImExporterContext_AddAccountInfo.argtypes = (
    ImExporterContext, AccountInfo)


class BufferedIO(c_void_p):
    "aqBanking io abstraction"

    def __init__(self, source):
	if hasattr(source, 'fileno') and hasattr(source, 'mode'):
	    c_void_p.__init__(
                self, gwen.GWEN_BufferedIO_File_new(source.fileno()))
            if source.mode[0] == 'r':
                gwen.GWEN_BufferedIO_SetReadBuffer(self, 0, 1024)
            else:
                gwen.GWEN_BufferedIO_SetWriteBuffer(self, 0, 1024)
	else:
	    raise ValueError, "don't know how to handle %s" % type(source)

    def __del__(self):
        gwen.GWEN_BufferedIO_free(self)

BufferedIO.from_param = from_cls_param


class ImExporter(c_void_p):
    "Importer or Exporter for a specific format"

    def _check_retval_(p):
        if not p:
            chk(AB_Error.not_available)
        v = ImExporter.__new__(ImExporter)
        c_void_p.__init__(v, p)
        return v
    _check_retval_ = staticmethod(_check_retval_)

    def __init__(self):
        raise RuntimeError("class Provider can't be instanciated directly")

    name = property(aqb.AB_ImExporter_GetName)

    def checkFile(self, fname):
        return AB_Error(aqb.AB_ImExporter_CheckFile(self, fname))

    def importIntoContext(self, source, dbProfile, ctx = None):
	if ctx is None:
	    ctx = ImExporterContext()
	aqb.AB_ImExporter_Import(self, ctx, BufferedIO(source), dbProfile)
	return ctx

    def exportFromContext(self, dest, dbProfile, ctx):
        chk(aqb.AB_ImExporter_Export(self, ctx, BufferedIO(dest), dbProfile))

ImExporter.from_param = from_cls_param

aqb.AB_ImExporter_CheckFile.argtypes = ImExporter, c_char_p
aqb.AB_ImExporter_GetName.restype = c_char_p


class PluginDescription(c_void_p):
    "describes a plugin (e.g. an ImExporter)"
    def _check_retval_(p):
        if not p:
            chk(AB_Error.not_avaiable)
        v = PluginDescription.__new__(PluginDescription)
        c_void_p.__init__(v, p)
        v.free = gwen.GWEN_PluginDescription_free
        return v
    _check_retval_ = staticmethod(_check_retval_)

    def __init__(self):
        raise RuntimeError(
            "class PluginDescription can't be instanciated directly")

    def __del__(self):
        self.free(self)

    name = property(gwen.GWEN_PluginDescription_GetName)
    type = property(gwen.GWEN_PluginDescription_GetType)
    version = property(gwen.GWEN_PluginDescription_GetVersion)
    author = property(gwen.GWEN_PluginDescription_GetAuthor)
    shortDescr = property(gwen.GWEN_PluginDescription_GetShortDescr)

    def __str__(self):
        return "<%s %s %s / %s %s %s>" % (self.__class__.__name__, self.name,
                                          self.version, self.type,
                                          self.author, self.shortDescr)

PluginDescription.from_param = from_cls_param
gwen.GWEN_PluginDescription_GetName.restype = c_char_p
gwen.GWEN_PluginDescription_GetType.restype = c_char_p
gwen.GWEN_PluginDescription_GetVersion.restype = c_char_p
gwen.GWEN_PluginDescription_GetAuthor.restype = c_char_p
gwen.GWEN_PluginDescription_GetShortDescr.restype = c_char_p


################################################################
# Jobs
################################################################

class JobType(Enum):
    "Job Types"
    (unknown,         # unknown job
     balance,         # retrieve the balance of an online account
     getTransactions, # retrieve transaction statements for an online account
     transfer,        # issue a transfer
     debitNote,       # issue a debit note (Lastschrift)
     euTransfer,      # issue a EU-wide transfer
     ) = range(6)

class JobStatus(Enum):
    "status of job execution"
    (new,
     updated,
     enqueued,
     sent,
     pending,
     finished,
     error,
     deferred,
     ) = range(8)
    unknown = 999

class Job(c_void_p):
    "base class for all banking jobs"

    def _check_retval_(job):
        "creates Job subclass instances from a C pointer - DO NOT USE DIRECTLY"
        assert job
        aqb.AB_Job_Attach(job)
        j = Job.__new__(Job._typetable[aqb.AB_Job_GetType(job)])
        Job.__init__(j,job)
        return j
    _check_retval_ = staticmethod(_check_retval_)

    def __init__(self, job):
        assert self.__class__ != Job, "class Job is abstract"
        c_void_p.__init__(self, job)

    def __del__(self):
        aqb.AB_Job_free(self)

    dele = __del__

    jobid = property(aqb.AB_Job_GetJobId)

    jobtype = property(aqb.AB_Job_GetType)

    jobstatus = property(aqb.AB_Job_GetStatus)

    lastStatusChange = property(aqb.AB_Job_GetLastStatusChange)

    createdBy = property(aqb.AB_Job_GetCreatedBy)

    account = property(aqb.AB_Job_GetAccount)

    def checkAvailability(self):
        return aqb.AB_Job_CheckAvailability(self)

    def __str__(self):
        return "%s(id=%d/%s, type=%s, status=%s(%s), mem=0x%x)" % \
               (self.__class__.__name__, self.jobid, self.createdBy,
                self.jobtype, self.jobstatus, self.lastStatusChange, self.value)

def from_param(cls, value):
    if not isinstance(value, Job):
        raise TypeError('class Job expected (found %s)' % value)
    return value
Job.from_param = classmethod(from_param)

aqb.AB_Job_GetLastStatusChange.restype = GWEN_Time
aqb.AB_Job_GetAccount.restype = Account
aqb.AB_Job_GetCreatedBy.restype = c_char_p
aqb.AB_Job_GetStatus.restype = JobStatus
aqb.AB_Job_CheckAvailability.restype = AB_Error
aqb.AB_Job_GetType.restype = JobType


class JobSingleDebitNote(Job):
    "retrieve money from another account"

    def __init__(self, account, transaction):
        Job.__init__(self, aqb.AB_JobSingleDebitNote_new(account))
        self.transaction = transaction

    transaction = property(
        aqb.AB_JobSingleDebitNote_GetTransaction,
        aqb.AB_JobSingleDebitNote_SetTransaction)

    fieldLimits = property(aqb.AB_JobSingleDebitNote_GetFieldLimits)

    #def iterTextKeys(self):
    #    p = aqb.AB_JobSingleDebitNote_GetTextKeys(self)
    #    if not p:
    #        chk(AB_Error.not_available)
    #    for i in p:
    #        if i < 0:
    #            break
    #        yield i

    #maxPurposeLines = property(aqb.AB_JobSingleDebitNote_GetMaxPurposeLines)

aqb.AB_JobSingleDebitNote_GetTransaction.restype = Transaction
aqb.AB_JobSingleDebitNote_GetFieldLimits.restype = TransactionLimits
#aqb.AB_JobSingleDebitNote_GetTextKeys.restype = c_int_p
#aqb.AB_JobSingleDebitNote_GetMaxPurposeLines.restype = c_int


class JobSingleTransfer(Job):
    "transfer money from your account to another"

    def __init__(self, account, transaction):
        Job.__init__(self, aqb.AB_JobSingleTransfer_new(account))
        self.transaction = transaction

    transaction = property(
        aqb.AB_JobSingleTransfer_GetTransaction,
        aqb.AB_JobSingleTransfer_SetTransaction)

    fieldLimits = property(aqb.AB_JobSingleTransfer_GetFieldLimits)

    #def iterTextKeys(self):
    #    p = aqb.AB_JobSingleTransfer_GetTextKeys(self)
    #    if not p:
    #        chk(AB_Error.not_available)
    #    for i in p:
    #        if i < 0:
    #            break
    #        yield i

    #maxPurposeLines = property(aqb.AB_JobSingleTransfer_GetMaxPurposeLines)

aqb.AB_JobSingleTransfer_GetTransaction.restype = Transaction
aqb.AB_JobSingleTransfer_GetFieldLimits.restype = TransactionLimits
#aqb.AB_JobSingleTransfer_GetTextKeys.restype = c_int_p
#aqb.AB_JobSingleTransfer_GetMaxPurposeLines.restype = c_int


class JobEuTransfer(Job):
    "transfer money from your account to another, EU-wide"

    class ChargeWhom(Enum):
        (unknown,
         local,
         remote,
         share,
         ) = range(4)

    def __init__(self, account, transaction):
        Job.__init__(self, aqb.AB_JobEuTransfer_new(account))
        self.transaction = transaction

    transaction = property(
        aqb.AB_JobEuTransfer_GetTransaction,
        aqb.AB_JobEuTransfer_SetTransaction)

    chargeWhom = property(
        aqb.AB_JobEuTransfer_GetChargeWhom,
        aqb.AB_JobEuTransfer_SetChargeWhom)

    ibanAllowed = property(aqb.AB_JobEuTransfer_GetIbanAllowed)

    def findCountryInfo(self, cnt):
        return aqb.AB_JobEuTransfer_FindCountryInfo(self, cnt)

    def iterCountryInfo(self):
	cl = aqb.AB_JobEuTransfer_GetCountryInfoList(self)
	if not sl:
	    return
	e = aqb.AB_EuTransferInfo_List_First(sl)
	while e:
	    yield EuTransferInfo._check_retval_(e)
	    e = aqb.AB_EuTransferInfo_List_Next(e)


class ChargeWhomAdapter(c_int):
    def _check_retval_(i):
        return JobEuTransfer.ChargeWhom(i)

def from_param(cls, e):
    check_enum(e, JobEuTransfer.ChargeWhom, 'argument')
    return int(e)
ChargeWhomAdapter.from_param = classmethod(from_param)

aqb.AB_JobEuTransfer_GetTransaction.restype = Transaction
aqb.AB_JobEuTransfer_GetChargeWhom.restype = ChargeWhomAdapter
aqb.AB_JobEuTransfer_SetChargeWhom.argtypes = (
    JobEuTransfer, ChargeWhomAdapter)
aqb.AB_JobEuTransfer_FindCountryInfo.restype = EuTransferInfo
aqb.AB_JobEuTransfer_FindCountryInfo.argtypes = JobEuTransfer, c_char_p


class JobGetTransactions(Job):
    "get the transactions for an account"

    def __init__(self, account):
        Job.__init__(self, aqb.AB_JobGetTransactions_new(account))

    fromTime = property(
        aqb.AB_JobGetTransactions_GetFromTime,
        aqb.AB_JobGetTransactions_SetFromTime)

    toTime = property(
        aqb.AB_JobGetTransactions_GetToTime,
        aqb.AB_JobGetTransactions_SetToTime)

    def iterTransactions(self):
        tl = aqb.AB_JobGetTransactions_GetTransactions(self)
        if not tl:
            return
        tit = aqb.AB_Transaction_List2_First(tl)
        tp = aqb.AB_Transaction_List2Iterator_Data(tit)
        while tp:
	    tr = Transaction._check_retval_(tp)
            tr._owner = self
            yield tr
            tp = aqb.AB_Transaction_List2Iterator_Next(tit)
        aqb.AB_Transaction_List2Iterator_free(tit)

aqb.AB_JobGetTransactions_GetFromTime.restype = GWEN_Time
aqb.AB_JobGetTransactions_SetFromTime.argtypes = [Job, GWEN_Time]
aqb.AB_JobGetTransactions_GetToTime.restype = GWEN_Time
aqb.AB_JobGetTransactions_SetToTime.argtypes = [Job, GWEN_Time]

class JobGetBalance(Job):
    "get balance for an account"

    def __init__(self, account):
        Job.__init__(self, aqb.AB_JobGetBalance_new(account))

    accountStatus = property_addref(
        aqb.AB_JobGetBalance_GetAccountStatus)


aqb.AB_JobGetBalance_GetAccountStatus.restype = AccountStatus

Job._typetable = {
    JobType.balance: JobGetBalance,
    JobType.getTransactions: JobGetTransactions,
    JobType.transfer: JobSingleTransfer,
    JobType.debitNote: JobSingleDebitNote,
    JobType.euTransfer: JobEuTransfer,
    }


################################################################
# User Interface
#

class LogLevel(Enum):
    "level of messages for display to the user"
    (panic,
     emergency,
     error,
     warn,
     notice,
     info,
     debug,
     verbous,
     ) = range(8)

class PinStatus(Enum):
    (bad,
     unknown,
     ok,
     ) = range(-1,2)

class TanStatus(Enum):
    (bad,
     unknown,
     used,
     unused,
     ) = range(-1,3)

def stripText(text, markup=True, encoding=None):
    "return the text or html part of a message"
    i = text.find('<html>')
    if markup:
        if i >= 0:
            text = text[i+6:]
            i = text.rfind('</html>')
            if i > 0:
                text = text[:i]
            text = re.sub(' *</?p> *', '',text)
            text = re.sub(' *\n *', '\n', text)
    elif i > 0:
        text = text[:i]
    text = text.decode('utf8')
    if encoding:
        text = text.encode(encoding)
    return text

MSG_FLAGS_TYPE_MASK  = 0x07
MSG_FLAGS_TYPE_INFO  = 0
MSG_FLAGS_TYPE_WARN  = 1
MSG_FLAGS_TYPE_ERROR = 2

MSG_FLAGS_CONFIRM_B1 = 1 << 3
MSG_FLAGS_CONFIRM_B2 = 2 << 3

MSG_FLAGS_SEVERITY_MASK      = 0x7 << 5
MSG_FLAGS_SEVERITY_NORMAL    = 0x0 << 5
MSG_FLAGS_SEVERITY_DANGEROUS = 0x1 << 5

def msg_flags_type(fl):
    return fl & MSG_FLAGS_TYPE_MASK

def msg_flags_confirm_button(fl):
    return (fl & 0x3) >> 3

################################################################
# Base class for main Banking Object
#
class BankingBase(c_void_p):
    """abstract base for the main banking class.

    cf. console.BankingConsole and gtkui.BankingGtk for useable classes.
    """
    callback_funcs = (

        ('getPin', aqb.AB_Banking_SetGetPinFn, CFUNCTYPE(
    	c_int, c_void_p, c_int, c_char_p, c_char_p,
        c_char_p, c_void_p, c_int, c_int)),

        ('messageBox', aqb.AB_Banking_SetMessageBoxFn, CFUNCTYPE(
        c_int, c_void_p, c_int, c_char_p, c_char_p,
        c_char_p, c_char_p, c_char_p)),

        ('inputBox', aqb.AB_Banking_SetInputBoxFn, CFUNCTYPE(
        c_int, c_void_p, c_int, c_char_p, c_char_p, c_char_p, c_int, c_int)),

        ('showBox', aqb.AB_Banking_SetShowBoxFn, CFUNCTYPE(
        c_int, c_void_p, c_int, c_char_p, c_char_p)),

        ('hideBox', aqb.AB_Banking_SetHideBoxFn, CFUNCTYPE(
        None, c_void_p, c_int)),

        ('progressStart', aqb.AB_Banking_SetProgressStartFn, CFUNCTYPE(
        c_int, c_void_p, c_char_p, c_char_p, c_int)),

        ('progressAdvance', aqb.AB_Banking_SetProgressAdvanceFn, CFUNCTYPE(
        c_int, c_void_p, c_int, c_int)),

        ('progressLog', aqb.AB_Banking_SetProgressLogFn, CFUNCTYPE(
        c_int, c_void_p, c_int, c_int, c_char_p)),

        ('progressEnd', aqb.AB_Banking_SetProgressEndFn, CFUNCTYPE(
        c_int, c_void_p, c_int)),

        ('printout', aqb.AB_Banking_SetPrintFn, CFUNCTYPE(
        c_int, c_void_p, c_char_p, c_char_p, c_char_p, c_char_p, c_char_p)),

        ('setPinStatus', aqb.AB_Banking_SetSetPinStatusFn, CFUNCTYPE(
        c_int, c_void_p, c_char_p, c_char_p, c_int)),

        ('getTan', aqb.AB_Banking_SetGetTanFn, CFUNCTYPE(
        c_int, c_void_p, c_char_p, c_char_p, c_char_p, c_char_p,
        c_int, c_int)),

        ('setTanStatus', aqb.AB_Banking_SetSetTanStatusFn, CFUNCTYPE(
        c_int, c_void_p, c_char_p, c_char_p, c_int)),
        )

    def __init__(self, name, configdir=None):
        global aqb
	c_void_p.__init__(self, aqb.AB_Banking_new(name, configdir))
        self.inited = False
        self.bankingFree = aqb.AB_Banking_free
        l = []
        for name, fn, tp in BankingBase.callback_funcs:
            try:
                f = getattr(self, name)
            except AttributeError:
                if self.__class__ == BankingBase:
                    raise RuntimeError('class BankingBase is abstract!')
                raise AttributeError(
                    'callback %s must be defined in class %s' % (name, self.__class__))
            f = tp(f)
            fn(self, f)
            l.append(f)
        self.func_refs = l
        # never unregistered (FIXME? could use 1 func + list + callback)
        atexit.register(lambda r=weakref.ref(self): r() and r().fini())
	chk(aqb.AB_Banking_Init(self))
        self.inited = True

    def fini(self):
        if self.inited:
            self.inited = False
            chk(aqb.AB_Banking_Fini(self))

    def save(self):
        chk(aqb.AB_Banking_Save(self))

    def __del__(self):
        self.fini()
        self.bankingFree(self)

    appName = property(aqb.AB_Banking_GetAppName)

    def getProviderDescrs(self):
        pl = aqb.AB_Banking_GetProviderDescrs(self)
        if not pl:
            return
        pit = gwen.GWEN_PluginDescription_List2_First(pl)
        pd = gwen.GWEN_PluginDescription_List2Iterator_Data(pit)
        while pd:
            yield PluginDescription._check_retval_(pd)
            pd = gwen.GWEN_PluginDescription_List2Iterator_Next(pit)
        gwen.GWEN_PluginDescription_List2Iterator_free(pit)
        gwen.GWEN_PluginDescription_List2_free(pl)

    def iterActiveProviders(self):
        return aqb.AB_Banking_GetActiveProviders(self)

    def activateProvider(self, backend):
        chk(aqb.AB_Banking_ActivateProvider(self, backend))

    def deactivateProvider(self, backend):
        chk(aqb.AB_Banking_DeactivateProvider(self, backend))

    def getAppUserDataDir(self):
        buf = GWEN_Buffer()
        chk(aqb.AB_Banking_GetAppUserDataDir(self, buf))
        return str(buf)

    appUserDataDir = property(getAppUserDataDir)

    appData = property(aqb.AB_Banking_GetAppData)

    def findWizard(self, backend, frontends='gnome;gtk;qt;kde'):
        buf = GWEN_Buffer()
        chk(aqb.AB_Banking_FindWizard(self, backend, frontends, buf))
        return str(buf)

    def iterImExporterDescrs(self):
	pl = aqb.AB_Banking_GetImExporterDescrs(self)
        if not pl:
            return
	pit = gwen.GWEN_PluginDescription_List2_First(pl)
	pd = gwen.GWEN_PluginDescription_List2Iterator_Data(pit)
	while pd:
	    yield PluginDescription._check_retval_(pd)
	    pd = gwen.GWEN_PluginDescription_List2Iterator_Next(pit)
        gwen.GWEN_PluginDescription_List2Iterator_free(pit)
        gwen.GWEN_PluginDescription_List2_free(pl)
        
    def getImExporter(self, tp):
	return aqb.AB_Banking_GetImExporter(self, tp)

    def getImExporterProfiles(self, tp):
	return aqb.AB_Banking_GetImExporterProfiles(self, tp)

    def getAccountByCodeAndNumber(self, bank, account):
	return aqb.AB_Banking_GetAccountByCodeAndNumber(
            self, bank, account)

    def enqueueJob(self, job):
    	chk(aqb.AB_Banking_EnqueueJob(self, job))

    def deferJob(self, job):
        chk(aqb.AB_Banking_DeferJob(self, job))

    def enqueuePendingJobs(self, mineOnly):
        chk(aqb.AB_Banking_EnqueuePendingJobs(self, mineOnly))

    def iterEnqueuedJobs(self):
        return aqb.AB_Banking_GetEnqueuedJobs(self)

    def iterFinishedJobs(self):
        return aqb.AB_Banking_GetFinishedJobs(self)

    def iterDeferredJobs(self):
        return aqb.AB_Banking_GetDeferredJobs(self)

    def iterPendingJobs(self):
        return aqb.AB_Banking_GetPendingJobs(self)

    def iterArchivedJobs(self):
        return aqb.AB_Banking_GetArchivedJobs(self)

    def delArchivedJob(self, job):
        chk(aqb.AB_Banking_DelArchivedJob(self, job))

    def delPendingJob(self, job):
        chk(aqb.AB_Banking_DelPendingJob(self, job))

    def delFinishedJob(self, job):
        chk(aqb.AB_Banking_DelFinishedJob(self, job))

    def delDeferredJob(self, job):
        chk(aqb.AB_Banking_DelDeferredJob(self, job))

    def dequeueJob(self, job):
	chk(aqb.AB_Banking_DequeueJob(self, job))

    def executeQueue(self):
	chk(aqb.AB_Banking_ExecuteQueue(self))

    def requestBalance(self, bankCode, accountNumber):
	chk(aqb.AB_Banking_RequestBalance(self, bankCode, accountNumber))

    def requestTransactions(self, bankCode, accountNumber, firstDate, lastDate):
        chk(aqb.AB_Banking_RequestTransactions(
            self, bankCode, accountNumber, firstDate, lastDate))

    def iterAccounts(self):
	al = aqb.AB_Banking_GetAccounts(self)
        if not al:
            return
        ait = aqb.AB_Account_List2_First(al)
        ap = aqb.AB_Account_List2Iterator_Data(ait)
 	while ap:
 	    yield Account._check_retval_(ap)
 	    ap = aqb.AB_Account_List2Iterator_Next(ait)
        gwen.GWEN_PluginDescription_List2Iterator_free(ait)
        gwen.GWEN_PluginDescription_List2_free(al)

    def getAccount(self, uniqueId):
	return aqb.AB_Banking_GetAccount(self, uniqueId)

    def getAlias(self, account, subst_id=True):
        """get the alias name of an account.

        account    Account instance or uniqueId
        subst_id   if no alias is found: if True return
                   the uniqueId as string, else return None
        """
        if isinstance(account, Account):
            aid = account.uniqueId
        for i, a in self.iterAliases():
            if i == aid:
                return a
        if subst_id:
            return str(aid)
        return None

    def setAccountAlias(self, account, alias):
        """Set an alias name for an account

        deletes all previous aliases for this account
        """
        n = self._getAliasNode()
        if n is not None:
            aid = account.uniqueId
            aliases = []
            for v in n.iterVars():
                if v.getIntValue() == aid:
                    aliases.append(v.name)
            for v in aliases:
                n.deleteVar(v)
        aqb.AB_Banking_SetAccountAlias(self, account, alias)

    def getAccountByAlias(self, alias):
        return aqb.AB_Banking_GetAccountByAlias(self, alias)

    def _getAliasNode(self):
        return self.appData.getGroup(PathFlags.NAMEMUSTEXIST,'banking/aliases')

    def iterAliases(self):
        n = self._getAliasNode()
        if n is None:
            return
        for v in n.iterVars():
            yield v.getIntValue(), v.name


BankingBase.from_param = from_cls_param

aqb.AB_Banking_GetActiveProviders.restype = iterStringlist
aqb.AB_Banking_GetEnqueuedJobs.restype = iterJobs
aqb.AB_Banking_GetPendingJobs.restype = iterJobs
aqb.AB_Banking_GetFinishedJobs.restype = iterJobs
aqb.AB_Banking_GetArchivedJobs.restype = iterJobs
aqb.AB_Banking_GetAppName.restype = c_char_p
aqb.AB_Banking_GetAccountByCodeAndNumber.restype = Account
aqb.AB_Banking_GetAccount.restype = Account
aqb.AB_Banking_GetAppData.restype = GWEN_DB_Node
aqb.AB_Banking_ActivateProvider.argtypes = BankingBase, c_char_p
aqb.AB_Banking_DeactivateProvider.argtypes = BankingBase, c_char_p
aqb.AB_Banking_GetAccountByAlias.restype = Account
aqb.AB_Banking_GetAccountByAlias.argtypes = BankingBase, c_char_p
aqb.AB_Banking_SetAccountAlias.argtypes = BankingBase, Account, c_char_p
aqb.AB_Banking_GetAccountByCodeAndNumber.argtypes = (
    BankingBase, c_char_p, c_char_p)
aqb.AB_Banking_GetImExporter.restype = ImExporter
aqb.AB_Banking_GetImExporter.argtypes = BankingBase, c_char_p
aqb.AB_Banking_GetImExporterProfiles.restype = GWEN_DB_Node
aqb.AB_Banking_GetImExporterProfiles.argtypes = BankingBase, c_char_p
aqb.AB_Banking_FindWizard.argtypes = (
    BankingBase, c_char_p, c_char_p, GWEN_Buffer)
aqb.AB_Banking_RequestTransactions.argtypes = (
    BankingBase, c_char_p, c_char_p, GWEN_Time, GWEN_Time)

# FIXME: should not be needed for stable version of aqBanking
# shut up aqBanking.. too noisy..
Logger_SetLevel('aqbanking', LoggerLevel.error)
Logger_SetLevel('gwenhywfar', LoggerLevel.error)
Logger_SetLevel('aqhbci', LoggerLevel.error)
Logger_SetLevel(None, LoggerLevel.error)