MAINT: Move possible side assignment to insert metadata method.

The previous workflow was:

- insert basic metadata for all assets

- iterate over all metadata entries. Create a sid if one does not
exist

- call insert_metadata again to update the assigned sid value.

Instead, create a sid, if missing on the initial metadata assignment and
remove the second pass of calling insert_metadata.

Also, since the sid assignment code is only done in this one context,
inline the sid assignment code so that there is less code step through
while looking for where sid assignment is done.
This commit is contained in:
Eddie Hebert
2015-07-08 15:05:00 -04:00
parent 9688989eba
commit bfcb91b359
+15 -23
View File
@@ -85,19 +85,6 @@ class AssetFinder(object):
self.populate_cache()
def _next_free_sid(self):
if len(self.cache) > 0:
return max(self.cache.keys()) + 1
return 0
def _assign_sid(self, identifier):
if hasattr(identifier, '__int__'):
return identifier.__int__()
if not self.allow_sid_assignment:
raise SidAssignmentError(identifier=identifier)
if isinstance(identifier, string_types):
return self._next_free_sid()
def retrieve_asset(self, sid, default_none=False):
if isinstance(sid, Asset):
return sid
@@ -290,16 +277,6 @@ class AssetFinder(object):
def _spawn_asset(self, identifier, **kwargs):
# Check if the sid is declared
try:
kwargs['sid']
pass
except KeyError:
# If the identifier is not a sid, assign one
kwargs['sid'] = self._assign_sid(identifier)
# Update the metadata object with the new sid
self.insert_metadata(identifier=identifier, sid=kwargs['sid'])
# If the file_name is in the kwargs, it will be used as the symbol
try:
kwargs['symbol'] = kwargs.pop('file_name')
@@ -545,6 +522,21 @@ class AssetFinder(object):
continue
entry[key] = value
# Check if the sid is declared
try:
entry['sid']
except KeyError:
# If the identifier is not a sid, assign one
if hasattr(identifier, '__int__'):
entry['sid'] = identifier.__int__()
else:
if self.allow_sid_assignment:
# Assign the sid the value of its insertion order.
# This assumes that we are assigning values to all assets.
entry['sid'] = len(self.metadata_cache)
else:
raise SidAssignmentError(identifier=identifier)
self.metadata_cache[identifier] = entry
def consume_identifiers(self, identifiers):