@@ -65,6 +65,18 @@ def quote(s):
65
65
return quoted .replace ('%3A' , ':' )
66
66
67
67
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
+
68
80
def normalize_qualifiers (qualifiers , encode = True ):
69
81
"""
70
82
Return normalized qualifiers.
@@ -75,12 +87,7 @@ def normalize_qualifiers(qualifiers, encode=True):
75
87
If `qualifiers` is a string of qualfiers, formatted to the purl specifications, and `encode`
76
88
is false, the string is then converted to a dictionary of qualifiers and their values.
77
89
"""
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 )
84
91
85
92
if qualifiers :
86
93
if isinstance (qualifiers , basestring ):
@@ -111,26 +118,21 @@ def normalize_qualifiers(qualifiers, encode=True):
111
118
qualifiers = ['{}={}' .format (k , v ) for k , v in qualifiers ]
112
119
qualifiers = '&' .join (qualifiers )
113
120
114
- return qualifiers
121
+ return qualifiers or None
115
122
116
123
117
124
def normalize (type , namespace , name , version , qualifiers , subpath , encode = True ): # NOQA
118
125
"""
119
126
Return normalized purl components.
120
127
"""
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 )
127
129
128
130
if type :
129
131
type = type .strip ().lower () # NOQA
130
132
131
133
if namespace :
132
134
namespace = namespace .strip ().strip ('/' )
133
- if type and type in ('bitbucket' , 'github' , 'pypi' ):
135
+ if type in ('bitbucket' , 'github' , 'pypi' ):
134
136
namespace = namespace .lower ()
135
137
segments = namespace .split ('/' )
136
138
segments = [seg for seg in segments if seg and seg .strip ()]
0 commit comments