Skip to content

Commit b0dc286

Browse files
committed
Modularize license and copyright diff methods #84
Signed-off-by: John M. Horan <johnmhoran@gmail.com>
1 parent 76b88dc commit b0dc286

3 files changed

Lines changed: 140 additions & 108 deletions

File tree

src/deltacode/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def license_diff(self):
204204
])
205205

206206
for delta in self.deltas:
207-
utils.determine_license_diff(delta, unique_categories)
207+
utils.update_from_license_info(delta, unique_categories)
208208

209209
def copyright_diff(self):
210210
"""
@@ -215,7 +215,7 @@ def copyright_diff(self):
215215
attribute -- if there has been a copyright change.
216216
"""
217217
for delta in self.deltas:
218-
utils.determine_copyright_diff(delta)
218+
utils.update_from_copyright_info(delta)
219219

220220
def index_deltas(self, index_key='path', delta_list=[]):
221221
"""
@@ -277,16 +277,16 @@ def is_unmodified(self):
277277
other than 'unmodified' and return True if all but 'unmodified' are
278278
ruled out.
279279
"""
280-
if (self.old_file and self.new_file and
281-
self.old_file.sha1 == self.new_file.sha1 and
282-
self.old_file.path == self.new_file.path):
280+
if (self.new_file and self.old_file and
281+
self.new_file.sha1 == self.old_file.sha1 and
282+
self.new_file.path == self.old_file.path):
283283
return True
284284

285285
def is_added(self):
286286
"""
287287
Identify a Delta object reflecting the addition of a File.
288288
"""
289-
if not self.old_file and self.new_file:
289+
if self.new_file and not self.old_file:
290290
return True
291291

292292
def to_dict(self):

src/deltacode/utils.py

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,43 @@
3333
from commoncode import paths
3434

3535

36-
def determine_license_diff(delta, unique_categories):
36+
def update_from_license_info(delta, unique_categories):
3737
"""
3838
Increase an 'added' or 'modified' Delta object's 'score' attribute and add
3939
one or more appropriate categories to its 'factors' attribute if there has
4040
been a license change and depending on the nature of that change.
4141
"""
4242
if delta.is_added():
43-
new_licenses = delta.new_file.licenses or []
44-
new_categories = set(license.category for license in new_licenses)
45-
46-
if delta.new_file.has_licenses():
47-
delta.update(20, 'license info added')
48-
# no license ==> 'Copyleft Limited'or higher
49-
for category in new_categories:
50-
if category in unique_categories:
51-
delta.update(20, category.lower() + ' added')
52-
return
53-
54-
if not delta.is_modified():
43+
update_added_from_license_info(delta, unique_categories)
44+
45+
if delta.is_modified():
46+
update_modified_from_license_info(delta, unique_categories)
47+
48+
49+
def update_added_from_license_info(delta, unique_categories):
50+
"""
51+
Increase an 'added' Delta object's 'score' attribute and add
52+
one or more categories to its 'factors' attribute if there has
53+
been a license change.
54+
"""
55+
new_licenses = delta.new_file.licenses or []
56+
new_categories = set(license.category for license in new_licenses)
57+
58+
if delta.new_file.has_licenses():
59+
delta.update(20, 'license info added')
60+
# no license ==> 'Copyleft Limited'or higher
61+
for category in new_categories:
62+
if category in unique_categories:
63+
delta.update(20, category.lower() + ' added')
5564
return
5665

66+
67+
def update_modified_from_license_info(delta, unique_categories):
68+
"""
69+
Increase a 'modified' Delta object's 'score' attribute and add
70+
one or more categories to its 'factors' attribute if there has
71+
been a license change.
72+
"""
5773
if not delta.new_file.has_licenses() and delta.old_file.has_licenses():
5874
delta.update(15, 'license info removed')
5975
return
@@ -83,20 +99,36 @@ def determine_license_diff(delta, unique_categories):
8399
delta.update(20, category.lower() + ' added')
84100

85101

86-
def determine_copyright_diff(delta):
102+
def update_from_copyright_info(delta):
87103
"""
88104
Increase an 'added' or 'modified' Delta object's 'score' attribute and add
89105
one or more appropriate categories to its 'factors' attribute if there has
90106
been a copyright change and depending on the nature of that change.
91107
"""
92108
if delta.is_added():
93-
if delta.new_file.has_copyrights():
94-
delta.update(10, 'copyright info added')
95-
return
109+
update_added_from_copyright_info(delta)
110+
111+
if delta.is_modified():
112+
update_modified_from_copyright_info(delta)
113+
96114

97-
if not delta.is_modified():
115+
def update_added_from_copyright_info(delta):
116+
"""
117+
Increase an 'added' Delta object's 'score' attribute and add
118+
one or more categories to its 'factors' attribute if there has
119+
been a copyright change.
120+
"""
121+
if delta.new_file.has_copyrights():
122+
delta.update(10, 'copyright info added')
98123
return
99124

125+
126+
def update_modified_from_copyright_info(delta):
127+
"""
128+
Increase a 'modified' Delta object's 'score' attribute and add
129+
one or more categories to its 'factors' attribute if there has
130+
been a copyright change.
131+
"""
100132
new_copyrights = delta.new_file.copyrights or []
101133
old_copyrights = delta.old_file.copyrights or []
102134

0 commit comments

Comments
 (0)