ATLAS Offline Software
Loading...
Searching...
No Matches
python.MetaDiff Namespace Reference

Functions

 summary (content)
 truncateDict (value)
 print_diff (parent_key, obj1, obj2, diff_format, filter_key, key_only)
 print_diff_type (parent_key, obj1, obj2, diff_format, filter_key, key_only)
 print_diff_dict_keys (parent_key, obj1, obj2, diff_format, filter_key, key_only)
 compare (obj1, obj2, parent_key=None, ordered=False, diff_format="simple", filter_key=None, key_only=False)
 compare_dicts (test, reference, ordered=False, diff_format="simple", filter_key=None, key_only=False)
 meta_diff (files, verbose=False, ordered=False, drop=None, mode="lite", meta_key_filter=None, file_type=None, promote=False, diff_format="simple", regex=False, key_only=False, ignore_trigger=False)

Detailed Description

The function in this module you should look to be using is meta_diff

Function Documentation

◆ compare()

python.MetaDiff.compare ( obj1,
obj2,
parent_key = None,
ordered = False,
diff_format = "simple",
filter_key = None,
key_only = False )
Caclulate difference between two objects

Keyword arguments:
obj1       -- first object in comparision
obj2       -- second object in comparision
parent_key -- the key of the objects in the parent, used in recursion
ordered    -- whether to check order of list content

Definition at line 198 of file MetaDiff.py.

198def compare(obj1, obj2, parent_key=None, ordered=False, diff_format="simple", filter_key=None, key_only=False):
199 """Caclulate difference between two objects
200
201 Keyword arguments:
202 obj1 -- first object in comparision
203 obj2 -- second object in comparision
204 parent_key -- the key of the objects in the parent, used in recursion
205 ordered -- whether to check order of list content
206 """
207 result = list()
208
209 if not ordered and isinstance(obj1, list):
210 obj1.sort()
211
212 if not ordered and isinstance(obj2, list):
213 obj2.sort()
214
215 if obj1 == obj2:
216 return result
217
218 if isinstance(obj1, type(obj2)):
219
220 if isinstance(obj1, dict):
221
222 if sorted(obj1.keys()) != sorted(obj2.keys()):
223 result += [
224 print_diff_dict_keys(parent_key, obj1, obj2, diff_format, filter_key, key_only)
225 ]
226 else:
227 for key in sorted(set(obj1.keys()) | set(obj2.keys())):
228 if parent_key:
229 child_key = "{}/{}".format(parent_key, key)
230 else:
231 child_key = key
232 result += compare(
233 obj1[key], obj2[key], child_key, ordered, diff_format, filter_key, key_only
234 )
235
236 else:
237 result += [print_diff(parent_key, obj1, obj2, diff_format, filter_key, key_only)]
238
239 else:
240 result += [print_diff_type(parent_key, obj1, obj2, diff_format, filter_key, key_only)]
241
242 return result
243
244
STL class.

◆ compare_dicts()

python.MetaDiff.compare_dicts ( test,
reference,
ordered = False,
diff_format = "simple",
filter_key = None,
key_only = False )
Show the differences between two dictionaries

Args:
    test          (dict): first object in comparision
    reference     (dict): second object in comparision
    ordered       (bool): whether to check order of list content
    diff_format (string): specify a format to display the difference in

Definition at line 245 of file MetaDiff.py.

245def compare_dicts(test, reference, ordered=False, diff_format="simple", filter_key = None, key_only = False):
246 """Show the differences between two dictionaries
247
248 Args:
249 test (dict): first object in comparision
250 reference (dict): second object in comparision
251 ordered (bool): whether to check order of list content
252 diff_format (string): specify a format to display the difference in
253 """
254 result = list()
255
256 keys = set(test.keys()).union(reference.keys())
257 for key in keys:
258
259 try:
260 val1 = test[key]
261 except KeyError:
262 val1 = None
263 try:
264 val2 = reference[key]
265 except KeyError:
266 val2 = None
267
268 result += compare(
269 obj1=val1,
270 obj2=val2,
271 parent_key=key,
272 ordered=ordered,
273 diff_format=diff_format,
274 filter_key=filter_key,
275 key_only=key_only
276 )
277 return result
278

◆ meta_diff()

python.MetaDiff.meta_diff ( files,
verbose = False,
ordered = False,
drop = None,
mode = "lite",
meta_key_filter = None,
file_type = None,
promote = False,
diff_format = "simple",
regex = False,
key_only = False,
ignore_trigger = False )
Compare the in-file metadata in two given files. Uses PyUtils.MetaReader
to obtain file content. Generates list of string that show difference.
Returns empty list if no difference is found

