Skip to content

Commit 33ee25f

Browse files
RISHII-BHARADHWAJWhite Devil
authored andcommitted
Organized and cleaned up Code
Signed-off-by: White Devil <[email protected]>
1 parent 0806554 commit 33ee25f

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

src/attributecode/model.py

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,26 @@
3737

3838
from license_expression import Licensing
3939
from packageurl import PackageURL
40-
41-
from attributecode import __version__
42-
from attributecode import CRITICAL
43-
from attributecode import ERROR
44-
from attributecode import INFO
45-
from attributecode import WARNING
46-
from attributecode import api
47-
from attributecode import Error
48-
from attributecode import saneyaml
49-
from attributecode import gen
50-
from attributecode import util
51-
from attributecode.transform import write_excel
52-
from attributecode.util import add_unc
53-
from attributecode.util import boolean_fields
54-
from attributecode.util import copy_license_notice_files
55-
from attributecode.util import copy_file
56-
from attributecode.util import csv
57-
from attributecode.util import file_fields
58-
from attributecode.util import filter_errors
59-
from attributecode.util import get_spdx_key_and_lic_key_from_licdb
60-
from attributecode.util import is_valid_name
61-
from attributecode.util import on_windows
62-
from attributecode.util import norm
63-
from attributecode.util import replace_tab_with_spaces
64-
from attributecode.util import wrap_boolean_value
65-
from attributecode.util import UNC_PREFIX
66-
from attributecode.util import ungroup_licenses
67-
from attributecode.util import ungroup_licenses_from_sctk
40+
from attributecode.util import (
41+
add_unc,
42+
boolean_fields,
43+
copy_license_notice_files,
44+
copy_file,
45+
csv,
46+
file_fields,
47+
filter_errors,
48+
get_spdx_key_and_lic_key_from_licdb,
49+
is_valid_name,
50+
on_windows,
51+
norm,
52+
replace_tab_with_spaces,
53+
wrap_boolean_value,
54+
UNC_PREFIX,
55+
ungroup_licenses,
56+
ungroup_licenses_from_sctk,
57+
parse_license_expression,
58+
detect_special_char,
59+
)
6860

6961
genereated_tk_version = "# Generated with AboutCode Toolkit Version %s \n\n" % __version__
7062

@@ -234,9 +226,10 @@ class StringField(Field):
234226
"""
235227

236228
def _validate(self, *args, **kwargs):
237-
errors = super(StringField, self)._validate(*args, ** kwargs)
229+
errors = super(StringField, self)._validate(*args, **kwargs)
230+
238231
no_special_char_field = [
239-
'license_expression', 'license_key', 'license_name', 'declared_license_expression', 'other_license_expression ']
232+
'license_expression', 'license_key', 'license_name', 'declared_license_expression', 'other_license_expression']
240233
name = self.name
241234
if name in no_special_char_field:
242235
val = self.value
@@ -2145,40 +2138,3 @@ def convert_spdx_expression_to_lic_expression(spdx_key, spdx_lic_dict):
21452138
return value
21462139

21472140

2148-
def parse_license_expression(lic_expression):
2149-
licensing = Licensing()
2150-
lic_list = []
2151-
invalid_lic_exp = ''
2152-
special_char = detect_special_char(lic_expression)
2153-
if not special_char:
2154-
# Parse the license expression and save it into a list
2155-
try:
2156-
lic_list = licensing.license_keys(lic_expression)
2157-
except:
2158-
invalid_lic_exp = lic_expression
2159-
return special_char, lic_list, invalid_lic_exp
2160-
2161-
2162-
def detect_special_char(expression):
2163-
not_support_char = [
2164-
'!', '@', '#', '$', '^', '&', '*', '=', '{', '}',
2165-
'|', '[', ']', '\\', ':', ';', '<', '>', '?', ',', '/']
2166-
special_character = []
2167-
for char in not_support_char:
2168-
if char in expression:
2169-
special_character.append(char)
2170-
return special_character
2171-
2172-
2173-
def valid_api_url(api_url):
2174-
try:
2175-
response = get(api_url)
2176-
# The 403 error code is expected if the api_url is pointing to DJE as no
2177-
# API key is provided. The 200 status code represent connection success
2178-
# to scancode's LicenseDB. All other exception yield to invalid api_url
2179-
if response.status_code == 403 or response.status_code == 200:
2180-
return True
2181-
else:
2182-
return False
2183-
except:
2184-
return False

src/attributecode/transform.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@
2323
from attributecode import CRITICAL
2424
from attributecode import Error
2525
from attributecode import saneyaml
26-
from attributecode.util import csv
27-
from attributecode.util import replace_tab_with_spaces
26+
from attributecode.util import (
27+
csv,
28+
replace_tab_with_spaces,
29+
parse_license_expression, # canonical implementation from util.py
30+
detect_special_char, # canonical implementation from util.py
31+
)
2832

2933

3034
def transform_csv(location):

src/attributecode/util.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,50 @@ def get_spdx_key_and_lic_key_from_licdb():
272272
lic_dict[other_spdx] = license["license_key"]
273273

274274
return lic_dict
275+
def detect_special_char(expression):
276+
"""
277+
Canonical implementation of license expression parsing and special character detection.
278+
Import and use these from util.py everywhere in the codebase to avoid duplication.
279+
"""
280+
def parse_license_expression(lic_expression):
281+
from license_expression import Licensing
282+
licensing = Licensing()
283+
lic_list = []
284+
invalid_lic_exp = ''
285+
special_char = detect_special_char(lic_expression)
286+
if not special_char:
287+
# Parse the license expression and save it into a list
288+
try:
289+
lic_list = licensing.license_keys(lic_expression)
290+
except Exception:
291+
invalid_lic_exp = lic_expression
292+
return special_char, lic_list, invalid_lic_exp
293+
294+
def detect_special_char(expression):
295+
not_support_char = [
296+
'!', '@', '#', '$', '^', '&', '*', '=', '{', '}',
297+
'|', '[', ']', '\\', ':', ';', '<', '>', '?', ',', '/']
298+
special_character = []
299+
if not isinstance(expression, str):
300+
return special_character
301+
for char in not_support_char:
302+
if char in expression:
303+
special_character.append(char)
304+
return special_character
305+
306+
307+
def valid_api_url(api_url):
308+
try:
309+
response = get(api_url)
310+
# The 403 error code is expected if the api_url is pointing to DJE as no
311+
# API key is provided. The 200 status code represent connection success
312+
# to scancode's LicenseDB. All other exception yield to invalid api_url
313+
if response.status_code == 403 or response.status_code == 200:
314+
return True
315+
else:
316+
return False
317+
except:
318+
return False
275319

276320

277321
def get_relative_path(base_loc, full_loc):

0 commit comments

Comments
 (0)