Dave Cinege Git Repo thesaurus / de1fc97
README.rst in progress Dave Cinege 3 years ago
3 changed file(s) with 181 addition(s) and 202 deletion(s). Raw diff Collapse all Expand all
+0
-99
README less more
0 EDITTING IN PROGRESS - NOT COHERENT
1
2 ####################################################
3
4
5 Thesaurus is a mapping data type with key recursion and attribute
6 aliasing. It is a subclass of dict() and compatible as a dictionary
7 replacement baring where key path recursion may take place. In
8 that regard Thesaurus is designed to .....
9
10
11 Thesaurus prefers to be imported and called as thes() in the
12 same way you would use dict(). Thesaurus likes to think of itself
13 as a Python data type primative that should be used along side of
14 dict(), similar to the relationship between list() and tuple();
15 they are overlappingly similar but serve different purposes
16 and diverge to incompatibilities. Thesaurus is currently
17 content with not having it's own data type tokens. Hopefully
18 so are you.
19
20
21 Telegram Discussion Group
22 https://t.me/PythonThesaurus
23
24 Thesaurus works with Python 2.6 to 3.8+.
25
26
27 ThesaurusExtended
28 ThesaurusCfg
29
30 ThesaurusExtended is a subclass of Thesaurus providing additional
31 usablity methods.
32
33 ThesaurusCfg is a subclass of ThesaurusExtended providing a configuration file
34 parser and per key data cohersion methods.
35
36
37 What is attribute alaising?
38 Attribute alaising refering to redirecting the methods __getattr__()
39 and__setattr__() to __getitem__() and __setitem__() respectivly.
40
41 The effect is to syntatically reference keys like attributes.
42
43 For example:
44 t['k']
45 t.k
46 are treated the same.
47
48 Thesaurus also provides a special alias
49
50
51 What is key recursion?
52
53
54 G.gws.lv._1.gw
55
56
57 For example:
58 t = thes()
59 t.set_path('k._k.__k.___k', ','Hello')
60 t.k._k.__k.___k
61 t['k']['_k']['__k']['___k']
62
63 Becasue of the nature pof Thesaurus, keys shodul be of trype str()
64
65
66 , difference being the latter will recurse or
67 'walk the key directory tree' until
68
69
70 What is a key path?
71 A key path is a string with path elements seperated by dots.
72 t['k']
73
74
75
76 Thesaurus
77
78 Nested dictionarys which I refert to as dictionary trees (aka nested dictionaries.)
79
80 DICTIONARY DIRECTORYS
81
82 from thesaurus import thes
83 from thesaurus import thesext as thes
84
85
86 Thesaurus is a pure dictionary subclass which allows calling keys as
87 if they are class attributes and will search through nested objects
88 recursively when __getitem__ is called.
89
90 You will notice that the code is very compact. However I have found that
91 this has completely changed the way I program in Python. I've re-written some
92 exiting programs using Thesaurus, and often realized 15-30% code reduction.
93 Additionally I find the new code much easier to read.
94
95 If you find yourself programing with nested dictionaries often, fighting to
96 generate output or command lines for external programs, or wish you had
97 a dictionary that could act (sort of) like a class, Thesaurus may be for you.
98
0 This is Python Thesaurus and ThesaurusCfg
1 =========================================
2 Copyright (c) 2012-2019 Dave Cinege. All rights reserved.
3
4 See the end of this file for further copyright and license information
5
6 .. contents::
7
8 Quick start
9 ------------
10 $ python thesauruscfg_sample.py
11
12 Then review thesauruscfg_sample.py and thesauruscfg_sample.cfg
13 and you will get an idea what Thesaurus and ThesaurusCfg
14 are all about.
15
16
17 General Information
18 -------------------
19
20 - Source code: https://git.cinege.com//thesaurus
21 - Telegram Group: https://t.me/PythonThesaurus
22
23
24 At the momement there is no email list, wiki, etc.
25
26 About
27 ------
28 Thesaurus is a mapping data type with key recursion and attribute
29 aliasing. It is a subclass of dict() and compatible as a dictionary
30 replacement baring where key path recursion may take place.
31
32 Thesaurus prefers to be imported and called as thes() in the
33 same way you would use dict(). Thesaurus likes to think of itself
34 as a Python data type primative that should be used along side of
35 dict(), similar to the relationship between list() and tuple();
36 they are overlappingly similar but serve different purposes
37 and diverge to incompatibilities. Thesaurus is currently
38 content with not having it's own data type tokens. Hopefully
39 so are you.
40
41 ThesaurusExtended is a subclass of Thesaurus providing additional
42 usablity methods.
43
44 ThesaurusCfg is a subclass of ThesaurusExtended providing a configuration
45 file parser and per key data coercion methods.
46
47 The Thesaurus family works with Python 2.6+ to 3.8+.
48
49
50 Currect State of Code - 2019-11-13
51 -----------------------------------
52 Thesaurus first came about in December 2012. It has gone through a total of
53 maybe 6 complete re-writes. In mid 2019 I think I finally got it right
54 with the implemintation of Keypath's and also the creation of ThesaurusCfg,
55 something I've been wishing I've had to use for myself for at least the
56 last 4 years.
57
58 The current version you will find here has recently had the recent addition
59 of Keypaths and while it is quite usable might still have a few bugs to
60 iron out.
61
62 Additonally parts should be cleaned and reimplemented now that Keypath's
63 have been implemented. If you are reviewing the code, this will likely
64 stand out. With that said, please remember that at this stage few things
65 have been done by accident. There are certainly section that can be cleaned
66 for readablity, however they are this way, on purpose, for performance.
67
68 ThesaurusCfg is quite new. It works. I have a version of it frozen in
69 production code. But it's not had the maturity of Thesaurus in practise
70 and requires a more thorough re-write.
71
72 I'm putting this code out now to gain feedback to finalize for a proper
73 release. As such, both modules are very subject to change at this time.
74 Specifically I have the following decisions to make -
75
76 Finalize name conventions. Use set_path() or setpath()?
77
78 Finalize specialtiy method names, Are merge(), mesh() and
79 screen() good names that make sense? I am struggling with the
80 ThesaurusExtended method names myself.
81
82 Resolve Thesaurus's schizoprenia. IE
83 .. code:: python
84 v = t['a.b.c'] # This recurses
85 t['a.b.c'] = v # This does not. Use set_path()
86
87 I want to be comfortable this feels nature to others, or make
88 changes.
89
90 Review how I do recursive copy/deepcopys.
91
92 Decide how to properly handle copying ThesaurusCfg coercion methods.
93
94
95
96
97
98
99
100
101
102 BELOW IS OLD/IN PROGRESS - NOT COHERENT
103
104 ####################################################
105
106
107 What is attribute alaising?
108 Attribute alaising refering to redirecting the methods __getattr__()
109 and__setattr__() to __getitem__() and __setitem__() respectivly.
110
111 The effect is to syntatically reference keys like attributes.
112
113 For example:
114 .. code:: python
115 t['k']
116 t.k
117
118 are treated the same.
119
120 Thesaurus also provides a special alias
121
122
123 What is key recursion?
124
125
126 G.gws.lv._1.gw
127
128
129 For example:
130 .. code:: python
131 t = thes()
132 t.set_path('k._k.__k.___k', ','Hello')
133 t['k']['_k']['__k']['___k']
134 t.k._k.__k.___k
135
136 Becasue of the nature pof Thesaurus, keys shodul be of trype str()
137
138
139 , difference being the latter will recurse or
140 'walk the key directory tree' until
141
142
143 What is a keypath?
144 A keypath is a
145 It may be represented by a string with path elements seperated by dots.
146 t['k']
147
148
149
150 Thesaurus
151
152 Nested dictionarys which I refer to as dictionary trees (aka nested dictionaries.)
153
154 DICTIONARY DIRECTORYS
155
156 from thesaurus import thes
157 from thesaurus import thesext as thes
158
159 # OLD README
160 Thesaurus is a pure dictionary subclass which allows calling keys as
161 if they are class attributes and will search through nested objects
162 recursively when __getitem__ is called.
163
164 You will notice that the code is very compact. However I have found that
165 this has completely changed the way I program in Python. I've re-written some
166 exiting programs using Thesaurus, and often realized 15-30% code reduction.
167 Additionally I find the new code much easier to read.
168
169 If you find yourself programing with nested dictionaries often, fighting to
170 generate output or command lines for external programs, or wish you had
171 a dictionary that could act (sort of) like a class, Thesaurus may be for you.
172
173
174 Copyright and License Information
175 ---------------------------------
176
177 Copyright (c) 2012 - 2019 Dave Cinege. All rights reserved.
178
179 See the file "LICENSE" for information on the history of this software,
180 terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES.
+0
-103
thesauruscfg_test.cfg less more
0 """
1 Multi line Comments (doc strings)
2 """
3 # Single line comment
4
5 abc (static_int) = 123
6 xyz (int) = 456
7 myvarname (str_list) = "Item 1's" "'Item_2'" "Item 3" "Item_4"
8 mybool (str_to_bool) = yes
9
10
11 [defaults.gateways]
12 table = = 10
13 gatewayifname = eth0
14 gatewayrouter (ipv4) = 192.168.0.1
15 gatewaydns (ipv4) = 9.9.9.9
16 inkbps (int) = 99000
17 outkbps (int) = 99000
18 url =
19
20 # Sectional inheritance: defaults.gateways always merged first
21 [gateways:defaults.gateways]
22 TEST_10.gatewaydns =
23 TEST_10.gwsname =
24 TEST_10.table = 129
25 TEST_10.gatewayrouter = 10.10.10.1
26 LAN.gatewaydns =
27 LAN.gwsname =
28 LAN.table = 130
29 LAN.gatewayrouter = 192.168.9.1
30 LAN_US.gatewaydns =
31 LAN_US.gwsname =
32 LAN_US.table = 131
33 LAN_US.gatewayrouter = 192.168.9.2
34
35 #Reset section
36 []
37 opt.user = openvpn-noc
38 opt.ip_cmd = /bin/ip
39 opt.conntrack_cmd = /bin/conntrack
40
41 opt.hosts = saturn jupiter
42 opt.names = vpn0
43
44 saturn.ip = 1.42.1.15
45 saturn.mng_ip = 172.27.1.1
46 saturn.protos = tcp udp
47
48 jupiter.ip = 1.42.1.45
49 jupiter.mng_ip = 172.27.2.1
50 jupiter.protos = tcp udp
51
52 tcp.server = tcp-server
53 udp.server = udp
54
55
56 bld0.hosts.0.ip = 172.27.1.1
57 bld0.servers.0.ip = 172.27.2.1
58 bld1.lab.servers.0.ip = 172.27.3.1
59 bld1.lab.servers.1.ip = 172.27.4.1
60 bld1.lab.servers.2.ip = 172.27.5.1
61
62 bld2.office.0.servers.0.ip = 172.27.6.1
63 bld2.office.0.servers.1.ip = 172.27.7.1
64 bld2.office.0.servers.2.ip = 172.27.8.1
65
66
67 [vpn]
68 vpn0.name = vpn0
69 vpn0.port = 10708
70 vpn0.ifconfig = 100.64.0.1 100.64.0.2
71 vpn0.route = 100.64.0.0 255.255.255.0
72 vpn0.route_table = 8
73 vpn0.client_ipstart = 100.64.0.
74
75 vpn0.tcp.tun = 7
76 vpn0.tcp.mng_port = 5700
77
78 vpn0.udp.tun = 8
79 vpn0.udp.mng_port = 5800
80
81 #vpn0.udp.newthes
82
83 cmd.client_connect_config:
84 push "route 172.20.0.0 255.255.255.0"
85 push "dhcp-option DNS 10.27.1.1"
86 push "dhcp-option DNS 10.27.1.2"
87 push "redirect-private"
88
89 cmd.client_connect:
90 {cfg.opt.ip_cmd} route replace {client.ip} dev {ovpn.dev} table {ovpn.route_table}
91 {cfg.opt.conntrack_cmd} -D --src {client.ip}
92 {cfg.opt.conntrack_cmd} -D --dst {client.ip}
93
94 cmd.client_disconnect:
95 {cfg.opt.ip_cmd} route delete {ovpn.ifconfig_pool_remote_ip} dev {ovpn.dev} table {ovpn.route_table}
96 {cfg.opt.conntrack_cmd} -D --src {ovpn.ifconfig_pool_remote_ip}
97 {cfg.opt.conntrack_cmd} -D --dst {ovpn.ifconfig_pool_remote_ip}
98
99 #Reset section
100 []
101 aaaa = 123
102 bbbb = 456