<s> import os <EOL> import re <EOL> import pcbmode . config as config <EOL> from . import utils <EOL> from . import messages as msg <EOL> def make_bom ( quantity = None ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def natural_key ( string_ ) : <EOL> """<STR_LIT>""" <EOL> return [ int ( s ) if s . isdigit ( ) else s for s in re . split ( r'<STR_LIT>' , string_ ) ] <EOL> dnp_text = '<STR_LIT>' <EOL> uncateg_text = '<STR_LIT>' <EOL> components_dict = config . brd [ '<STR_LIT>' ] <EOL> bom_dict = { } <EOL> for refdef in components_dict : <EOL> description = '<STR_LIT>' <EOL> try : <EOL> place = components_dict [ refdef ] [ '<STR_LIT>' ] <EOL> except : <EOL> place = True <EOL> try : <EOL> ignore = components_dict [ refdef ] [ '<STR_LIT>' ] [ '<STR_LIT:ignore>' ] <EOL> except : <EOL> ignore = False <EOL> if place == True and ignore == False : <EOL> try : <EOL> footprint_name = components_dict [ refdef ] [ '<STR_LIT>' ] <EOL> except : <EOL> msg . error ( "<STR_LIT>" % refdef ) <EOL> fname = os . path . join ( config . cfg [ '<STR_LIT>' ] , <EOL> config . cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> footprint_name + '<STR_LIT>' ) <EOL> footprint_dict = utils . dictFromJsonFile ( fname ) <EOL> info_dict = footprint_dict . get ( '<STR_LIT:info>' ) or { } <EOL> try : <EOL> comp_bom_dict = components_dict [ refdef ] [ '<STR_LIT>' ] <EOL> except : <EOL> comp_bom_dict = { } <EOL> try : <EOL> fp_bom_dict = footprint_dict [ '<STR_LIT:info>' ] <EOL> except : <EOL> fp_bom_dict = { } <EOL> for key in comp_bom_dict : <EOL> fp_bom_dict [ key ] = comp_bom_dict [ key ] <EOL> description = fp_bom_dict . get ( '<STR_LIT:description>' ) or uncateg_text <EOL> try : <EOL> dnp = components_dict [ refdef ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except : <EOL> dnp = False <EOL> if dnp == True : <EOL> description = dnp_text <EOL> if description not in bom_dict : <EOL> bom_dict [ description ] = fp_bom_dict <EOL> bom_dict [ description ] [ '<STR_LIT>' ] = [ ] <EOL> bom_dict [ description ] [ '<STR_LIT>' ] . append ( refdef ) <EOL> bom_dict [ description ] [ '<STR_LIT>' ] = components_dict [ refdef ] [ '<STR_LIT>' ] <EOL> try : <EOL> bom_content = config . brd [ '<STR_LIT>' ] <EOL> except : <EOL> bom_content = [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT:#>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT:description>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <EOL> [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:text>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> bom_path = os . path . join ( config . cfg [ '<STR_LIT>' ] , <EOL> config . cfg [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> utils . create_dir ( bom_path ) <EOL> board_name = config . cfg [ '<STR_LIT:name>' ] <EOL> board_revision = config . brd [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> base_name = "<STR_LIT>" % ( board_name , board_revision ) <EOL> bom_html = os . path . join ( bom_path , base_name + '<STR_LIT>' % '<STR_LIT>' ) <EOL> bom_csv = os . path . join ( bom_path , base_name + '<STR_LIT>' % '<STR_LIT>' ) <EOL> html = [ ] <EOL> csv = [ ] <EOL> html . append ( '<STR_LIT>' ) <EOL> html . append ( '<STR_LIT>' ) <EOL> try : <EOL> css = config . stl [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> except : <EOL> css = [ ] <EOL> for line in css : <EOL> html . append ( line ) <EOL> html . append ( '<STR_LIT>' ) <EOL> html . append ( '<STR_LIT>' ) <EOL> header = [ ] <EOL> for item in bom_content : <EOL> if item [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> for supplier in item [ '<STR_LIT>' ] : <EOL> header . append ( "<STR_LIT:%s>" % supplier [ '<STR_LIT:text>' ] ) <EOL> else : <EOL> header . append ( "<STR_LIT:%s>" % item [ '<STR_LIT:text>' ] ) <EOL> if item [ '<STR_LIT>' ] == '<STR_LIT>' and quantity != None : <EOL> header . append ( "<STR_LIT>" % quantity ) <EOL> html . append ( '<STR_LIT>' ) <EOL> html . append ( '<STR_LIT>' % ( len ( header ) , board_name , board_revision ) ) <EOL> html . append ( '<STR_LIT>' ) <EOL> html . append ( '<STR_LIT>' ) <EOL> for item in header : <EOL> html . append ( '<STR_LIT>' % item ) <EOL> html . append ( '<STR_LIT>' ) <EOL> uncateg_content = [ ] <EOL> dnp_content = [ ] <EOL> index = <NUM_LIT:1> <EOL> for desc in bom_dict : <EOL> content = [ ] <EOL> for item in bom_content : <EOL> if item [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> content . append ( "<STR_LIT>" % str ( index ) ) <EOL> elif item [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> for supplier in item [ '<STR_LIT>' ] : <EOL> try : <EOL> number = bom_dict [ desc ] [ item [ '<STR_LIT>' ] ] [ supplier [ '<STR_LIT>' ] ] <EOL> except : <EOL> number = "<STR_LIT>" <EOL> search_url = supplier . get ( '<STR_LIT>' ) <EOL> if search_url != None : <EOL> content . append ( '<STR_LIT>' % ( search_url , number , number ) ) <EOL> else : <EOL> content . append ( number ) <EOL> elif item [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> units = len ( bom_dict [ desc ] [ '<STR_LIT>' ] ) <EOL> content . append ( "<STR_LIT:%s>" % ( str ( units ) ) ) <EOL> if quantity != None : <EOL> content . append ( "<STR_LIT:%s>" % ( str ( units * int ( quantity ) ) ) ) <EOL> elif item [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> sorted_list = sorted ( bom_dict [ desc ] [ '<STR_LIT>' ] , key = natural_key ) <EOL> refdefs = '<STR_LIT>' <EOL> for refdef in sorted_list [ : - <NUM_LIT:1> ] : <EOL> refdefs += "<STR_LIT>" % refdef <EOL> refdefs += "<STR_LIT:%s>" % sorted_list [ - <NUM_LIT:1> ] <EOL> content . append ( "<STR_LIT>" % refdefs ) <EOL> elif item [ '<STR_LIT>' ] == '<STR_LIT:description>' : <EOL> content . append ( "<STR_LIT>" % desc ) <EOL> else : <EOL> try : <EOL> content . append ( bom_dict [ desc ] [ item [ '<STR_LIT>' ] ] ) <EOL> except : <EOL> content . append ( "<STR_LIT>" ) <EOL> if desc == uncateg_text : <EOL> uncateg_content = content <EOL> elif desc == dnp_text : <EOL> dnp_content = content <EOL> else : <EOL> html . append ( '<STR_LIT>' ) <EOL> for item in content : <EOL> html . append ( '<STR_LIT>' % ( ( '<STR_LIT>' , '<STR_LIT>' ) [ index % <NUM_LIT:2> == <NUM_LIT:0> ] , item ) ) <EOL> html . append ( '<STR_LIT>' ) <EOL> index += <NUM_LIT:1> <EOL> for content in ( dnp_content , uncateg_content ) : <EOL> html . append ( '<STR_LIT>' ) <EOL> html . append ( '<STR_LIT>' ) <EOL> html . append ( '<STR_LIT>' ) <EOL> if len ( content ) > <NUM_LIT:0> : <EOL> content [ <NUM_LIT:0> ] = index <EOL> for item in content : <EOL> html . append ( '<STR_LIT>' % ( ( '<STR_LIT>' , '<STR_LIT>' ) [ index % <NUM_LIT:2> == <NUM_LIT:0> ] , item ) ) <EOL> html . append ( '<STR_LIT>' ) <EOL> index += <NUM_LIT:1> <EOL> html . append ( '<STR_LIT>' ) <EOL> html . append ( '<STR_LIT>' ) <EOL> html . append ( '<STR_LIT>' ) <EOL> with open ( bom_html , "<STR_LIT:wb>" ) as f : <EOL> for line in html : <EOL> f . write ( line + '<STR_LIT:\n>' ) </s>
