Skip to content

Commit aad5cdc

Browse files
author
Bryan Worrell
committed
Added docs and expanded some dictionary creation loops to make things easier to understand.
1 parent 872ba4f commit aad5cdc

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

stix/utils/nsparser.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,17 @@ def _finalize_namespaces(self, ns_dict=None):
239239
for alias, ns in namespace_dicts:
240240
ns_dict[alias].add(ns)
241241

242-
# Check that all the aliases are mapped to only one namespace
242+
# Check that all the prefixes are mapped to only one namespace
243243
self._check_namespaces(ns_dict)
244244

245-
# Return a flattened dictionary
246-
return dict((alias, tuple(ns)[0]) for alias, ns in ns_dict.iteritems())
245+
# Flatten the dictionary by popping the namespace from the namespace
246+
# set values in ns_dict.
247+
flattened = {}
248+
for alias, ns_set in ns_dict.iteritems():
249+
flattened[alias] = ns_set.pop()
250+
251+
# Return the flattened dictionary
252+
return flattened
247253

248254
def _finalize_schemalocs(self, schemaloc_dict=None):
249255
# If schemaloc_dict was passed in, make a copy so we don't mistakenly
@@ -287,16 +293,28 @@ def _finalize_schemalocs(self, schemaloc_dict=None):
287293
return schemaloc_dict
288294

289295
def _finalize_binding_namespaces(self):
296+
"""Returns a namespace-to-prefix dictionary view of the
297+
finalized_namespaces (which are mapped prefix-to-namespace).
298+
299+
The bindings expect an NS-to-prefix mapping, while our ns processing
300+
code builds dictionaries that map prefix-to-Namespace(s). Because of
301+
this, we need to flip our dictionaries before handing them off to the
302+
bindings for serialization.
303+
304+
"""
290305
if not self.finalized_namespaces:
291-
return {}
306+
return {} # TODO: Should this return the DEFAULT_STIX_NAMESPACES?
292307

293-
final = dict(
294-
(ns, alias) for alias, ns in self.finalized_namespaces.iteritems()
295-
)
296-
297-
final.update(DEFAULT_STIX_NAMESPACES)
308+
binding_namespaces = {}
309+
for alias, ns in self.finalized_namespaces.iteritems():
310+
binding_namespaces[ns] = alias
311+
312+
# Always use the default STIX prefixes for STIX namespaces.
313+
# This is because of xsi:type prefixes used by the STIX/CybOX user-level
314+
# API classes.
315+
binding_namespaces.update(DEFAULT_STIX_NAMESPACES)
298316

299-
return final
317+
return binding_namespaces
300318

301319
def finalize(self, ns_dict=None, schemaloc_dict=None):
302320
self._parse_collected_classes()

0 commit comments

Comments
 (0)