Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def groups_sort(new_component_groups):
'''@brief Order the groups in a alphabetical way.
Put the components groups in the spreadsheet rows in a spefic order
using the reference string of the components. The order is defined
by BOM_ORDER.
@param components Part components in a `list()` of `dict()`, format given by the EDA modules.
@return Same as input.
'''
logger.log(DEBUG_OVERVIEW, 'Sorting the groups for better visualization...')
ref_identifiers = re.split('(?0:
# If found more than one group with the reference, use the 'manf#'
# Now we have groups of seemingly identical parts. But some of the parts
# within a group may have different manufacturer's part numbers, and these
# groups may need to be split into smaller groups of parts all having the
# same manufacturer's number. Here are the cases that need to be handled:
# One manf# number: All parts have the same manf#. Don't split this group.
# Two manf# numbers, but one is None: Some of the parts have no manf# but
# are otherwise identical to the other parts in the group. Don't split
# this group. Instead, propagate the non-None manf# to all the parts.
# Two manf#, neither is None: All parts have non-None manf# numbers.
# Split the group into two smaller groups of parts all having the same
# manf#.
# Three or more manf#: Split this group into smaller groups, each one with
# parts having the same manf#, even if it's None. It's impossible to
# determine which manf# the None parts should be assigned to, so leave
# their manf# as None.
logger.log(DEBUG_OVERVIEW, 'Checking the seemingly identical parts group...')
new_component_groups = [] # Copy new component groups into this.
for g, grp in list(component_groups.items()):
num_manf_nums = len(grp.manf_nums)
if num_manf_nums == 1:
new_component_groups.append(grp)
continue # Single manf#. Don't split this group.
elif num_manf_nums == 2 and None in grp.manf_nums:
new_component_groups.append(grp)
continue # Two manf#, but one of them is None. Don't split this group.
# Otherwise, split the group into subgroups, each with the same manf#.
for manf_num in grp.manf_nums:
sub_group = IdenticalComponents()
sub_group.manf_nums = [manf_num]
sub_group.refs = []
for ref in grp.refs:
sub_group.manf_nums = [manf_num]
sub_group.refs = []
for ref in grp.refs:
# Use get() which returns None if the component has no manf# field.
# That will match if the group manf_num is also None.
if components[ref].get('manf#') == manf_num:
sub_group.refs.append(ref)
new_component_groups.append(sub_group)
# If the identical components grouped have diference in the `fields_merge`
# so replace this field with a string composed line-by-line with the
# ocorrences (definition `SGROUP_SEPRTR`) preceded with the refs
# collapsed plus `SEPRTR`. Implementation of the ISSUE #102.
logger.log(DEBUG_OVERVIEW, 'Merging field asked in the identical components groups...')
if fields_merge:
fields_merge = [field_name_translations.get(f.lower(), f.lower()) for f in fields_merge]
for grp in new_component_groups:
components_grp = dict()
components_grp = {i:components[i] for i in grp.refs}
for f in fields_merge:
values_field = [v.get(f) or '' for k,v in components_grp.items()]
ocurrences = Counter(values_field)
ocurrences = {v_g:[ r for r in grp.refs if components[r].get(f) == v_g] for v_g in Counter(values_field)}
if len(ocurrences)>1:
value = SGROUP_SEPRTR.join( [collapse_refs(r) + SEPRTR + ' ' + t for t,r in ocurrences.items()] )
for r in grp.refs:
components[r][f] = value
#print('++++++++++++++',len(new_component_groups))
#for grp in new_component_groups:
# print(grp.refs)