@@ -26,7 +26,6 @@ class Config:
2626    )  # type: str 
2727
2828    # Helper constants 
29-     config_file  =  os .path .join (os .path .dirname (__file__ ), '..' , 'config.ini' )
3029    bom_view_choices  =  ['bom-only' , 'left-right' , 'top-bottom' ]
3130    layer_view_choices  =  ['F' , 'FB' , 'B' ]
3231    default_sort_order  =  [
@@ -88,14 +87,22 @@ def _split(s):
8887    def  _join (lst ):
8988        return  ',' .join ([s .replace (',' , '\\ ,' ) for  s  in  lst ])
9089
91-     def  __init__ (self , version ):
90+     def  __init__ (self , version ,  local_dir ):
9291        self .version  =  version 
92+         self .local_config_file  =  os .path .join (local_dir , 'ibom.config.ini' )
93+         self .global_config_file  =  os .path .join (
94+             os .path .dirname (__file__ ), '..' , 'config.ini' )
9395
9496    def  load_from_ini (self ):
9597        """Init from config file if it exists.""" 
96-         if  not  os .path .isfile (self .config_file ):
98+         if  os .path .isfile (self .local_config_file ):
99+             file  =  self .local_config_file 
100+         elif  os .path .isfile (self .global_config_file ):
101+             file  =  self .global_config_file 
102+         else :
97103            return 
98-         f  =  FileConfig (localFilename = self .config_file )
104+ 
105+         f  =  FileConfig (localFilename = file )
99106
100107        f .SetPath ('/html_defaults' )
101108        self .dark_mode  =  f .ReadBool ('dark_mode' , self .dark_mode )
@@ -146,8 +153,10 @@ def load_from_ini(self):
146153            self ._join (self .board_variant_blacklist )))
147154        self .dnp_field  =  f .Read ('dnp_field' , self .dnp_field )
148155
149-     def  save (self ):
150-         f  =  FileConfig (localFilename = self .config_file )
156+     def  save (self , locally ):
157+         file  =  self .local_config_file  if  locally  else  self .global_config_file 
158+         print ('Saving to' , file )
159+         f  =  FileConfig (localFilename = file )
151160
152161        f .SetPath ('/html_defaults' )
153162        f .WriteBool ('dark_mode' , self .dark_mode )
@@ -294,9 +303,9 @@ def safe_set_checked_strings(clb, strings):
294303
295304        dlg .finish_init ()
296305
297-     # noinspection PyTypeChecker 
298-     def  add_options (self , parser ,  file_name_format_hint ):
299-         # type: (argparse.ArgumentParser, str ) -> None 
306+     @ classmethod 
307+     def  add_options (cls , parser ):
308+         # type: (argparse.ArgumentParser) -> None 
300309        parser .add_argument ('--show-dialog' , action = 'store_true' ,
301310                            help = 'Shows config dialog. All other flags ' 
302311                                 'will be ignored.' )
@@ -319,17 +328,17 @@ def add_options(self, parser, file_name_format_hint):
319328                            help = 'Do not redraw pcb on drag by default.' ,
320329                            action = 'store_true' )
321330        parser .add_argument ('--board-rotation' , type = int ,
322-                             default = self .board_rotation  *  5 ,
331+                             default = cls .board_rotation  *  5 ,
323332                            help = 'Board rotation in degrees (-180 to 180). ' 
324333                                 'Will be rounded to multiple of 5.' )
325334        parser .add_argument ('--checkboxes' ,
326-                             default = self .checkboxes ,
335+                             default = cls .checkboxes ,
327336                            help = 'Comma separated list of checkbox columns.' )
328-         parser .add_argument ('--bom-view' , default = self .bom_view ,
329-                             choices = self .bom_view_choices ,
337+         parser .add_argument ('--bom-view' , default = cls .bom_view ,
338+                             choices = cls .bom_view_choices ,
330339                            help = 'Default BOM view.' )
331-         parser .add_argument ('--layer-view' , default = self .layer_view ,
332-                             choices = self .layer_view_choices ,
340+         parser .add_argument ('--layer-view' , default = cls .layer_view ,
341+                             choices = cls .layer_view_choices ,
333342                            help = 'Default layer view.' )
334343        parser .add_argument ('--no-compression' ,
335344                            help = 'Disable compression of pcb data.' ,
@@ -338,11 +347,11 @@ def add_options(self, parser, file_name_format_hint):
338347                            action = 'store_true' )
339348
340349        # General 
341-         parser .add_argument ('--dest-dir' , default = self .bom_dest_dir ,
350+         parser .add_argument ('--dest-dir' , default = cls .bom_dest_dir ,
342351                            help = 'Destination directory for bom file ' 
343352                                 'relative to pcb file directory.' )
344-         parser .add_argument ('--name-format' , default = self .bom_name_format ,
345-                             help = file_name_format_hint .replace ('%' , '%%' ))
353+         parser .add_argument ('--name-format' , default = cls .bom_name_format ,
354+                             help = cls . FILE_NAME_FORMAT_HINT .replace ('%' , '%%' ))
346355        parser .add_argument ('--include-tracks' , action = 'store_true' ,
347356                            help = 'Include track/zone information in output. ' 
348357                                 'F.Cu and B.Cu layers only.' )
@@ -351,9 +360,9 @@ def add_options(self, parser, file_name_format_hint):
351360        parser .add_argument ('--sort-order' ,
352361                            help = 'Default sort order for components. ' 
353362                                 'Must contain "~" once.' ,
354-                             default = ',' .join (self .component_sort_order ))
363+                             default = ',' .join (cls .component_sort_order ))
355364        parser .add_argument ('--blacklist' ,
356-                             default = ',' .join (self .component_blacklist ),
365+                             default = ',' .join (cls .component_blacklist ),
357366                            help = 'List of comma separated blacklisted ' 
358367                                 'components or prefixes with *. ' 
359368                                 'E.g. "X1,MH*"' )
@@ -372,10 +381,10 @@ def add_options(self, parser, file_name_format_hint):
372381                                 'for --show-fields and --group-fields ' 
373382                                 'with values "Value,Footprint,X,Y"' )
374383        parser .add_argument ('--show-fields' ,
375-                             default = self ._join (self .show_fields ),
384+                             default = cls ._join (cls .show_fields ),
376385                            help = 'List of fields to show in the BOM.' )
377386        parser .add_argument ('--group-fields' ,
378-                             default = self ._join (self .group_fields ),
387+                             default = cls ._join (cls .group_fields ),
379388                            help = 'Fields that components will be grouped by.' )
380389        parser .add_argument ('--normalize-field-case' ,
381390                            help = 'Normalize extra field name case. E.g. "MPN" ' 
@@ -390,7 +399,7 @@ def add_options(self, parser, file_name_format_hint):
390399        parser .add_argument ('--variants-blacklist' , default = '' ,
391400                            help = 'List of board variants to ' 
392401                                 'exclude from the BOM.' )
393-         parser .add_argument ('--dnp-field' , default = self .dnp_field ,
402+         parser .add_argument ('--dnp-field' , default = cls .dnp_field ,
394403                            help = 'Name of the extra field that indicates ' 
395404                                 'do not populate status. Components with ' 
396405                                 'this field not empty will be excluded.' )
0 commit comments