Dave Cinege Git Repo thesaurus / 55ca774
thesauruscfg_sample created. ThesaurusCfg: nested coercion methods fixed. Dave Cinege 2 years ago
2 changed file(s) with 208 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 """
1 thesauruscfg_sample.cfg
2 Copyright (c) 2013-2019 Dave Cinege
3 Licensed under the Apache License, Version 2.0 (the "License");
4
5 VERSION = 20191112
6
7 This is the sample configuration file and specification document
8 for Python ThesaurusCfg, which utilizes the Thesaurus mapping datatype.
9
10 ThesaurusCfg file format is generally very similar in structure to the INI
11 file format used by Python ConfigParser, with some major differences.
12
13 Multi line Comments (doc strings) are supported, using double or single
14 triple-quotes. Triple quotes should start at the begining of a line,
15 but may end anywhere.
16
17 ThesaurusCfg is designed to work with dot seperated (Keypath) configuration
18 trees (nested ThesaurusCfg objects). 'Sections' may be used as a
19 keypath prefix for easier human parsing.
20
21 For example:
22 a.b.c.v0 = 0
23 a.b.c.v1 = 0
24 a.b.c.v2 = 0
25
26 Can also be writen as:
27 [a.b.c]
28 v0 = 0
29 v1 = 0
30 v2 = 0
31
32 To reset the keypath prefix use empty brackets on a line:
33 []
34
35 Keypath's and Sections must start at the beginng of a line
36
37 Lines take the form of:
38 keypath(optional_coercion_method)=value
39
40 Arbitrary whitespace may exist between any tokens:
41 keypath ( optional_coercion_method ) = value
42
43 Value coercion methods are defined in ThesaurusCfg itself and
44 explained here.
45
46 Since the keypath is split on 'dots' it is a good idea to avoid dots in
47 key names. However if you must use dots (or other bad idea characters,
48 like whitespace) in a key name is it supported by quoting.
49
50 For example:
51 [lab.domain.'someplace.net']
52 admin.'joe doe'.phone = 867-5309
53 admin.'joe doe'.email = joe@doe.org
54
55 configparser._value_lstrip=False
56 The parser strips at most 1 whitespace charter to the right of the value
57 delimiter.
58 For example:
59 a = hello == 'hello'
60 a=hello == 'hello'
61 a = hello == ' hello'
62
63 configparser._value_rstrip_comment=True
64 Comments on the right side of single line keypath/value will be
65 stripped prior to the possible stripping of any whitespace.
66
67 a = hello # Lets make a equal hello
68
69 """
70 # Single line comment
71
72
73 # Simple key values using coercion methods
74 abc (static_int) = 123
75 a.bc (static_int) = 456
76 xyz (int) = 678
77 myitemlist (str_list) = "Item 1's" "'Item_2'" "Item 3" "Item_4"
78 mybool (str_to_bool) = yes
79
80 # Keypath and value
81 program.name = thesauruscfg_sample.py
82
83 # Multi-line keypath and value
84 program.description:
85 This program is to test the functionality of the ThesaurusCfg
86 Python module. We prefer ':\n' value delimiters for multi-line.
87
88 # Using a keypath prefix 'section'. 'program.name' and
89 # 'program.description' defined above will already be here.
90 [program]
91 version = 3.0
92 release = 20191112
93 author = Dave Cinege
94 licence = Apache 2.0
95
96 # Reset (clear) the keypath prefix 'section'. While you can do this,
97 # it's of course cleaner to keep the root level keys all together.
98 []
99 abc_2 (static_int) = 1234
100
101 [defaults.gateways]
102 table = 10
103 ifname = eth0
104 router (ipv4) = 192.168.0.1
105 dns (ipv4) = 9.9.9.9
106 inkbps (int) = 99000
107 outkbps (int) = 99000
108
109 [networks.nyc9234.gateways]
110 router = 100.64.1.1
111
112 [networks.mia00234.gateways]
113 ifname = eth2
114 router = 100.65.1.1
115
116 [networks.buda793.gateways]
117 router = 100.66.2.1
118
119
120 [hosts]
121 'mercury.mydom.org'.ip = 192.168.1.1
122 'saturn.mydom.org'.ip = 192.168.1.2
123 'venus.mydom.org'.ip = 192.168.1.3
124
125 [hosts.'earth.mydom.org']
126 ip = 192.168.1.10
127 router = 192.168.1.254
128 notes:
129 Primary host to access the universe.
130 Replaced keyboard 20170104
131
132
133
134 # NOTE: 2019112 NOT YET IMPLEMENTED
135 ## Sectional inheritance: defaults.gateways always merged first
136 #[networks.buda793.gateways:defaults.gateways]
137 #router = 100.66.2.1
0 ###
1 # Python Thesaurus Config Sample
2 #
3 # Copyright (c) 2019 Dave Cinege
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 ###
17 from __future__ import print_function
18
19 __VERSION__ = (2, 9, 0, 20191112)
20
21 import os
22 import sys
23
24 from thesauruscfg import thescfg
25
26 def main ():
27 cfg = thescfg()
28 cfg.read('thesauruscfg_sample.cfg')
29
30 print('cfg.print_tree()\n---')
31 cfg.print_tree()
32 print('---')
33
34 # mesh in the default gateways values (don't overwrite existing)
35 # FIX ME not yet copying the coercion methods
36 for t in cfg.networks.values():
37 t.gateways.mesh(cfg.defaults.gateways)
38
39 print('cfg.dump()\n---')
40 print(cfg.dump())
41 print('---')
42
43 # abc is using the static_int() coercion method. The method not only
44 # converts to and from an int, it will not allow further value assignment
45 # keeping it static.
46 cfg.abc = 0
47 cfg['abc'] = 0
48 if cfg.abc == 123:
49 print('abc: static value = 123 will not change.')
50 else:
51 print('abc: is broken!')
52
53 cfg.a.bc = 'aaa'
54 if cfg.a.bc == 456:
55 print('a.bc: static value = 456 will not change.')
56 else:
57 print('a.bc: is broken!')
58
59
60 try:
61 cfg.defaults.gateways.router = '1.1.1.666'
62 except ValueError:
63 print('cfg.defaults.gateways.router: That was a bad ipv4.')
64 else:
65 print('cfg.defaults.gateways.router: ok ipv4.')
66
67
68 if __name__ == '__main__':
69 main()