Keyword arguments:
files   -- Names of two files to compare
verbose -- toggle to get debug information
ordered -- whether to check order of lists in the metadata
drop    -- keys to drop from metadata retrieved by MetaReader
mode    -- MetaReader argument setting amount of content (default 'lite').
           Allowed values are: tiny, lite, peeker, and full
meta_key_filter -- MetaReader argument selecting keys to retrieve (default
                   get all)
file_type    -- Type of files, POOL or BS (default: auto-configure)
promote      -- MetaReader argument (default: False)
diff_format  -- Return 'simple' or 'diff' style string (default: 'simple')
regex        -- Use regex for the drop filter (default: False)
key_only     -- Show only the keys instead of their value (default: False)

Definition at line 279 of file MetaDiff.py.

292):
293 """
294 Compare the in-file metadata in two given files. Uses PyUtils.MetaReader
295 to obtain file content. Generates list of string that show difference.
296 Returns empty list if no difference is found
297
298 Keyword arguments:
299 files -- Names of two files to compare
300 verbose -- toggle to get debug information
301 ordered -- whether to check order of lists in the metadata
302 drop -- keys to drop from metadata retrieved by MetaReader
303 mode -- MetaReader argument setting amount of content (default 'lite').
304 Allowed values are: tiny, lite, peeker, and full
305 meta_key_filter -- MetaReader argument selecting keys to retrieve (default
306 get all)
307 file_type -- Type of files, POOL or BS (default: auto-configure)
308 promote -- MetaReader argument (default: False)
309 diff_format -- Return 'simple' or 'diff' style string (default: 'simple')
310 regex -- Use regex for the drop filter (default: False)
311 key_only -- Show only the keys instead of their value (default: False)
312 """
313 if len(files) != 2:
314 raise ValueError("Wrong number of files passes, need two")
315
316 reader_msg = logging.getLogger("MetaReader")
317 reader_msg.setLevel(logging.INFO if verbose else logging.WARNING)
318
319 msg = logging.getLogger("MetaDiff")
320 msg.setLevel(logging.DEBUG if verbose else logging.INFO)
321
322 msg.debug("Reading from %s and %s", files[0], files[1])
323
324 metadata = read_metadata(
325 files,
326 file_type,
327 mode=mode,
328 meta_key_filter=meta_key_filter,
329 promote=promote,
330 )
331
332 if drop is not None and regex:
333 for i in range(len(drop)):
334 drop[i] = re.compile( drop[i] )
335
336 def filter_key(key):
337 key_str = str(key) # force conversion to plain Python string
338
339 if drop is not None:
340 for drop_key in drop:
341 if not regex:
342 if key_str.startswith(drop_key):
343 return False
344 else:
345 if drop_key.match(key_str):
346 return False
347
348 if ignore_trigger:
349 for trigger_key in trigger_keys:
350 if key_str.startswith(trigger_key):
351 return False
352
353 return True
354
355 result = compare_dicts(
356 metadata[files[0]],
357 metadata[files[1]],
358 ordered=ordered,
359 diff_format=diff_format,
360 filter_key=filter_key,
361 key_only=key_only
362 )
363
364 if not result:
365 msg.info("No differences found")
366
367 return list(sorted([r for r in result if r is not None ]))

◆ print_diff()

python.MetaDiff.print_diff ( parent_key,
obj1,
obj2,
diff_format,
filter_key,
key_only )
build comparison string for two non-dictionary objects

Definition at line 54 of file MetaDiff.py.

54def print_diff(parent_key, obj1, obj2, diff_format, filter_key, key_only):
55 """build comparison string for two non-dictionary objects"""
56
57 if filter_key is not None and filter_key(parent_key) is False:
58 # skip this key
59 return
60
61 result = "\n"
62
63 if diff_format == "simple":
64 if not obj1:
65 result += "{} has been inserted".format(parent_key)
66 elif not obj2:
67 result += "{} has been deleted".format(parent_key)
68 else:
69 if key_only:
70 result += "{} has changed".format( parent_key )
71 else:
72 result += "{} has changed from '{}' to '{}'".format(
73 parent_key, obj1, obj2
74 )
75 result += "\n"
76 else:
77
78 if parent_key is not None:
79
80 if key_only:
81 result += "{}".format(parent_key)
82 else:
83 result += "{}:\n".format(parent_key)
84 try:
85 overlap = set(obj1).intersection(set(obj2))
86 for item in overlap:
87 obj1.remove(item)
88 obj2.remove(item)
89 except (AttributeError, TypeError,):
90 pass
91 result += """\
92 < {}
93 ----------
94 > {}
95 """.format(
96 summary(obj1), summary(obj2)
97 )
98
99 return result
100
101
std::vector< std::string > intersection(std::vector< std::string > &v1, std::vector< std::string > &v2)

