11"""Config object"""
22
33import argparse
4+ import os
5+
6+ from wx import FileConfig
47
58import dialog .settings_dialog
69
710
811class Config :
912 # Helper constants
13+ config_file = os .path .join (os .path .dirname (__file__ ), 'config.ini' )
1014 bom_view_choices = ['bom-only' , 'left-right' , 'top-bottom' ]
1115 layer_view_choices = ['F' , 'FB' , 'B' ]
1216 default_sort_order = [
@@ -29,8 +33,8 @@ class Config:
2933 redraw_on_drag = True
3034 board_rotation = 0
3135 checkboxes = ',' .join (default_checkboxes )
32- bom_view = 1
33- layer_view = 1
36+ bom_view = bom_view_choices [ 1 ]
37+ layer_view = layer_view_choices [ 1 ]
3438 open_browser = True
3539
3640 # General section
@@ -49,8 +53,88 @@ class Config:
4953 dnp_field = ''
5054
5155 def __init__ (self ):
52- """Init with defaults"""
53- pass
56+ """Init from config file if it exists."""
57+ if not os .path .isfile (self .config_file ):
58+ return
59+ f = FileConfig (localFilename = self .config_file )
60+
61+ f .SetPath ('/html_defaults' )
62+ self .dark_mode = f .ReadBool ('dark_mode' , self .dark_mode )
63+ self .show_silkscreen = f .ReadBool (
64+ 'show_silkscreen' , self .show_silkscreen )
65+ self .highlight_pin1 = f .ReadBool ('highlight_pin1' , self .highlight_pin1 )
66+ self .redraw_on_drag = f .ReadBool ('redraw_on_drag' , self .redraw_on_drag )
67+ self .board_rotation = f .ReadInt ('board_rotation' , self .board_rotation )
68+ self .checkboxes = f .Read ('checkboxes' , self .checkboxes )
69+ self .bom_view = f .Read ('bom_view' , self .bom_view )
70+ self .layer_view = f .Read ('layer_view' , self .layer_view )
71+ self .open_browser = f .ReadBool ('open_browser' , self .open_browser )
72+
73+ f .SetPath ('/general' )
74+ self .bom_dest_dir = f .Read ('bom_dest_dir' , self .bom_dest_dir )
75+ self .component_sort_order = f .Read (
76+ 'component_sort_order' ,
77+ ',' .join (self .component_sort_order )
78+ ).split (',' )
79+ self .component_blacklist = f .Read (
80+ 'component_blacklist' ,
81+ ',' .join (self .component_blacklist )
82+ ).split (',' )
83+ self .blacklist_virtual = f .ReadBool (
84+ 'blacklist_virtual' , self .blacklist_virtual )
85+
86+ f .SetPath ('/extra_fields' )
87+ self .extra_fields = f .Read (
88+ 'extra_fields' ,
89+ ',' .join (self .extra_fields )
90+ ).split (',' )
91+ self .board_variant_field = f .Read (
92+ 'board_variant_field' , self .board_variant_field )
93+ self .board_variant_whitelist = f .Read (
94+ 'board_variant_whitelist' ,
95+ ',' .join (self .board_variant_whitelist )
96+ ).split (',' )
97+ self .board_variant_blacklist = f .Read (
98+ 'board_variant_blacklist' ,
99+ ',' .join (self .board_variant_blacklist )
100+ ).split (',' )
101+ self .dnp_field = f .Read ('dnp_field' , self .dnp_field )
102+
103+ def save (self ):
104+ f = FileConfig (localFilename = self .config_file )
105+
106+ f .SetPath ('/html_defaults' )
107+ f .WriteBool ('dark_mode' , self .dark_mode )
108+ f .WriteBool ('show_silkscreen' , self .show_silkscreen )
109+ f .WriteBool ('highlight_pin1' , self .highlight_pin1 )
110+ f .WriteBool ('redraw_on_drag' , self .redraw_on_drag )
111+ f .WriteInt ('board_rotation' , self .board_rotation )
112+ f .Write ('checkboxes' , self .checkboxes )
113+ f .Write ('bom_view' , self .bom_view )
114+ f .Write ('layer_view' , self .layer_view )
115+ f .WriteBool ('open_browser' , self .open_browser )
116+
117+ f .SetPath ('/general' )
118+ bom_dest_dir = self .bom_dest_dir
119+ if bom_dest_dir .startswith (self .netlist_initial_directory ):
120+ bom_dest_dir = os .path .relpath (
121+ bom_dest_dir , self .netlist_initial_directory )
122+ f .Write ('bom_dest_dir' , bom_dest_dir )
123+ f .Write ('component_sort_order' ,
124+ ',' .join (self .component_sort_order ))
125+ f .Write ('component_blacklist' ,
126+ ',' .join (self .component_blacklist ))
127+ f .WriteBool ('blacklist_virtual' , self .blacklist_virtual )
128+
129+ f .SetPath ('/extra_fields' )
130+ f .Write ('extra_fields' , ',' .join (self .extra_fields ))
131+ f .Write ('board_variant_field' , self .board_variant_field )
132+ f .Write ('board_variant_whitelist' ,
133+ ',' .join (self .board_variant_whitelist ))
134+ f .Write ('board_variant_blacklist' ,
135+ ',' .join (self .board_variant_blacklist ))
136+ f .Write ('dnp_field' , self .dnp_field )
137+ f .Flush ()
54138
55139 def set_from_dialog (self , dlg ):
56140 # type: (dialog.settings_dialog.SettingsDialogPanel) -> None
@@ -96,8 +180,10 @@ def transfer_to_dialog(self, dlg):
96180 dlg .html .continuousRedrawCheckbox .value = self .redraw_on_drag
97181 dlg .html .boardRotationSlider .Value = self .board_rotation
98182 dlg .html .bomCheckboxesCtrl .Value = self .checkboxes
99- dlg .html .bomDefaultView .Selection = self .bom_view
100- dlg .html .layerDefaultView .Selection = self .layer_view
183+ dlg .html .bomDefaultView .Selection = self .bom_view_choices .index (
184+ self .bom_view )
185+ dlg .html .layerDefaultView .Selection = self .layer_view_choices .index (
186+ self .layer_view )
101187 dlg .html .openBrowserCheckbox .Value = self .open_browser
102188
103189 # General
@@ -114,14 +200,22 @@ def transfer_to_dialog(self, dlg):
114200 # Extra fields
115201 dlg .extra .netlistFilePicker .SetInitialDirectory (
116202 self .netlist_initial_directory )
117- dlg .extra .extraFieldsList .SetCheckedStrings (self .extra_fields )
203+
204+ def safe_set_checked_strings (clb , strings ):
205+ safe_strings = list (clb .GetStrings ())
206+ clb .SetCheckedStrings ([s for s in strings if s in safe_strings ])
207+
208+ safe_set_checked_strings (dlg .extra .extraFieldsList , self .extra_fields )
118209 dlg .extra .boardVariantFieldBox .Value = self .board_variant_field
119- dlg .extra .boardVariantWhitelist .SetCheckedStrings (
120- self .board_variant_whitelist )
121- dlg .extra .boardVariantBlacklist .SetCheckedStrings (
122- self .board_variant_blacklist )
210+ dlg .extra .OnBoardVariantFieldChange (None )
211+ safe_set_checked_strings (dlg .extra .boardVariantWhitelist ,
212+ self .board_variant_whitelist )
213+ safe_set_checked_strings (dlg .extra .boardVariantBlacklist ,
214+ self .board_variant_blacklist )
123215 dlg .extra .dnpFieldBox .Value = self .dnp_field
124216
217+ dlg .finish_init ()
218+
125219 # noinspection PyTypeChecker
126220 @classmethod
127221 def add_options (cls , parser ):
0 commit comments