Skip to content

Commit c2ffbd7

Browse files
committed
Move quoter selection logic to its own function
Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent 8bd45f4 commit c2ffbd7

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/packageurl.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ def quote(s):
6565
return quoted.replace('%3A', ':')
6666

6767

68+
def get_quoter(encode=True):
69+
"""
70+
Return quoting callable given an `encode` tri-boolean (True, False or None)
71+
"""
72+
if encode is True:
73+
return quote
74+
elif encode is False:
75+
return percent_unquote
76+
elif encode is None:
77+
return lambda x: x
78+
79+
6880
def normalize_qualifiers(qualifiers, encode=True):
6981
"""
7082
Return normalized qualifiers.
@@ -75,12 +87,7 @@ def normalize_qualifiers(qualifiers, encode=True):
7587
If `qualifiers` is a string of qualfiers, formatted to the purl specifications, and `encode`
7688
is false, the string is then converted to a dictionary of qualifiers and their values.
7789
"""
78-
if encode is True:
79-
quoting = quote
80-
elif encode is False:
81-
quoting = percent_unquote
82-
elif encode is None:
83-
quoting = lambda x: x
90+
quoting = get_quoter(encode)
8491

8592
if qualifiers:
8693
if isinstance(qualifiers, basestring):
@@ -111,26 +118,21 @@ def normalize_qualifiers(qualifiers, encode=True):
111118
qualifiers = ['{}={}'.format(k, v) for k, v in qualifiers]
112119
qualifiers = '&'.join(qualifiers)
113120

114-
return qualifiers
121+
return qualifiers or None
115122

116123

117124
def normalize(type, namespace, name, version, qualifiers, subpath, encode=True): # NOQA
118125
"""
119126
Return normalized purl components.
120127
"""
121-
if encode is True:
122-
quoting = quote
123-
elif encode is False:
124-
quoting = percent_unquote
125-
elif encode is None:
126-
quoting = lambda x: x
128+
quoting = get_quoter(encode)
127129

128130
if type:
129131
type = type.strip().lower() # NOQA
130132

131133
if namespace:
132134
namespace = namespace.strip().strip('/')
133-
if type and type in ('bitbucket', 'github', 'pypi'):
135+
if type in ('bitbucket', 'github', 'pypi'):
134136
namespace = namespace.lower()
135137
segments = namespace.split('/')
136138
segments = [seg for seg in segments if seg and seg.strip()]

0 commit comments

Comments
 (0)