@@ -66,9 +66,14 @@ class ConfigTreeError(Exception):
66
66
67
67
68
68
class ConfigTree (object ):
69
- def __init__ (self , config_string = None , address = None , libpath = LIBPATH ):
70
- if config_string is None and address is None :
71
- raise TypeError ("ConfigTree() requires one of 'config_string' or 'address'" )
69
+ def __init__ (
70
+ self , config_string = None , address = None , internal = None , libpath = LIBPATH
71
+ ):
72
+ if config_string is None and address is None and internal is None :
73
+ raise TypeError (
74
+ "ConfigTree() requires one of 'config_string', 'address', or 'internal'"
75
+ )
76
+
72
77
self .__config = None
73
78
self .__lib = cdll .LoadLibrary (libpath )
74
79
@@ -89,6 +94,13 @@ def __init__(self, config_string=None, address=None, libpath=LIBPATH):
89
94
self .__to_commands .argtypes = [c_void_p , c_char_p ]
90
95
self .__to_commands .restype = c_char_p
91
96
97
+ self .__read_internal = self .__lib .read_internal
98
+ self .__read_internal .argtypes = [c_char_p ]
99
+ self .__read_internal .restype = c_void_p
100
+
101
+ self .__write_internal = self .__lib .write_internal
102
+ self .__write_internal .argtypes = [c_void_p , c_char_p ]
103
+
92
104
self .__to_json = self .__lib .to_json
93
105
self .__to_json .argtypes = [c_void_p ]
94
106
self .__to_json .restype = c_char_p
@@ -168,7 +180,21 @@ def __init__(self, config_string=None, address=None, libpath=LIBPATH):
168
180
self .__destroy = self .__lib .destroy
169
181
self .__destroy .argtypes = [c_void_p ]
170
182
171
- if address is None :
183
+ self .__equal = self .__lib .equal
184
+ self .__equal .argtypes = [c_void_p , c_void_p ]
185
+ self .__equal .restype = c_bool
186
+
187
+ if address is not None :
188
+ self .__config = address
189
+ self .__version = ''
190
+ elif internal is not None :
191
+ config = self .__read_internal (internal .encode ())
192
+ if config is None :
193
+ msg = self .__get_error ().decode ()
194
+ raise ValueError ('Failed to read internal rep: {0}' .format (msg ))
195
+ else :
196
+ self .__config = config
197
+ elif config_string is not None :
172
198
config_section , version_section = extract_version (config_string )
173
199
config_section = escape_backslash (config_section )
174
200
config = self .__from_string (config_section .encode ())
@@ -179,8 +205,9 @@ def __init__(self, config_string=None, address=None, libpath=LIBPATH):
179
205
self .__config = config
180
206
self .__version = version_section
181
207
else :
182
- self .__config = address
183
- self .__version = ''
208
+ raise TypeError (
209
+ "ConfigTree() requires one of 'config_string', 'address', or 'internal'"
210
+ )
184
211
185
212
self .__migration = os .environ .get ('VYOS_MIGRATION' )
186
213
if self .__migration :
@@ -190,6 +217,11 @@ def __del__(self):
190
217
if self .__config is not None :
191
218
self .__destroy (self .__config )
192
219
220
+ def __eq__ (self , other ):
221
+ if isinstance (other , ConfigTree ):
222
+ return self .__equal (self ._get_config (), other ._get_config ())
223
+ return False
224
+
193
225
def __str__ (self ):
194
226
return self .to_string ()
195
227
@@ -199,6 +231,9 @@ def _get_config(self):
199
231
def get_version_string (self ):
200
232
return self .__version
201
233
234
+ def write_cache (self , file_name ):
235
+ self .__write_internal (self ._get_config (), file_name )
236
+
202
237
def to_string (self , ordered_values = False , no_version = False ):
203
238
config_string = self .__to_string (self .__config , ordered_values ).decode ()
204
239
config_string = unescape_backslash (config_string )
@@ -488,6 +523,35 @@ def mask_inclusive(left, right, libpath=LIBPATH):
488
523
return tree
489
524
490
525
526
+ def show_commit_data (active_tree , proposed_tree , libpath = LIBPATH ):
527
+ if not (
528
+ isinstance (active_tree , ConfigTree ) and isinstance (proposed_tree , ConfigTree )
529
+ ):
530
+ raise TypeError ('Arguments must be instances of ConfigTree' )
531
+
532
+ __lib = cdll .LoadLibrary (libpath )
533
+ __show_commit_data = __lib .show_commit_data
534
+ __show_commit_data .argtypes = [c_void_p , c_void_p ]
535
+ __show_commit_data .restype = c_char_p
536
+
537
+ res = __show_commit_data (active_tree ._get_config (), proposed_tree ._get_config ())
538
+
539
+ return res .decode ()
540
+
541
+
542
+ def test_commit (active_tree , proposed_tree , libpath = LIBPATH ):
543
+ if not (
544
+ isinstance (active_tree , ConfigTree ) and isinstance (proposed_tree , ConfigTree )
545
+ ):
546
+ raise TypeError ('Arguments must be instances of ConfigTree' )
547
+
548
+ __lib = cdll .LoadLibrary (libpath )
549
+ __test_commit = __lib .test_commit
550
+ __test_commit .argtypes = [c_void_p , c_void_p ]
551
+
552
+ __test_commit (active_tree ._get_config (), proposed_tree ._get_config ())
553
+
554
+
491
555
def reference_tree_to_json (from_dir , to_file , internal_cache = '' , libpath = LIBPATH ):
492
556
try :
493
557
__lib = cdll .LoadLibrary (libpath )
0 commit comments