◆ print_diff_dict_keys()

python.MetaDiff.print_diff_dict_keys ( parent_key,
obj1,
obj2,
diff_format,
filter_key,
key_only )
build diff style string for dictionary objects

Definition at line 146 of file MetaDiff.py.

146def print_diff_dict_keys(parent_key, obj1, obj2, diff_format, filter_key, key_only):
147 """build diff style string for dictionary objects"""
148
149 if filter_key is not None and filter_key(parent_key) is False:
150 # skip this key
151 return
152
153 result = '\n'
154 if diff_format != 'simple':
155 shared_keys = set(obj1.keys()).intersection(obj2.keys())
156 for k in shared_keys:
157 if obj1[k] == obj2[k]:
158 try:
159 obj1.pop(k, None)
160 obj2.pop(k, None)
161 except TypeError:
162 pass
163
164 if diff_format == 'simple':
165 if obj1 is None:
166 result += "{} has been inserted".format(parent_key)
167 elif obj2 is None:
168 result += "{} has been deleted".format(parent_key)
169 else:
170
171 if key_only:
172 result += "{} has changed".format(parent_key)
173 else:
174 value1 = truncateDict(obj1)
175 value2 = truncateDict(obj2)
176 result += "{} has changed from '{}' to '{}'".format(
177 parent_key, value1, value2
178 )
179 else:
180 if parent_key is not None:
181
182
183 if key_only:
184 result += "{}".format(parent_key)
185 else:
186 result += "{}:\n".format(parent_key)
187 result += """\
188 < {}
189 ----------
190 > {}
191 """.format( summary(obj1), summary(obj2) )
192
193 result += "\n"
194
195 return result
196
197

◆ print_diff_type()

python.MetaDiff.print_diff_type ( parent_key,
obj1,
obj2,
diff_format,
filter_key,
key_only )
Build diff string for objet of different type

Definition at line 102 of file MetaDiff.py.

102def print_diff_type(parent_key, obj1, obj2, diff_format, filter_key, key_only):
103 """Build diff string for objet of different type"""
104
105 if filter_key is not None and filter_key(parent_key) is False:
106 # skip this key
107 return
108
109 result = "\n"
110
111 if diff_format == "simple":
112 if obj1 is None:
113 result += "{} has been inserted".format(parent_key)
114 elif obj2 is None:
115 result += "{} has been deleted".format(parent_key)
116 else:
117 if key_only:
118 result += (
119 "{} has changed changed type from {} to {}"
120 ).format(parent_key, type(obj1), type(obj2))
121 else:
122 result += (
123 "{} has changed changed type from {} (value: '{}') to "
124 "{} (value: '{}')"
125 ).format(parent_key,
126 type(obj1), obj1,
127 type(obj2), obj2)
128 result += "\n"
129 else:
130 if parent_key is not None:
131 if key_only:
132 result += "{}".format(parent_key)
133 else:
134 result += "{}:\n".format(parent_key)
135 result += """\
136 < {} (type: {})
137 ----------
138 > {} (type: {})
139 """.format(
140 summary(obj1), type(obj1), summary(obj2), type(obj2)
141 )
142
143 return result
144
145

◆ summary()

python.MetaDiff.summary ( content)
Create a summary string for an object

Definition at line 12 of file MetaDiff.py.

12def summary(content):
13 """Create a summary string for an object"""
14 if isinstance(content, str):
15 return content
16
17 try:
18 try:
19 working_copy = content.items()
20 except AttributeError:
21 working_copy = content
22 result = ''
23 for key, value in working_copy:
24 result += "{}: {}, ".format(key, summary(value))
25 return result
26 except (TypeError, ValueError,):
27 pass
28
29 try:
30 if len(content) < 3:
31 return str(content)
32 return "[{}, {}, ..., {}]".format(
33 summary(content[0]), summary(content[1]), summary(content[-1])
34 )
35 except TypeError:
36 pass
37
38 return str(content)
39
40

◆ truncateDict()

python.MetaDiff.truncateDict ( value)
Create truncted string replaceing dicts with {...}

Definition at line 41 of file MetaDiff.py.

41def truncateDict(value):
42 """Create truncted string replaceing dicts with {...}"""
43 return ', '.join(
44 [
45 '{}: {}'.format(
46 k,
47 '{...}' if isinstance(v, dict) else v
48 )
49 for k, v in sorted(value.items())
50 ]
51 )
52
53