forked from infobyte/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.py
87 lines (64 loc) · 3.21 KB
/
diff.py
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
'''
Faraday Penetration Test IDE
Copyright (C) 2013 Infobyte LLC (http://www.infobytesec.com/)
See the file 'doc/LICENSE' for the license information
'''
class ModelObjectDiff(object):
def __init__(self, objLeft, objRight):
try:
if not getattr(objLeft, 'class_signature') == getattr(objRight, 'class_signature'):
raise Exception("Cannot compare objects of different signature. objLeft (%s) vs objRight (%s)"
% (objLeft.class_signature, objRight.class_signature))
except:
raise Exception("Cannot compare objects of different classes. objLeft (%s) vs objRight (%s)"
% (objLeft.__class__.__name__, objRight.__class__.__name__))
self.obj1, self.obj2 = objLeft, objRight
self.conflicting = []
self.conflicting.extend(self.getPropertiesDiff())
self.only_in_obj1 = {}
self.only_in_obj2 = {}
def existDiff(self):
return bool(self.conflicting) or bool(self.only_in_obj1) or bool(self.only_in_obj2)
def getPropertiesDiff(self):
prop_diff = {}
for attrname in self.obj1.publicattrsrefs().keys():
def info(attr_ref): return attr_ref() if callable(attr_ref) else attr_ref
prop1 = info(self.obj1.__getattribute__(self.obj1.publicattrsrefs().get(attrname)))
prop2 = info(self.obj2.__getattribute__(self.obj2.publicattrsrefs().get(attrname)))
if prop1 != prop2:
prop_diff[attrname] = (prop1, prop2)
return prop_diff
# def getDifferences(self, ObjDiff, getAllFunc, getById):
# """ Polymorphic method to get the differences between the list of objects on a ModelObject.
# Pass the ObjectDiff class, the unbound method to get all the objects and the one to get one by ID"""
# only_in_obj1 = [i for i in getAllFunc(self.obj1) if not i in getAllFunc(self.obj2)]
# only_in_obj2 = [i for i in getAllFunc(self.obj2) if not i in getAllFunc(self.obj1)]
# return (only_in_obj1, only_in_obj2)
# def getDifferencesIn(self, getAllFunc):
# """ Polymorphic method to get the differences between the list of objects on a ModelObject.
# Pass the ObjectDiff class, the unbound method to get all the objects and the one to get one by ID"""
# only_in_obj1 = [i for i in getAllFunc(self.obj1) if not i in getAllFunc(self.obj2)]
# only_in_obj2 = [i for i in getAllFunc(self.obj2) if not i in getAllFunc(self.obj1)]
# return only_in_obj1, only_in_obj2
class MergeStrategy(object):
@staticmethod
def solve(old, new):
raise NotImplementedError("This is an abstract class")
class MergeKeepNew(MergeStrategy):
@staticmethod
def solve(old, new):
return new
class MergeKeepOld(MergeStrategy):
@staticmethod
def solve(old, new):
return old
class MergeSolver(object):
def __init__(self, strategy):
if strategy == "new":
self.strategy = MergeKeepNew
elif strategy == "old":
self.strategy = MergeKeepOld
else:
raise Exception("Invalid strategy to resolve merges")
def solve(self, old, new):
return self.strategy.solve(old, new)