11import os
2- from collections import defaultdict
32from itertools import chain
43import json
54
@@ -30,12 +29,6 @@ class InvalidFileFormat(Exception):
3029 pass
3130
3231
33- def _check_dict_str_str (d ):
34- if not isinstance (d , dict ) or \
35- not all (isinstance (val , str ) for val in chain (d , d .values ())):
36- raise InvalidFileFormat
37-
38-
3932class AttrDesc :
4033 """
4134 Describes modifications that will be applied to variable.
@@ -142,6 +135,13 @@ def to_dict(self):
142135
143136 @classmethod
144137 def from_dict (cls , var , data ):
138+
139+ def _check_dict_str_str (d ):
140+ if not isinstance (d , dict ) or \
141+ not all (isinstance (val , str )
142+ for val in chain (d , d .values ())):
143+ raise InvalidFileFormat
144+
145145 obj , warnings = super ().from_dict (var , data )
146146
147147 val_map = data .get ("renamed_values" )
@@ -159,7 +159,7 @@ def from_dict(cls, var, data):
159159 if new_colors is not None :
160160 _check_dict_str_str (new_colors )
161161 colors = []
162- for value , def_color in zip (var .values , var .palette ):
162+ for value , def_color in zip (var .values , var .palette . palette ):
163163 if value in new_colors :
164164 try :
165165 color = hex_to_color (new_colors [value ])
@@ -552,10 +552,6 @@ class Outputs:
552552
553553 want_main_area = False
554554
555- FileFilters = [
556- "Settings for individual variables (*.vdefs)" ,
557- "General color encoding for values (*.colors)" ]
558-
559555 def __init__ (self ):
560556 super ().__init__ ()
561557 self .data = None
@@ -623,16 +619,14 @@ def reset(self):
623619 self .commit ()
624620
625621 def save (self ):
626- fname , ffilter = QFileDialog .getSaveFileName (
627- self , "File name" , self ._start_dir (), ";;" .join (self .FileFilters ))
622+ fname , _ = QFileDialog .getSaveFileName (
623+ self , "File name" , self ._start_dir (),
624+ "Variable definitions (*.colors)" )
628625 if not fname :
629626 return
630627 QSettings ().setValue ("colorwidget/last-location" ,
631628 os .path .split (fname )[0 ])
632- if ffilter == self .FileFilters [0 ]:
633- self ._save_var_defs (fname )
634- else :
635- self ._save_value_colors (fname )
629+ self ._save_var_defs (fname )
636630
637631 def _save_var_defs (self , fname ):
638632 json .dump (
@@ -647,44 +641,29 @@ def _save_var_defs(self, fname):
647641 open (fname , "w" ),
648642 indent = 4 )
649643
650- def _save_value_colors (self , fname ):
651- color_map = defaultdict (set )
652- for desc in self .disc_descs :
653- if desc .new_colors is None :
654- continue
655- for value , old_color , new_color in zip (
656- desc .var .values , desc .var .palette .palette , desc .new_colors ):
657- old_hex , new_hex = map (color_to_hex , (old_color , new_color ))
658- if old_hex != new_hex :
659- color_map [value ].add (new_hex )
660- js = {value : colors .pop ()
661- for value , colors in color_map .items ()
662- if len (colors ) == 1 }
663- json .dump (js , open (fname , "w" ), indent = 4 )
664-
665644 def load (self ):
645+ fname , _ = QFileDialog .getOpenFileName (
646+ self , "File name" , self ._start_dir (),
647+ "Variable definitions (*.colors)" )
648+ if not fname :
649+ return
650+
666651 try :
667- fname , ffilter = QFileDialog .getOpenFileName (
668- self , "File name" , self ._start_dir (),
669- ";;" .join (self .FileFilters ))
670- if not fname :
671- return
672- try :
673- js = json .load (open (fname )) #: dict
674- except IOError :
675- QMessageBox .critical (self , "File error" ,
676- "File cannot be opened." )
677- return
678- except json .JSONDecodeError as exc :
679- raise InvalidFileFormat from exc
680- if ffilter == self .FileFilters [0 ]:
681- self ._parse_var_defs (js )
682- else :
683- self ._parse_value_colors (js )
652+ f = open (fname )
653+ except IOError :
654+ QMessageBox .critical (self , "File error" , "File cannot be opened." )
655+ return
656+
657+ try :
658+ js = json .load (f ) #: dict
659+ except json .JSONDecodeError as exc :
660+ raise InvalidFileFormat from exc
661+
662+ try :
663+ self ._parse_var_defs (js )
684664 except InvalidFileFormat :
685665 QMessageBox .critical (self , "File error" , "Invalid file format." )
686- else :
687- self .unconditional_commit ()
666+ return
688667
689668 def _parse_var_defs (self , js ):
690669 if not isinstance (js , dict ):
@@ -740,23 +719,6 @@ def _parse_var_defs(self, js):
740719 self .cont_model .set_data (self .cont_descs )
741720 self .unconditional_commit ()
742721
743- def _parse_value_colors (self , js ):
744- if not isinstance (js , dict ) or \
745- any (not isinstance (obj , str ) for obj in chain (js , js .values ())):
746- raise InvalidFileFormat
747- try :
748- js = {k : hex_to_color (v ) for k , v in js .items ()}
749- except (ValueError , IndexError ) as exc :
750- raise InvalidFileFormat from exc
751-
752- for desc in self .disc_descs :
753- for i , value in enumerate (desc .var .values ):
754- if value in js :
755- desc .set_color (i , js [value ])
756-
757- self .disc_model .set_data (self .disc_descs )
758- self .unconditional_commit ()
759-
760722 def _start_dir (self ):
761723 return self .workflowEnv ().get ("basedir" ) \
762724 or QSettings ().value ("colorwidget/last-location" ) \
0 commit comments