summaryrefslogtreecommitdiff
path: root/doc/doc.html
blob: c68b44789865ec1ecf693b25b3b907c8f24c3932 (plain)
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2022-02-22 mar 07:37 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Taler-Util Library</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Taler Contributors" />
<style type="text/css">
 <!--/*--><![CDATA[/*><!--*/
  .title  { text-align: center;
             margin-bottom: .2em; }
  .subtitle { text-align: center;
              font-size: medium;
              font-weight: bold;
              margin-top:0; }
  .todo   { font-family: monospace; color: red; }
  .done   { font-family: monospace; color: green; }
  .priority { font-family: monospace; color: orange; }
  .tag    { background-color: #eee; font-family: monospace;
            padding: 2px; font-size: 80%; font-weight: normal; }
  .timestamp { color: #bebebe; }
  .timestamp-kwd { color: #5f9ea0; }
  .org-right  { margin-left: auto; margin-right: 0px;  text-align: right; }
  .org-left   { margin-left: 0px;  margin-right: auto; text-align: left; }
  .org-center { margin-left: auto; margin-right: auto; text-align: center; }
  .underline { text-decoration: underline; }
  #postamble p, #preamble p { font-size: 90%; margin: .2em; }
  p.verse { margin-left: 3%; }
  pre {
    border: 1px solid #ccc;
    box-shadow: 3px 3px 3px #eee;
    padding: 8pt;
    font-family: monospace;
    overflow: auto;
    margin: 1.2em;
  }
  pre.src {
    position: relative;
    overflow: visible;
    padding-top: 1.2em;
  }
  pre.src:before {
    display: none;
    position: absolute;
    background-color: white;
    top: -10px;
    right: 10px;
    padding: 3px;
    border: 1px solid black;
  }
  pre.src:hover:before { display: inline;}
  /* Languages per Org manual */
  pre.src-asymptote:before { content: 'Asymptote'; }
  pre.src-awk:before { content: 'Awk'; }
  pre.src-C:before { content: 'C'; }
  /* pre.src-C++ doesn't work in CSS */
  pre.src-clojure:before { content: 'Clojure'; }
  pre.src-css:before { content: 'CSS'; }
  pre.src-D:before { content: 'D'; }
  pre.src-ditaa:before { content: 'ditaa'; }
  pre.src-dot:before { content: 'Graphviz'; }
  pre.src-calc:before { content: 'Emacs Calc'; }
  pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  pre.src-fortran:before { content: 'Fortran'; }
  pre.src-gnuplot:before { content: 'gnuplot'; }
  pre.src-haskell:before { content: 'Haskell'; }
  pre.src-hledger:before { content: 'hledger'; }
  pre.src-java:before { content: 'Java'; }
  pre.src-js:before { content: 'Javascript'; }
  pre.src-latex:before { content: 'LaTeX'; }
  pre.src-ledger:before { content: 'Ledger'; }
  pre.src-lisp:before { content: 'Lisp'; }
  pre.src-lilypond:before { content: 'Lilypond'; }
  pre.src-lua:before { content: 'Lua'; }
  pre.src-matlab:before { content: 'MATLAB'; }
  pre.src-mscgen:before { content: 'Mscgen'; }
  pre.src-ocaml:before { content: 'Objective Caml'; }
  pre.src-octave:before { content: 'Octave'; }
  pre.src-org:before { content: 'Org mode'; }
  pre.src-oz:before { content: 'OZ'; }
  pre.src-plantuml:before { content: 'Plantuml'; }
  pre.src-processing:before { content: 'Processing.js'; }
  pre.src-python:before { content: 'Python'; }
  pre.src-R:before { content: 'R'; }
  pre.src-ruby:before { content: 'Ruby'; }
  pre.src-sass:before { content: 'Sass'; }
  pre.src-scheme:before { content: 'Scheme'; }
  pre.src-screen:before { content: 'Gnu Screen'; }
  pre.src-sed:before { content: 'Sed'; }
  pre.src-sh:before { content: 'shell'; }
  pre.src-sql:before { content: 'SQL'; }
  pre.src-sqlite:before { content: 'SQLite'; }
  /* additional languages in org.el's org-babel-load-languages alist */
  pre.src-forth:before { content: 'Forth'; }
  pre.src-io:before { content: 'IO'; }
  pre.src-J:before { content: 'J'; }
  pre.src-makefile:before { content: 'Makefile'; }
  pre.src-maxima:before { content: 'Maxima'; }
  pre.src-perl:before { content: 'Perl'; }
  pre.src-picolisp:before { content: 'Pico Lisp'; }
  pre.src-scala:before { content: 'Scala'; }
  pre.src-shell:before { content: 'Shell Script'; }
  pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
  /* additional language identifiers per "defun org-babel-execute"
       in ob-*.el */
  pre.src-cpp:before  { content: 'C++'; }
  pre.src-abc:before  { content: 'ABC'; }
  pre.src-coq:before  { content: 'Coq'; }
  pre.src-groovy:before  { content: 'Groovy'; }
  /* additional language identifiers from org-babel-shell-names in
     ob-shell.el: ob-shell is the only babel language using a lambda to put
     the execution function name together. */
  pre.src-bash:before  { content: 'bash'; }
  pre.src-csh:before  { content: 'csh'; }
  pre.src-ash:before  { content: 'ash'; }
  pre.src-dash:before  { content: 'dash'; }
  pre.src-ksh:before  { content: 'ksh'; }
  pre.src-mksh:before  { content: 'mksh'; }
  pre.src-posh:before  { content: 'posh'; }
  /* Additional Emacs modes also supported by the LaTeX listings package */
  pre.src-ada:before { content: 'Ada'; }
  pre.src-asm:before { content: 'Assembler'; }
  pre.src-caml:before { content: 'Caml'; }
  pre.src-delphi:before { content: 'Delphi'; }
  pre.src-html:before { content: 'HTML'; }
  pre.src-idl:before { content: 'IDL'; }
  pre.src-mercury:before { content: 'Mercury'; }
  pre.src-metapost:before { content: 'MetaPost'; }
  pre.src-modula-2:before { content: 'Modula-2'; }
  pre.src-pascal:before { content: 'Pascal'; }
  pre.src-ps:before { content: 'PostScript'; }
  pre.src-prolog:before { content: 'Prolog'; }
  pre.src-simula:before { content: 'Simula'; }
  pre.src-tcl:before { content: 'tcl'; }
  pre.src-tex:before { content: 'TeX'; }
  pre.src-plain-tex:before { content: 'Plain TeX'; }
  pre.src-verilog:before { content: 'Verilog'; }
  pre.src-vhdl:before { content: 'VHDL'; }
  pre.src-xml:before { content: 'XML'; }
  pre.src-nxml:before { content: 'XML'; }
  /* add a generic configuration mode; LaTeX export needs an additional
     (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
  pre.src-conf:before { content: 'Configuration File'; }

  table { border-collapse:collapse; }
  caption.t-above { caption-side: top; }
  caption.t-bottom { caption-side: bottom; }
  td, th { vertical-align:top;  }
  th.org-right  { text-align: center;  }
  th.org-left   { text-align: center;   }
  th.org-center { text-align: center; }
  td.org-right  { text-align: right;  }
  td.org-left   { text-align: left;   }
  td.org-center { text-align: center; }
  dt { font-weight: bold; }
  .footpara { display: inline; }
  .footdef  { margin-bottom: 1em; }
  .figure { padding: 1em; }
  .figure p { text-align: center; }
  .equation-container {
    display: table;
    text-align: center;
    width: 100%;
  }
  .equation {
    vertical-align: middle;
  }
  .equation-label {
    display: table-cell;
    text-align: right;
    vertical-align: middle;
  }
  .inlinetask {
    padding: 10px;
    border: 2px solid gray;
    margin: 10px;
    background: #ffffcc;
  }
  #org-div-home-and-up
   { text-align: right; font-size: 70%; white-space: nowrap; }
  textarea { overflow-x: auto; }
  .linenr { font-size: smaller }
  .code-highlighted { background-color: #ffff00; }
  .org-info-js_info-navigation { border-style: none; }
  #org-info-js_console-label
    { font-size: 10px; font-weight: bold; white-space: nowrap; }
  .org-info-js_search-highlight
    { background-color: #ffff00; color: #000000; font-weight: bold; }
  .org-svg { width: 90%; }
  /*]]>*/-->
</style>
<script type="text/javascript">
/*
@licstart  The following is the entire license notice for the
JavaScript code in this tag.

Copyright (C) 2012-2020 Free Software Foundation, Inc.

The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.  The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU GPL for more details.

As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.


@licend  The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
 function CodeHighlightOn(elem, id)
 {
   var target = document.getElementById(id);
   if(null != target) {
     elem.cacheClassElem = elem.className;
     elem.cacheClassTarget = target.className;
     target.className = "code-highlighted";
     elem.className   = "code-highlighted";
   }
 }
 function CodeHighlightOff(elem, id)
 {
   var target = document.getElementById(id);
   if(elem.cacheClassElem)
     elem.className = elem.cacheClassElem;
   if(elem.cacheClassTarget)
     target.className = elem.cacheClassTarget;
 }
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">Taler-Util Library</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org25ea71b">1. classes overview</a>
<ul>
<li><a href="#orgb49dd7e">1.1. amount (currency + value + fraction)</a></li>
<li><a href="#orgb7c6fb1">1.2. logging</a></li>
<li><a href="#org4307498">1.3. ‘payto’ URI particularities</a></li>
<li><a href="#org44d03ed">1.4. configuration</a></li>
</ul>
</li>
<li><a href="#orgd6a98cd">2. classes for handling currency plus value plus fraction</a>
<ul>
<li><a href="#org5d287dc">2.1. class <code>Amount</code></a></li>
<li><a href="#org56a0ae4">2.2. class <code>SignedAmount</code></a></li>
</ul>
</li>
<li><a href="#orgb9b4b04">3. classes <code>LogDefinition</code>, <code>GnunetLoglevel</code></a></li>
<li><a href="#orga6828fa">4. class <code>GnunetLogger</code></a>
<ul>
<li><a href="#org8d3839f">4.1. log definition, environment variables</a></li>
<li><a href="#org9a2a746">4.2. environment variable <code>GNUNET_FORCE_LOGFILE</code></a></li>
<li><a href="#orga5014e4">4.3. constructor</a></li>
<li><a href="#orgfb9a8c1">4.4. method <code>log</code></a></li>
</ul>
</li>
<li><a href="#orgaadd9a7">5. ‘payto’ URI parsing</a></li>
<li><a href="#orgdf9cef8">6. class <code>TalerConfig</code></a>
<ul>
<li><a href="#org03f14e5">6.1. reading</a></li>
<li><a href="#orgd929224">6.2. value types</a></li>
<li><a href="#orgc4601e4">6.3. retrieving values</a>
<ul>
<li><a href="#org76aedbb">6.3.1. specific values</a></li>
<li><a href="#org1feca74">6.3.2. entire set</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<p>
The Taler-Util library provides several classes that deal with various
aspects of Taler programming in the Python language.
This is documentation for the library.
</p>

<p>
This file is in Org Mode format and can be processed (by Emacs)
to produce HTML, etc.  When we figure out <b>where</b> to put this
documentation, we can convert it to Sphinx (or whatever) format.
Ongoing discussion: <a href="https://bugs.gnunet.org/view.php?id=6649">https://bugs.gnunet.org/view.php?id=6649</a>
</p>

<div id="outline-container-org25ea71b" class="outline-2">
<h2 id="org25ea71b"><span class="section-number-2">1</span> classes overview</h2>
<div class="outline-text-2" id="text-1">
<p>
These are grouped according to area of concern, which (uncoincidentally)
is also how the source code is organized.
</p>

<p>
Several of these derive from the <code>Exception</code> class.
The rest are <i>leaf classes</i>.
</p>
</div>

<div id="outline-container-orgb49dd7e" class="outline-3">
<h3 id="orgb49dd7e"><span class="section-number-3">1.1</span> amount (currency + value + fraction)</h3>
<div class="outline-text-3" id="text-1-1">
<ul class="org-ul">
<li>CurrencyMismatchError(Exception)</li>
<li>AmountOverflowError(Exception)</li>
<li>AmountFormatError(Exception)</li>
<li>Amount</li>
<li>SignedAmount</li>
</ul>
</div>
</div>

<div id="outline-container-orgb7c6fb1" class="outline-3">
<h3 id="orgb7c6fb1"><span class="section-number-3">1.2</span> logging</h3>
<div class="outline-text-3" id="text-1-2">
<ul class="org-ul">
<li>LogDefinition</li>
<li>GnunetLoglevel</li>
<li>GnunetLogger</li>
</ul>
</div>
</div>

<div id="outline-container-org4307498" class="outline-3">
<h3 id="org4307498"><span class="section-number-3">1.3</span> ‘payto’ URI particularities</h3>
<div class="outline-text-3" id="text-1-3">
<ul class="org-ul">
<li>PaytoFormatError(Exception)</li>
<li>PaytoParse</li>
</ul>
</div>
</div>

<div id="outline-container-org44d03ed" class="outline-3">
<h3 id="org44d03ed"><span class="section-number-3">1.4</span> configuration</h3>
<div class="outline-text-3" id="text-1-4">
<ul class="org-ul">
<li>TalerConfig</li>
</ul>
</div>
</div>
</div>

<div id="outline-container-orgd6a98cd" class="outline-2">
<h2 id="orgd6a98cd"><span class="section-number-2">2</span> classes for handling currency plus value plus fraction</h2>
<div class="outline-text-2" id="text-2">
<p>
The <code>Amount</code> and <code>SignedAmount</code> handle Taler <i>amounts</i>,
objects that combine a <code>CURRENCY</code> (e.g., "KUDOS")
with a <code>VALUE</code> and <code>FRACTION</code> (both integers).
An amount is written as follows:
</p>

<pre class="example">
CURRENCY:VALUE.FRACTION
</pre>


<p>
Note the <code>:</code> (colon) and <code>.</code> (period).
This is also known as the <code>CUR:X.Y</code> format.
</p>

<p>
The maximum <code>VALUE</code> is 2<sup>52</sup> (i.e., 4503599627370496).
The <code>FRACTION</code> can be at most 8 digits (i.e., smallest non-zero
<code>FRACTION</code> of <code>1</code> represents the number 0.00000001, and the largest
<code>FRACTION</code> of 99999999 represents the number 0.99999999).
If an amount is specified that exceeds these limits,
the constructor throws an <code>AmountOverflowError</code> exception.
</p>
</div>

<div id="outline-container-org5d287dc" class="outline-3">
<h3 id="org5d287dc"><span class="section-number-3">2.1</span> class <code>Amount</code></h3>
<div class="outline-text-3" id="text-2-1">
<p>
The constructor takes three args: <code>currency</code>, <code>value</code>, <code>fraction</code>.
</p>

<pre class="example">
&gt;&gt;&gt; from taler.util.amount import Amount, SignedAmount

# KUDOS 10.50
&gt;&gt;&gt; amt = Amount ("KUDOS", 10, 50000000)

&gt;&gt;&gt; amt
Amount(currency='KUDOS', value=10, fraction=50000000)
</pre>


<p>
<code>Amount</code> has three getter properties: <code>currency</code>, <code>value</code>, <code>fraction</code>.
</p>

<pre class="example">
&gt;&gt;&gt; amt.value, amt.fraction
(10, 50000000)
</pre>


<p>
You can use classmethod <code>parse</code> to read a string as an <code>Amount</code> object.
This function can throw <code>AmountFormatError</code> if the string is malformed,
and <code>AmountOverflowError</code> if the fraction portion is too long.
</p>

<pre class="example">
&gt;&gt;&gt; Amount.parse ("KUDOS:10.12345678")
Amount(currency='KUDOS', value=10, fraction=12345678)
</pre>


<p>
An <code>Amount</code> object supports addition and subtraction.
The <code>currency</code> property must match, otherwise the operation throws
a <code>CurrencyMismatchError</code> exception.
</p>

<pre class="example">
&gt;&gt;&gt; amt + amt
Amount(currency='KUDOS', value=21, fraction=0)

&gt;&gt;&gt; another = Amount ("KUDOS", 5, 42)

&gt;&gt;&gt; amt - another
Amount(currency='KUDOS', value=5, fraction=49999958)
</pre>


<p>
Note, however, that a subtraction that results in a numerically negative
<code>value</code> causes the operation to throw an <code>AmountOverflowError</code> exception.
</p>

<pre class="example">
&gt;&gt;&gt; another - amt
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File ".../amount.py", line 124, in __sub__
    raise AmountOverflowError()
taler.util.amount.AmountOverflowError
</pre>


<p>
The method <code>stringify</code> (which is also used in the <code>__str__</code> definition)
takes optional keyword <code>pretty</code> (default <code>False</code>) that changes the
output.
</p>

<pre class="example">
&gt;&gt;&gt; str (amt)
'KUDOS:10.5'

&gt;&gt;&gt; amt.stringify()
'KUDOS:10.5'

&gt;&gt;&gt; amt.stringify(pretty=True)
'10.5 KUDOS'

&gt;&gt;&gt; (amt + amt).stringify(pretty=True)
'21 KUDOS'
</pre>


<p>
The method <code>is_zero</code> returns <code>True</code> if the <code>Amount</code> object has a zero
<code>value</code> component and a zero <code>fraction</code> component.
</p>

<pre class="example">
&gt;&gt;&gt; amt.is_zero ()
False
</pre>


<p>
An <code>Amount</code> object can be numerically compared with another <code>Amount</code>
for both equality and inequality.
Comparison can throw a <code>CurrencyMismatchError</code> exception
if both currencies are not the same.
</p>

<pre class="example">
&gt;&gt;&gt; amt &gt; another
True

&gt;&gt;&gt; amt == another
False
</pre>
</div>
</div>

<div id="outline-container-org56a0ae4" class="outline-3">
<h3 id="org56a0ae4"><span class="section-number-3">2.2</span> class <code>SignedAmount</code></h3>
<div class="outline-text-3" id="text-2-2">
<p>
A <code>SignedAmount</code> object is an <i>amount with a sign</i>.
It has properties <code>is_positive</code> and <code>amount</code>.
You can derive a <code>SignedAmount</code> from a simple <code>Amount</code> with the
<code>as_signed</code> method.
</p>

<pre class="example">
&gt;&gt;&gt; p = amt.as_signed ()

&gt;&gt;&gt; p.is_positive
True

&gt;&gt;&gt; p.amount
Amount(currency='KUDOS', value=10, fraction=50000000)
</pre>


<p>
A <code>SignedAmount</code> object supports addition, subtraction, and comparison
(equality and inequality) with another <code>SignedAmount</code> object.
</p>

<pre class="example">
&gt;&gt;&gt; q = another.as_signed ()

&gt;&gt;&gt; (p - q).is_positive
True

&gt;&gt;&gt; (q - p).is_positive
False
</pre>


<p>
The <code>stringify</code> method, like that for <code>Amount</code>, takes optional keyword
<code>pretty</code>.  It always prefixes the output with either <code>+</code> or <code>-</code>.
</p>

<pre class="example">
&gt;&gt;&gt; (p - q).stringify (pretty=False)
'+KUDOS:5.49999958'

&gt;&gt;&gt; (q - p).stringify (pretty=True)
'-5.49999958 KUDOS'
</pre>


<p>
The classmethod <code>parse</code> recognizes a leading <code>+</code> or <code>-</code>, and
additionally accepts a plain <code>CURRENCY:VALUE.FRACTION</code> form as a
positive <code>SignedAmount</code>.
</p>

<pre class="example">
&gt;&gt;&gt; SignedAmount.parse ("-KUDOS:2.34")
SignedAmount(is_positive=False, amount=Amount(currency='KUDOS', value=2, fraction=34000000))
</pre>


<p>
Lastly, a <code>SignedAmount</code> object can flip its sign using a unary minus.
</p>

<pre class="example">
&gt;&gt;&gt; n = q - p

&gt;&gt;&gt; n.is_positive
False

&gt;&gt;&gt; (- n).is_positive
True
</pre>
</div>
</div>
</div>

<div id="outline-container-orgb9b4b04" class="outline-2">
<h2 id="orgb9b4b04"><span class="section-number-2">3</span> classes <code>LogDefinition</code>, <code>GnunetLoglevel</code></h2>
<div class="outline-text-2" id="text-3">
<p>
These two classes are deliberately undocumented (until further notice).
They exist primarily to support the <code>GnunetLogger</code> class.
</p>
</div>
</div>

<div id="outline-container-orga6828fa" class="outline-2">
<h2 id="orga6828fa"><span class="section-number-2">4</span> class <code>GnunetLogger</code></h2>
<div class="outline-text-2" id="text-4">
<p>
The <code>GnunetLogger</code> class wraps the native <code>logging</code> module and provides
two primary entry points: the constructor and the <code>log</code> method.
It supports the usual list of <i>log levels</i>:
<code>ERROR</code>, <code>WARNING</code>, <code>INFO</code>, <code>DEBUG</code>.
</p>
</div>

<div id="outline-container-org8d3839f" class="outline-3">
<h3 id="org8d3839f"><span class="section-number-3">4.1</span> log definition, environment variables</h3>
<div class="outline-text-3" id="text-4-1">
<p>
What to log is controlled by a <i>log definition</i>, lists of
which are taken from one or both environment variables when
the <code>GnunetLogger</code> object is initialized:
</p>

<ul class="org-ul">
<li><code>GNUNET_FORCE_LOG</code></li>
<li><code>GNUNET_LOG</code></li>
</ul>

<p>
A log definition looks like:
</p>

<pre class="example">
[component];[file];[function];[from_line[-to_line]];loglevel
</pre>


<p>
The <code>component</code>, <code>file</code>, <code>function</code>, and line information portions
are optional; the <code>loglevel</code> is required.
No portion may contain the <code>;</code> (semicolon) or <code>/</code> (slash) character.
When a portion is omitted, it defaults to <i>all</i> of that item.
The line information can be a single line number or a range,
written in <code>LOW-HIGH</code> format &#x2013; note <code>-</code> (hyphen).
For example, a minimal log definition could be:
</p>

<pre class="example">
;;;;ERROR
</pre>


<p>
This example definition matches all components, all files, all
functions, and all lines, but only the <code>ERROR</code> log level.
</p>

<p>
Multiple log definitions are specified by separating them with a <code>/</code>
(slash) character.
</p>

<pre class="example">
network;;;ERROR/database;;;DEBUG
</pre>


<p>
The difference between <code>GNUNET_FORCE_LOG</code> and <code>GNUNET_LOG</code> is that the
former takes priority over the latter, in case of conflict.
Also, logging done via <code>GNUNET_FORCE_LOG</code> respects environment
variable <code>GNUNET_FORCE_LOGFILE</code>.
</p>
</div>
</div>

<div id="outline-container-org9a2a746" class="outline-3">
<h3 id="org9a2a746"><span class="section-number-3">4.2</span> environment variable <code>GNUNET_FORCE_LOGFILE</code></h3>
<div class="outline-text-3" id="text-4-2">
<p>
The filename specified by <code>GNUNET_FORCE_LOGFILE</code> can
have special character sequences replaced (like a template):
</p>

<dl class="org-dl">
<dt><code>{}</code></dt><dd>component</dd>
<dt><code>[]</code></dt><dd>process id</dd>
<dt><code>%Y</code></dt><dd>numeric year</dd>
<dt><code>%m</code></dt><dd>numeric month</dd>
<dt><code>%d</code></dt><dd>numeric day</dd>
</dl>

<p>
For example, if <code>GNUNET_FORCE_LOGFILE</code> has value:
</p>

<pre class="example">
/var/log/[].{}.%Y-%m-%d.error.log
</pre>


<p>
then the expansion might be:
</p>

<pre class="example">
/var/log/14916.monolith.2022-02-10.error.log
</pre>
</div>
</div>

<div id="outline-container-orga5014e4" class="outline-3">
<h3 id="orga5014e4"><span class="section-number-3">4.3</span> constructor</h3>
<div class="outline-text-3" id="text-4-3">
<p>
The <code>GnunetLogger</code> constructor takes one argument, <code>component</code>.
</p>

<pre class="example">
&gt;&gt;&gt; from taler.util.gnunet_log import GnunetLogger

&gt;&gt;&gt; l = GnunetLogger ("ui")
</pre>
</div>
</div>

<div id="outline-container-orgfb9a8c1" class="outline-3">
<h3 id="orgfb9a8c1"><span class="section-number-3">4.4</span> method <code>log</code></h3>
<div class="outline-text-3" id="text-4-4">
<p>
The <code>log</code> method takes two arguments, <code>message</code> (a string)
and <code>message_loglevel</code> (a property of the <code>GnunetLogger</code> class with
the same name as the string log level).
</p>

<pre class="example">
&gt;&gt;&gt; l.log ("user clicked button", l.INFO)
INFO:ui:user clicked button
</pre>
</div>
</div>
</div>

<div id="outline-container-orgaadd9a7" class="outline-2">
<h2 id="orgaadd9a7"><span class="section-number-2">5</span> ‘payto’ URI parsing</h2>
<div class="outline-text-2" id="text-5">
<p>
The <code>PaytoParse</code> class has only one entry point, its constructor.
The argument is <code>payto_uri</code>, a string in the <i>payto URI scheme</i>
that has exactly two components in the <i>upath</i> portion.
See RFC 8905 (<a href="https://datatracker.ietf.org/doc/html/rfc8905">https://datatracker.ietf.org/doc/html/rfc8905</a>) for more info.
If parsing fails, the constructor throws a <code>PaytoFormatError</code> exception.
</p>

<p>
On successful parse, the object has the following properties:
</p>

<dl class="org-dl">
<dt><code>target</code></dt><dd>destination of the payment</dd>
<dt><code>bank</code></dt><dd>bank handling the payment</dd>
<dt><code>authority</code></dt><dd>payment type (e.g., <code>iban</code>)</dd>
<dt><code>message</code></dt><dd>short human-readable description of the payment</dd>
<dt><code>amount</code></dt><dd>in <code>CUR:X.Y</code> format (<a href="#org5d287dc">2.1</a>)</dd>
</dl>

<p>
Note that <code>amount</code> may be <code>None</code> if none was specified.
</p>

<pre class="example">
&gt;&gt;&gt; from taler.util.payto import PaytoParse

# from RFC 8905
&gt;&gt;&gt; uri = "payto://iban/DE75512108001245126199?amount=EUR:200.0&amp;message=hello"

&gt;&gt;&gt; p = PaytoParse (uri)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/home/ttn/build/GNU/T/taler-util/taler/util/payto.py", line 41, in __init__
    raise PaytoFormatError(f"Bad Payto URI: {payto_uri}")
taler.util.payto.PaytoFormatError: Bad Payto URI: payto://iban/DE75512108001245126199?amount=EUR:200.0&amp;message=hello
</pre>


<p>
This example shows that the <i>single-component</i> IBAN fails to parse
(even though that is a valid RFC 8905 ‘payto’ URI).
It's necessary to use the <i>two-component</i> IBAN.
</p>

<pre class="example">
&gt;&gt;&gt; uri = "payto://iban/SOGEDEFFXXX/DE75512108001245126199?amount=EUR:200.0&amp;message=hello"

&gt;&gt;&gt; p = PaytoParse (uri)

&gt;&gt;&gt; p.target
'DE75512108001245126199'

&gt;&gt;&gt; p.bank
'SOGEDEFFXXX'

&gt;&gt;&gt; p.authority
'iban'

&gt;&gt;&gt; p.message
'hello'

&gt;&gt;&gt; p.amount
Amount(currency='EUR', value=200, fraction=0)
</pre>
</div>
</div>

<div id="outline-container-orgdf9cef8" class="outline-2">
<h2 id="orgdf9cef8"><span class="section-number-2">6</span> class <code>TalerConfig</code></h2>
<div class="outline-text-2" id="text-6">
<p>
The <code>TalerConfig</code> class represents a <i>Taler configuration</i>, a set
of <i>sections</i> with <i>options</i> and associated <i>values</i> (basically,
a nested dictionary), and provides methods for initializing,
and accessing those values, keyed by section and option.
</p>

<p>
When a Taler configuration is written to a file (the usual case),
it follows the typical Windows INI format.
For more information, see the taler-config(5) manpage.
</p>
</div>

<div id="outline-container-org03f14e5" class="outline-3">
<h3 id="org03f14e5"><span class="section-number-3">6.1</span> reading</h3>
<div class="outline-text-3" id="text-6-1">
<p>
The standard way to construct a <code>TalerConfig</code> object is to start
with one of two initialization methods: <code>from_file</code> or <code>from_env</code>.
The former reads the configuration from a file, given its name.
If no name is provided (it is <code>None</code>), <code>from_file</code> first tries
to find <code>taler.conf</code> in two directories:
</p>

<ul class="org-ul">
<li>directory named by environment variable <code>XDG_CONFIG_HOME</code></li>

<li><code>$HOME/.config</code> (where <code>HOME</code> is the user's home directory)</li>
</ul>

<p>
The <code>from_env</code> initialization method first determines a filename
by consulting environment variable <code>TALER_CONFIG_FILE</code> and then
uses <code>from_file</code> on that.
</p>

<p>
Both initialization methods take keyword arg <code>load_defaults</code>
(default <code>True</code>) that directs the method to also call
the <code>load_defaults</code> method before reading the file.
</p>

<p>
The <code>load_defaults</code> method takes no arguments.
It looks in the canonical locations (directories) and
uses method <code>load_dir</code> on them.
Once it finds a specified dir, it stops searching.
The canonical locations are:
</p>

<ul class="org-ul">
<li>environment variable <code>TALER_BASE_CONFIG</code></li>

<li><p>
environment variable <code>TALER_PREFIX</code>, with any trailing component
<code>lib</code> discarded, and suffixed with <code>share/taler/config.d</code>
</p>

<p>
For example, if <code>TALER_PREFIX</code> is <code>/usr/local/lib</code>, then
<code>load_defaults</code> would look in <code>/usr/local/share/taler/config.d</code>.
The same would result if <code>TALER_PREFIX</code> were <code>/usr/local</code>
(the suffixing is unconditional).
</p></li>
</ul>

<p>
If <code>load_defaults</code> cannot find something to load it logs a warning
"no base directory found".
</p>

<p>
The <code>load_dir</code> method takes one argument <code>dirname</code>, and
uses <code>load_file</code> on all files that directory whose name ends
with <code>.conf</code>.
</p>

<p>
At its core, all file reading uses method <code>load_file</code>,
which takes one argument, the <code>filename</code> to read.
If <code>filename</code> cannot be found, <code>load_file</code> causes
the process to exit with exit value <code>3</code>.
</p>
</div>
</div>

<div id="outline-container-orgd929224" class="outline-3">
<h3 id="orgd929224"><span class="section-number-3">6.2</span> value types</h3>
<div class="outline-text-3" id="text-6-2">
<p>
There are three types of values in a Taler configuration: <code>int</code> (integer),
<code>string</code> and <code>filename</code>.
The <code>int</code> and <code>string</code> types are self-explanatory.
The <code>filename</code> type is a string that has certain constructs expanded:
</p>

<ul class="org-ul">
<li><code>${X}</code></li>
<li><code>${X:-Y}</code></li>
<li><code>$X</code></li>
</ul>

<p>
These mimic shell-style variable expansion.
In all these constructs, the value of <code>X</code> replaces the construct.
In the second one only, if the value of <code>X</code> is empty, use the
value of <code>Y</code> instead.
Also, the second type can be nested, i.e., <code>${X:-${Y:-Z}}</code>.
That is, if <code>X</code> is empty, try <code>Y</code>, and if <code>Y</code> is empty, try <code>Z</code>.
</p>

<p>
For example, <code>${TMPDIR:-/tmp}/taler-test</code> expands to <code>/var/tmp/taler-test</code>
if environment variable <code>TMPDIR</code> has value <code>/var/tmp</code>, otherwise
simply <code>/tmp/taler-test</code>.
</p>
</div>
</div>

<div id="outline-container-orgc4601e4" class="outline-3">
<h3 id="orgc4601e4"><span class="section-number-3">6.3</span> retrieving values</h3>
<div class="outline-text-3" id="text-6-3">
<p>
Once a Taler configuration is read, you can retrieve specific
values from it, or display the entire set to stdout.
</p>
</div>

<div id="outline-container-org76aedbb" class="outline-4">
<h4 id="org76aedbb"><span class="section-number-4">6.3.1</span> specific values</h4>
<div class="outline-text-4" id="text-6-3-1">
<p>
Each type <i>foo</i> has a <code>value_foo</code> method (e.g., <code>value_int</code> for integer).
The method takes two required arguments, the <code>section</code> and <code>option</code>,
both strings.
Case does not matter.
</p>

<p>
In addition to the required arguments, <code>value_string</code> accepts
the following keyword arguments:
</p>

<dl class="org-dl">
<dt><code>default</code></dt><dd>If the requested value is not found, return this
value instead.  Default is no default.  :-D</dd>

<dt><code>required</code></dt><dd>If the requested value is not found, print an error
message and cause the process to exit with exit value <code>1</code>.</dd>

<dt><code>warn</code></dt><dd>If the requested value is not found, log a warning.
If <code>default</code> is also given, return that, otherwise return <code>None</code>.</dd>
</dl>

<p>
(Both <code>value_int</code> and <code>value_filename</code> also accept these keyword
arguments, but they are ignored.)
</p>
</div>
</div>

<div id="outline-container-org1feca74" class="outline-4">
<h4 id="org1feca74"><span class="section-number-4">6.3.2</span> entire set</h4>
<div class="outline-text-4" id="text-6-3-2">
<p>
The <code>dump</code> method takes no arguments.
It displays to stdout each section and its options (and values)
in the format:
</p>

<pre class="example">
[section]
option = value # filename &amp; line number pair
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Taler Contributors</p>
<p class="date">Created: 2022-02-22 mar 07:37</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>